X-Git-Url: https://git.saurik.com/apple/libdispatch.git/blobdiff_plain/d4e0f4aade4f18254fb326a5aa6c521fad1930d2..24954c793bca436d612851de3a460c72ef1f7b6d:/testing/dispatch_test_sync_on_main.c diff --git a/testing/dispatch_test_sync_on_main.c b/testing/dispatch_test_sync_on_main.c new file mode 100644 index 0000000..6adba96 --- /dev/null +++ b/testing/dispatch_test_sync_on_main.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include + +int global_count; + +void +main_work(void* ctxt) +{ + if (global_count == 20) { + exit(0); + } + uint64_t time = random() % NSEC_PER_SEC; + printf("Firing timer on main %d\n", ++global_count); + dispatch_after_f(dispatch_time(0, time), dispatch_get_main_queue(), NULL, main_work); +} + + +int main(void) { + global_count = 0; + + dispatch_queue_t dq = dispatch_queue_create("foo.bar", NULL); + dispatch_async(dq, ^{ + + dispatch_async_f(dispatch_get_main_queue(), NULL, main_work); + + int i; + for (i=0; i<5; ++i) { + dispatch_sync(dispatch_get_main_queue(), ^{ + printf("Calling sync %d\n", i); + assert(pthread_main_np() == 1); + if (i==4) { + global_count = 20; + } + }); + } + }); + + //dispatch_main(); + CFRunLoopRun(); + return 0; +}