]>
git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_group.c
1 #include <dispatch/dispatch.h>
7 #include <libkern/OSAtomic.h>
9 #include "dispatch_test.h"
12 #define NSEC_PER_SEC 1000000000
16 create_group(size_t count
, int delay
)
20 dispatch_group_t group
= dispatch_group_create();
22 for (i
= 0; i
< count
; ++i
) {
23 dispatch_queue_t queue
= dispatch_queue_create(NULL
, NULL
);
26 dispatch_group_async(group
, queue
, ^{
28 fprintf(stderr
, "sleeping...\n");
30 fprintf(stderr
, "done.\n");
34 dispatch_release(queue
);
44 test_start("Dispatch Group");
46 dispatch_group_t group
;
48 group
= create_group(100, 0);
49 test_ptr_notnull("dispatch_group_async", group
);
51 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
53 // should be OK to re-use a group
54 dispatch_group_async(group
, dispatch_get_concurrent_queue(0), ^{});
55 dispatch_group_wait(group
, DISPATCH_TIME_FOREVER
);
57 dispatch_release(group
);
60 group
= create_group(3, 7);
61 test_ptr_notnull("dispatch_group_async", group
);
63 res
= dispatch_group_wait(group
, dispatch_time(DISPATCH_TIME_NOW
, 5ull * NSEC_PER_SEC
));
64 test_long("dispatch_group_wait", !res
, 0);
66 // retry after timeout (this time succeed)
67 res
= dispatch_group_wait(group
, dispatch_time(DISPATCH_TIME_NOW
, 5ull * NSEC_PER_SEC
));
68 test_long("dispatch_group_wait", res
, 0);
70 dispatch_release(group
);
73 group
= create_group(100, 0);
74 test_ptr_notnull("dispatch_group_async", group
);
76 dispatch_group_notify(group
, dispatch_get_main_queue(), ^{
77 dispatch_queue_t m
= dispatch_get_main_queue();
78 dispatch_queue_t c
= dispatch_get_current_queue();
79 test_ptr("Notification Received", m
, c
);
83 dispatch_release(group
);