2 #include <mach/mach_time.h>
3 #include <dispatch/dispatch.h>
7 #include <TargetConditionals.h>
9 #include "dispatch_test.h"
11 #if TARGET_OS_EMBEDDED
19 static dispatch_queue_t queues
[COUNT
];
20 static size_t lap_count_down
= LAPS
;
21 static size_t count_down
;
22 static uint64_t start
;
23 static mach_timebase_info_data_t tbi
;
25 static void do_test(void);
28 collect(void *context
__attribute__((unused
)))
38 delta
= mach_absolute_time() - start
;
42 math
/= COUNT
* COUNT
* 2ul + COUNT
* 2ul;
44 printf("lap: %ld\n", lap_count_down
);
45 printf("count: %lu\n", COUNT
);
46 printf("delta: %llu ns\n", delta
);
47 printf("math: %Lf ns / lap\n", math
);
49 for (i
= 0; i
< COUNT
; i
++) {
50 dispatch_release(queues
[i
]);
53 // our malloc could be a lot better,
54 // this result is really a malloc torture test
55 test_long_less_than("Latency" , (unsigned long)math
, 1000);
57 if (--lap_count_down
) {
61 // give the threads some time to settle before test_stop() runs "leaks"
62 // ...also note, this is a total cheat. dispatch_after lets this
63 // thread go idle, so dispatch cleans up the continuations cache.
64 // Doign the "old style" sleep left that stuff around and leaks
65 // took a LONG TIME to complete. Long enough that the test harness
66 // decided to kill us.
67 dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW
, 2 * NSEC_PER_SEC
), dispatch_get_main_queue(), NULL
, test_stop_after_delay
);
73 dispatch_queue_t this_q
= context
;
74 size_t replies
= (size_t)dispatch_get_context(this_q
);
76 dispatch_set_context(this_q
, (void *)--replies
);
78 //printf("collect from: %s\n", dispatch_queue_get_label(dispatch_get_current_queue()));
79 dispatch_async_f(dispatch_get_main_queue(), NULL
, collect
);
86 dispatch_queue_t reply_q
= context
;
88 dispatch_async_f(reply_q
, reply_q
, pong
);
92 start_node(void *context
)
94 dispatch_queue_t this_q
= context
;
97 dispatch_set_context(this_q
, (void *)COUNT
);
99 for (i
= 0; i
< COUNT
; i
++) {
100 dispatch_async_f(queues
[i
], this_q
, ping
);
112 kr
= mach_timebase_info(&tbi
);
115 start
= mach_absolute_time();
117 for (i
= 0; i
< COUNT
; i
++) {
119 snprintf(buf
, sizeof(buf
), "com.example.starfish-node#%ld", i
);
120 queues
[i
] = dispatch_queue_create(buf
, NULL
);
121 dispatch_suspend(queues
[i
]);
124 for (i
= 0; i
< COUNT
; i
++) {
125 dispatch_async_f(queues
[i
], queues
[i
], start_node
);
128 for (i
= 0; i
< COUNT
; i
++) {
129 dispatch_resume(queues
[i
]);
136 test_start("Dispatch Starfish");