]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_pingpong.c
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / testing / dispatch_pingpong.c
1 #include <dispatch/dispatch.h>
2 #include <stdio.h>
3
4 #include "dispatch_test.h"
5
6 uint32_t count = 0;
7 const uint32_t final = 1000000; // 10M
8
9 void pingpongloop(dispatch_group_t group, dispatch_queue_t ping, dispatch_queue_t pong, size_t counter) {
10 //printf("[%p] %s: %lu\n", (void*)(uintptr_t)pthread_self(), dispatch_queue_get_label(dispatch_get_current_queue()), counter);
11 if (counter < final) {
12 dispatch_group_async(group, pong, ^{ pingpongloop(group, pong, ping, counter+1); });
13 } else {
14 count = counter;
15 }
16 }
17
18 int main(void) {
19
20 test_start("Dispatch Ping Pong");
21
22 dispatch_queue_t ping = dispatch_queue_create("ping", NULL);
23 test_ptr_notnull("dispatch_queue_create(ping)", ping);
24 dispatch_queue_t pong = dispatch_queue_create("pong", NULL);
25 test_ptr_notnull("dispatch_queue_create(pong)", pong);
26
27 dispatch_group_t group = dispatch_group_create();
28 test_ptr_notnull("dispatch_group_create", group);
29
30 pingpongloop(group, ping, pong, 0);
31 dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
32
33 test_long("count", count, final);
34 test_stop();
35
36 return 0;
37 }