1 #include <dispatch/dispatch.h>
2 #include <dispatch/private.h>
6 static volatile size_t concur
;
7 static volatile size_t final
;
8 dispatch_queue_t resultsq
;
9 dispatch_group_t rgroup
;
11 void finish(void* ctxt
)
13 int c
= (uintptr_t)ctxt
;
14 if (c
> final
) final
= c
;
19 int c
= __sync_add_and_fetch(&concur
, 1);
23 for (int i
=0; i
<100000; i
++) {
24 __asm__
__volatile__ ("");
27 dispatch_group_async_f(rgroup
, resultsq
, (void*)(uintptr_t)c
, finish
);
28 __sync_sub_and_fetch(&concur
, 1);
31 int main(int argc
, const char *argv
[])
35 rgroup
= dispatch_group_create();
36 resultsq
= dispatch_queue_create("results", 0);
37 dispatch_suspend(resultsq
);
39 dispatch_group_t group
= dispatch_group_create();
42 for (i
=0; i
<NUM
; i
++) {
43 dispatch_group_async_f(group
, dispatch_get_global_queue(0, 0), NULL
, work
);
46 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
47 dispatch_resume(resultsq
);
49 dispatch_group_wait(rgroup
, DISPATCH_TIME_FOREVER
);
50 printf("max concurrency: %zd threads.\n", final
);
52 dispatch_suspend(resultsq
);
57 for (i
=0; i
<NUM
; i
++) {
58 dispatch_group_async_f(group
, dispatch_get_global_queue(0, 0), (void*)1, work
);
61 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
62 dispatch_resume(resultsq
);
64 dispatch_group_wait(rgroup
, DISPATCH_TIME_FOREVER
);
65 printf("max blocking concurrency: %zd threads.\n", final
);
67 dispatch_suspend(resultsq
);
72 for (i
=0; i
<NUM
; i
++) {
73 dispatch_group_async_f(group
, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT
), NULL
, work
);
76 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
77 dispatch_resume(resultsq
);
79 dispatch_group_wait(rgroup
, DISPATCH_TIME_FOREVER
);
80 printf("max overcommit concurrency: %zd threads.\n", final
);
81 dispatch_suspend(resultsq
);
86 for (i
=0; i
<NUM
; i
++) {
87 dispatch_group_async_f(group
, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT
), (void*)1, work
);
90 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
91 dispatch_resume(resultsq
);
93 dispatch_group_wait(rgroup
, DISPATCH_TIME_FOREVER
);
94 printf("max blocking overcommit concurrency: %zd threads.\n", final
);