]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_test_sync_on_main.c
libdispatch-84.5.3.tar.gz
[apple/libdispatch.git] / testing / dispatch_test_sync_on_main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <dispatch/dispatch.h>
5 #include <pthread.h>
6 #include <assert.h>
7 #include <CoreFoundation/CoreFoundation.h>
8
9 int global_count;
10
11 void
12 main_work(void* ctxt)
13 {
14 if (global_count == 20) {
15 exit(0);
16 }
17 uint64_t time = random() % NSEC_PER_SEC;
18 printf("Firing timer on main %d\n", ++global_count);
19 dispatch_after_f(dispatch_time(0, time), dispatch_get_main_queue(), NULL, main_work);
20 }
21
22
23 int main(void) {
24 global_count = 0;
25
26 dispatch_queue_t dq = dispatch_queue_create("foo.bar", NULL);
27 dispatch_async(dq, ^{
28
29 dispatch_async_f(dispatch_get_main_queue(), NULL, main_work);
30
31 int i;
32 for (i=0; i<5; ++i) {
33 dispatch_sync(dispatch_get_main_queue(), ^{
34 printf("Calling sync %d\n", i);
35 assert(pthread_main_np() == 1);
36 if (i==4) {
37 global_count = 20;
38 }
39 });
40 }
41 });
42
43 //dispatch_main();
44 CFRunLoopRun();
45 return 0;
46 }