]> git.saurik.com Git - apple/libpthread.git/blob - tests/maxwidth.c
libpthread-301.1.6.tar.gz
[apple/libpthread.git] / tests / maxwidth.c
1 #include <dispatch/dispatch.h>
2 #include <dispatch/private.h>
3 #include <stdio.h>
4
5 #define NUM 100000
6 static volatile size_t concur;
7 static volatile size_t final;
8 dispatch_queue_t resultsq;
9 dispatch_group_t rgroup;
10
11 void finish(void* ctxt)
12 {
13 int c = (uintptr_t)ctxt;
14 if (c > final) final = c;
15 }
16
17 void work(void* ctxt)
18 {
19 int c = __sync_add_and_fetch(&concur, 1);
20 if (ctxt) {
21 usleep(1000);
22 } else {
23 for (int i=0; i<100000; i++) {
24 __asm__ __volatile__ ("");
25 }
26 }
27 dispatch_group_async_f(rgroup, resultsq, (void*)(uintptr_t)c, finish);
28 __sync_sub_and_fetch(&concur, 1);
29 }
30
31 int main(int argc, const char *argv[])
32 {
33 size_t i;
34
35 rgroup = dispatch_group_create();
36 resultsq = dispatch_queue_create("results", 0);
37 dispatch_suspend(resultsq);
38
39 dispatch_group_t group = dispatch_group_create();
40
41 final = concur = 0;
42 for (i=0; i<NUM; i++) {
43 dispatch_group_async_f(group, dispatch_get_global_queue(0, 0), NULL, work);
44 }
45
46 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
47 dispatch_resume(resultsq);
48
49 dispatch_group_wait(rgroup, DISPATCH_TIME_FOREVER);
50 printf("max concurrency: %zd threads.\n", final);
51
52 dispatch_suspend(resultsq);
53
54 /* ******* */
55
56 final = concur = 0;
57 for (i=0; i<NUM; i++) {
58 dispatch_group_async_f(group, dispatch_get_global_queue(0, 0), (void*)1, work);
59 }
60
61 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
62 dispatch_resume(resultsq);
63
64 dispatch_group_wait(rgroup, DISPATCH_TIME_FOREVER);
65 printf("max blocking concurrency: %zd threads.\n", final);
66
67 dispatch_suspend(resultsq);
68
69 /* ******* */
70
71 final = concur = 0;
72 for (i=0; i<NUM; i++) {
73 dispatch_group_async_f(group, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT), NULL, work);
74 }
75
76 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
77 dispatch_resume(resultsq);
78
79 dispatch_group_wait(rgroup, DISPATCH_TIME_FOREVER);
80 printf("max overcommit concurrency: %zd threads.\n", final);
81 dispatch_suspend(resultsq);
82
83 /* ******* */
84
85 final = concur = 0;
86 for (i=0; i<NUM; i++) {
87 dispatch_group_async_f(group, dispatch_get_global_queue(0, DISPATCH_QUEUE_OVERCOMMIT), (void*)1, work);
88 }
89
90 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
91 dispatch_resume(resultsq);
92
93 dispatch_group_wait(rgroup, DISPATCH_TIME_FOREVER);
94 printf("max blocking overcommit concurrency: %zd threads.\n", final);
95
96 return 0;
97 }