]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_cffd.c
libdispatch-84.5.3.tar.gz
[apple/libdispatch.git] / testing / dispatch_cffd.c
1 #include <dispatch/dispatch.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <sys/param.h>
8 #include <sys/ucred.h>
9 #include <sys/mount.h>
10 #include <sys/queue.h>
11 #include <sys/errno.h>
12 #include <sys/types.h>
13 #include <sys/event.h>
14 #include <sys/time.h>
15
16 #include <CoreServices/CoreServices.h>
17
18 #include "dispatch_test.h"
19
20 int debug = 0;
21
22 #define DEBUG(...) do { \
23 if (debug) fprintf(stderr, __VA_ARGS__); \
24 } while(0);
25
26 #define assert_errno(str, expr) do { \
27 if (!(expr)) { \
28 fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
29 exit(1); \
30 } } while(0);
31
32 int
33 init_kqueue(void)
34 {
35 int kq;
36 int res;
37 struct kevent ke;
38 static struct timespec t0;
39
40 kq = kqueue();
41 assert_errno("kqueue", kq >= 0);
42
43 EV_SET(&ke, 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, 0);
44
45 res = kevent(kq, &ke, 1, NULL, 0, &t0);
46 assert_errno("kevent", res == 0);
47
48 return kq;
49 }
50
51 int
52 read_kevent(int kq)
53 {
54 int res;
55 struct kevent ke;
56 //static struct timespec t0;
57
58 res = kevent(kq, NULL, 0, &ke, 1, NULL);
59 assert_errno("kevent", res >= 0);
60
61 fprintf(stdout, "kevent.data = %ld\n", ke.data);
62
63 return (res < 0);
64 }
65
66
67 static void
68 cffd_callback(CFFileDescriptorRef cffd,
69 CFOptionFlags callBackTypes __attribute__((unused)),
70 void *info __attribute__((unused)))
71 {
72 int kq;
73
74 kq = CFFileDescriptorGetNativeDescriptor(cffd);
75 if (read_kevent(kq) == 0) {
76 // ...
77 }
78
79 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
80 }
81
82 void
83 timer()
84 {
85 dispatch_source_t ds;
86 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
87 assert(ds);
88 dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0);
89 dispatch_source_set_event_handler(ds, ^{
90 printf("ping\n");
91 });
92 dispatch_resume(ds);
93 }
94
95 void
96 hangup()
97 {
98 dispatch_source_t ds;
99 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue());
100 assert(ds);
101 dispatch_source_set_event_handler(ds, ^{
102 printf("hangup\n");
103 });
104 dispatch_resume(ds);
105 }
106
107 int
108 main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
109 {
110 int kq;
111 CFFileDescriptorRef cffd;
112 CFRunLoopSourceRef rls;
113 CFFileDescriptorContext ctx;
114
115 test_start("CFFileDescriptor");
116
117 signal(SIGHUP, SIG_IGN);
118
119 kq = init_kqueue();
120
121 memset(&ctx, 0, sizeof(CFFileDescriptorContext));
122 cffd = CFFileDescriptorCreate(NULL, kq, 1, cffd_callback, &ctx);
123 assert(cffd);
124
125 rls = CFFileDescriptorCreateRunLoopSource(NULL, cffd, 0);
126 assert(rls);
127 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
128 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
129
130 // timer();
131 // hangup();
132
133 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, false);
134
135 test_stop();
136
137 return 0;
138 }
139