2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #include "config/config.h"
23 #include <dispatch/dispatch.h>
30 #include <libkern/OSAtomic.h>
32 #include "dispatch_test.h"
36 static long event_cnt;
41 dispatch_source_t proc;
45 test_start("Dispatch Proc");
47 // Creates a process and register multiple observers. Send a signal,
48 // exit the process, etc., and verify all observers were notified.
51 // Simple child process that sleeps 2 seconds.
54 posix_spawnattr_t attr;
55 res = posix_spawnattr_init(&attr);
57 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
61 "/bin/sleep", "2", NULL
64 res = posix_spawnp(&pid, args[0], NULL, &attr, args, NULL);
70 res = posix_spawnattr_destroy(&attr);
73 dispatch_queue_t completion = dispatch_queue_create("completion", NULL);
78 // Suspend the "completion" queue when each observer is created.
79 // Each observer resumes the queue when the child process exits.
80 // If the queue is resumed too few times (indicating that not all
81 // observers received the exit event) then the test case will not exit
82 // within the alloted time and result in failure.
86 for (i = 0; i < PID_CNT; ++i) {
87 dispatch_suspend(completion);
88 proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, dispatch_get_main_queue());
89 test_ptr_notnull("DISPATCH_SOURCE_TYPE_PROC", proc);
91 dispatch_source_set_event_handler(proc, ^{
92 long flags = dispatch_source_get_data(proc);
93 test_long("DISPATCH_PROC_EXIT", flags, DISPATCH_PROC_EXIT);
95 dispatch_release(proc);
96 dispatch_resume(completion);
99 dispatch_resume(proc);
104 // The completion block will be pending on the completion queue until it
105 // has been fully resumed, at which point the test will exit successfully.
108 dispatch_async(completion, ^{
110 int res2 = waitpid(pid, &status, 0);
112 test_long("Sub-process exited", WEXITSTATUS(status) | WTERMSIG(status), 0);
113 test_long("Event count", event_cnt, PID_CNT);