asyncengine 0.0.1.testing1 → 0.0.2.alpha1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -27,36 +27,39 @@
27
27
  #ifndef UV_COMMON_H_
28
28
  #define UV_COMMON_H_
29
29
 
30
+ #include <assert.h>
31
+ #include <stddef.h>
32
+ #include <stdint.h>
33
+
30
34
  #include "uv.h"
31
35
  #include "tree.h"
32
36
 
37
+
33
38
  #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
34
39
 
40
+ #define container_of(ptr, type, member) \
41
+ ((type *) ((char *) (ptr) - offsetof(type, member)))
35
42
 
36
- struct uv_ares_task_s {
37
- UV_HANDLE_FIELDS
38
- #if _WIN32
39
- struct uv_req_s ares_req;
40
- SOCKET sock;
41
- HANDLE h_wait;
42
- WSAEVENT h_event;
43
- HANDLE h_close_event;
43
+ #ifdef _MSC_VER
44
+ # define UNUSED /* empty */
45
+ # define INLINE __inline
44
46
  #else
45
- int sock;
46
- ev_io read_watcher;
47
- ev_io write_watcher;
47
+ # define UNUSED __attribute__((unused))
48
+ # define INLINE inline
48
49
  #endif
49
- RB_ENTRY(uv_ares_task_s) node;
50
- };
51
-
52
-
53
- void uv_remove_ares_handle(uv_ares_task_t* handle);
54
- uv_ares_task_t* uv_find_ares_handle(uv_loop_t*, ares_socket_t sock);
55
50
 
56
- /* TODO Rename to uv_ares_task_init? */
57
- void uv_add_ares_handle(uv_loop_t* loop, uv_ares_task_t* handle);
58
51
 
59
- int uv_ares_handles_empty(uv_loop_t* loop);
52
+ #ifndef _WIN32
53
+ enum {
54
+ UV__HANDLE_INTERNAL = 0x8000,
55
+ UV__HANDLE_ACTIVE = 0x4000,
56
+ UV__HANDLE_REF = 0x2000
57
+ };
58
+ #else
59
+ # define UV__HANDLE_INTERNAL 0x80
60
+ # define UV__HANDLE_ACTIVE 0x40
61
+ # define UV__HANDLE_REF 0x20
62
+ #endif
60
63
 
61
64
  extern const uv_err_t uv_ok_;
62
65
 
@@ -83,5 +86,86 @@ int uv__tcp_connect6(uv_connect_t* req,
83
86
  struct sockaddr_in6 address,
84
87
  uv_connect_cb cb);
85
88
 
