asyncengine 0.0.1.testing1 → 0.0.2.alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (251) hide show
  1. data/README.markdown +3 -0
  2. data/Rakefile +38 -0
  3. data/asyncengine.gemspec +8 -4
  4. data/ext/asyncengine/ae_call_from_other_thread.c +106 -0
  5. data/ext/asyncengine/ae_call_from_other_thread.h +12 -0
  6. data/ext/asyncengine/ae_handle_common.c +193 -48
  7. data/ext/asyncengine/ae_handle_common.h +40 -13
  8. data/ext/asyncengine/ae_ip_utils.c +246 -0
  9. data/ext/asyncengine/ae_ip_utils.h +25 -0
  10. data/ext/asyncengine/ae_next_tick.c +81 -21
  11. data/ext/asyncengine/ae_next_tick.h +4 -2
  12. data/ext/asyncengine/ae_resolver.c +156 -0
  13. data/ext/asyncengine/ae_resolver.h +10 -0
  14. data/ext/asyncengine/ae_tcp.c +908 -0
  15. data/ext/asyncengine/ae_tcp.h +20 -0
  16. data/ext/asyncengine/ae_timer.c +355 -81
  17. data/ext/asyncengine/ae_timer.h +11 -4
  18. data/ext/asyncengine/ae_udp.c +579 -13
  19. data/ext/asyncengine/ae_udp.h +15 -2
  20. data/ext/asyncengine/ae_utils.c +192 -0
  21. data/ext/asyncengine/ae_utils.h +16 -0
  22. data/ext/asyncengine/asyncengine_ruby.c +469 -26
  23. data/ext/asyncengine/asyncengine_ruby.h +49 -11
  24. data/ext/asyncengine/debug.h +68 -0
  25. data/ext/asyncengine/extconf.rb +26 -2
  26. data/ext/asyncengine/ip_parser.c +5954 -0
  27. data/ext/asyncengine/ip_parser.h +16 -0
  28. data/ext/asyncengine/libuv/AUTHORS +16 -0
  29. data/ext/asyncengine/libuv/common.gypi +4 -4
  30. data/ext/asyncengine/libuv/config-mingw.mk +6 -6
  31. data/ext/asyncengine/libuv/config-unix.mk +13 -13
  32. data/ext/asyncengine/libuv/gyp_uv +5 -1
  33. data/ext/asyncengine/libuv/ibc_tests/exec_test.sh +8 -0
  34. data/ext/asyncengine/libuv/ibc_tests/uv_shutdown_write_issue.c +171 -0
  35. data/ext/asyncengine/libuv/ibc_tests/uv_tcp_close_while_connecting.c +102 -0
  36. data/ext/asyncengine/libuv/include/uv-private/ngx-queue.h +3 -1
  37. data/ext/asyncengine/libuv/include/uv-private/uv-unix.h +103 -50
  38. data/ext/asyncengine/libuv/include/uv-private/uv-win.h +76 -24
  39. data/ext/asyncengine/libuv/include/uv.h +353 -88
  40. data/ext/asyncengine/libuv/src/ares/ares__close_sockets.o +0 -0
  41. data/ext/asyncengine/libuv/src/ares/ares__get_hostent.o +0 -0
  42. data/ext/asyncengine/libuv/src/ares/ares__read_line.o +0 -0
  43. data/ext/asyncengine/libuv/src/ares/ares__timeval.o +0 -0
  44. data/ext/asyncengine/libuv/src/ares/ares_cancel.o +0 -0
  45. data/ext/asyncengine/libuv/src/ares/ares_data.o +0 -0
  46. data/ext/asyncengine/libuv/src/ares/ares_destroy.o +0 -0
  47. data/ext/asyncengine/libuv/src/ares/ares_expand_name.o +0 -0
  48. data/ext/asyncengine/libuv/src/ares/ares_expand_string.o +0 -0
  49. data/ext/asyncengine/libuv/src/ares/ares_fds.o +0 -0
  50. data/ext/asyncengine/libuv/src/ares/ares_free_hostent.o +0 -0
  51. data/ext/asyncengine/libuv/src/ares/ares_free_string.o +0 -0
  52. data/ext/asyncengine/libuv/src/ares/ares_gethostbyaddr.o +0 -0
  53. data/ext/asyncengine/libuv/src/ares/ares_gethostbyname.o +0 -0
  54. data/ext/asyncengine/libuv/src/ares/ares_getnameinfo.o +0 -0
  55. data/ext/asyncengine/libuv/src/ares/ares_getopt.o +0 -0
  56. data/ext/asyncengine/libuv/src/ares/ares_getsock.o +0 -0
  57. data/ext/asyncengine/libuv/src/ares/ares_init.o +0 -0
  58. data/ext/asyncengine/libuv/src/ares/ares_library_init.o +0 -0
  59. data/ext/asyncengine/libuv/src/ares/ares_llist.o +0 -0
  60. data/ext/asyncengine/libuv/src/ares/ares_mkquery.o +0 -0
  61. data/ext/asyncengine/libuv/src/ares/ares_nowarn.o +0 -0
  62. data/ext/asyncengine/libuv/src/ares/ares_options.o +0 -0
  63. data/ext/asyncengine/libuv/src/ares/ares_parse_a_reply.o +0 -0
  64. data/ext/asyncengine/libuv/src/ares/ares_parse_aaaa_reply.o +0 -0
  65. data/ext/asyncengine/libuv/src/ares/ares_parse_mx_reply.o +0 -0
  66. data/ext/asyncengine/libuv/src/ares/ares_parse_ns_reply.o +0 -0
  67. data/ext/asyncengine/libuv/src/ares/ares_parse_ptr_reply.o +0 -0
  68. data/ext/asyncengine/libuv/src/ares/ares_parse_srv_reply.o +0 -0
  69. data/ext/asyncengine/libuv/src/ares/ares_parse_txt_reply.o +0 -0
  70. data/ext/asyncengine/libuv/src/ares/ares_process.o +0 -0
  71. data/ext/asyncengine/libuv/src/ares/ares_query.o +0 -0
  72. data/ext/asyncengine/libuv/src/ares/ares_search.o +0 -0
  73. data/ext/asyncengine/libuv/src/ares/ares_send.o +0 -0
  74. data/ext/asyncengine/libuv/src/ares/ares_strcasecmp.o +0 -0
  75. data/ext/asyncengine/libuv/src/ares/ares_strdup.o +0 -0
  76. data/ext/asyncengine/libuv/src/ares/ares_strerror.o +0 -0
  77. data/ext/asyncengine/libuv/src/ares/ares_timeout.o +0 -0
  78. data/ext/asyncengine/libuv/src/ares/ares_version.o +0 -0
  79. data/ext/asyncengine/libuv/src/ares/ares_writev.o +0 -0
  80. data/ext/asyncengine/libuv/src/ares/bitncmp.o +0 -0
  81. data/ext/asyncengine/libuv/src/ares/inet_net_pton.o +0 -0
  82. data/ext/asyncengine/libuv/src/ares/inet_ntop.o +0 -0
  83. data/ext/asyncengine/libuv/src/cares.c +225 -0
  84. data/ext/asyncengine/libuv/src/cares.o +0 -0
  85. data/ext/asyncengine/libuv/src/fs-poll.c +237 -0
  86. data/ext/asyncengine/libuv/src/fs-poll.o +0 -0
  87. data/ext/asyncengine/libuv/src/unix/async.c +78 -17
  88. data/ext/asyncengine/libuv/src/unix/async.o +0 -0
  89. data/ext/asyncengine/libuv/src/unix/core.c +305 -213
  90. data/ext/asyncengine/libuv/src/unix/core.o +0 -0
  91. data/ext/asyncengine/libuv/src/unix/cygwin.c +1 -1
  92. data/ext/asyncengine/libuv/src/unix/darwin.c +2 -1
  93. data/ext/asyncengine/libuv/src/unix/dl.c +36 -44
  94. data/ext/asyncengine/libuv/src/unix/dl.o +0 -0
  95. data/ext/asyncengine/libuv/src/unix/eio/eio.o +0 -0
  96. data/ext/asyncengine/libuv/src/unix/error.c +6 -0
  97. data/ext/asyncengine/libuv/src/unix/error.o +0 -0
  98. data/ext/asyncengine/libuv/src/unix/ev/ev.c +8 -4
  99. data/ext/asyncengine/libuv/src/unix/ev/ev.o +0 -0
  100. data/ext/asyncengine/libuv/src/unix/freebsd.c +1 -1
  101. data/ext/asyncengine/libuv/src/unix/fs.c +25 -33
  102. data/ext/asyncengine/libuv/src/unix/fs.o +0 -0
  103. data/ext/asyncengine/libuv/src/unix/internal.h +50 -31
  104. data/ext/asyncengine/libuv/src/unix/kqueue.c +2 -7
  105. data/ext/asyncengine/libuv/src/unix/linux/core.o +0 -0
  106. data/ext/asyncengine/libuv/src/unix/linux/inotify.c +12 -14
  107. data/ext/asyncengine/libuv/src/unix/linux/inotify.o +0 -0
  108. data/ext/asyncengine/libuv/src/unix/linux/{core.c → linux-core.c} +1 -1
  109. data/ext/asyncengine/libuv/src/unix/linux/linux-core.o +0 -0
  110. data/ext/asyncengine/libuv/src/unix/linux/syscalls.c +147 -1
  111. data/ext/asyncengine/libuv/src/unix/linux/syscalls.h +39 -2
  112. data/ext/asyncengine/libuv/src/unix/linux/syscalls.o +0 -0
  113. data/ext/asyncengine/libuv/src/unix/loop-watcher.c +63 -0
  114. data/ext/asyncengine/libuv/src/unix/loop-watcher.o +0 -0
  115. data/ext/asyncengine/libuv/src/unix/loop.c +29 -6
  116. data/ext/asyncengine/libuv/src/unix/loop.o +0 -0
  117. data/ext/asyncengine/libuv/src/unix/netbsd.c +1 -1
  118. data/ext/asyncengine/libuv/src/unix/openbsd.c +1 -1
  119. data/ext/asyncengine/libuv/src/unix/pipe.c +31 -36
  120. data/ext/asyncengine/libuv/src/unix/pipe.o +0 -0
  121. data/ext/asyncengine/libuv/src/unix/poll.c +116 -0
  122. data/ext/asyncengine/libuv/src/unix/poll.o +0 -0
  123. data/ext/asyncengine/libuv/src/unix/process.c +193 -115
  124. data/ext/asyncengine/libuv/src/unix/process.o +0 -0
  125. data/ext/asyncengine/libuv/src/unix/stream.c +146 -153
  126. data/ext/asyncengine/libuv/src/unix/stream.o +0 -0
  127. data/ext/asyncengine/libuv/src/unix/sunos.c +45 -36
  128. data/ext/asyncengine/libuv/src/unix/tcp.c +6 -5
  129. data/ext/asyncengine/libuv/src/unix/tcp.o +0 -0
  130. data/ext/asyncengine/libuv/src/unix/thread.c +82 -25
  131. data/ext/asyncengine/libuv/src/unix/thread.o +0 -0
  132. data/ext/asyncengine/libuv/src/unix/timer.c +69 -58
  133. data/ext/asyncengine/libuv/src/unix/timer.o +0 -0
  134. data/ext/asyncengine/libuv/src/unix/tty.c +3 -3
  135. data/ext/asyncengine/libuv/src/unix/tty.o +0 -0
  136. data/ext/asyncengine/libuv/src/unix/udp.c +57 -66
  137. data/ext/asyncengine/libuv/src/unix/udp.o +0 -0
  138. data/ext/asyncengine/libuv/src/unix/uv-eio.c +33 -50
  139. data/ext/asyncengine/libuv/src/unix/uv-eio.o +0 -0
  140. data/ext/asyncengine/libuv/src/uv-common.c +68 -38
  141. data/ext/asyncengine/libuv/src/uv-common.h +104 -20
  142. data/ext/asyncengine/libuv/src/uv-common.o +0 -0
  143. data/ext/asyncengine/libuv/src/win/async.c +20 -17
  144. data/ext/asyncengine/libuv/src/win/core.c +44 -31
  145. data/ext/asyncengine/libuv/src/win/dl.c +40 -36
  146. data/ext/asyncengine/libuv/src/win/error.c +21 -1
  147. data/ext/asyncengine/libuv/src/win/fs-event.c +19 -21
  148. data/ext/asyncengine/libuv/src/win/fs.c +541 -189
  149. data/ext/asyncengine/libuv/src/win/getaddrinfo.c +56 -63
  150. data/ext/asyncengine/libuv/src/win/handle-inl.h +145 -0
  151. data/ext/asyncengine/libuv/src/win/handle.c +26 -101
  152. data/ext/asyncengine/libuv/src/win/internal.h +92 -107
  153. data/ext/asyncengine/libuv/src/win/loop-watcher.c +6 -14
  154. data/ext/asyncengine/libuv/src/win/pipe.c +78 -64
  155. data/ext/asyncengine/libuv/src/win/poll.c +618 -0
  156. data/ext/asyncengine/libuv/src/win/process-stdio.c +479 -0
  157. data/ext/asyncengine/libuv/src/win/process.c +147 -274
  158. data/ext/asyncengine/libuv/src/win/req-inl.h +225 -0
  159. data/ext/asyncengine/libuv/src/win/req.c +0 -149
  160. data/ext/asyncengine/libuv/src/{unix/check.c → win/stream-inl.h} +31 -42
  161. data/ext/asyncengine/libuv/src/win/stream.c +9 -43
  162. data/ext/asyncengine/libuv/src/win/tcp.c +200 -82
  163. data/ext/asyncengine/libuv/src/win/thread.c +42 -2
  164. data/ext/asyncengine/libuv/src/win/threadpool.c +3 -2
  165. data/ext/asyncengine/libuv/src/win/timer.c +13 -63
  166. data/ext/asyncengine/libuv/src/win/tty.c +26 -20
  167. data/ext/asyncengine/libuv/src/win/udp.c +26 -17
  168. data/ext/asyncengine/libuv/src/win/util.c +312 -167
  169. data/ext/asyncengine/libuv/src/win/winapi.c +16 -1
  170. data/ext/asyncengine/libuv/src/win/winapi.h +33 -9
  171. data/ext/asyncengine/libuv/src/win/winsock.c +88 -1
  172. data/ext/asyncengine/libuv/src/win/winsock.h +36 -3
  173. data/ext/asyncengine/libuv/test/benchmark-ares.c +16 -17
  174. data/ext/asyncengine/libuv/test/benchmark-fs-stat.c +164 -0
  175. data/ext/asyncengine/libuv/test/benchmark-list.h +9 -0
  176. data/ext/asyncengine/libuv/{src/unix/prepare.c → test/benchmark-loop-count.c} +42 -33
  177. data/ext/asyncengine/libuv/test/benchmark-million-timers.c +65 -0
  178. data/ext/asyncengine/libuv/test/benchmark-pound.c +1 -1
  179. data/ext/asyncengine/libuv/test/benchmark-sizes.c +2 -0
  180. data/ext/asyncengine/libuv/test/benchmark-spawn.c +7 -1
  181. data/ext/asyncengine/libuv/test/benchmark-udp-packet-storm.c +1 -1
  182. data/ext/asyncengine/libuv/test/echo-server.c +8 -0
  183. data/ext/asyncengine/libuv/test/run-tests.c +30 -0
  184. data/ext/asyncengine/libuv/test/runner-unix.c +6 -26
  185. data/ext/asyncengine/libuv/test/runner-win.c +5 -63
  186. data/ext/asyncengine/libuv/test/runner.c +10 -1
  187. data/ext/asyncengine/libuv/test/task.h +0 -8
  188. data/ext/asyncengine/libuv/test/test-async.c +43 -141
  189. data/ext/asyncengine/libuv/test/test-callback-order.c +76 -0
  190. data/ext/asyncengine/libuv/test/test-counters-init.c +2 -3
  191. data/ext/asyncengine/libuv/test/test-dlerror.c +17 -8
  192. data/ext/asyncengine/libuv/test/test-fs-event.c +31 -39
  193. data/ext/asyncengine/libuv/test/test-fs-poll.c +146 -0
  194. data/ext/asyncengine/libuv/test/test-fs.c +114 -2
  195. data/ext/asyncengine/libuv/test/test-gethostbyname.c +8 -8
  196. data/ext/asyncengine/libuv/test/test-hrtime.c +18 -15
  197. data/ext/asyncengine/libuv/test/test-ipc.c +8 -2
  198. data/ext/asyncengine/libuv/test/test-list.h +59 -9
  199. data/ext/asyncengine/libuv/test/test-loop-handles.c +2 -25
  200. data/ext/asyncengine/libuv/{src/unix/idle.c → test/test-poll-close.c} +37 -39
  201. data/ext/asyncengine/libuv/test/test-poll.c +573 -0
  202. data/ext/asyncengine/libuv/test/test-ref.c +79 -63
  203. data/ext/asyncengine/libuv/test/test-run-once.c +15 -11
  204. data/ext/asyncengine/libuv/test/test-semaphore.c +111 -0
  205. data/ext/asyncengine/libuv/test/test-spawn.c +368 -20
  206. data/ext/asyncengine/libuv/test/test-stdio-over-pipes.c +25 -35
  207. data/ext/asyncengine/libuv/test/test-tcp-close-while-connecting.c +80 -0
  208. data/ext/asyncengine/libuv/test/test-tcp-close.c +1 -1
  209. data/ext/asyncengine/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  210. data/ext/asyncengine/libuv/test/test-tcp-connect-timeout.c +85 -0
  211. data/ext/asyncengine/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  212. data/ext/asyncengine/libuv/test/test-tcp-write-error.c +2 -2
  213. data/ext/asyncengine/libuv/test/test-tcp-writealot.c +29 -54
  214. data/ext/asyncengine/libuv/test/test-timer-again.c +1 -1
  215. data/ext/asyncengine/libuv/test/test-timer.c +23 -1
  216. data/ext/asyncengine/libuv/test/test-udp-options.c +1 -1
  217. data/ext/asyncengine/libuv/test/{test-eio-overflow.c → test-walk-handles.c} +31 -44
  218. data/ext/asyncengine/libuv/uv.gyp +26 -9
  219. data/ext/asyncengine/rb_utilities.c +54 -0
  220. data/ext/asyncengine/rb_utilities.h +63 -0
  221. data/lib/asyncengine.rb +45 -38
  222. data/lib/asyncengine/asyncengine_ext.so +0 -0
  223. data/lib/asyncengine/debug.rb +37 -0
  224. data/lib/asyncengine/handle.rb +9 -0
  225. data/lib/asyncengine/tcp.rb +28 -0
  226. data/lib/asyncengine/timer.rb +18 -28
  227. data/lib/asyncengine/udp.rb +29 -0
  228. data/lib/asyncengine/utils.rb +32 -0
  229. data/lib/asyncengine/uv_error.rb +17 -0
  230. data/lib/asyncengine/version.rb +9 -1
  231. data/test/ae_test_helper.rb +62 -0
  232. data/test/test_basic.rb +169 -0
  233. data/test/test_call_from_other_thread.rb +55 -0
  234. data/test/test_error.rb +92 -0
  235. data/test/test_ip_utils.rb +44 -0
  236. data/test/test_next_tick.rb +37 -0
  237. data/test/test_resolver.rb +51 -0
  238. data/test/test_threads.rb +69 -0
  239. data/test/test_timer.rb +95 -0
  240. data/test/test_udp.rb +216 -0
  241. data/test/test_utils.rb +49 -0
  242. metadata +84 -57
  243. data/ext/asyncengine/libuv/mkmf.log +0 -24
  244. data/ext/asyncengine/libuv/src/unix/cares.c +0 -194
  245. data/ext/asyncengine/libuv/src/unix/cares.o +0 -0
  246. data/ext/asyncengine/libuv/src/unix/check.o +0 -0
  247. data/ext/asyncengine/libuv/src/unix/idle.o +0 -0
  248. data/ext/asyncengine/libuv/src/unix/prepare.o +0 -0
  249. data/ext/asyncengine/libuv/src/win/cares.c +0 -290
  250. data/lib/asyncengine/errors.rb +0 -5
  251. data/lib/asyncengine/next_tick.rb +0 -24
