5 #include <dispatch/dispatch.h>
7 #include "dispatch_test.h"
9 dispatch_source_t tweedledee
;
10 dispatch_source_t tweedledum
;
14 test_start("Dispatch Suspend Timer");
16 dispatch_queue_t main_q
= dispatch_get_main_queue();
17 test_ptr("dispatch_get_main_queue", main_q
, dispatch_get_current_queue());
22 dispatch_source_attr_t attr
= dispatch_source_attr_create();
23 test_ptr_notnull("dispatch_source_attr_create", attr
);
25 dispatch_source_attr_set_finalizer(attr
, ^(dispatch_source_t ds
) {
26 test_ptr_notnull("finalizer ran", ds
);
27 if (ds
== tweedledum
) test_stop();
30 tweedledee
= dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL
,
31 1000000000ull, 0, attr
, main_q
, ^(dispatch_event_t ev
) {
33 if (dispatch_event_get_error(ev
, &err
)) {
34 test_errno("dispatch_event_get_error", err
, ECANCELED
);
35 dispatch_release(dispatch_event_get_source(ev
));
37 fprintf(stderr
, "%d\n", ++i
);
39 dispatch_cancel(dispatch_event_get_source(ev
));
43 test_ptr_notnull("dispatch_source_timer_create", tweedledee
);
44 dispatch_retain(tweedledee
);
46 tweedledum
= dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL
,
47 3000000000ull, 0, attr
, main_q
, ^(dispatch_event_t ev
) {
49 if (dispatch_event_get_error(ev
, &err
)) {
50 test_errno("dispatch_event_get_error", err
, ECANCELED
);
51 dispatch_release(dispatch_event_get_source(ev
));
55 fprintf(stderr
, "suspending timer for 3 seconds\n");
56 dispatch_suspend(tweedledee
);
59 fprintf(stderr
, "resuming timer\n");
60 dispatch_resume(tweedledee
);
61 dispatch_release(tweedledee
);
64 dispatch_cancel(dispatch_event_get_source(ev
));
69 test_ptr_notnull("dispatch_source_timer_create", tweedledum
);
71 dispatch_release(attr
);