89
+ void uv__fs_poll_close(uv_fs_poll_t* handle);
90
+
91
+
92
+ UNUSED static int uv__has_active_reqs(const uv_loop_t* loop) {
93
+ return !ngx_queue_empty(&loop->active_reqs);
94
+ }
95
+
96
+ UNUSED static void uv__req_register(uv_loop_t* loop, uv_req_t* req) {
97
+ ngx_queue_insert_tail(&loop->active_reqs, &req->active_queue);
98
+ }
99
+
100
+ UNUSED static void uv__req_unregister(uv_loop_t* loop, uv_req_t* req) {
101
+ assert(uv__has_active_reqs(loop));
102
+ ngx_queue_remove(&req->active_queue);
103
+ }
104
+
105
+
106
+ UNUSED static int uv__has_active_handles(const uv_loop_t* loop) {
107
+ return loop->active_handles > 0;
108
+ }
109
+
110
+ UNUSED static void uv__active_handle_add(uv_handle_t* h) {
111
+ h->loop->active_handles++;
112
+ }
113
+
114
+ UNUSED static void uv__active_handle_rm(uv_handle_t* h) {
115
+ h->loop->active_handles--;
116
+ }
117
+
118
+
119
+ #define uv__active_handle_add(h) uv__active_handle_add((uv_handle_t*)(h))
120
+ #define uv__active_handle_rm(h) uv__active_handle_rm((uv_handle_t*)(h))
121
+
122
+ #define uv__req_register(loop, req) uv__req_register((loop), (uv_req_t*)(req))
123
+ #define uv__req_unregister(loop, req) uv__req_unregister((loop), (uv_req_t*)(req))
124
+
125
+ UNUSED static int uv__is_active(const uv_handle_t* h) {
126
+ return !!(h->flags & UV__HANDLE_ACTIVE);
127
+ }
128
+ #define uv__is_active(h) uv__is_active((const uv_handle_t*)(h))
129
+
130
+ UNUSED static void uv__handle_start(uv_handle_t* h) {
131
+ if (h->flags & UV__HANDLE_ACTIVE) return;
132
+ if (h->flags & UV__HANDLE_REF) uv__active_handle_add(h);
133
+ h->flags |= UV__HANDLE_ACTIVE;
134
+ }
135
+ #define uv__handle_start(h) uv__handle_start((uv_handle_t*)(h))
136
+
137
+ UNUSED static void uv__handle_stop(uv_handle_t* h) {
138
+ if (!(h->flags & UV__HANDLE_ACTIVE)) return;
139
+ if (h->flags & UV__HANDLE_REF) uv__active_handle_rm(h);
140
+ h->flags &= ~UV__HANDLE_ACTIVE;
141
+ }
142
+ #define uv__handle_stop(h) uv__handle_stop((uv_handle_t*)(h))
143
+
144
+ UNUSED static void uv__handle_ref(uv_handle_t* h) {
145
+ if (h->flags & UV__HANDLE_REF) return;
146
+ if (h->flags & UV__HANDLE_ACTIVE) uv__active_handle_add(h);
147
+ h->flags |= UV__HANDLE_REF;
148
+ }
149
+ #define uv__handle_ref(h) uv__handle_ref((uv_handle_t*)(h))
150
+
151
+ UNUSED static void uv__handle_unref(uv_handle_t* h) {
152
+ if (!(h->flags & UV__HANDLE_REF)) return;
153
+ if (h->flags & UV__HANDLE_ACTIVE) uv__active_handle_rm(h);
154
+ h->flags &= ~UV__HANDLE_REF;
155
+ }
156
+ #define uv__handle_unref(h) uv__handle_unref((uv_handle_t*)(h))
157
+
158
+ UNUSED static void uv__handle_init(uv_loop_t* loop,
159
+ uv_handle_t* handle,
160
+ uv_handle_type type) {
161
+ loop->counters.handle_init++;
162
+ handle->loop = loop;
163
+ handle->type = type;
164
+ handle->flags = UV__HANDLE_REF; /* ref the loop when active */
165
+ ngx_queue_insert_tail(&loop->handle_queue, &handle->handle_queue);
166
+ #ifndef _WIN32
167
+ handle->next_closing = NULL;
168
+ #endif
169
+ }
86
170
 
87
171
  #endif /* UV_COMMON_H_ */
@@ -23,6 +23,8 @@
23
23
 
24
24
  #include "uv.h"
25
25
  #include "internal.h"
26
+ #include "handle-inl.h"
27
+ #include "req-inl.h"
26
28
 
27
29
 
28
30
  /* Atomic set operation on char */
@@ -58,13 +60,8 @@ void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
58
60
  if (handle->flags & UV_HANDLE_CLOSING &&
59
61
  !handle->async_sent) {
60
62
  assert(!(handle->flags & UV_HANDLE_CLOSED));
61
- handle->flags |= UV_HANDLE_CLOSED;
62
-
63
- if (handle->close_cb) {
64
- handle->close_cb((uv_handle_t*)handle);
65
- }
66
-
67
- uv_unref(loop);
63
+ uv__handle_stop(handle);
64
+ uv__handle_close(handle);
68
65
  }
69
66
  }
70
67
 
@@ -72,12 +69,7 @@ void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
72
69
  int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
73
70
  uv_req_t* req;
74
71
 
75
- loop->counters.handle_init++;
76
- loop->counters.async_init++;
77
-
78
- handle->type = UV_ASYNC;
79
- handle->loop = loop;
80
- handle->flags = 0;
72
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_ASYNC);
81
73
  handle->async_sent = 0;
82
74
  handle->async_cb = async_cb;
83
75
 
@@ -86,12 +78,23 @@ int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
86
78
  req->type = UV_WAKEUP;
87
79
  req->data = handle;