@@ -28,7 +28,7 @@
28
28
  #include <assert.h>
29
29
  #include <errno.h>
30
30
 
31
- #ifdef SUNOS_HAVE_IFADDRS
31
+ #ifndef SUNOS_NO_IFADDRS
32
32
  # include <ifaddrs.h>
33
33
  #endif
34
34
  #include <net/if.h>
@@ -42,6 +42,11 @@
42
42
  #if HAVE_PORTS_FS
43
43
  # include <sys/port.h>
44
44
  # include <port.h>
45
+
46
+ # define PORT_FIRED 0x69
47
+ # define PORT_UNUSED 0x0
48
+ # define PORT_LOADED 0x99
49
+ # define PORT_DELETED -1
45
50
  #endif
46
51
 
47
52
  #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
@@ -70,7 +75,6 @@ uint64_t uv_hrtime() {
70
75
  */
71
76
  int uv_exepath(char* buffer, size_t* size) {
72
77
  ssize_t res;
73
- pid_t pid;
74
78
  char buf[128];
75
79
 
76
80
  if (buffer == NULL)
@@ -79,8 +83,7 @@ int uv_exepath(char* buffer, size_t* size) {
79
83
  if (size == NULL)
80
84
  return (-1);
81
85
 
82
- pid = getpid();
83
- (void) snprintf(buf, sizeof (buf), "/proc/%d/path/a.out", pid);
86
+ (void) snprintf(buf, sizeof(buf), "/proc/%lu/path/a.out", (unsigned long) getpid());
84
87
  res = readlink(buf, buffer, *size - 1);
85
88
 
86
89
  if (res < 0)
@@ -109,36 +112,42 @@ void uv_loadavg(double avg[3]) {
109
112
 
110
113
  #if HAVE_PORTS_FS
111
114
  static void uv__fs_event_rearm(uv_fs_event_t *handle) {
112
- if (port_associate(handle->fd,
115
+ if (handle->fd == -1)
116
+ return;
117
+
118
+ if (port_associate(handle->loop->fs_fd,
113
119
  PORT_SOURCE_FILE,
114
120
  (uintptr_t) &handle->fo,
115
121
  FILE_ATTRIB | FILE_MODIFIED,
116
- NULL) == -1) {
122
+ handle) == -1) {
117
123
  uv__set_sys_error(handle->loop, errno);
118
124
  }
125
+ handle->fd = PORT_LOADED;
119
126
  }
120
127
 
121
128
 
122
- static void uv__fs_event_read(EV_P_ ev_io* w, int revents) {
129
+ static void uv__fs_event_read(uv_loop_t* loop, uv__io_t* w, int revents) {
123
130
  uv_fs_event_t *handle;
124
131
  timespec_t timeout;
125
132
  port_event_t pe;
126
133
  int events;
127
134
  int r;
128
135
 
129
- handle = container_of(w, uv_fs_event_t, event_watcher);
136
+ (void) w;
137
+ (void) revents;
130
138
 
131
139
  do {
132
140
  /* TODO use port_getn() */
133
141
  do {
134
142
  memset(&timeout, 0, sizeof timeout);
135
- r = port_get(handle->fd, &pe, &timeout);
143
+ r = port_get(loop->fs_fd, &pe, &timeout);
136
144
  }
137
145
  while (r == -1 && errno == EINTR);
138
146
 
139
147
  if (r == -1 && errno == ETIME)
140
148
  break;
141
149
 
150
+ handle = (uv_fs_event_t *)pe.portev_user;
142
151
  assert((r == 0) && "unexpected port_get() error");
143
152
 
144
153
  events = 0;
@@ -147,12 +156,12 @@ static void uv__fs_event_read(EV_P_ ev_io* w, int revents) {
147
156
  if (pe.portev_events & ~(FILE_ATTRIB | FILE_MODIFIED))
148
157
  events |= UV_RENAME;
149
158
  assert(events != 0);
150
-
159
+ handle->fd = PORT_FIRED;
151
160
  handle->cb(handle, NULL, events, 0);
152
161
  }
153
- while (handle->fd != -1);
162
+ while (handle->fd != PORT_DELETED);
154
163
 
155
- if (handle->fd != -1)
164
+ if (handle->fd != PORT_DELETED)
156
165
  uv__fs_event_rearm(handle);
157
166
  }
158
167
 
@@ -163,42 +172,49 @@ int uv_fs_event_init(uv_loop_t* loop,
163
172
  uv_fs_event_cb cb,
164
173
  int flags) {
165
174
  int portfd;
175
+ int first_run = 0;
166
176
 
167
177
  loop->counters.fs_event_init++;
168
178
 
169
179
  /* We don't support any flags yet. */
170
180
  assert(!flags);
171
-
172
- if ((portfd = port_create()) == -1) {
173
- uv__set_sys_error(loop, errno);
174
- return -1;
181
+ if (loop->fs_fd == -1) {
182
+ if ((portfd = port_create()) == -1) {
183
+ uv__set_sys_error(loop, errno);
184
+ return -1;
185
+ }
186
+ loop->fs_fd = portfd;
187
+ first_run = 1;
175
188
  }
176
189
 
177
190
  uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
191
+ uv__handle_start(handle); /* FIXME shouldn't start automatically */
178
192
  handle->filename = strdup(filename);
179
- handle->fd = portfd;
193
+ handle->fd = PORT_UNUSED;
180
194
  handle->cb = cb;
181
195
 
182
196
  memset(&handle->fo, 0, sizeof handle->fo);
183
197
  handle->fo.fo_name = handle->filename;
184
198
  uv__fs_event_rearm(handle);
185
199
 
186
- ev_io_init(&handle->event_watcher, uv__fs_event_read, portfd, EV_READ);
187
- ev_io_start(loop->ev, &handle->event_watcher);
188
- ev_unref(loop->ev);
200
+ if (first_run) {
201
+ uv__io_init(&loop->fs_event_watcher, uv__fs_event_read, portfd, UV__IO_READ);
202
+ uv__io_start(loop, &loop->fs_event_watcher);
203
+ }
189
204
 
190
205
  return 0;
191
206
  }
192
207
 
193
208
 
194
209
  void uv__fs_event_close(uv_fs_event_t* handle) {
195
- ev_ref(handle->loop->ev);
196
- ev_io_stop(handle->loop->ev, &handle->event_watcher);
197
- close(handle->fd);
198
- handle->fd = -1;
210
+ if (handle->fd == PORT_FIRED) {
211
+ port_dissociate(handle->loop->fs_fd, PORT_SOURCE_FILE, (uintptr_t)&handle->fo);
212
+ }
213
+ handle->fd = PORT_DELETED;
199
214
  free(handle->filename);
200
215
  handle->filename = NULL;
201
216
  handle->fo.fo_name = NULL;
217
+ uv__handle_stop(handle);
202
218
  }
203
219
 
204
220
  #else /* !HAVE_PORTS_FS */
@@ -315,14 +331,6 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
315
331
  lookup_instance = 0;
316
332
  while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
317
333
  if (kstat_read(kc, ksp, NULL) == -1) {
318
- /*
319
- * It is deeply annoying, but some kstats can return errors
320
- * under otherwise routine conditions. (ACPI is one
321
- * offender; there are surely others.) To prevent these
322
- * fouled kstats from completely ruining our day, we assign
323
- * an "error" member to the return value that consists of
324
- * the strerror().
325
- */
326
334
  cpu_info->speed = 0;
327
335
  cpu_info->model = NULL;
328
336
  } else {
@@ -332,7 +340,7 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
332
340
 
333
341
  knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"brand");
334
342
  assert(knp->data_type == KSTAT_DATA_STRING);
335
- cpu_info->model = KSTAT_NAMED_STR_PTR(knp);
343
+ cpu_info->model = strdup(KSTAT_NAMED_STR_PTR(knp));
336
344
  }
337
345
 
338
346
  lookup_instance++;
@@ -391,7 +399,7 @@ void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
391
399
 
392
400
  uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
393
401
  int* count) {
394
- #ifndef SUNOS_HAVE_IFADDRS
402
+ #ifdef SUNOS_NO_IFADDRS
395
403
  return uv__new_artificial_error(UV_ENOSYS);
396
404
  #else
397
405
  struct ifaddrs *addrs, *ent;
@@ -424,7 +432,8 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
424
432
  address = *addresses;
425
433
 
426
434
  for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
427
- bzero(&ip, sizeof (ip));
435
+ memset(&ip, 0, sizeof(ip));
436
+
428
437
  if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING)) {
429
438
  continue;
430
439
  }
@@ -450,7 +459,7 @@ uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
450
459
  freeifaddrs(addrs);
451
460
 
452
461
  return uv_ok_;
453
- #endif /* SUNOS_HAVE_IFADDRS */
462
+ #endif /* SUNOS_NO_IFADDRS */
454
463
  }
455
464
 
456
465
 
@@ -50,7 +50,9 @@ static int uv__bind(uv_tcp_t* tcp,
50
50
  goto out;
51
51
  }
52
52
 
53
- if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_READABLE | UV_WRITABLE)) {
53
+ if (uv__stream_open((uv_stream_t*)tcp,
54
+ tcp->fd,
55
+ UV_STREAM_READABLE | UV_STREAM_WRITABLE)) {
54
56
  close(tcp->fd);
55
57
  tcp->fd = -1;
56
58
  status = -2;
@@ -181,7 +183,7 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
181
183
  return -1;
182
184
  }
183
185
 
184
- if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_READABLE)) {
186
+ if (uv__stream_open((uv_stream_t*)tcp, tcp->fd, UV_STREAM_READABLE)) {
185
187
  close(tcp->fd);
186
188
  tcp->fd = -1;
187
189
  return -1;
@@ -199,9 +201,8 @@ int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb) {
199
201
  tcp->connection_cb = cb;
200
202
 
201
203
  /* Start listening for connections. */
202
- ev_io_set(&tcp->read_watcher, tcp->fd, EV_READ);
203
- ev_set_cb(&tcp->read_watcher, uv__server_io);
204
- ev_io_start(tcp->loop->ev, &tcp->read_watcher);
204
+ uv__io_set(&tcp->read_watcher, uv__server_io, tcp->fd, UV__IO_READ);
205
+ uv__io_start(tcp->loop, &tcp->read_watcher);
205
206
 
206
207
  return 0;
207
208
  }
@@ -26,19 +26,6 @@
26
26
  #include <assert.h>
27
27
  #include <errno.h>
28
28
 
29
- #ifdef NDEBUG
30
- # define CHECK(r) ((void) (r))
31
- #else
32
- # include <stdio.h>
33
- # include <stdlib.h>
34
- # define CHECK(r) \
35
- do { \
36
- int __r = (r); \
37
- if (__r) errno = __r, perror(#r), abort(); \
38
- } \
39
- while (0)
40
- #endif
41
-
42
29
 
43
30
  int uv_thread_join(uv_thread_t *tid) {
44
31
  if (pthread_join(*tid, NULL))
@@ -49,20 +36,40 @@ int uv_thread_join(uv_thread_t *tid) {
49
36
 
50
37
 
51
38
  int uv_mutex_init(uv_mutex_t* mutex) {
39
+ #ifdef NDEBUG
52
40
  if (pthread_mutex_init(mutex, NULL))
53
41
  return -1;
54
42
  else
55
43
  return 0;
44
+ #else
45
+ pthread_mutexattr_t attr;
46
+ int r;
47
+
48
+ if (pthread_mutexattr_init(&attr))
49
+ abort();
50
+
51
+ if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK))
52
+ abort();
53
+
54
+ r = pthread_mutex_init(mutex, &attr);
55
+
56
+ if (pthread_mutexattr_destroy(&attr))
57
+ abort();
58
+
59
+ return r ? -1 : 0;
60
+ #endif
56
61
  }
57
62
 
58
63
 
59
64
  void uv_mutex_destroy(uv_mutex_t* mutex) {
60
- CHECK(pthread_mutex_destroy(mutex));
65
+ if (pthread_mutex_destroy(mutex))
66
+ abort();
61
67
  }
62
68
 
63
69
 
64
70
  void uv_mutex_lock(uv_mutex_t* mutex) {
65
- CHECK(pthread_mutex_lock(mutex));
71
+ if (pthread_mutex_lock(mutex))
72
+ abort();
66
73
  }
67
74
 
68
75
 
@@ -72,7 +79,7 @@ int uv_mutex_trylock(uv_mutex_t* mutex) {
72
79
  r = pthread_mutex_trylock(mutex);
73
80
 
74
81
  if (r && r != EAGAIN)
75
- CHECK(r);
82
+ abort();
76
83
 
77
84
  if (r)
78
85
  return -1;
@@ -82,7 +89,8 @@ int uv_mutex_trylock(uv_mutex_t* mutex) {
82
89
 
83
90
 
84
91
  void uv_mutex_unlock(uv_mutex_t* mutex) {
85
- CHECK(pthread_mutex_unlock(mutex));
92
+ if (pthread_mutex_unlock(mutex))
93
+ abort();
86
94
  }
87
95
 
88
96
 
@@ -95,12 +103,14 @@ int uv_rwlock_init(uv_rwlock_t* rwlock) {
95
103
 
96
104
 
97
105
  void uv_rwlock_destroy(uv_rwlock_t* rwlock) {
98
- CHECK(pthread_rwlock_destroy(rwlock));
106
+ if (pthread_rwlock_destroy(rwlock))
107
+ abort();
99
108
  }
100
109
 
101
110
 
102
111
  void uv_rwlock_rdlock(uv_rwlock_t* rwlock) {
103
- CHECK(pthread_rwlock_rdlock(rwlock));
112
+ if (pthread_rwlock_rdlock(rwlock))
113
+ abort();
104
114
  }
105
115
 
106
116
 
@@ -110,7 +120,7 @@ int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock) {
110
120
  r = pthread_rwlock_tryrdlock(rwlock);
111
121
 
112
122
  if (r && r != EAGAIN)
113
- CHECK(r);
123
+ abort();
114
124
 
115
125
  if (r)
116
126
  return -1;
@@ -120,12 +130,14 @@ int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock) {
120
130
 
121
131
 
122
132
  void uv_rwlock_rdunlock(uv_rwlock_t* rwlock) {
123
- CHECK(pthread_rwlock_unlock(rwlock));
133
+ if (pthread_rwlock_unlock(rwlock))
134
+ abort();
124
135
  }
125
136
 
126
137
 
127
138
  void uv_rwlock_wrlock(uv_rwlock_t* rwlock) {
128
- CHECK(pthread_rwlock_wrlock(rwlock));
139
+ if (pthread_rwlock_wrlock(rwlock))
140
+ abort();
129
141
  }
130
142
 
131
143
 
@@ -135,7 +147,7 @@ int uv_rwlock_trywrlock(uv_rwlock_t* rwlock) {
135
147
  r = pthread_rwlock_trywrlock(rwlock);
136
148
 
137
149
  if (r && r != EAGAIN)
138
- CHECK(r);
150
+ abort();
139
151
 
140
152
  if (r)
141
153
  return -1;
@@ -145,10 +157,55 @@ int uv_rwlock_trywrlock(uv_rwlock_t* rwlock) {
145
157
 
146
158
 
147
159
  void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) {
148
- CHECK(pthread_rwlock_unlock(rwlock));
160
+ if (pthread_rwlock_unlock(rwlock))
161
+ abort();
149
162
  }
150
163
 
151
164
 
152
165
  void uv_once(uv_once_t* guard, void (*callback)(void)) {
153
- CHECK(pthread_once(guard, callback));
166
+ if (pthread_once(guard, callback))
167
+ abort();
168
+ }
169
+
170
+
171
+ int uv_sem_init(uv_sem_t* sem, unsigned int value) {
172
+ return sem_init(sem, 0, value);
173
+ }
174
+
175
+
176
+ void uv_sem_destroy(uv_sem_t* sem) {
177
+ if (sem_destroy(sem))
178
+ abort();
179
+ }
180
+
181
+
182
+ void uv_sem_post(uv_sem_t* sem) {
183
+ if (sem_post(sem))
184
+ abort();
185
+ }
186
+
187
+
188
+ void uv_sem_wait(uv_sem_t* sem) {
189
+ int r;
190
+
191
+ do
192
+ r = sem_wait(sem);
193
+ while (r == -1 && errno == EINTR);
194
+
195
+ if (r)
196
+ abort();
197
+ }
198
+
199
+
200
+ int uv_sem_trywait(uv_sem_t* sem) {
201
+ int r;
202
+
203
+ do
204
+ r = sem_trywait(sem);
205
+ while (r == -1 && errno == EINTR);
206
+
207
+ if (r && errno != EAGAIN)
208
+ abort();
209
+
210
+ return r;
154
211
  }
@@ -22,103 +22,114 @@
22
22
  #include "internal.h"
23
23
  #include <assert.h>
24
24
 
25
-
26
- static int uv__timer_repeating(const uv_timer_t* timer) {
27
- return timer->flags & UV_TIMER_REPEAT;
25
+ static int uv__timer_cmp(const uv_timer_t* a, const uv_timer_t* b) {
26
+ if (a->timeout < b->timeout)
27
+ return -1;
28
+ if (a->timeout > b->timeout)
29
+ return 1;
30
+ if (a < b)
31
+ return -1;
32
+ if (a > b)
33
+ return 1;
34
+ return 0;
28
35
  }
29
36
 
30
37
 
31
- static void uv__timer_cb(EV_P_ ev_timer* w, int revents) {
32
- uv_timer_t* timer = container_of(w, uv_timer_t, timer_watcher);
33
-
34
- assert(uv__timer_active(timer));
35
-
36
- if (!uv__timer_repeating(timer)) {
37
- timer->flags &= ~UV_TIMER_ACTIVE;
38
- ev_ref(EV_A);
39
- }
40
-
41
- if (timer->timer_cb) {
42
- timer->timer_cb(timer, 0);
43
- }
44
- }
38
+ RB_GENERATE_STATIC(uv__timers, uv_timer_s, tree_entry, uv__timer_cmp)
45
39
 
46
40
 
47
- int uv_timer_init(uv_loop_t* loop, uv_timer_t* timer) {
48
- uv__handle_init(loop, (uv_handle_t*)timer, UV_TIMER);
41
+ int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
49
42
  loop->counters.timer_init++;
50
43
 
51
- ev_init(&timer->timer_watcher, uv__timer_cb);
44
+ uv__handle_init(loop, (uv_handle_t*)handle, UV_TIMER);
45
+ handle->timer_cb = NULL;
52
46
 
53
47
  return 0;
54
48
  }
55
49
 
56
50
 
57
- int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb, int64_t timeout,
58
- int64_t repeat) {
59
- if (uv__timer_active(timer)) {
60
- return -1;
61
- }
51
+ int uv_timer_start(uv_timer_t* handle,
52
+ uv_timer_cb cb,
53
+ int64_t timeout,
54
+ int64_t repeat) {
55
+ assert(timeout >= 0);
56
+ assert(repeat >= 0);
62
57
 
63
- timer->timer_cb = cb;
64
- timer->flags |= UV_TIMER_ACTIVE;
58
+ if (uv__is_active(handle))
59
+ uv_timer_stop(handle);
65
60
 
66
- if (repeat)
67
- timer->flags |= UV_TIMER_REPEAT;
68
- else
69
- timer->flags &= ~UV_TIMER_REPEAT;
61
+ handle->timer_cb = cb;
62
+ handle->timeout = handle->loop->time + timeout;
63
+ handle->repeat = repeat;
70
64
 
71
- ev_timer_set(&timer->timer_watcher, timeout / 1000.0, repeat / 1000.0);
72
- ev_timer_start(timer->loop->ev, &timer->timer_watcher);
73
- ev_unref(timer->loop->ev);
65
+ RB_INSERT(uv__timers, &handle->loop->timer_handles, handle);
66
+ uv__handle_start(handle);
74
67
 
75
68
  return 0;
76
69
  }
77
70
 
78
71
 
79
- int uv_timer_stop(uv_timer_t* timer) {
80
- if (uv__timer_active(timer)) {
81
- ev_ref(timer->loop->ev);
82
- }
72
+ int uv_timer_stop(uv_timer_t* handle) {
73
+ if (!uv__is_active(handle))
74
+ return 0;
83
75
 
84
- timer->flags &= ~(UV_TIMER_ACTIVE | UV_TIMER_REPEAT);
85
- ev_timer_stop(timer->loop->ev, &timer->timer_watcher);
76
+ RB_REMOVE(uv__timers, &handle->loop->timer_handles, handle);
77
+ uv__handle_stop(handle);
86
78
 
87
79
  return 0;
88
80
  }
89
81
 
90
82
 
91
- int uv_timer_again(uv_timer_t* timer) {
92
- if (!uv__timer_active(timer)) {
93
- uv__set_artificial_error(timer->loop, UV_EINVAL);
94
- return -1;
83
+ int uv_timer_again(uv_timer_t* handle) {
84
+ if (handle->timer_cb == NULL)
85
+ return uv__set_artificial_error(handle->loop, UV_EINVAL);
86
+
87
+ if (handle->repeat) {
88
+ uv_timer_stop(handle);
89
+ uv_timer_start(handle, handle->timer_cb, handle->repeat, handle->repeat);
95
90
  }
96
91
 
97
- assert(uv__timer_repeating(timer));
98
- ev_timer_again(timer->loop->ev, &timer->timer_watcher);
99
92
  return 0;
100
93
  }
101
94
 
102
95
 
103
- void uv_timer_set_repeat(uv_timer_t* timer, int64_t repeat) {
104
- assert(timer->type == UV_TIMER);
105
- timer->timer_watcher.repeat = repeat / 1000.0;
96
+ void uv_timer_set_repeat(uv_timer_t* handle, int64_t repeat) {
97
+ assert(repeat >= 0);
98
+ handle->repeat = repeat;
99
+ }
100
+
106
101
 
107
- if (repeat)
108
- timer->flags |= UV_TIMER_REPEAT;
109
- else
110
- timer->flags &= ~UV_TIMER_REPEAT;
102
+ int64_t uv_timer_get_repeat(uv_timer_t* handle) {
103
+ return handle->repeat;
111
104
  }
112
105
 
113
106
 
114
- int64_t uv_timer_get_repeat(uv_timer_t* timer) {
115
- assert(timer->type == UV_TIMER);
116
- return (int64_t)(1000 * timer->timer_watcher.repeat);
107
+ unsigned int uv__next_timeout(uv_loop_t* loop) {
108
+ uv_timer_t* handle;
109
+
110
+ handle = RB_MIN(uv__timers, &loop->timer_handles);
111
+
112
+ if (handle == NULL)
113
+ return (unsigned int) -1; /* block indefinitely */
114
+
115
+ if (handle->timeout <= loop->time)
116
+ return 0;
117
+
118
+ return handle->timeout - loop->time;
117
119
  }
118
120
 
119
121
 
120
- int uv__timer_active(const uv_timer_t* timer) {
121
- return timer->flags & UV_TIMER_ACTIVE;
122
+ void uv__run_timers(uv_loop_t* loop) {
123
+ uv_timer_t* handle;
124
+
125
+ while ((handle = RB_MIN(uv__timers, &loop->timer_handles))) {
126
+ if (handle->timeout > loop->time)
127
+ break;
128
+
129
+ uv_timer_stop(handle);
130
+ uv_timer_again(handle);
131
+ handle->timer_cb(handle, 0);
132
+ }
122
133
  }
123
134
 
124
135