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
@@ -19,10 +19,12 @@
19
19
  * IN THE SOFTWARE.
20
20
  */
21
21
 
22
+ #include <assert.h>
23
+ #include <limits.h>
24
+
22
25
  #include "uv.h"
23
- #include "../uv-common.h"
24
26
  #include "internal.h"
25
- #include <assert.h>
27
+
26
28
 
27
29
  #define HAVE_SRWLOCK_API() (pTryAcquireSRWLockShared != NULL)
28
30
 
@@ -206,6 +208,44 @@ void uv_rwlock_wrunlock(uv_rwlock_t* rwlock) {
206
208
  }
207
209
 
208
210
 
211
+ int uv_sem_init(uv_sem_t* sem, unsigned int value) {
212
+ *sem = CreateSemaphore(NULL, value, INT_MAX, NULL);
213
+ return *sem ? 0 : -1;
214
+ }
215
+
216
+
217
+ void uv_sem_destroy(uv_sem_t* sem) {
218
+ if (!CloseHandle(*sem))
219
+ abort();
220
+ }
221
+
222
+
223
+ void uv_sem_post(uv_sem_t* sem) {
224
+ if (!ReleaseSemaphore(*sem, 1, NULL))
225
+ abort();
226
+ }
227
+
228
+
229
+ void uv_sem_wait(uv_sem_t* sem) {
230
+ if (WaitForSingleObject(*sem, INFINITE) != WAIT_OBJECT_0)
231
+ abort();
232
+ }
233
+
234
+
235
+ int uv_sem_trywait(uv_sem_t* sem) {
236
+ DWORD r = WaitForSingleObject(*sem, 0);
237
+
238
+ if (r == WAIT_OBJECT_0)
239
+ return 0;
240
+
241
+ if (r == WAIT_TIMEOUT)
242
+ return -1;
243
+
244
+ abort();
245
+ return -1; /* Satisfy the compiler. */
246
+ }
247
+
248
+
209
249
  inline static int uv__rwlock_srwlock_init(uv_rwlock_t* rwlock) {
210
250
  pInitializeSRWLock(&rwlock->srwlock_);
211
251
  return 0;
@@ -23,6 +23,7 @@
23
23
 
24
24
  #include "uv.h"
25
25
  #include "internal.h"
26
+ #include "req-inl.h"
26
27
 
27
28
 
28
29
  static void uv_work_req_init(uv_loop_t* loop, uv_work_t* req,
@@ -61,13 +62,13 @@ int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb,
61
62
  return -1;
62
63
  }
63
64
 
64
- uv_ref(loop);
65
+ uv__req_register(loop, req);
65
66
  return 0;
66
67
  }
67
68
 
68
69
 
69
70
  void uv_process_work_req(uv_loop_t* loop, uv_work_t* req) {
70
71
  assert(req->after_work_cb);
72
+ uv__req_unregister(loop, req);
71
73
  req->after_work_cb(req);
72
- uv_unref(loop);
73
74
  }
@@ -25,15 +25,7 @@
25
25
  #include "uv.h"
26
26
  #include "internal.h"
27
27
  #include "tree.h"
28
-
29
- #undef NANOSEC
30
- #define NANOSEC 1000000000
31
-
32
-
33
- /* The resolution of the high-resolution clock. */
34
- static int64_t uv_ticks_per_msec_ = 0;
35
- static uint64_t uv_hrtime_frequency_ = 0;
36
- static uv_once_t uv_hrtime_init_guard_ = UV_ONCE_INIT;
28
+ #include "handle-inl.h"
37
29
 
38
30
 
