6 #include <dispatch/dispatch.h>
8 #include "dispatch_test.h"
12 test_start("Dispatch Source Timer");
14 dispatch_queue_t main_q
= dispatch_get_main_queue();
15 test_ptr("dispatch_get_main_queue", main_q
, dispatch_get_current_queue());
19 // create several timers and release them.
20 for (j
= 1; j
<= 5; ++j
)
22 dispatch_source_t s
= dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL
,
23 (uint64_t)j
* (uint64_t)1000000000ull, 0, NULL
, dispatch_get_concurrent_queue(0),
24 ^(dispatch_event_t ev
) {
25 if (!dispatch_event_get_error(ev
, NULL
)) {
26 fprintf(stderr
, "timer[%lld]\n", j
);
27 dispatch_release(dispatch_event_get_source(ev
));
30 test_ptr_notnull("dispatch_source_timer_create", s
);
33 dispatch_source_attr_t attr
= dispatch_source_attr_create();
34 dispatch_source_attr_set_finalizer(attr
, ^(dispatch_source_t ds
) {
35 test_ptr_notnull("finalizer ran", ds
);
41 dispatch_source_t s
= dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL
,
46 ^(dispatch_event_t ev
) {
48 if (dispatch_event_get_error(ev
, &err
)) {
49 test_errno("dispatch_event_get_error", err
, ECANCELED
);
50 dispatch_release(dispatch_event_get_source(ev
));
52 fprintf(stderr
, "%d\n", ++i
);
54 dispatch_cancel(dispatch_event_get_source(ev
));
58 test_ptr_notnull("dispatch_source_timer_create", s
);
60 dispatch_release(attr
);