1 #include <dispatch/dispatch.h>
10 #include <sys/queue.h>
11 #include <sys/errno.h>
12 #include <sys/types.h>
13 #include <sys/event.h>
16 #include <CoreServices/CoreServices.h>
18 #include "dispatch_test.h"
22 #define DEBUG(...) do { \
23 if (debug) fprintf(stderr, __VA_ARGS__); \
26 #define assert_errno(str, expr) do { \
28 fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
38 static struct timespec t0
;
41 assert_errno("kqueue", kq
>= 0);
43 EV_SET(&ke
, 1, EVFILT_TIMER
, EV_ADD
, NOTE_SECONDS
, 1, 0);
45 res
= kevent(kq
, &ke
, 1, NULL
, 0, &t0
);
46 assert_errno("kevent", res
== 0);
56 //static struct timespec t0;
58 res
= kevent(kq
, NULL
, 0, &ke
, 1, NULL
);
59 assert_errno("kevent", res
>= 0);
61 fprintf(stdout
, "kevent.data = %ld\n", ke
.data
);
68 cffd_callback(CFFileDescriptorRef cffd
,
69 CFOptionFlags callBackTypes
__attribute__((unused
)),
70 void *info
__attribute__((unused
)))
74 kq
= CFFileDescriptorGetNativeDescriptor(cffd
);
75 if (read_kevent(kq
) == 0) {
79 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorReadCallBack
);
86 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER
, 0, 0, dispatch_get_main_queue());
88 dispatch_source_set_timer(ds
, dispatch_time(0, 1*NSEC_PER_SEC
), NSEC_PER_SEC
, 0);
89 dispatch_source_set_event_handler(ds
, ^{
99 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL
, SIGHUP
, 0, dispatch_get_main_queue());
101 dispatch_source_set_event_handler(ds
, ^{
108 main(int argc
__attribute__((unused
)), char *argv
[] __attribute__((unused
)))
111 CFFileDescriptorRef cffd
;
112 CFRunLoopSourceRef rls
;
113 CFFileDescriptorContext ctx
;
115 test_start("CFFileDescriptor");
117 signal(SIGHUP
, SIG_IGN
);
121 memset(&ctx
, 0, sizeof(CFFileDescriptorContext
));
122 cffd
= CFFileDescriptorCreate(NULL
, kq
, 1, cffd_callback
, &ctx
);
125 rls
= CFFileDescriptorCreateRunLoopSource(NULL
, cffd
, 0);
127 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
128 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorReadCallBack
);
133 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 10.0, false);