cffd.c 3.7 KB

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