88
80
 
89
- uv_ref(loop);
81
+ loop->counters.async_init++;
82
+
83
+ uv__handle_start(handle);
90
84
 
91
85
  return 0;
92
86
  }
93
87
 
94
88
 
89
+ void uv_async_close(uv_loop_t* loop, uv_async_t* handle) {
90
+ if (!((uv_async_t*)handle)->async_sent) {
91
+ uv_want_endgame(loop, (uv_handle_t*) handle);
92
+ }
93
+
94
+ uv__handle_start(handle);
95
+ }
96
+
97
+
95
98
  int uv_async_send(uv_async_t* handle) {
96
99
  uv_loop_t* loop = handle->loop;
97
100
 
@@ -118,10 +121,10 @@ void uv_process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle,
118
121
  assert(req->type == UV_WAKEUP);
119
122
 
120
123
  handle->async_sent = 0;
121
- if (handle->async_cb) {
124
+
125
+ if (!(handle->flags & UV_HANDLE_CLOSING)) {
122
126
  handle->async_cb((uv_async_t*) handle, 0);
123
- }
124
- if (handle->flags & UV_HANDLE_CLOSING) {
127
+ } else {
125
128
  uv_want_endgame(loop, (uv_handle_t*)handle);
126
129
  }
127
130
  }
@@ -27,8 +27,9 @@
27
27
  #include <string.h>
28
28
 
29
29
  #include "uv.h"
30
- #include "../uv-common.h"
31
30
  #include "internal.h"
31
+ #include "handle-inl.h"
32
+ #include "req-inl.h"
32
33
 
33
34
 
34
35
  /* The only event loop we support right now */
@@ -44,17 +45,21 @@ static void uv_init(void) {
44
45
  SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
45
46
  SEM_NOOPENFILEERRORBOX);
46
47
 
48
+ /* Fetch winapi function pointers. This must be done first because other */
49
+ /* intialization code might need these function pointers to be loaded. */
50
+ uv_winapi_init();
51
+
47
52
  /* Initialize winsock */
48
53
  uv_winsock_init();
49
54
 
50
- /* Fetch winapi function pointers */
51
- uv_winapi_init();
52
-
53
55
  /* Initialize FS */
54
56
  uv_fs_init();
55
57
 
56
58
  /* Initialize console */
57
59
  uv_console_init();
60
+
61
+ /* Initialize utilities */
62
+ uv__util_init();
58
63
  }
59
64
 
60
65
 
@@ -65,16 +70,17 @@ static void uv_loop_init(uv_loop_t* loop) {
65
70
  uv_fatal_error(GetLastError(), "CreateIoCompletionPort");
66
71
  }
67
72
 
68
- loop->refs = 0;
69
-
70
73
  uv_update_time(loop);
71
74
 
75
+ ngx_queue_init(&loop->handle_queue);
76
+ ngx_queue_init(&loop->active_reqs);
77
+ loop->active_handles = 0;
78
+
72
79
  loop->pending_reqs_tail = NULL;
73
80
 
74
81
  loop->endgame_handles = NULL;
75
82
 
76
83
  RB_INIT(&loop->timers);
77
- RB_INIT(&loop->uv_ares_handles_);
78
84
 
79
85
  loop->check_handles = NULL;
80
86
  loop->prepare_handles = NULL;
@@ -84,25 +90,34 @@ static void uv_loop_init(uv_loop_t* loop) {
84
90
  loop->next_check_handle = NULL;
85
91
  loop->next_idle_handle = NULL;
86
92
 
87
- loop->ares_active_sockets = 0;
88
- loop->ares_chan = NULL;
93
+ memset(&loop->poll_peer_sockets, 0, sizeof loop->poll_peer_sockets);
94
+
95
+ loop->channel = NULL;
96
+ RB_INIT(&loop->ares_handles);
89
97
 
90
98
  loop->active_tcp_streams = 0;
91
99
  loop->active_udp_streams = 0;
92
100
 
93
101
  loop->last_err = uv_ok_;
102
+
103
+ memset(&loop->counters, 0, sizeof loop->counters);
94
104
  }
95
105
 
96
106
 
