2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #include "config/config.h"
23 #include <dispatch/dispatch.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>
38 #include <CoreServices/CoreServices.h>
40 #include "dispatch_test.h"
44 #define DEBUG(...) do { \
45 if (debug) fprintf(stderr, __VA_ARGS__); \
48 #define assert_errno(str, expr) do { \
50 fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
60 static struct timespec t0;
63 assert_errno("kqueue", kq >= 0);
65 EV_SET(&ke, 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, 0);
67 res = kevent(kq, &ke, 1, NULL, 0, &t0);
68 assert_errno("kevent", res == 0);
78 //static struct timespec t0;
80 res = kevent(kq, NULL, 0, &ke, 1, NULL);
81 assert_errno("kevent", res >= 0);
83 fprintf(stdout, "kevent.data = %ld\n", ke.data);
90 cffd_callback(CFFileDescriptorRef cffd,
91 CFOptionFlags callBackTypes __attribute__((unused)),
92 void *info __attribute__((unused)))
96 kq = CFFileDescriptorGetNativeDescriptor(cffd);
97 if (read_kevent(kq) == 0) {
101 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
107 dispatch_source_t ds;
108 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
110 dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0);
111 dispatch_source_set_event_handler(ds, ^{
120 dispatch_source_t ds;
121 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue());
123 dispatch_source_set_event_handler(ds, ^{
130 main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
133 CFFileDescriptorRef cffd;
134 CFRunLoopSourceRef rls;
135 CFFileDescriptorContext ctx;
137 test_start("CFFileDescriptor");
139 signal(SIGHUP, SIG_IGN);
143 memset(&ctx, 0, sizeof(CFFileDescriptorContext));
144 cffd = CFFileDescriptorCreate(NULL, kq, 1, cffd_callback, &ctx);
147 rls = CFFileDescriptorCreateRunLoopSource(NULL, cffd, 0);
149 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
150 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
155 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, false);