Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_cffd.c
1 /*
2  * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
3  *
4  * @APPLE_APACHE_LICENSE_HEADER_START@
5  * 
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * 
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * 
18  * @APPLE_APACHE_LICENSE_HEADER_END@
19  */
20
21 #include "config/config.h"
22
23 #include <dispatch/dispatch.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/param.h>
30 #include <sys/ucred.h>
31 #include <sys/mount.h>
32 #include <sys/queue.h>
33 #include <sys/errno.h>
34 #include <sys/types.h>
35 #include <sys/event.h>
36 #include <sys/time.h>
37
38 #include <CoreServices/CoreServices.h>
39
40 #include "dispatch_test.h"
41
42 int debug = 0;
43
44 #define DEBUG(...) do { \
45                 if (debug) fprintf(stderr, __VA_ARGS__); \
46         } while(0);
47
48 #define assert_errno(str, expr) do { \
49         if (!(expr)) { \
50                 fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
51                 exit(1); \
52         } } while(0);
53
54 int
55 init_kqueue(void)
56 {
57         int kq;
58         int res;
59         struct kevent ke;
60         static struct timespec t0;
61
62         kq = kqueue();
63         assert_errno("kqueue", kq >= 0);
64         
65         EV_SET(&ke, 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, 0);
66         
67         res = kevent(kq, &ke, 1, NULL, 0, &t0);
68         assert_errno("kevent", res == 0);
69
70         return kq;
71 }
72
73 int
74 read_kevent(int kq)
75 {
76         int res;
77         struct kevent ke;
78         //static struct timespec t0;
79
80         res = kevent(kq, NULL, 0, &ke, 1, NULL);
81         assert_errno("kevent", res >= 0);
82
83         fprintf(stdout, "kevent.data = %ld\n", ke.data);
84
85         return (res < 0);
86 }
87
88
89 static void
90 cffd_callback(CFFileDescriptorRef cffd,
91         CFOptionFlags callBackTypes __attribute__((unused)),
92         void *info __attribute__((unused)))
93 {
94         int kq;
95
96         kq = CFFileDescriptorGetNativeDescriptor(cffd);
97         if (read_kevent(kq) == 0) {
98                 // ...
99         }
100  
101         CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
102 }
103
104 void
105 timer()
106 {
107         dispatch_source_t ds;
108         ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
109         assert(ds);
110         dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0);
111         dispatch_source_set_event_handler(ds, ^{
112                 printf("ping\n");
113         });
114         dispatch_resume(ds);
115 }
116
117 void
118 hangup()
119 {
120         dispatch_source_t ds;
121         ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue());
122         assert(ds);
123         dispatch_source_set_event_handler(ds, ^{
124                 printf("hangup\n");
125         });
126         dispatch_resume(ds);
127 }
128
129 int
130 main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
131 {
132         int kq;
133         CFFileDescriptorRef cffd;
134         CFRunLoopSourceRef  rls;
135         CFFileDescriptorContext ctx;
136
137         test_start("CFFileDescriptor");
138
139         signal(SIGHUP, SIG_IGN);
140
141         kq = init_kqueue();
142
143         memset(&ctx, 0, sizeof(CFFileDescriptorContext));
144         cffd = CFFileDescriptorCreate(NULL, kq, 1, cffd_callback, &ctx);
145         assert(cffd);
146     
147         rls = CFFileDescriptorCreateRunLoopSource(NULL, cffd, 0);
148         assert(rls);
149         CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
150         CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
151
152 //      timer();
153 //      hangup();
154         
155         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, false);
156
157         test_stop();
158
159         return 0;
160 }
161