39
31
  void uv_update_time(uv_loop_t* loop) {
@@ -58,43 +50,6 @@ int64_t uv_now(uv_loop_t* loop) {
58
50
  }
59
51
 
60
52
 
61
- static void uv_hrtime_init(void) {
62
- LARGE_INTEGER frequency;
63
-
64
- if (!QueryPerformanceFrequency(&frequency)) {
65
- uv_hrtime_frequency_ = 0;
66
- return;
67
- }
68
-
69
- uv_hrtime_frequency_ = frequency.QuadPart;
70
- }
71
-
72
-
73
- uint64_t uv_hrtime(void) {
74
- LARGE_INTEGER counter;
75
-
76
- uv_once(&uv_hrtime_init_guard_, uv_hrtime_init);
77
-
78
- /* If the performance frequency is zero, there's no support. */
79
- if (!uv_hrtime_frequency_) {
80
- /* uv__set_sys_error(loop, ERROR_NOT_SUPPORTED); */
81
- return 0;
82
- }
83
-
84
- if (!QueryPerformanceCounter(&counter)) {
85
- /* uv__set_sys_error(loop, GetLastError()); */
86
- return 0;
87
- }
88
-
89
- /* Because we have no guarantee about the order of magnitude of the */
90
- /* performance counter frequency, and there may not be much headroom to */
91
- /* multiply by NANOSEC without overflowing, we use 128-bit math instead. */
92
- return ((uint64_t) counter.LowPart * NANOSEC / uv_hrtime_frequency_) +
93
- (((uint64_t) counter.HighPart * NANOSEC / uv_hrtime_frequency_)
94
- << 32);
95
- }
96
-
97
-
98
53
  static int uv_timer_compare(uv_timer_t* a, uv_timer_t* b) {
99
54
  if (a->due < b->due)
100
55
  return -1;
@@ -112,16 +67,11 @@ RB_GENERATE_STATIC(uv_timer_tree_s, uv_timer_s, tree_entry, uv_timer_compare);
112
67
 
113
68
 
114
69
  int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
115
- loop->counters.handle_init++;
116
- loop->counters.timer_init++;
117
-
118
- handle->type = UV_TIMER;
119
- handle->loop = loop;
120
- handle->flags = 0;
70
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_TIMER);
121
71
  handle->timer_cb = NULL;
122
72
  handle->repeat = 0;
123
73
 
124
- uv_ref(loop);
74
+ loop->counters.timer_init++;
125
75
 
126
76
  return 0;
127
77
  }
