1 #include <dispatch/dispatch.h>
2 #include <sys/sysctl.h>
10 /* found in <rdar://problem/16326400> 12A216: Spotlight takes a long time to show results */
12 /* we need to start up NCPU-1 threads in a given bucket, then fire up one more at a separate
15 * each of these waiters needs to be non-blocked until the point where dispatch wants to
16 * request a new thread.
18 * if dispatch ever fixes sync_barrier -> sync handoff to not require an extra thread,
19 * then this test will never fail and will be invalid.
22 printf("[TEST] barrier_sync -> async @ ncpu threads\n");
24 dispatch_semaphore_t sema
= dispatch_semaphore_create(0);
27 size_t sz
= sizeof(ncpu
);
28 sysctlbyname("hw.ncpu", &ncpu
, &sz
, NULL
, 0);
29 printf("starting up %d waiters.\n", ncpu
);
31 dispatch_queue_t q
= dispatch_queue_create("moo", DISPATCH_QUEUE_CONCURRENT
);
32 dispatch_barrier_sync(q
, ^{
35 dispatch_semaphore_signal(sema
);
37 for (int i
=0; i
<ncpu
-1; i
++) {
38 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH
, 0), ^{
39 printf("waiter %d* up.\n", i
);
43 dispatch_async(dispatch_get_global_queue(0, 0), ^{
44 printf("waiter %d up.\n", ncpu
-1);
46 printf("waiter %d idle.\n", ncpu
-1);
48 dispatch_sync(q
, ^{ printf("quack %d\n", ncpu
-1); });
50 printf("waiting...\n");
56 int rv
= dispatch_semaphore_wait(sema
, dispatch_time(DISPATCH_TIME_NOW
, 2ull * NSEC_PER_SEC
));
57 printf("[%s] barrier_sync -> async completed\n", rv
== 0 ? "PASS" : "FAIL");