97
107
  static void uv_default_loop_init(void) {
98
108
  /* Initialize libuv itself first */
99
- uv_once(&uv_init_guard_, uv_init);
109
+ uv__once_init();
100
110
 
101
111
  /* Initialize the main loop */
102
112
  uv_loop_init(&uv_default_loop_);
103
113
  }
104
114
 
105
115
 
116
+ void uv__once_init(void) {
117
+ uv_once(&uv_init_guard_, uv_init);
118
+ }
119
+
120
+
106
121
  uv_loop_t* uv_default_loop(void) {
107
122
  uv_once(&uv_default_loop_init_guard_, uv_default_loop_init);
108
123
  return &uv_default_loop_;
@@ -113,7 +128,7 @@ uv_loop_t* uv_loop_new(void) {
113
128
  uv_loop_t* loop;
114
129
 
115
130
  /* Initialize libuv itself first */
116
- uv_once(&uv_init_guard_, uv_init);
131
+ uv__once_init();
117
132
 
118
133
  loop = (uv_loop_t*)malloc(sizeof(uv_loop_t));
119
134
 
@@ -128,26 +143,19 @@ uv_loop_t* uv_loop_new(void) {
128
143
 
129
144
  void uv_loop_delete(uv_loop_t* loop) {
130
145
  if (loop != &uv_default_loop_) {
146
+ int i;
147
+ for (i = 0; i < ARRAY_SIZE(loop->poll_peer_sockets); i++) {
148
+ SOCKET sock = loop->poll_peer_sockets[i];
149
+ if (sock != 0 && sock != INVALID_SOCKET) {
150
+ closesocket(sock);
151
+ }
152
+ }
153
+
131
154
  free(loop);
132
155
  }
133
156
  }
134
157
 
135
158
 
136
- int uv_loop_refcount(const uv_loop_t* loop) {
137
- return loop->refs;
138
- }
139
-
140
-
141
- void uv_ref(uv_loop_t* loop) {
142
- loop->refs++;
143
- }
144
-
145
-
146
- void uv_unref(uv_loop_t* loop) {
147
- loop->refs--;
148
- }
149
-
150
-
151
159
  static void uv_poll(uv_loop_t* loop, int block) {
152
160
  BOOL success;
153
161
  DWORD bytes, timeout;
@@ -214,6 +222,11 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
214
222
  }
215
223
  }
216
224
 
225
+ #define UV_LOOP_ALIVE(loop) \
226
+ ((loop)->active_handles > 0 || \
227
+ !ngx_queue_empty(&(loop)->active_reqs) || \
228
+ (loop)->endgame_handles != NULL)
229
+
217
230
  #define UV_LOOP_ONCE(loop, poll) \
218
231
  do { \
219
232
  uv_update_time((loop)); \
@@ -228,7 +241,7 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
228
241
  uv_process_reqs((loop)); \
229
242
  uv_process_endgames((loop)); \
230
243
  \
231
- if ((loop)->refs <= 0) { \
244
+ if (!UV_LOOP_ALIVE((loop))) { \
232
245
  break; \
233
246
  } \
234
247
  \
@@ -237,13 +250,13 @@ static void uv_poll_ex(uv_loop_t* loop, int block) {
237
250
  poll((loop), (loop)->idle_handles == NULL && \
238
251
  (loop)->pending_reqs_tail == NULL && \
239
252
  (loop)->endgame_handles == NULL && \
240
- (loop)->refs > 0); \
253
+ UV_LOOP_ALIVE((loop))); \
241
254
  \
242
255
  uv_check_invoke((loop)); \
243
256
  } while (0);
244
257
 
245
258
  #define UV_LOOP(loop, poll) \
246
- while ((loop)->refs > 0) { \
259
+ while (UV_LOOP_ALIVE((loop))) { \
247
260
  UV_LOOP_ONCE(loop, poll) \
248
261
  }
249
262
 
@@ -254,7 +267,7 @@ int uv_run_once(uv_loop_t* loop) {
254
267
  } else {
255
268
  UV_LOOP_ONCE(loop, uv_poll);
256
269
  }
257
- return 0;
270
+ return UV_LOOP_ALIVE(loop);
258
271
  }
259
272
 
260
273
 
@@ -265,6 +278,6 @@ int uv_run(uv_loop_t* loop) {
265
278
  UV_LOOP(loop, uv_poll);
266
279
  }
267
280
 
268
- assert(loop->refs == 0);
281
+ assert(!UV_LOOP_ALIVE((loop)));
269
282
  return 0;
270
283
  }
@@ -22,61 +22,65 @@
22
22
  #include "uv.h"
23
23
  #include "internal.h"
24
24
 
25
- __declspec( thread ) DWORD saved_errno = 0;
25
+ static int uv__dlerror(uv_lib_t* lib, int errorno);
26
26
 
27
- uv_err_t uv_dlopen(const char* filename, uv_lib_t* library) {
27
+
28
+ int uv_dlopen(const char* filename, uv_lib_t* lib) {
28
29
  wchar_t filename_w[32768];
29
- HMODULE handle;
30
30
 
31
- if (!uv_utf8_to_utf16(filename,
32
- filename_w,
33
- sizeof(filename_w) / sizeof(wchar_t))) {
34
- saved_errno = GetLastError();
35
- return uv__new_sys_error(saved_errno);
31
+ lib->handle = NULL;
32
+ lib->errmsg = NULL;
33
+
34
+ if (!uv_utf8_to_utf16(filename, filename_w, ARRAY_SIZE(filename_w))) {
35
+ return uv__dlerror(lib, GetLastError());
36
36
  }
37
37
 
38
- handle = LoadLibraryW(filename_w);
39
- if (handle == NULL) {
40
- saved_errno = GetLastError();
41
- return uv__new_sys_error(saved_errno);
38
+ lib->handle = LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
39
+ if (lib->handle == NULL) {
40
+ return uv__dlerror(lib, GetLastError());
42
41
  }
43
42
 
44
- *library = handle;
45
- return uv_ok_;
43
+ return 0;
46
44
  }
47
45
 
48
46
 
49
- uv_err_t uv_dlclose(uv_lib_t library) {
50
- if (!FreeLibrary(library)) {
51
- saved_errno = GetLastError();
52
- return uv__new_sys_error(saved_errno);
47
+ void uv_dlclose(uv_lib_t* lib) {
48
+ if (lib->errmsg) {
49
+ LocalFree((void*)lib->errmsg);
50
+ lib->errmsg = NULL;
53
51
  }
54
52
 
55
- return uv_ok_;
53
+ if (lib->handle) {
54
+ /* Ignore errors. No good way to signal them without leaking memory. */
55
+ FreeLibrary(lib->handle);
56
+ lib->handle = NULL;
57
+ }
56
58
  }
57
59
 
58
60
 
59
- uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr) {
60
- FARPROC proc = GetProcAddress(library, name);
61
- if (proc == NULL) {
62
- saved_errno = GetLastError();
63
- return uv__new_sys_error(saved_errno);
64
- }
65
-
66
- *ptr = (void*) proc;
67
- return uv_ok_;
61
+ int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) {
62
+ *ptr = (void*) GetProcAddress(lib->handle, name);
63
+ return uv__dlerror(lib, *ptr ? 0 : GetLastError());
68
64
  }
69
65
 
70
66
 
71
- const char *uv_dlerror(uv_lib_t library) {
72
- char* buf = NULL;
73
- FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
74
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, saved_errno,
75
- MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&buf, 0, NULL);
76
- return buf;
67
+ const char* uv_dlerror(uv_lib_t* lib) {
68
+ return lib->errmsg ? lib->errmsg : "no error";
77
69
  }
78
70
 
79
71
 
80
- void uv_dlerror_free(uv_lib_t library, const char *msg) {
81
- LocalFree((LPVOID)msg);
72
+ static int uv__dlerror(uv_lib_t* lib, int errorno) {
73
+ if (lib->errmsg) {
74
+ LocalFree((void*)lib->errmsg);
75
+ lib->errmsg = NULL;
76
+ }
77
+
78
+ if (errorno) {
79
+ FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
80
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
81
+ MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
82
+ (LPSTR)&lib->errmsg, 0, NULL);
83
+ }
84
+
85
+ return errorno ? -1 : 0;
82
86
  }