@@ -130,13 +80,8 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* handle) {
130
80
  void uv_timer_endgame(uv_loop_t* loop, uv_timer_t* handle) {
131
81
  if (handle->flags & UV_HANDLE_CLOSING) {
132
82
  assert(!(handle->flags & UV_HANDLE_CLOSED));
133
- handle->flags |= UV_HANDLE_CLOSED;
134
-
135
- if (handle->close_cb) {
136
- handle->close_cb((uv_handle_t*)handle);
137
- }
138
-
139
- uv_unref(loop);
83
+ uv__handle_stop(handle);
84
+ uv__handle_close(handle);
140
85
  }
141
86
  }
142
87
 
@@ -144,6 +89,7 @@ void uv_timer_endgame(uv_loop_t* loop, uv_timer_t* handle) {
144
89
  int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout,
145
90
  int64_t repeat) {
146
91
  uv_loop_t* loop = handle->loop;
92
+ uv_timer_t* old;
147
93
 
148
94
  if (handle->flags & UV_HANDLE_ACTIVE) {
149
95
  RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
@@ -153,10 +99,10 @@ int uv_timer_start(uv_timer_t* handle, uv_timer_cb timer_cb, int64_t timeout,
153
99
  handle->due = loop->time + timeout;
154
100
  handle->repeat = repeat;
155
101
  handle->flags |= UV_HANDLE_ACTIVE;
102
+ uv__handle_start(handle);
156
103
 
157
- if (RB_INSERT(uv_timer_tree_s, &loop->timers, handle) != NULL) {
158
- uv_fatal_error(ERROR_INVALID_DATA, "RB_INSERT");
159
- }
104
+ old = RB_INSERT(uv_timer_tree_s, &loop->timers, handle);
105
+ assert(old == NULL);
160
106
 
161
107
  return 0;
162
108
  }
@@ -171,6 +117,7 @@ int uv_timer_stop(uv_timer_t* handle) {
171
117
  RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
172
118
 
173
119
  handle->flags &= ~UV_HANDLE_ACTIVE;
120
+ uv__handle_stop(handle);
174
121
 
175
122
  return 0;
176
123
  }
@@ -188,6 +135,7 @@ int uv_timer_again(uv_timer_t* handle) {
188
135
  if (handle->flags & UV_HANDLE_ACTIVE) {
189
136
  RB_REMOVE(uv_timer_tree_s, &loop->timers, handle);
190
137
  handle->flags &= ~UV_HANDLE_ACTIVE;
138
+ uv__handle_stop(handle);
191
139
  }
192
140
 
193
141
  if (handle->repeat) {
@@ -198,6 +146,7 @@ int uv_timer_again(uv_timer_t* handle) {
198
146
  }
199
147
 
200
148
  handle->flags |= UV_HANDLE_ACTIVE;
149
+ uv__handle_start(handle);
201
150
  }
202
151
 
203
152
  return 0;
@@ -269,6 +218,7 @@ void uv_process_timers(uv_loop_t* loop) {
269
218
  } else {
270
219
  /* If non-repeating, mark the timer as inactive. */
271
220
  timer->flags &= ~UV_HANDLE_ACTIVE;
221
+ uv__handle_stop(timer);
272
222
  }
273
223
 
274
224
  timer->timer_cb((uv_timer_t*) timer, 0);
@@ -25,8 +25,10 @@
25
25
  #include <stdint.h>
26
26
 
27
27
  #include "uv.h"
28
- #include "../uv-common.h"
29
28
  #include "internal.h"
29
+ #include "handle-inl.h"
30
+ #include "stream-inl.h"
31
+ #include "req-inl.h"
30
32
 
31
33
 
32
34
  #define UNICODE_REPLACEMENT_CHARACTER (0xfffd)
@@ -110,10 +112,9 @@ int uv_tty_init(uv_loop_t* loop, uv_tty_t* tty, uv_file fd, int readable) {
110
112
  LeaveCriticalSection(&uv_tty_output_lock);
111
113
  }
112
114
 
113
- uv_stream_init(loop, (uv_stream_t*) tty);
115
+ uv_stream_init(loop, (uv_stream_t*) tty, UV_TTY);
114
116
  uv_connection_init((uv_stream_t*) tty);
115
117
 
116
- tty->type = UV_TTY;
117
118
  tty->handle = win_handle;
118
119
  tty->read_line_handle = NULL;
119
120
  tty->read_line_buffer = uv_null_buf_;
@@ -462,8 +463,9 @@ void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle,
462
463
 
463
464
  /* Fetch the number of events */
464
465
  if (!GetNumberOfConsoleInputEvents(handle->handle, &records_left)) {
465
- handle->flags &= ~UV_HANDLE_READING;
466
466
  uv__set_sys_error(loop, GetLastError());
467
+ handle->flags &= ~UV_HANDLE_READING;
468
+ DECREASE_ACTIVE_COUNT(loop, handle);
467
469
  handle->read_cb((uv_stream_t*)handle, -1, uv_null_buf_);
468
470
  goto out;
469
471
  }
@@ -483,6 +485,7 @@ void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle,
483
485
  &records_read)) {
484
486
  uv__set_sys_error(loop, GetLastError());
485
487
  handle->flags &= ~UV_HANDLE_READING;
488
+ DECREASE_ACTIVE_COUNT(loop, handle);
486
489
  handle->read_cb((uv_stream_t*) handle, -1, buf);
487
490
  goto out;
488
491
  }
@@ -583,6 +586,7 @@ void uv_process_tty_read_raw_req(uv_loop_t* loop, uv_tty_t* handle,
583
586
  if (!char_len) {
584
587
  uv__set_sys_error(loop, GetLastError());
585
588
  handle->flags &= ~UV_HANDLE_READING;
589
+ DECREASE_ACTIVE_COUNT(loop, handle);
586
590
  handle->read_cb((uv_stream_t*) handle, -1, buf);
587
591
  goto out;
588
592
  }
@@ -688,9 +692,10 @@ void uv_process_tty_read_line_req(uv_loop_t* loop, uv_tty_t* handle,
688
692
  if (!REQ_SUCCESS(req)) {
689
693
  /* Read was not successful */
690
694
  if ((handle->flags & UV_HANDLE_READING) &&
691
- !(handle->flags & UV_HANDLE_TTY_RAW)) {
695
+ handle->read_line_handle != NULL) {
692
696
  /* Real error */
693
697
  handle->flags &= ~UV_HANDLE_READING;
698
+ DECREASE_ACTIVE_COUNT(loop, handle);
694
699
  uv__set_sys_error(loop, GET_REQ_ERROR(req));
695
700
  handle->read_cb((uv_stream_t*) handle, -1, buf);
696
701
  } else {
@@ -738,6 +743,7 @@ int uv_tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb,
738
743
  uv_loop_t* loop = handle->loop;
739
744
 
740
745
  handle->flags |= UV_HANDLE_READING;
746
+ INCREASE_ACTIVE_COUNT(loop, handle);
741
747
  handle->read_cb = read_cb;
742
748
  handle->alloc_cb = alloc_cb;
743
749
 
@@ -762,7 +768,12 @@ int uv_tty_read_start(uv_tty_t* handle, uv_alloc_cb alloc_cb,
762
768
 
763
769
 
764
770
  int uv_tty_read_stop(uv_tty_t* handle) {
765
- handle->flags &= ~UV_HANDLE_READING;
771
+ uv_loop_t* loop = handle->loop;
772
+
773
+ if (handle->flags & UV_HANDLE_READING) {
774
+ handle->flags &= ~UV_HANDLE_READING;
775
+ DECREASE_ACTIVE_COUNT(loop, handle);
776
+ }
766
777
 
767
778
  /* Cancel raw read */
768
779
  if ((handle->flags & UV_HANDLE_READ_PENDING) &&
@@ -772,7 +783,7 @@ int uv_tty_read_stop(uv_tty_t* handle) {
772
783
  DWORD written;
773
784
  memset(&record, 0, sizeof record);
774
785
  if (!WriteConsoleInputW(handle->handle, &record, 1, &written)) {
775
- uv__set_sys_error(handle->loop, GetLastError());
786
+ uv__set_sys_error(loop, GetLastError());
776
787
  return -1;
777
788
  }
778
789
  }
@@ -960,9 +971,6 @@ static int uv_tty_reset(uv_tty_t* handle, DWORD* error) {
960
971
 
961
972
  static int uv_tty_clear(uv_tty_t* handle, int dir, char entire_screen,
962
973
  DWORD* error) {
963
- unsigned short argc = handle->ansi_csi_argc;
964
- unsigned short* argv = handle->ansi_csi_argv;
965
-
966
974
  CONSOLE_SCREEN_BUFFER_INFO info;
967
975
  COORD start, end;
968
976
  DWORD count, written;
@@ -1683,7 +1691,7 @@ int uv_tty_write(uv_loop_t* loop, uv_write_t* req, uv_tty_t* handle,
1683
1691
 
1684
1692
  handle->reqs_pending++;
1685
1693
  handle->write_reqs_pending++;
1686
- uv_ref(loop);
1694
+ REGISTER_HANDLE_REQ(loop, handle, req);
1687
1695
 
1688
1696
  req->queued_bytes = 0;
1689
1697
 
@@ -1703,6 +1711,7 @@ void uv_process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
1703
1711
  uv_write_t* req) {
1704
1712
 
1705
1713
  handle->write_queue_size -= req->queued_bytes;
1714
+ UNREGISTER_HANDLE_REQ(loop, handle, req);
1706
1715
 
1707
1716
  if (req->cb) {
1708
1717
  uv__set_sys_error(loop, GET_REQ_ERROR(req));
@@ -1716,7 +1725,6 @@ void uv_process_tty_write_req(uv_loop_t* loop, uv_tty_t* handle,
1716
1725
  }
1717
1726
 
1718
1727
  DECREASE_PENDING_REQ_COUNT(handle);
1719
- uv_unref(loop);
1720
1728
  }
1721
1729
 
1722
1730
 
@@ -1726,6 +1734,8 @@ void uv_tty_close(uv_tty_t* handle) {
1726
1734
  uv_tty_read_stop(handle);
1727
1735
  CloseHandle(handle->handle);
1728
1736
 
1737
+ uv__handle_start(handle);
1738
+
1729
1739
  if (handle->reqs_pending == 0) {
1730
1740
  uv_want_endgame(handle->loop, (uv_handle_t*) handle);
1731
1741
  }
@@ -1736,6 +1746,8 @@ void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
1736
1746
  if ((handle->flags && UV_HANDLE_CONNECTION) &&
1737
1747
  handle->shutdown_req != NULL &&
1738
1748
  handle->write_reqs_pending == 0) {
1749
+ UNREGISTER_HANDLE_REQ(loop, handle, handle->shutdown_req);
1750
+
1739
1751
  /* TTY shutdown is really just a no-op */
1740
1752
  if (handle->shutdown_req->cb) {
1741
1753
  if (handle->flags & UV_HANDLE_CLOSING) {
@@ -1748,7 +1760,6 @@ void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
1748
1760
 
1749
1761
  handle->shutdown_req = NULL;
1750
1762
 
1751
- uv_unref(loop);
1752
1763
  DECREASE_PENDING_REQ_COUNT(handle);
1753
1764
  return;
1754
1765
  }
@@ -1764,13 +1775,8 @@ void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle) {
1764
1775
  assert(handle->read_raw_wait == NULL);
1765
1776
 
1766
1777
  assert(!(handle->flags & UV_HANDLE_CLOSED));
1767
- handle->flags |= UV_HANDLE_CLOSED;
1768
-
1769
- if (handle->close_cb) {
1770
- handle->close_cb((uv_handle_t*)handle);
1771
- }
1772
-
1773
- uv_unref(loop);
1778
+ uv__handle_stop(handle);
1779
+ uv__handle_close(handle);
1774
1780
  }
1775
1781
  }
1776
1782
 
@@ -22,8 +22,10 @@
22
22
  #include <assert.h>
23
23
 
24
24
  #include "uv.h"
25
- #include "../uv-common.h"
26
25
  #include "internal.h"
26
+ #include "handle-inl.h"
27
+ #include "stream-inl.h"
28
+ #include "req-inl.h"
27
29
 
28
30
 
29
31
  /*
@@ -121,11 +123,11 @@ static int uv_udp_set_socket(uv_loop_t* loop, uv_udp_t* handle,
121
123
 
122
124
 
123
125
  int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
124
- handle->type = UV_UDP;
126
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_UDP);
127
+
125
128
  handle->socket = INVALID_SOCKET;
126
129
  handle->reqs_pending = 0;
127
- handle->loop = loop;
128
- handle->flags = 0;
130
+ handle->activecnt = 0;
129
131
  handle->func_wsarecv = WSARecv;
130
132
  handle->func_wsarecvfrom = WSARecvFrom;
131
133
 
@@ -133,26 +135,30 @@ int uv_udp_init(uv_loop_t* loop, uv_udp_t* handle) {
133
135
  handle->recv_req.type = UV_UDP_RECV;
134
136
  handle->recv_req.data = handle;
135
137
 
136
- uv_ref(loop);
137
-
138
- loop->counters.handle_init++;
139
138
  loop->counters.udp_init++;
140
139
 
141
140
  return 0;
142
141
  }
143
142
 
144
143
 
144
+ void uv_udp_close(uv_loop_t* loop, uv_udp_t* handle) {
145
+ uv_udp_recv_stop(handle);
146
+ closesocket(handle->socket);
147
+
148
+ uv__handle_start(handle);
149
+
150
+ if (handle->reqs_pending == 0) {
151
+ uv_want_endgame(loop, (uv_handle_t*) handle);
152
+ }
153
+ }
154
+
155
+
145
156
  void uv_udp_endgame(uv_loop_t* loop, uv_udp_t* handle) {
146
157
  if (handle->flags & UV_HANDLE_CLOSING &&
147
158
  handle->reqs_pending == 0) {
148
159
  assert(!(handle->flags & UV_HANDLE_CLOSED));
149
- handle->flags |= UV_HANDLE_CLOSED;
150
-
151
- if (handle->close_cb) {
152
- handle->close_cb((uv_handle_t*)handle);
153
- }
154
-
155
- uv_unref(loop);
160
+ uv__handle_stop(handle);
161
+ uv__handle_close(handle);
156
162
  }
157
163
  }
158
164
 
@@ -350,6 +356,7 @@ int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
350
356
  }
351
357
 
352
358
  handle->flags |= UV_HANDLE_READING;
359
+ INCREASE_ACTIVE_COUNT(loop, handle);
353
360
  loop->active_udp_streams++;
354
361
 
355
362
  handle->recv_cb = recv_cb;
@@ -368,6 +375,7 @@ int uv_udp_recv_stop(uv_udp_t* handle) {
368
375
  if (handle->flags & UV_HANDLE_READING) {
369
376
  handle->flags &= ~UV_HANDLE_READING;
370
377
  handle->loop->active_udp_streams--;
378
+ DECREASE_ACTIVE_COUNT(loop, handle);
371
379
  }
372
380
 
373
381
  return 0;
@@ -399,13 +407,13 @@ static int uv__udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[],
399
407
  /* Request completed immediately. */
400
408
  req->queued_bytes = 0;
401
409
  handle->reqs_pending++;
402
- uv_ref(loop);
410
+ REGISTER_HANDLE_REQ(loop, handle, req);
403
411
  uv_insert_pending_req(loop, (uv_req_t*)req);
404
412
  } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
405
413
  /* Request queued by the kernel. */
406
414
  req->queued_bytes = uv_count_bufs(bufs, bufcnt);
407
415
  handle->reqs_pending++;
408
- uv_ref(loop);
416
+ REGISTER_HANDLE_REQ(loop, handle, req);
409
417
  } else {
410
418
  /* Send failed due to an error. */
411
419
  uv__set_sys_error(loop, WSAGetLastError());
@@ -561,6 +569,8 @@ void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle,
561
569
  uv_udp_send_t* req) {
562
570
  assert(handle->type == UV_UDP);
563
571
 
572
+ UNREGISTER_HANDLE_REQ(loop, handle, req);
573
+
564
574
  if (req->cb) {
565
575
  if (REQ_SUCCESS(req)) {
566
576
  req->cb(req, 0);
@@ -570,7 +580,6 @@ void uv_process_udp_send_req(uv_loop_t* loop, uv_udp_t* handle,
570
580
  }
571
581
  }
572
582
 
573
- uv_unref(loop);
574
583
  DECREASE_PENDING_REQ_COUNT(handle);
575
584
  }
576
585