]> git.saurik.com Git - apple/libdispatch.git/blob - testing/queue_finalizer.c
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / testing / queue_finalizer.c
1 #include <dispatch/dispatch.h>
2 #include <unistd.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <assert.h>
7
8 #include "dispatch_test.h"
9
10 int main(void) {
11 long res;
12
13 test_start("Dispatch Queue Finalizer");
14
15 #ifdef __LP64__
16 void* ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random());
17 #else
18 void* ctxt_magic = (void*)arc4random();
19 #endif
20
21 dispatch_queue_attr_t attr = dispatch_queue_attr_create();
22 test_ptr_notnull("dispatch_queue_attr_create", attr);
23
24 __block long finalizer_ran = 0;
25
26 res = dispatch_queue_attr_set_finalizer(attr, ^(dispatch_queue_t dq) {
27 void* ctxt = dispatch_queue_get_context(dq);
28 test_ptr("dispatch_queue_get_context", ctxt, ctxt_magic);
29 test_ptr_notnull("finalizer ran", dq);
30 test_stop();
31 });
32 test_long("dispatch_queue_attr_set_finalizer", res, 0);
33
34 dispatch_queue_t q = dispatch_queue_create(NULL, attr);
35 test_ptr_notnull("dispatch_queue_new", q);
36
37 dispatch_queue_set_context(q, ctxt_magic);
38
39 dispatch_release(attr);
40
41 dispatch_release(q);
42
43 dispatch_main();
44
45 return 0;
46 }