4 #include <dispatch/dispatch.h>
7 atomic_int finished_enqueueing_work
= 0;
8 atomic_int thread_count
= 0;
11 doing_the_work_1(void *in
)
13 // This is only counted once because the first job in the queue
14 // starves all the others.
15 atomic_fetch_add(&thread_count
, 1);
21 submit_work_1a(void *in
)
23 dispatch_queue_t
*work_performer_1
= (dispatch_queue_t
*) in
;
24 dispatch_async_f (*work_performer_1
, NULL
, doing_the_work_1
);
25 dispatch_async_f (*work_performer_1
, NULL
, doing_the_work_1
);
29 submit_work_1b(void *in
)
31 dispatch_queue_t
*work_performer_1
= (dispatch_queue_t
*) in
;
32 dispatch_async_f (*work_performer_1
, NULL
, doing_the_work_1
);
33 dispatch_async_f (*work_performer_1
, NULL
, doing_the_work_1
);
34 atomic_fetch_add(&thread_count
, 1);
40 doing_the_work_2(void *in
)
42 atomic_fetch_add(&thread_count
, 1);
48 submit_work_2(void *in
)
50 dispatch_queue_t
*work_performer_2
= (dispatch_queue_t
*) in
;
54 dispatch_async_f (*work_performer_2
, NULL
, doing_the_work_2
);
55 dispatch_async_f (*work_performer_2
, NULL
, doing_the_work_2
);
57 atomic_fetch_add(&finished_enqueueing_work
, 1);
62 doing_the_work_3(void *in
)
64 // This counts four times, since the queue is marked as CONCURRENT.
65 atomic_fetch_add(&thread_count
, 1);
71 submit_work_3(void *in
)
73 dispatch_queue_t
*work_performer_3
= (dispatch_queue_t
*) in
;
74 dispatch_async_f (*work_performer_3
, NULL
, doing_the_work_3
);
75 dispatch_async_f (*work_performer_3
, NULL
, doing_the_work_3
);
76 dispatch_async_f (*work_performer_3
, NULL
, doing_the_work_3
);
77 dispatch_async_f (*work_performer_3
, NULL
, doing_the_work_3
);
89 int main (int argc
, const char **argv
)
91 dispatch_queue_t work_submittor_1
= dispatch_queue_create ("com.apple.work_submittor_1", DISPATCH_QUEUE_SERIAL
);
92 dispatch_queue_t work_submittor_2
= dispatch_queue_create ("com.apple.work_submittor_and_quit_2", DISPATCH_QUEUE_SERIAL
);
93 dispatch_queue_t work_submittor_3
= dispatch_queue_create ("com.apple.work_submittor_3", DISPATCH_QUEUE_SERIAL
);
95 dispatch_queue_t work_performer_1
= dispatch_queue_create ("com.apple.work_performer_1", DISPATCH_QUEUE_SERIAL
);
96 dispatch_queue_t work_performer_2
= dispatch_queue_create ("com.apple.work_performer_2", DISPATCH_QUEUE_SERIAL
);
98 dispatch_queue_t work_performer_3
= dispatch_queue_create ("com.apple.work_performer_3", DISPATCH_QUEUE_CONCURRENT
);
100 dispatch_async_f (work_submittor_1
, (void*) &work_performer_1
, submit_work_1a
);
101 dispatch_async_f (work_submittor_1
, (void*) &work_performer_1
, submit_work_1b
);
103 dispatch_async_f (work_submittor_2
, (void*) &work_performer_2
, submit_work_2
);
105 dispatch_async_f (work_submittor_3
, (void*) &work_performer_3
, submit_work_3
);
108 // Spin up threads with each of the different libdispatch QoS values.
109 dispatch_async (dispatch_get_global_queue(QOS_CLASS_USER_INITIATED
, 0), ^{
110 pthread_setname_np ("user initiated QoS");
111 atomic_fetch_add(&thread_count
, 1);
115 dispatch_async (dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE
, 0), ^{
116 pthread_setname_np ("user interactive QoS");
117 atomic_fetch_add(&thread_count
, 1);
121 dispatch_async (dispatch_get_global_queue(QOS_CLASS_DEFAULT
, 0), ^{
122 pthread_setname_np ("default QoS");
123 atomic_fetch_add(&thread_count
, 1);
127 dispatch_async (dispatch_get_global_queue(QOS_CLASS_UTILITY
, 0), ^{
128 pthread_setname_np ("utility QoS");
129 atomic_fetch_add(&thread_count
, 1);
133 dispatch_async (dispatch_get_global_queue(QOS_CLASS_BACKGROUND
, 0), ^{
134 pthread_setname_np ("background QoS");
135 atomic_fetch_add(&thread_count
, 1);
139 dispatch_async (dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED
, 0), ^{
140 pthread_setname_np ("unspecified QoS");
141 atomic_fetch_add(&thread_count
, 1);
146 // Unfortunately there is no pthread_barrier on darwin.
147 while ((atomic_load(&thread_count
) < 13) || (finished_enqueueing_work
== 0))