1 #include <dispatch/dispatch.h>
8 #include <libkern/OSAtomic.h>
10 #include "dispatch_test.h"
14 static long event_cnt
;
15 static long cancel_cnt
;
20 dispatch_source_t proc
;
24 test_start("Dispatch Proc");
26 // Creates a process and register multiple observers. Send a signal,
27 // exit the process, etc., and verify all observers were notified.
29 posix_spawnattr_t attr
;
30 res
= posix_spawnattr_init(&attr
);
32 res
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_START_SUSPENDED
);
36 "/bin/sleep", "2", NULL
39 res
= posix_spawnp(&pid
, args
[0], NULL
, &attr
, args
, NULL
);
45 res
= posix_spawnattr_destroy(&attr
);
48 dispatch_queue_t semaphore
= dispatch_queue_create("semaphore", NULL
);
53 for (i
= 0; i
< PID_CNT
; ++i
) {
54 dispatch_suspend(semaphore
);
55 proc
= dispatch_source_proc_create(pid
, DISPATCH_PROC_EXIT
, NULL
, dispatch_get_main_queue(),
56 ^(dispatch_event_t ev
) {
57 long err_dom
, err_val
;
58 if ((err_dom
= dispatch_event_get_error(ev
, &err_val
))) {
59 test_long("PROC error domain", err_dom
, DISPATCH_ERROR_DOMAIN_POSIX
);
60 test_long("PROC error value", err_val
, ECANCELED
);
62 dispatch_resume(semaphore
);
64 long flags
= dispatch_event_get_flags(ev
);
65 test_long("DISPATCH_PROC_EXIT", flags
, DISPATCH_PROC_EXIT
);
67 dispatch_release(dispatch_event_get_source(ev
));
70 test_ptr_notnull("dispatch_source_proc_create", proc
);
73 dispatch_async(semaphore
, ^{
75 int res2
= waitpid(pid
, &status
, 0);
77 //int passed = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
78 test_long("Sub-process exited", WEXITSTATUS(status
) | WTERMSIG(status
), 0);
79 test_long("Event count", event_cnt
, PID_CNT
);
80 test_long("Cancel count", cancel_cnt
, PID_CNT
);