foolio 0.0.3

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 (261) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +21 -0
  6. data/examples/timer.rb +20 -0
  7. data/ext/foolio/extconf.rb +34 -0
  8. data/ext/foolio/foolio_ext.c +921 -0
  9. data/ext/foolio/gen.rb +50 -0
  10. data/ext/foolio/make_table.rb +12 -0
  11. data/ext/foolio/templ +243 -0
  12. data/ext/libuv/.gitignore +33 -0
  13. data/ext/libuv/.mailmap +13 -0
  14. data/ext/libuv/.travis.yml +9 -0
  15. data/ext/libuv/AUTHORS +61 -0
  16. data/ext/libuv/LICENSE +44 -0
  17. data/ext/libuv/Makefile +71 -0
  18. data/ext/libuv/README.md +90 -0
  19. data/ext/libuv/common.gypi +178 -0
  20. data/ext/libuv/gyp_uv +73 -0
  21. data/ext/libuv/include/uv-private/eio.h +403 -0
  22. data/ext/libuv/include/uv-private/ev.h +838 -0
  23. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  24. data/ext/libuv/include/uv-private/tree.h +768 -0
  25. data/ext/libuv/include/uv-private/uv-unix.h +324 -0
  26. data/ext/libuv/include/uv-private/uv-win.h +517 -0
  27. data/ext/libuv/include/uv.h +1838 -0
  28. data/ext/libuv/src/fs-poll.c +235 -0
  29. data/ext/libuv/src/inet.c +293 -0
  30. data/ext/libuv/src/unix/async.c +148 -0
  31. data/ext/libuv/src/unix/core.c +696 -0
  32. data/ext/libuv/src/unix/cygwin.c +83 -0
  33. data/ext/libuv/src/unix/darwin.c +342 -0
  34. data/ext/libuv/src/unix/dl.c +83 -0
  35. data/ext/libuv/src/unix/eio/Changes +63 -0
  36. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  37. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  38. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  39. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  40. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  41. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  42. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  43. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  44. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  45. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  46. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  47. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  48. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  49. data/ext/libuv/src/unix/eio/demo.c +194 -0
  50. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  51. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  52. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  53. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  54. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  55. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  56. data/ext/libuv/src/unix/error.c +105 -0
  57. data/ext/libuv/src/unix/ev/Changes +388 -0
  58. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  59. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  60. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  61. data/ext/libuv/src/unix/ev/README +58 -0
  62. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  63. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  64. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  65. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  66. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  67. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  68. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  69. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  70. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  71. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  72. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  73. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  74. data/ext/libuv/src/unix/ev/configure +13037 -0
  75. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  76. data/ext/libuv/src/unix/ev/depcomp +630 -0
  77. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  78. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  79. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  80. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  81. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  82. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  83. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  84. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  85. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  86. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  87. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  88. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  89. data/ext/libuv/src/unix/ev/event.c +402 -0
  90. data/ext/libuv/src/unix/ev/event.h +170 -0
  91. data/ext/libuv/src/unix/ev/install-sh +294 -0
  92. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  93. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  94. data/ext/libuv/src/unix/ev/missing +336 -0
  95. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  96. data/ext/libuv/src/unix/freebsd.c +326 -0
  97. data/ext/libuv/src/unix/fs.c +739 -0
  98. data/ext/libuv/src/unix/internal.h +188 -0
  99. data/ext/libuv/src/unix/kqueue.c +120 -0
  100. data/ext/libuv/src/unix/linux/inotify.c +239 -0
  101. data/ext/libuv/src/unix/linux/linux-core.c +557 -0
  102. data/ext/libuv/src/unix/linux/syscalls.c +388 -0
  103. data/ext/libuv/src/unix/linux/syscalls.h +124 -0
  104. data/ext/libuv/src/unix/loop-watcher.c +62 -0
  105. data/ext/libuv/src/unix/loop.c +94 -0
  106. data/ext/libuv/src/unix/netbsd.c +108 -0
  107. data/ext/libuv/src/unix/openbsd.c +295 -0
  108. data/ext/libuv/src/unix/pipe.c +259 -0
  109. data/ext/libuv/src/unix/poll.c +114 -0
  110. data/ext/libuv/src/unix/process.c +495 -0
  111. data/ext/libuv/src/unix/signal.c +269 -0
  112. data/ext/libuv/src/unix/stream.c +990 -0
  113. data/ext/libuv/src/unix/sunos.c +481 -0
  114. data/ext/libuv/src/unix/tcp.c +393 -0
  115. data/ext/libuv/src/unix/thread.c +251 -0
  116. data/ext/libuv/src/unix/timer.c +136 -0
  117. data/ext/libuv/src/unix/tty.c +145 -0
  118. data/ext/libuv/src/unix/udp.c +659 -0
  119. data/ext/libuv/src/unix/uv-eio.c +107 -0
  120. data/ext/libuv/src/unix/uv-eio.h +13 -0
  121. data/ext/libuv/src/uv-common.c +380 -0
  122. data/ext/libuv/src/uv-common.h +170 -0
  123. data/ext/libuv/src/win/async.c +100 -0
  124. data/ext/libuv/src/win/atomicops-inl.h +56 -0
  125. data/ext/libuv/src/win/core.c +278 -0
  126. data/ext/libuv/src/win/dl.c +86 -0
  127. data/ext/libuv/src/win/error.c +155 -0
  128. data/ext/libuv/src/win/fs-event.c +510 -0
  129. data/ext/libuv/src/win/fs.c +1948 -0
  130. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  131. data/ext/libuv/src/win/handle-inl.h +149 -0
  132. data/ext/libuv/src/win/handle.c +154 -0
  133. data/ext/libuv/src/win/internal.h +343 -0
  134. data/ext/libuv/src/win/loop-watcher.c +122 -0
  135. data/ext/libuv/src/win/pipe.c +1672 -0
  136. data/ext/libuv/src/win/poll.c +616 -0
  137. data/ext/libuv/src/win/process-stdio.c +500 -0
  138. data/ext/libuv/src/win/process.c +1013 -0
  139. data/ext/libuv/src/win/req-inl.h +220 -0
  140. data/ext/libuv/src/win/req.c +25 -0
  141. data/ext/libuv/src/win/signal.c +57 -0
  142. data/ext/libuv/src/win/stream-inl.h +67 -0
  143. data/ext/libuv/src/win/stream.c +167 -0
  144. data/ext/libuv/src/win/tcp.c +1394 -0
  145. data/ext/libuv/src/win/thread.c +372 -0
  146. data/ext/libuv/src/win/threadpool.c +74 -0
  147. data/ext/libuv/src/win/timer.c +224 -0
  148. data/ext/libuv/src/win/tty.c +1799 -0
  149. data/ext/libuv/src/win/udp.c +716 -0
  150. data/ext/libuv/src/win/util.c +864 -0
  151. data/ext/libuv/src/win/winapi.c +132 -0
  152. data/ext/libuv/src/win/winapi.h +4452 -0
  153. data/ext/libuv/src/win/winsock.c +557 -0
  154. data/ext/libuv/src/win/winsock.h +171 -0
  155. data/ext/libuv/test/benchmark-async-pummel.c +97 -0
  156. data/ext/libuv/test/benchmark-async.c +137 -0
  157. data/ext/libuv/test/benchmark-fs-stat.c +135 -0
  158. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  159. data/ext/libuv/test/benchmark-list.h +127 -0
  160. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  161. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  162. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  163. data/ext/libuv/test/benchmark-pound.c +324 -0
  164. data/ext/libuv/test/benchmark-pump.c +462 -0
  165. data/ext/libuv/test/benchmark-sizes.c +44 -0
  166. data/ext/libuv/test/benchmark-spawn.c +162 -0
  167. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  168. data/ext/libuv/test/benchmark-thread.c +64 -0
  169. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  170. data/ext/libuv/test/blackhole-server.c +118 -0
  171. data/ext/libuv/test/dns-server.c +321 -0
  172. data/ext/libuv/test/echo-server.c +378 -0
  173. data/ext/libuv/test/fixtures/empty_file +0 -0
  174. data/ext/libuv/test/fixtures/load_error.node +1 -0
  175. data/ext/libuv/test/run-benchmarks.c +64 -0
  176. data/ext/libuv/test/run-tests.c +138 -0
  177. data/ext/libuv/test/runner-unix.c +295 -0
  178. data/ext/libuv/test/runner-unix.h +36 -0
  179. data/ext/libuv/test/runner-win.c +285 -0
  180. data/ext/libuv/test/runner-win.h +42 -0
  181. data/ext/libuv/test/runner.c +355 -0
  182. data/ext/libuv/test/runner.h +159 -0
  183. data/ext/libuv/test/task.h +112 -0
  184. data/ext/libuv/test/test-async.c +118 -0
  185. data/ext/libuv/test/test-callback-order.c +76 -0
  186. data/ext/libuv/test/test-callback-stack.c +203 -0
  187. data/ext/libuv/test/test-connection-fail.c +148 -0
  188. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  189. data/ext/libuv/test/test-delayed-accept.c +188 -0
  190. data/ext/libuv/test/test-dlerror.c +58 -0
  191. data/ext/libuv/test/test-error.c +59 -0
  192. data/ext/libuv/test/test-fail-always.c +29 -0
  193. data/ext/libuv/test/test-fs-event.c +474 -0
  194. data/ext/libuv/test/test-fs-poll.c +146 -0
  195. data/ext/libuv/test/test-fs.c +1843 -0
  196. data/ext/libuv/test/test-get-currentexe.c +63 -0
  197. data/ext/libuv/test/test-get-loadavg.c +36 -0
  198. data/ext/libuv/test/test-get-memory.c +38 -0
  199. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  200. data/ext/libuv/test/test-getsockname.c +342 -0
  201. data/ext/libuv/test/test-hrtime.c +54 -0
  202. data/ext/libuv/test/test-idle.c +81 -0
  203. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  204. data/ext/libuv/test/test-ipc.c +620 -0
  205. data/ext/libuv/test/test-list.h +427 -0
  206. data/ext/libuv/test/test-loop-handles.c +336 -0
  207. data/ext/libuv/test/test-multiple-listen.c +102 -0
  208. data/ext/libuv/test/test-mutexes.c +63 -0
  209. data/ext/libuv/test/test-pass-always.c +28 -0
  210. data/ext/libuv/test/test-ping-pong.c +253 -0
  211. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  212. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  213. data/ext/libuv/test/test-platform-output.c +87 -0
  214. data/ext/libuv/test/test-poll-close.c +72 -0
  215. data/ext/libuv/test/test-poll.c +573 -0
  216. data/ext/libuv/test/test-process-title.c +49 -0
  217. data/ext/libuv/test/test-ref.c +338 -0
  218. data/ext/libuv/test/test-run-once.c +48 -0
  219. data/ext/libuv/test/test-semaphore.c +111 -0
  220. data/ext/libuv/test/test-shutdown-close.c +103 -0
  221. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  222. data/ext/libuv/test/test-signal.c +162 -0
  223. data/ext/libuv/test/test-spawn.c +863 -0
  224. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  225. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  226. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  227. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  228. data/ext/libuv/test/test-tcp-close.c +129 -0
  229. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  230. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  231. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  232. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  233. data/ext/libuv/test/test-tcp-flags.c +51 -0
  234. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  235. data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
  236. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  237. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  238. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  239. data/ext/libuv/test/test-thread.c +183 -0
  240. data/ext/libuv/test/test-threadpool.c +57 -0
  241. data/ext/libuv/test/test-timer-again.c +141 -0
  242. data/ext/libuv/test/test-timer.c +152 -0
  243. data/ext/libuv/test/test-tty.c +110 -0
  244. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  245. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  246. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  247. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  248. data/ext/libuv/test/test-udp-options.c +86 -0
  249. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  250. data/ext/libuv/test/test-util.c +97 -0
  251. data/ext/libuv/test/test-walk-handles.c +77 -0
  252. data/ext/libuv/uv.gyp +375 -0
  253. data/ext/libuv/vcbuild.bat +105 -0
  254. data/foolio.gemspec +18 -0
  255. data/lib/foolio.rb +9 -0
  256. data/lib/foolio/handle.rb +27 -0
  257. data/lib/foolio/listener.rb +26 -0
  258. data/lib/foolio/loop.rb +79 -0
  259. data/lib/foolio/stream.rb +109 -0
  260. data/lib/foolio/version.rb +3 -0
  261. metadata +309 -0
@@ -0,0 +1,3925 @@
1
+ /*
2
+ * libev event processing core, watcher management
3
+ *
4
+ * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without modifica-
8
+ * tion, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19
+ * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21
+ * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25
+ * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ *
28
+ * Alternatively, the contents of this file may be used under the terms of
29
+ * the GNU General Public License ("GPL") version 2 or any later version,
30
+ * in which case the provisions of the GPL are applicable instead of
31
+ * the above. If you wish to allow the use of your version of this file
32
+ * only under the terms of the GPL and not to allow others to use your
33
+ * version of this file under the BSD license, indicate your decision
34
+ * by deleting the provisions above and replace them with the notice
35
+ * and other provisions required by the GPL. If you do not delete the
36
+ * provisions above, a recipient may use your version of this file under
37
+ * either the BSD or the GPL.
38
+ */
39
+
40
+ /* this big block deduces configuration from config.h */
41
+ #ifndef EV_STANDALONE
42
+ # ifdef EV_CONFIG_H
43
+ # include EV_CONFIG_H
44
+ # else
45
+ # include "config.h"
46
+ # endif
47
+
48
+ # if HAVE_CLOCK_SYSCALL
49
+ # ifndef EV_USE_CLOCK_SYSCALL
50
+ # define EV_USE_CLOCK_SYSCALL 1
51
+ # ifndef EV_USE_REALTIME
52
+ # define EV_USE_REALTIME 0
53
+ # endif
54
+ # ifndef EV_USE_MONOTONIC
55
+ # define EV_USE_MONOTONIC 1
56
+ # endif
57
+ # endif
58
+ # elif !defined(EV_USE_CLOCK_SYSCALL)
59
+ # define EV_USE_CLOCK_SYSCALL 0
60
+ # endif
61
+
62
+ # if HAVE_CLOCK_GETTIME
63
+ # ifndef EV_USE_MONOTONIC
64
+ # define EV_USE_MONOTONIC 1
65
+ # endif
66
+ # ifndef EV_USE_REALTIME
67
+ # define EV_USE_REALTIME 0
68
+ # endif
69
+ # else
70
+ # ifndef EV_USE_MONOTONIC
71
+ # define EV_USE_MONOTONIC 0
72
+ # endif
73
+ # ifndef EV_USE_REALTIME
74
+ # define EV_USE_REALTIME 0
75
+ # endif
76
+ # endif
77
+
78
+ # if HAVE_NANOSLEEP
79
+ # ifndef EV_USE_NANOSLEEP
80
+ # define EV_USE_NANOSLEEP EV_FEATURE_OS
81
+ # endif
82
+ # else
83
+ # undef EV_USE_NANOSLEEP
84
+ # define EV_USE_NANOSLEEP 0
85
+ # endif
86
+
87
+ # if HAVE_SELECT && HAVE_SYS_SELECT_H
88
+ # ifndef EV_USE_SELECT
89
+ # define EV_USE_SELECT EV_FEATURE_BACKENDS
90
+ # endif
91
+ # else
92
+ # undef EV_USE_SELECT
93
+ # define EV_USE_SELECT 0
94
+ # endif
95
+
96
+ # if HAVE_POLL && HAVE_POLL_H
97
+ # ifndef EV_USE_POLL
98
+ # define EV_USE_POLL EV_FEATURE_BACKENDS
99
+ # endif
100
+ # else
101
+ # undef EV_USE_POLL
102
+ # define EV_USE_POLL 0
103
+ # endif
104
+
105
+ # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
106
+ # ifndef EV_USE_EPOLL
107
+ # define EV_USE_EPOLL EV_FEATURE_BACKENDS
108
+ # endif
109
+ # else
110
+ # undef EV_USE_EPOLL
111
+ # define EV_USE_EPOLL 0
112
+ # endif
113
+
114
+ # if HAVE_KQUEUE && HAVE_SYS_EVENT_H
115
+ # ifndef EV_USE_KQUEUE
116
+ # define EV_USE_KQUEUE EV_FEATURE_BACKENDS
117
+ # endif
118
+ # else
119
+ # undef EV_USE_KQUEUE
120
+ # define EV_USE_KQUEUE 0
121
+ # endif
122
+
123
+ # if HAVE_PORT_H && HAVE_PORT_CREATE
124
+ # ifndef EV_USE_PORT
125
+ # define EV_USE_PORT EV_FEATURE_BACKENDS
126
+ # endif
127
+ # else
128
+ # undef EV_USE_PORT
129
+ # define EV_USE_PORT 0
130
+ # endif
131
+
132
+ # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
133
+ # ifndef EV_USE_INOTIFY
134
+ # define EV_USE_INOTIFY EV_FEATURE_OS
135
+ # endif
136
+ # else
137
+ # undef EV_USE_INOTIFY
138
+ # define EV_USE_INOTIFY 0
139
+ # endif
140
+
141
+ # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
142
+ # ifndef EV_USE_SIGNALFD
143
+ # define EV_USE_SIGNALFD EV_FEATURE_OS
144
+ # endif
145
+ # else
146
+ # undef EV_USE_SIGNALFD
147
+ # define EV_USE_SIGNALFD 0
148
+ # endif
149
+
150
+ # if HAVE_EVENTFD
151
+ # ifndef EV_USE_EVENTFD
152
+ # define EV_USE_EVENTFD EV_FEATURE_OS
153
+ # endif
154
+ # else
155
+ # undef EV_USE_EVENTFD
156
+ # define EV_USE_EVENTFD 0
157
+ # endif
158
+
159
+ #endif
160
+
161
+ #include <math.h>
162
+ #include <stdlib.h>
163
+ #include <string.h>
164
+ #include <fcntl.h>
165
+ #include <stddef.h>
166
+
167
+ #include <stdio.h>
168
+
169
+ #include <assert.h>
170
+ #include <errno.h>
171
+ #include <sys/types.h>
172
+ #include <time.h>
173
+ #include <limits.h>
174
+
175
+ #include <signal.h>
176
+
177
+ #ifdef EV_H
178
+ # include EV_H
179
+ #else
180
+ # include "ev.h"
181
+ #endif
182
+
183
+ EV_CPP(extern "C" {)
184
+
185
+ #ifndef _WIN32
186
+ # include <sys/time.h>
187
+ # include <sys/wait.h>
188
+ # include <unistd.h>
189
+ #else
190
+ # include <io.h>
191
+ # define WIN32_LEAN_AND_MEAN
192
+ # include <windows.h>
193
+ # ifndef EV_SELECT_IS_WINSOCKET
194
+ # define EV_SELECT_IS_WINSOCKET 1
195
+ # endif
196
+ # undef EV_AVOID_STDIO
197
+ #endif
198
+
199
+ /* OS X, in its infinite idiocy, actually HARDCODES
200
+ * a limit of 1024 into their select. Where people have brains,
201
+ * OS X engineers apparently have a vacuum. Or maybe they were
202
+ * ordered to have a vacuum, or they do anything for money.
203
+ * This might help. Or not.
204
+ */
205
+ #define _DARWIN_UNLIMITED_SELECT 1
206
+
207
+ /* this block tries to deduce configuration from header-defined symbols and defaults */
208
+
209
+ /* try to deduce the maximum number of signals on this platform */
210
+ #if defined (EV_NSIG)
211
+ /* use what's provided */
212
+ #elif defined (NSIG)
213
+ # define EV_NSIG (NSIG)
214
+ #elif defined(_NSIG)
215
+ # define EV_NSIG (_NSIG)
216
+ #elif defined (SIGMAX)
217
+ # define EV_NSIG (SIGMAX+1)
218
+ #elif defined (SIG_MAX)
219
+ # define EV_NSIG (SIG_MAX+1)
220
+ #elif defined (_SIG_MAX)
221
+ # define EV_NSIG (_SIG_MAX+1)
222
+ #elif defined (MAXSIG)
223
+ # define EV_NSIG (MAXSIG+1)
224
+ #elif defined (MAX_SIG)
225
+ # define EV_NSIG (MAX_SIG+1)
226
+ #elif defined (SIGARRAYSIZE)
227
+ # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
228
+ #elif defined (_sys_nsig)
229
+ # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
230
+ #else
231
+ # error "unable to find value for NSIG, please report"
232
+ /* to make it compile regardless, just remove the above line, */
233
+ /* but consider reporting it, too! :) */
234
+ # define EV_NSIG 65
235
+ #endif
236
+
237
+ #ifndef EV_USE_CLOCK_SYSCALL
238
+ # if __linux && __GLIBC__ >= 2
239
+ # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
240
+ # else
241
+ # define EV_USE_CLOCK_SYSCALL 0
242
+ # endif
243
+ #endif
244
+
245
+ #ifndef EV_USE_MONOTONIC
246
+ # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
247
+ # define EV_USE_MONOTONIC EV_FEATURE_OS
248
+ # else
249
+ # define EV_USE_MONOTONIC 0
250
+ # endif
251
+ #endif
252
+
253
+ #ifndef EV_USE_REALTIME
254
+ # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
255
+ #endif
256
+
257
+ #ifndef EV_USE_NANOSLEEP
258
+ # if _POSIX_C_SOURCE >= 199309L
259
+ # define EV_USE_NANOSLEEP EV_FEATURE_OS
260
+ # else
261
+ # define EV_USE_NANOSLEEP 0
262
+ # endif
263
+ #endif
264
+
265
+ #ifndef EV_USE_SELECT
266
+ # define EV_USE_SELECT EV_FEATURE_BACKENDS
267
+ #endif
268
+
269
+ #ifndef EV_USE_POLL
270
+ # ifdef _WIN32
271
+ # define EV_USE_POLL 0
272
+ # else
273
+ # define EV_USE_POLL EV_FEATURE_BACKENDS
274
+ # endif
275
+ #endif
276
+
277
+ #ifndef EV_USE_EPOLL
278
+ # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
279
+ # define EV_USE_EPOLL EV_FEATURE_BACKENDS
280
+ # else
281
+ # define EV_USE_EPOLL 0
282
+ # endif
283
+ #endif
284
+
285
+ #ifndef EV_USE_KQUEUE
286
+ # define EV_USE_KQUEUE 0
287
+ #endif
288
+
289
+ #ifndef EV_USE_PORT
290
+ # define EV_USE_PORT 0
291
+ #endif
292
+
293
+ #ifndef EV_USE_INOTIFY
294
+ # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
295
+ # define EV_USE_INOTIFY EV_FEATURE_OS
296
+ # else
297
+ # define EV_USE_INOTIFY 0
298
+ # endif
299
+ #endif
300
+
301
+ #ifndef EV_PID_HASHSIZE
302
+ # define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
303
+ #endif
304
+
305
+ #ifndef EV_INOTIFY_HASHSIZE
306
+ # define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
307
+ #endif
308
+
309
+ #ifndef EV_USE_EVENTFD
310
+ # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
311
+ # define EV_USE_EVENTFD EV_FEATURE_OS
312
+ # else
313
+ # define EV_USE_EVENTFD 0
314
+ # endif
315
+ #endif
316
+
317
+ #ifndef EV_USE_SIGNALFD
318
+ # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
319
+ # define EV_USE_SIGNALFD EV_FEATURE_OS
320
+ # else
321
+ # define EV_USE_SIGNALFD 0
322
+ # endif
323
+ #endif
324
+
325
+ #if 0 /* debugging */
326
+ # define EV_VERIFY 3
327
+ # define EV_USE_4HEAP 1
328
+ # define EV_HEAP_CACHE_AT 1
329
+ #endif
330
+
331
+ #ifndef EV_VERIFY
332
+ # define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
333
+ #endif
334
+
335
+ #ifndef EV_USE_4HEAP
336
+ # define EV_USE_4HEAP EV_FEATURE_DATA
337
+ #endif
338
+
339
+ #ifndef EV_HEAP_CACHE_AT
340
+ # define EV_HEAP_CACHE_AT EV_FEATURE_DATA
341
+ #endif
342
+
343
+ /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
344
+ /* which makes programs even slower. might work on other unices, too. */
345
+ #if EV_USE_CLOCK_SYSCALL
346
+ # include <syscall.h>
347
+ # ifdef SYS_clock_gettime
348
+ # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
349
+ # undef EV_USE_MONOTONIC
350
+ # define EV_USE_MONOTONIC 1
351
+ # else
352
+ # undef EV_USE_CLOCK_SYSCALL
353
+ # define EV_USE_CLOCK_SYSCALL 0
354
+ # endif
355
+ #endif
356
+
357
+ /* this block fixes any misconfiguration where we know we run into trouble otherwise */
358
+
359
+ #ifdef _AIX
360
+ /* AIX has a completely broken poll.h header */
361
+ # undef EV_USE_POLL
362
+ # define EV_USE_POLL 0
363
+ #endif
364
+
365
+ #ifndef CLOCK_MONOTONIC
366
+ # undef EV_USE_MONOTONIC
367
+ # define EV_USE_MONOTONIC 0
368
+ #endif
369
+
370
+ #ifndef CLOCK_REALTIME
371
+ # undef EV_USE_REALTIME
372
+ # define EV_USE_REALTIME 0
373
+ #endif
374
+
375
+ #if !EV_STAT_ENABLE
376
+ # undef EV_USE_INOTIFY
377
+ # define EV_USE_INOTIFY 0
378
+ #endif
379
+
380
+ #if !EV_USE_NANOSLEEP
381
+ /* hp-ux has it in sys/time.h, which we unconditionally include above */
382
+ # if !defined(_WIN32) && !defined(__hpux)
383
+ # include <sys/select.h>
384
+ # endif
385
+ #endif
386
+
387
+ #if EV_USE_INOTIFY
388
+ # include <sys/statfs.h>
389
+ # include <sys/inotify.h>
390
+ /* some very old inotify.h headers don't have IN_DONT_FOLLOW */
391
+ # ifndef IN_DONT_FOLLOW
392
+ # undef EV_USE_INOTIFY
393
+ # define EV_USE_INOTIFY 0
394
+ # endif
395
+ #endif
396
+
397
+ #if EV_SELECT_IS_WINSOCKET
398
+ # include <winsock.h>
399
+ #endif
400
+
401
+ #if EV_USE_EVENTFD
402
+ /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
403
+ # include <stdint.h>
404
+ # ifndef EFD_NONBLOCK
405
+ # define EFD_NONBLOCK O_NONBLOCK
406
+ # endif
407
+ # ifndef EFD_CLOEXEC
408
+ # ifdef O_CLOEXEC
409
+ # define EFD_CLOEXEC O_CLOEXEC
410
+ # else
411
+ # define EFD_CLOEXEC 02000000
412
+ # endif
413
+ # endif
414
+ EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
415
+ #endif
416
+
417
+ #if EV_USE_SIGNALFD
418
+ /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
419
+ # include <stdint.h>
420
+ # ifndef SFD_NONBLOCK
421
+ # define SFD_NONBLOCK O_NONBLOCK
422
+ # endif
423
+ # ifndef SFD_CLOEXEC
424
+ # ifdef O_CLOEXEC
425
+ # define SFD_CLOEXEC O_CLOEXEC
426
+ # else
427
+ # define SFD_CLOEXEC 02000000
428
+ # endif
429
+ # endif
430
+ EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
431
+
432
+ struct signalfd_siginfo
433
+ {
434
+ uint32_t ssi_signo;
435
+ char pad[128 - sizeof (uint32_t)];
436
+ };
437
+ #endif
438
+
439
+ /**/
440
+
441
+ #if EV_VERIFY >= 3
442
+ # define EV_FREQUENT_CHECK ev_verify (EV_A)
443
+ #else
444
+ # define EV_FREQUENT_CHECK do { } while (0)
445
+ #endif
446
+
447
+ /*
448
+ * This is used to avoid floating point rounding problems.
449
+ * It is added to ev_rt_now when scheduling periodics
450
+ * to ensure progress, time-wise, even when rounding
451
+ * errors are against us.
452
+ * This value is good at least till the year 4000.
453
+ * Better solutions welcome.
454
+ */
455
+ #define TIME_EPSILON 0.0001220703125 /* 1/8192 */
456
+
457
+ #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
458
+ #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
459
+
460
+ #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
461
+ #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
462
+
463
+ #if __GNUC__ >= 4
464
+ # define expect(expr,value) __builtin_expect ((expr),(value))
465
+ # define noinline __attribute__ ((noinline))
466
+ #else
467
+ # define expect(expr,value) (expr)
468
+ # define noinline
469
+ # if __STDC_VERSION__ < 199901L && __GNUC__ < 2
470
+ # define inline
471
+ # endif
472
+ #endif
473
+
474
+ #define expect_false(expr) expect ((expr) != 0, 0)
475
+ #define expect_true(expr) expect ((expr) != 0, 1)
476
+ #define inline_size static inline
477
+
478
+ #if EV_FEATURE_CODE
479
+ # define inline_speed static inline
480
+ #else
481
+ # define inline_speed static noinline
482
+ #endif
483
+
484
+ #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
485
+
486
+ #if EV_MINPRI == EV_MAXPRI
487
+ # define ABSPRI(w) (((W)w), 0)
488
+ #else
489
+ # define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
490
+ #endif
491
+
492
+ #define EMPTY /* required for microsofts broken pseudo-c compiler */
493
+ #define EMPTY2(a,b) /* used to suppress some warnings */
494
+
495
+ typedef ev_watcher *W;
496
+ typedef ev_watcher_list *WL;
497
+ typedef ev_watcher_time *WT;
498
+
499
+ #define ev_active(w) ((W)(w))->active
500
+ #define ev_at(w) ((WT)(w))->at
501
+
502
+ #if EV_USE_REALTIME
503
+ /* sig_atomic_t is used to avoid per-thread variables or locking but still */
504
+ /* giving it a reasonably high chance of working on typical architectures */
505
+ static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
506
+ #endif
507
+
508
+ #if EV_USE_MONOTONIC
509
+ static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
510
+ #endif
511
+
512
+ #ifndef EV_FD_TO_WIN32_HANDLE
513
+ # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
514
+ #endif
515
+ #ifndef EV_WIN32_HANDLE_TO_FD
516
+ # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
517
+ #endif
518
+ #ifndef EV_WIN32_CLOSE_FD
519
+ # define EV_WIN32_CLOSE_FD(fd) close (fd)
520
+ #endif
521
+
522
+ #ifdef _WIN32
523
+ # include "ev_win32.c"
524
+ #endif
525
+
526
+ /*****************************************************************************/
527
+
528
+ #ifdef __linux
529
+ # include <sys/utsname.h>
530
+ #endif
531
+
532
+ static unsigned int noinline
533
+ ev_linux_version (void)
534
+ {
535
+ #ifdef __linux
536
+ unsigned int v = 0;
537
+ struct utsname buf;
538
+ int i;
539
+ char *p = buf.release;
540
+
541
+ if (uname (&buf))
542
+ return 0;
543
+
544
+ for (i = 3+1; --i; )
545
+ {
546
+ unsigned int c = 0;
547
+
548
+ for (;;)
549
+ {
550
+ if (*p >= '0' && *p <= '9')
551
+ c = c * 10 + *p++ - '0';
552
+ else
553
+ {
554
+ p += *p == '.';
555
+ break;
556
+ }
557
+ }
558
+
559
+ v = (v << 8) | c;
560
+ }
561
+
562
+ return v;
563
+ #else
564
+ return 0;
565
+ #endif
566
+ }
567
+
568
+ /*****************************************************************************/
569
+
570
+ #if EV_AVOID_STDIO
571
+ static void noinline
572
+ ev_printerr (const char *msg)
573
+ {
574
+ write (STDERR_FILENO, msg, strlen (msg));
575
+ }
576
+ #endif
577
+
578
+ static void (*syserr_cb)(const char *msg);
579
+
580
+ void
581
+ ev_set_syserr_cb (void (*cb)(const char *msg))
582
+ {
583
+ syserr_cb = cb;
584
+ }
585
+
586
+ static void noinline
587
+ ev_syserr (const char *msg)
588
+ {
589
+ if (!msg)
590
+ msg = "(libev) system error";
591
+
592
+ if (syserr_cb)
593
+ syserr_cb (msg);
594
+ else
595
+ {
596
+ #if EV_AVOID_STDIO
597
+ ev_printerr (msg);
598
+ ev_printerr (": ");
599
+ ev_printerr (strerror (errno));
600
+ ev_printerr ("\n");
601
+ #else
602
+ perror (msg);
603
+ #endif
604
+ abort ();
605
+ }
606
+ }
607
+
608
+ static void *
609
+ ev_realloc_emul (void *ptr, long size)
610
+ {
611
+ /* some systems, notably openbsd and darwin, fail to properly
612
+ * implement realloc (x, 0) (as required by both ansi c-89 and
613
+ * the single unix specification, so work around them here.
614
+ */
615
+
616
+ if (size)
617
+ return realloc (ptr, size);
618
+
619
+ free (ptr);
620
+ return 0;
621
+ }
622
+
623
+ static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
624
+
625
+ void
626
+ ev_set_allocator (void *(*cb)(void *ptr, long size))
627
+ {
628
+ alloc = cb;
629
+ }
630
+
631
+ inline_speed void *
632
+ ev_realloc (void *ptr, long size)
633
+ {
634
+ ptr = alloc (ptr, size);
635
+
636
+ if (!ptr && size)
637
+ {
638
+ #if EV_AVOID_STDIO
639
+ ev_printerr ("(libev) memory allocation failed, aborting.\n");
640
+ #else
641
+ fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
642
+ #endif
643
+ abort ();
644
+ }
645
+
646
+ return ptr;
647
+ }
648
+
649
+ #define ev_malloc(size) ev_realloc (0, (size))
650
+ #define ev_free(ptr) ev_realloc ((ptr), 0)
651
+
652
+ /*****************************************************************************/
653
+
654
+ /* set in reify when reification needed */
655
+ #define EV_ANFD_REIFY 1
656
+
657
+ /* file descriptor info structure */
658
+ typedef struct
659
+ {
660
+ WL head;
661
+ unsigned char events; /* the events watched for */
662
+ unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
663
+ unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
664
+ unsigned char unused;
665
+ #if EV_USE_EPOLL
666
+ unsigned int egen; /* generation counter to counter epoll bugs */
667
+ #endif
668
+ #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
669
+ SOCKET handle;
670
+ #endif
671
+ #if EV_USE_IOCP
672
+ OVERLAPPED or, ow;
673
+ #endif
674
+ } ANFD;
675
+
676
+ /* stores the pending event set for a given watcher */
677
+ typedef struct
678
+ {
679
+ W w;
680
+ int events; /* the pending event set for the given watcher */
681
+ } ANPENDING;
682
+
683
+ #if EV_USE_INOTIFY
684
+ /* hash table entry per inotify-id */
685
+ typedef struct
686
+ {
687
+ WL head;
688
+ } ANFS;
689
+ #endif
690
+
691
+ /* Heap Entry */
692
+ #if EV_HEAP_CACHE_AT
693
+ /* a heap element */
694
+ typedef struct {
695
+ ev_tstamp at;
696
+ WT w;
697
+ } ANHE;
698
+
699
+ #define ANHE_w(he) (he).w /* access watcher, read-write */
700
+ #define ANHE_at(he) (he).at /* access cached at, read-only */
701
+ #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
702
+ #else
703
+ /* a heap element */
704
+ typedef WT ANHE;
705
+
706
+ #define ANHE_w(he) (he)
707
+ #define ANHE_at(he) (he)->at
708
+ #define ANHE_at_cache(he)
709
+ #endif
710
+
711
+ #if EV_MULTIPLICITY
712
+
713
+ struct ev_loop
714
+ {
715
+ ev_tstamp ev_rt_now;
716
+ #define ev_rt_now ((loop)->ev_rt_now)
717
+ #define VAR(name,decl) decl;
718
+ #include "ev_vars.h"
719
+ #undef VAR
720
+ };
721
+ #include "ev_wrap.h"
722
+
723
+ static struct ev_loop default_loop_struct;
724
+ struct ev_loop *ev_default_loop_ptr;
725
+
726
+ #else
727
+
728
+ ev_tstamp ev_rt_now;
729
+ #define VAR(name,decl) static decl;
730
+ #include "ev_vars.h"
731
+ #undef VAR
732
+
733
+ static int ev_default_loop_ptr;
734
+
735
+ #endif
736
+
737
+ #if EV_FEATURE_API
738
+ # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
739
+ # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
740
+ # define EV_INVOKE_PENDING invoke_cb (EV_A)
741
+ #else
742
+ # define EV_RELEASE_CB (void)0
743
+ # define EV_ACQUIRE_CB (void)0
744
+ # define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
745
+ #endif
746
+
747
+ #define EVBREAK_RECURSE 0x80
748
+
749
+ /*****************************************************************************/
750
+
751
+ #ifndef EV_HAVE_EV_TIME
752
+ ev_tstamp
753
+ ev_time (void)
754
+ {
755
+ #if EV_USE_REALTIME
756
+ if (expect_true (have_realtime))
757
+ {
758
+ struct timespec ts;
759
+ clock_gettime (CLOCK_REALTIME, &ts);
760
+ return ts.tv_sec + ts.tv_nsec * 1e-9;
761
+ }
762
+ #endif
763
+
764
+ struct timeval tv;
765
+ gettimeofday (&tv, 0);
766
+ return tv.tv_sec + tv.tv_usec * 1e-6;
767
+ }
768
+ #endif
769
+
770
+ inline_size ev_tstamp
771
+ get_clock (void)
772
+ {
773
+ #if EV_USE_MONOTONIC
774
+ if (expect_true (have_monotonic))
775
+ {
776
+ struct timespec ts;
777
+ clock_gettime (CLOCK_MONOTONIC, &ts);
778
+ return ts.tv_sec + ts.tv_nsec * 1e-9;
779
+ }
780
+ #endif
781
+
782
+ return ev_time ();
783
+ }
784
+
785
+ #if EV_MULTIPLICITY
786
+ ev_tstamp
787
+ ev_now (EV_P)
788
+ {
789
+ return ev_rt_now;
790
+ }
791
+ #endif
792
+
793
+ void
794
+ ev_sleep (ev_tstamp delay)
795
+ {
796
+ if (delay > 0.)
797
+ {
798
+ #if EV_USE_NANOSLEEP
799
+ struct timespec ts;
800
+
801
+ EV_TS_SET (ts, delay);
802
+ nanosleep (&ts, 0);
803
+ #elif defined(_WIN32)
804
+ Sleep ((unsigned long)(delay * 1e3));
805
+ #else
806
+ struct timeval tv;
807
+
808
+ /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
809
+ /* something not guaranteed by newer posix versions, but guaranteed */
810
+ /* by older ones */
811
+ EV_TV_SET (tv, delay);
812
+ select (0, 0, 0, 0, &tv);
813
+ #endif
814
+ }
815
+ }
816
+
817
+ inline_speed int
818
+ ev_timeout_to_ms (ev_tstamp timeout)
819
+ {
820
+ int ms = timeout * 1000. + .999999;
821
+
822
+ return expect_true (ms) ? ms : timeout < 1e-6 ? 0 : 1;
823
+ }
824
+
825
+ /*****************************************************************************/
826
+
827
+ #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
828
+
829
+ /* find a suitable new size for the given array, */
830
+ /* hopefully by rounding to a nice-to-malloc size */
831
+ inline_size int
832
+ array_nextsize (int elem, int cur, int cnt)
833
+ {
834
+ int ncur = cur + 1;
835
+
836
+ do
837
+ ncur <<= 1;
838
+ while (cnt > ncur);
839
+
840
+ /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
841
+ if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
842
+ {
843
+ ncur *= elem;
844
+ ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
845
+ ncur = ncur - sizeof (void *) * 4;
846
+ ncur /= elem;
847
+ }
848
+
849
+ return ncur;
850
+ }
851
+
852
+ static noinline void *
853
+ array_realloc (int elem, void *base, int *cur, int cnt)
854
+ {
855
+ *cur = array_nextsize (elem, *cur, cnt);
856
+ return ev_realloc (base, elem * *cur);
857
+ }
858
+
859
+ #define array_init_zero(base,count) \
860
+ memset ((void *)(base), 0, sizeof (*(base)) * (count))
861
+
862
+ #define array_needsize(type,base,cur,cnt,init) \
863
+ if (expect_false ((cnt) > (cur))) \
864
+ { \
865
+ int ocur_ = (cur); \
866
+ (base) = (type *)array_realloc \
867
+ (sizeof (type), (base), &(cur), (cnt)); \
868
+ init ((base) + (ocur_), (cur) - ocur_); \
869
+ }
870
+
871
+ #if 0
872
+ #define array_slim(type,stem) \
873
+ if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
874
+ { \
875
+ stem ## max = array_roundsize (stem ## cnt >> 1); \
876
+ base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
877
+ fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
878
+ }
879
+ #endif
880
+
881
+ #define array_free(stem, idx) \
882
+ ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
883
+
884
+ /*****************************************************************************/
885
+
886
+ /* dummy callback for pending events */
887
+ static void noinline
888
+ pendingcb (EV_P_ ev_prepare *w, int revents)
889
+ {
890
+ }
891
+
892
+ void noinline
893
+ ev_feed_event (EV_P_ void *w, int revents)
894
+ {
895
+ W w_ = (W)w;
896
+ int pri = ABSPRI (w_);
897
+
898
+ if (expect_false (w_->pending))
899
+ pendings [pri][w_->pending - 1].events |= revents;
900
+ else
901
+ {
902
+ w_->pending = ++pendingcnt [pri];
903
+ array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
904
+ pendings [pri][w_->pending - 1].w = w_;
905
+ pendings [pri][w_->pending - 1].events = revents;
906
+ }
907
+ }
908
+
909
+ inline_speed void
910
+ feed_reverse (EV_P_ W w)
911
+ {
912
+ array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
913
+ rfeeds [rfeedcnt++] = w;
914
+ }
915
+
916
+ inline_size void
917
+ feed_reverse_done (EV_P_ int revents)
918
+ {
919
+ do
920
+ ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
921
+ while (rfeedcnt);
922
+ }
923
+
924
+ inline_speed void
925
+ queue_events (EV_P_ W *events, int eventcnt, int type)
926
+ {
927
+ int i;
928
+
929
+ for (i = 0; i < eventcnt; ++i)
930
+ ev_feed_event (EV_A_ events [i], type);
931
+ }
932
+
933
+ /*****************************************************************************/
934
+
935
+ inline_speed void
936
+ fd_event_nocheck (EV_P_ int fd, int revents)
937
+ {
938
+ ANFD *anfd = anfds + fd;
939
+ ev_io *w;
940
+
941
+ for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
942
+ {
943
+ int ev = w->events & revents;
944
+
945
+ if (ev)
946
+ ev_feed_event (EV_A_ (W)w, ev);
947
+ }
948
+ }
949
+
950
+ /* do not submit kernel events for fds that have reify set */
951
+ /* because that means they changed while we were polling for new events */
952
+ inline_speed void
953
+ fd_event (EV_P_ int fd, int revents)
954
+ {
955
+ ANFD *anfd = anfds + fd;
956
+
957
+ if (expect_true (!anfd->reify))
958
+ fd_event_nocheck (EV_A_ fd, revents);
959
+ }
960
+
961
+ void
962
+ ev_feed_fd_event (EV_P_ int fd, int revents)
963
+ {
964
+ if (fd >= 0 && fd < anfdmax)
965
+ fd_event_nocheck (EV_A_ fd, revents);
966
+ }
967
+
968
+ /* make sure the external fd watch events are in-sync */
969
+ /* with the kernel/libev internal state */
970
+ inline_size void
971
+ fd_reify (EV_P)
972
+ {
973
+ int i;
974
+
975
+ #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
976
+ for (i = 0; i < fdchangecnt; ++i)
977
+ {
978
+ int fd = fdchanges [i];
979
+ ANFD *anfd = anfds + fd;
980
+
981
+ if (anfd->reify & EV__IOFDSET)
982
+ {
983
+ SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
984
+
985
+ if (handle != anfd->handle)
986
+ {
987
+ unsigned long arg;
988
+
989
+ assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
990
+
991
+ /* handle changed, but fd didn't - we need to do it in two steps */
992
+ backend_modify (EV_A_ fd, anfd->events, 0);
993
+ anfd->events = 0;
994
+ anfd->handle = handle;
995
+ }
996
+ }
997
+ }
998
+ #endif
999
+
1000
+ for (i = 0; i < fdchangecnt; ++i)
1001
+ {
1002
+ int fd = fdchanges [i];
1003
+ ANFD *anfd = anfds + fd;
1004
+ ev_io *w;
1005
+
1006
+ unsigned char o_events = anfd->events;
1007
+ unsigned char o_reify = anfd->reify;
1008
+
1009
+ anfd->reify = 0;
1010
+
1011
+ /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
1012
+ {
1013
+ anfd->events = 0;
1014
+
1015
+ for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
1016
+ anfd->events |= (unsigned char)w->events;
1017
+
1018
+ if (o_events != anfd->events)
1019
+ o_reify = EV__IOFDSET; /* actually |= */
1020
+ }
1021
+
1022
+ if (o_reify & EV__IOFDSET)
1023
+ backend_modify (EV_A_ fd, o_events, anfd->events);
1024
+ }
1025
+
1026
+ fdchangecnt = 0;
1027
+ }
1028
+
1029
+ /* something about the given fd changed */
1030
+ inline_size void
1031
+ fd_change (EV_P_ int fd, int flags)
1032
+ {
1033
+ unsigned char reify = anfds [fd].reify;
1034
+ anfds [fd].reify |= flags;
1035
+
1036
+ if (expect_true (!reify))
1037
+ {
1038
+ ++fdchangecnt;
1039
+ array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
1040
+ fdchanges [fdchangecnt - 1] = fd;
1041
+ }
1042
+ }
1043
+
1044
+ /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
1045
+ inline_speed void
1046
+ fd_kill (EV_P_ int fd)
1047
+ {
1048
+ ev_io *w;
1049
+
1050
+ while ((w = (ev_io *)anfds [fd].head))
1051
+ {
1052
+ ev_io_stop (EV_A_ w);
1053
+ ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
1054
+ }
1055
+ }
1056
+
1057
+ /* check whether the given fd is actually valid, for error recovery */
1058
+ inline_size int
1059
+ fd_valid (int fd)
1060
+ {
1061
+ #ifdef _WIN32
1062
+ return EV_FD_TO_WIN32_HANDLE (fd) != -1;
1063
+ #else
1064
+ return fcntl (fd, F_GETFD) != -1;
1065
+ #endif
1066
+ }
1067
+
1068
+ /* called on EBADF to verify fds */
1069
+ static void noinline
1070
+ fd_ebadf (EV_P)
1071
+ {
1072
+ int fd;
1073
+
1074
+ for (fd = 0; fd < anfdmax; ++fd)
1075
+ if (anfds [fd].events)
1076
+ if (!fd_valid (fd) && errno == EBADF)
1077
+ fd_kill (EV_A_ fd);
1078
+ }
1079
+
1080
+ /* called on ENOMEM in select/poll to kill some fds and retry */
1081
+ static void noinline
1082
+ fd_enomem (EV_P)
1083
+ {
1084
+ int fd;
1085
+
1086
+ for (fd = anfdmax; fd--; )
1087
+ if (anfds [fd].events)
1088
+ {
1089
+ fd_kill (EV_A_ fd);
1090
+ break;
1091
+ }
1092
+ }
1093
+
1094
+ /* usually called after fork if backend needs to re-arm all fds from scratch */
1095
+ static void noinline
1096
+ fd_rearm_all (EV_P)
1097
+ {
1098
+ int fd;
1099
+
1100
+ for (fd = 0; fd < anfdmax; ++fd)
1101
+ if (anfds [fd].events)
1102
+ {
1103
+ anfds [fd].events = 0;
1104
+ anfds [fd].emask = 0;
1105
+ fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
1106
+ }
1107
+ }
1108
+
1109
+ /* used to prepare libev internal fd's */
1110
+ /* this is not fork-safe */
1111
+ inline_speed void
1112
+ fd_intern (int fd)
1113
+ {
1114
+ #ifdef _WIN32
1115
+ unsigned long arg = 1;
1116
+ ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
1117
+ #else
1118
+ fcntl (fd, F_SETFD, FD_CLOEXEC);
1119
+ fcntl (fd, F_SETFL, O_NONBLOCK);
1120
+ #endif
1121
+ }
1122
+
1123
+ /*****************************************************************************/
1124
+
1125
+ /*
1126
+ * the heap functions want a real array index. array index 0 is guaranteed to not
1127
+ * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
1128
+ * the branching factor of the d-tree.
1129
+ */
1130
+
1131
+ /*
1132
+ * at the moment we allow libev the luxury of two heaps,
1133
+ * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
1134
+ * which is more cache-efficient.
1135
+ * the difference is about 5% with 50000+ watchers.
1136
+ */
1137
+ #if EV_USE_4HEAP
1138
+
1139
+ #define DHEAP 4
1140
+ #define HEAP0 (DHEAP - 1) /* index of first element in heap */
1141
+ #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
1142
+ #define UPHEAP_DONE(p,k) ((p) == (k))
1143
+
1144
+ /* away from the root */
1145
+ inline_speed void
1146
+ downheap (ANHE *heap, int N, int k)
1147
+ {
1148
+ ANHE he = heap [k];
1149
+ ANHE *E = heap + N + HEAP0;
1150
+
1151
+ for (;;)
1152
+ {
1153
+ ev_tstamp minat;
1154
+ ANHE *minpos;
1155
+ ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
1156
+
1157
+ /* find minimum child */
1158
+ if (expect_true (pos + DHEAP - 1 < E))
1159
+ {
1160
+ /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
1161
+ if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
1162
+ if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
1163
+ if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
1164
+ }
1165
+ else if (pos < E)
1166
+ {
1167
+ /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
1168
+ if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
1169
+ if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
1170
+ if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
1171
+ }
1172
+ else
1173
+ break;
1174
+
1175
+ if (ANHE_at (he) <= minat)
1176
+ break;
1177
+
1178
+ heap [k] = *minpos;
1179
+ ev_active (ANHE_w (*minpos)) = k;
1180
+
1181
+ k = minpos - heap;
1182
+ }
1183
+
1184
+ heap [k] = he;
1185
+ ev_active (ANHE_w (he)) = k;
1186
+ }
1187
+
1188
+ #else /* 4HEAP */
1189
+
1190
+ #define HEAP0 1
1191
+ #define HPARENT(k) ((k) >> 1)
1192
+ #define UPHEAP_DONE(p,k) (!(p))
1193
+
1194
+ /* away from the root */
1195
+ inline_speed void
1196
+ downheap (ANHE *heap, int N, int k)
1197
+ {
1198
+ ANHE he = heap [k];
1199
+
1200
+ for (;;)
1201
+ {
1202
+ int c = k << 1;
1203
+
1204
+ if (c >= N + HEAP0)
1205
+ break;
1206
+
1207
+ c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
1208
+ ? 1 : 0;
1209
+
1210
+ if (ANHE_at (he) <= ANHE_at (heap [c]))
1211
+ break;
1212
+
1213
+ heap [k] = heap [c];
1214
+ ev_active (ANHE_w (heap [k])) = k;
1215
+
1216
+ k = c;
1217
+ }
1218
+
1219
+ heap [k] = he;
1220
+ ev_active (ANHE_w (he)) = k;
1221
+ }
1222
+ #endif
1223
+
1224
+ /* towards the root */
1225
+ inline_speed void
1226
+ upheap (ANHE *heap, int k)
1227
+ {
1228
+ ANHE he = heap [k];
1229
+
1230
+ for (;;)
1231
+ {
1232
+ int p = HPARENT (k);
1233
+
1234
+ if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he))
1235
+ break;
1236
+
1237
+ heap [k] = heap [p];
1238
+ ev_active (ANHE_w (heap [k])) = k;
1239
+ k = p;
1240
+ }
1241
+
1242
+ heap [k] = he;
1243
+ ev_active (ANHE_w (he)) = k;
1244
+ }
1245
+
1246
+ /* move an element suitably so it is in a correct place */
1247
+ inline_size void
1248
+ adjustheap (ANHE *heap, int N, int k)
1249
+ {
1250
+ if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
1251
+ upheap (heap, k);
1252
+ else
1253
+ downheap (heap, N, k);
1254
+ }
1255
+
1256
+ /* rebuild the heap: this function is used only once and executed rarely */
1257
+ inline_size void
1258
+ reheap (ANHE *heap, int N)
1259
+ {
1260
+ int i;
1261
+
1262
+ /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
1263
+ /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
1264
+ for (i = 0; i < N; ++i)
1265
+ upheap (heap, i + HEAP0);
1266
+ }
1267
+
1268
+ /*****************************************************************************/
1269
+
1270
+ /* associate signal watchers to a signal signal */
1271
+ typedef struct
1272
+ {
1273
+ EV_ATOMIC_T pending;
1274
+ #if EV_MULTIPLICITY
1275
+ EV_P;
1276
+ #endif
1277
+ WL head;
1278
+ } ANSIG;
1279
+
1280
+ static ANSIG signals [EV_NSIG - 1];
1281
+
1282
+ /*****************************************************************************/
1283
+
1284
+ #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1285
+
1286
+ static void noinline
1287
+ evpipe_init (EV_P)
1288
+ {
1289
+ if (!ev_is_active (&pipe_w))
1290
+ {
1291
+ # if EV_USE_EVENTFD
1292
+ evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
1293
+ if (evfd < 0 && errno == EINVAL)
1294
+ evfd = eventfd (0, 0);
1295
+
1296
+ if (evfd >= 0)
1297
+ {
1298
+ evpipe [0] = -1;
1299
+ fd_intern (evfd); /* doing it twice doesn't hurt */
1300
+ ev_io_set (&pipe_w, evfd, EV_READ);
1301
+ }
1302
+ else
1303
+ # endif
1304
+ {
1305
+ while (pipe (evpipe))
1306
+ ev_syserr ("(libev) error creating signal/async pipe");
1307
+
1308
+ fd_intern (evpipe [0]);
1309
+ fd_intern (evpipe [1]);
1310
+ ev_io_set (&pipe_w, evpipe [0], EV_READ);
1311
+ }
1312
+
1313
+ ev_io_start (EV_A_ &pipe_w);
1314
+ ev_unref (EV_A); /* watcher should not keep loop alive */
1315
+ }
1316
+ }
1317
+
1318
+ inline_size void
1319
+ evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1320
+ {
1321
+ if (!*flag)
1322
+ {
1323
+ int old_errno = errno; /* save errno because write might clobber it */
1324
+ char dummy;
1325
+
1326
+ *flag = 1;
1327
+
1328
+ #if EV_USE_EVENTFD
1329
+ if (evfd >= 0)
1330
+ {
1331
+ uint64_t counter = 1;
1332
+ write (evfd, &counter, sizeof (uint64_t));
1333
+ }
1334
+ else
1335
+ #endif
1336
+ /* win32 people keep sending patches that change this write() to send() */
1337
+ /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1338
+ /* so when you think this write should be a send instead, please find out */
1339
+ /* where your send() is from - it's definitely not the microsoft send, and */
1340
+ /* tell me. thank you. */
1341
+ write (evpipe [1], &dummy, 1);
1342
+
1343
+ errno = old_errno;
1344
+ }
1345
+ }
1346
+
1347
+ /* called whenever the libev signal pipe */
1348
+ /* got some events (signal, async) */
1349
+ static void
1350
+ pipecb (EV_P_ ev_io *iow, int revents)
1351
+ {
1352
+ int i;
1353
+
1354
+ #if EV_USE_EVENTFD
1355
+ if (evfd >= 0)
1356
+ {
1357
+ uint64_t counter;
1358
+ read (evfd, &counter, sizeof (uint64_t));
1359
+ }
1360
+ else
1361
+ #endif
1362
+ {
1363
+ char dummy;
1364
+ /* see discussion in evpipe_write when you think this read should be recv in win32 */
1365
+ read (evpipe [0], &dummy, 1);
1366
+ }
1367
+
1368
+ #if EV_SIGNAL_ENABLE
1369
+ if (sig_pending)
1370
+ {
1371
+ sig_pending = 0;
1372
+
1373
+ for (i = EV_NSIG - 1; i--; )
1374
+ if (expect_false (signals [i].pending))
1375
+ ev_feed_signal_event (EV_A_ i + 1);
1376
+ }
1377
+ #endif
1378
+
1379
+ #if EV_ASYNC_ENABLE
1380
+ if (async_pending)
1381
+ {
1382
+ async_pending = 0;
1383
+
1384
+ for (i = asynccnt; i--; )
1385
+ if (asyncs [i]->sent)
1386
+ {
1387
+ asyncs [i]->sent = 0;
1388
+ ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
1389
+ }
1390
+ }
1391
+ #endif
1392
+ }
1393
+
1394
+ /*****************************************************************************/
1395
+
1396
+ void
1397
+ ev_feed_signal (int signum)
1398
+ {
1399
+ #if EV_MULTIPLICITY
1400
+ EV_P = signals [signum - 1].loop;
1401
+
1402
+ if (!EV_A)
1403
+ return;
1404
+ #endif
1405
+
1406
+ signals [signum - 1].pending = 1;
1407
+ evpipe_write (EV_A_ &sig_pending);
1408
+ }
1409
+
1410
+ static void
1411
+ ev_sighandler (int signum)
1412
+ {
1413
+ #ifdef _WIN32
1414
+ signal (signum, ev_sighandler);
1415
+ #endif
1416
+
1417
+ ev_feed_signal (signum);
1418
+ }
1419
+
1420
+ void noinline
1421
+ ev_feed_signal_event (EV_P_ int signum)
1422
+ {
1423
+ WL w;
1424
+
1425
+ if (expect_false (signum <= 0 || signum > EV_NSIG))
1426
+ return;
1427
+
1428
+ --signum;
1429
+
1430
+ #if EV_MULTIPLICITY
1431
+ /* it is permissible to try to feed a signal to the wrong loop */
1432
+ /* or, likely more useful, feeding a signal nobody is waiting for */
1433
+
1434
+ if (expect_false (signals [signum].loop != EV_A))
1435
+ return;
1436
+ #endif
1437
+
1438
+ signals [signum].pending = 0;
1439
+
1440
+ for (w = signals [signum].head; w; w = w->next)
1441
+ ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1442
+ }
1443
+
1444
+ #if EV_USE_SIGNALFD
1445
+ static void
1446
+ sigfdcb (EV_P_ ev_io *iow, int revents)
1447
+ {
1448
+ struct signalfd_siginfo si[2], *sip; /* these structs are big */
1449
+
1450
+ for (;;)
1451
+ {
1452
+ ssize_t res = read (sigfd, si, sizeof (si));
1453
+
1454
+ /* not ISO-C, as res might be -1, but works with SuS */
1455
+ for (sip = si; (char *)sip < (char *)si + res; ++sip)
1456
+ ev_feed_signal_event (EV_A_ sip->ssi_signo);
1457
+
1458
+ if (res < (ssize_t)sizeof (si))
1459
+ break;
1460
+ }
1461
+ }
1462
+ #endif
1463
+
1464
+ #endif
1465
+
1466
+ /*****************************************************************************/
1467
+
1468
+ #if EV_CHILD_ENABLE
1469
+ static WL childs [EV_PID_HASHSIZE];
1470
+
1471
+ static ev_signal childev;
1472
+
1473
+ #ifndef WIFCONTINUED
1474
+ # define WIFCONTINUED(status) 0
1475
+ #endif
1476
+
1477
+ /* handle a single child status event */
1478
+ inline_speed void
1479
+ child_reap (EV_P_ int chain, int pid, int status)
1480
+ {
1481
+ ev_child *w;
1482
+ int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
1483
+
1484
+ for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
1485
+ {
1486
+ if ((w->pid == pid || !w->pid)
1487
+ && (!traced || (w->flags & 1)))
1488
+ {
1489
+ ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
1490
+ w->rpid = pid;
1491
+ w->rstatus = status;
1492
+ ev_feed_event (EV_A_ (W)w, EV_CHILD);
1493
+ }
1494
+ }
1495
+ }
1496
+
1497
+ #ifndef WCONTINUED
1498
+ # define WCONTINUED 0
1499
+ #endif
1500
+
1501
+ /* called on sigchld etc., calls waitpid */
1502
+ static void
1503
+ childcb (EV_P_ ev_signal *sw, int revents)
1504
+ {
1505
+ int pid, status;
1506
+
1507
+ /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
1508
+ if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
1509
+ if (!WCONTINUED
1510
+ || errno != EINVAL
1511
+ || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
1512
+ return;
1513
+
1514
+ /* make sure we are called again until all children have been reaped */
1515
+ /* we need to do it this way so that the callback gets called before we continue */
1516
+ ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
1517
+
1518
+ child_reap (EV_A_ pid, pid, status);
1519
+ if ((EV_PID_HASHSIZE) > 1)
1520
+ child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
1521
+ }
1522
+
1523
+ #endif
1524
+
1525
+ /*****************************************************************************/
1526
+
1527
+ #if EV_USE_IOCP
1528
+ # include "ev_iocp.c"
1529
+ #endif
1530
+ #if EV_USE_PORT
1531
+ # include "ev_port.c"
1532
+ #endif
1533
+ #if EV_USE_KQUEUE
1534
+ # include "ev_kqueue.c"
1535
+ #endif
1536
+ #if EV_USE_EPOLL
1537
+ # include "ev_epoll.c"
1538
+ #endif
1539
+ #if EV_USE_POLL
1540
+ # include "ev_poll.c"
1541
+ #endif
1542
+ #if EV_USE_SELECT
1543
+ # include "ev_select.c"
1544
+ #endif
1545
+
1546
+ int
1547
+ ev_version_major (void)
1548
+ {
1549
+ return EV_VERSION_MAJOR;
1550
+ }
1551
+
1552
+ int
1553
+ ev_version_minor (void)
1554
+ {
1555
+ return EV_VERSION_MINOR;
1556
+ }
1557
+
1558
+ /* return true if we are running with elevated privileges and should ignore env variables */
1559
+ int inline_size
1560
+ enable_secure (void)
1561
+ {
1562
+ #ifdef _WIN32
1563
+ return 0;
1564
+ #else
1565
+ return getuid () != geteuid ()
1566
+ || getgid () != getegid ();
1567
+ #endif
1568
+ }
1569
+
1570
+ unsigned int
1571
+ ev_supported_backends (void)
1572
+ {
1573
+ unsigned int flags = 0;
1574
+
1575
+ if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
1576
+ if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
1577
+ if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
1578
+ if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
1579
+ if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
1580
+
1581
+ return flags;
1582
+ }
1583
+
1584
+ unsigned int
1585
+ ev_recommended_backends (void)
1586
+ {
1587
+ unsigned int flags = ev_supported_backends ();
1588
+
1589
+ #ifndef __NetBSD__
1590
+ /* kqueue is borked on everything but netbsd apparently */
1591
+ /* it usually doesn't work correctly on anything but sockets and pipes */
1592
+ flags &= ~EVBACKEND_KQUEUE;
1593
+ #endif
1594
+ #ifdef __APPLE__
1595
+ /* only select works correctly on that "unix-certified" platform */
1596
+ flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
1597
+ flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
1598
+ #endif
1599
+ #ifdef __FreeBSD__
1600
+ flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
1601
+ #endif
1602
+
1603
+ return flags;
1604
+ }
1605
+
1606
+ unsigned int
1607
+ ev_embeddable_backends (void)
1608
+ {
1609
+ int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1610
+
1611
+ /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1612
+ if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
1613
+ flags &= ~EVBACKEND_EPOLL;
1614
+
1615
+ return flags;
1616
+ }
1617
+
1618
+ unsigned int
1619
+ ev_backend (EV_P)
1620
+ {
1621
+ return backend;
1622
+ }
1623
+
1624
+ #if EV_FEATURE_API
1625
+ unsigned int
1626
+ ev_iteration (EV_P)
1627
+ {
1628
+ return loop_count;
1629
+ }
1630
+
1631
+ unsigned int
1632
+ ev_depth (EV_P)
1633
+ {
1634
+ return loop_depth;
1635
+ }
1636
+
1637
+ void
1638
+ ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
1639
+ {
1640
+ io_blocktime = interval;
1641
+ }
1642
+
1643
+ void
1644
+ ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
1645
+ {
1646
+ timeout_blocktime = interval;
1647
+ }
1648
+
1649
+ void
1650
+ ev_set_userdata (EV_P_ void *data)
1651
+ {
1652
+ userdata = data;
1653
+ }
1654
+
1655
+ void *
1656
+ ev_userdata (EV_P)
1657
+ {
1658
+ return userdata;
1659
+ }
1660
+
1661
+ void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
1662
+ {
1663
+ invoke_cb = invoke_pending_cb;
1664
+ }
1665
+
1666
+ void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
1667
+ {
1668
+ release_cb = release;
1669
+ acquire_cb = acquire;
1670
+ }
1671
+ #endif
1672
+
1673
+ /* initialise a loop structure, must be zero-initialised */
1674
+ static void noinline
1675
+ loop_init (EV_P_ unsigned int flags)
1676
+ {
1677
+ if (!backend)
1678
+ {
1679
+ origflags = flags;
1680
+
1681
+ #if EV_USE_REALTIME
1682
+ if (!have_realtime)
1683
+ {
1684
+ struct timespec ts;
1685
+
1686
+ if (!clock_gettime (CLOCK_REALTIME, &ts))
1687
+ have_realtime = 1;
1688
+ }
1689
+ #endif
1690
+
1691
+ #if EV_USE_MONOTONIC
1692
+ if (!have_monotonic)
1693
+ {
1694
+ struct timespec ts;
1695
+
1696
+ if (!clock_gettime (CLOCK_MONOTONIC, &ts))
1697
+ have_monotonic = 1;
1698
+ }
1699
+ #endif
1700
+
1701
+ /* pid check not overridable via env */
1702
+ #ifndef _WIN32
1703
+ if (flags & EVFLAG_FORKCHECK)
1704
+ curpid = getpid ();
1705
+ #endif
1706
+
1707
+ if (!(flags & EVFLAG_NOENV)
1708
+ && !enable_secure ()
1709
+ && getenv ("LIBEV_FLAGS"))
1710
+ flags = atoi (getenv ("LIBEV_FLAGS"));
1711
+
1712
+ ev_rt_now = ev_time ();
1713
+ mn_now = get_clock ();
1714
+ now_floor = mn_now;
1715
+ rtmn_diff = ev_rt_now - mn_now;
1716
+ #if EV_FEATURE_API
1717
+ invoke_cb = ev_invoke_pending;
1718
+ #endif
1719
+
1720
+ io_blocktime = 0.;
1721
+ timeout_blocktime = 0.;
1722
+ backend = 0;
1723
+ backend_fd = -1;
1724
+ sig_pending = 0;
1725
+ #if EV_ASYNC_ENABLE
1726
+ async_pending = 0;
1727
+ #endif
1728
+ #if EV_USE_INOTIFY
1729
+ fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1730
+ #endif
1731
+ #if EV_USE_SIGNALFD
1732
+ sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
1733
+ #endif
1734
+
1735
+ if (!(flags & EVBACKEND_MASK))
1736
+ flags |= ev_recommended_backends ();
1737
+
1738
+ #if EV_USE_IOCP
1739
+ if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
1740
+ #endif
1741
+ #if EV_USE_PORT
1742
+ if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1743
+ #endif
1744
+ #if EV_USE_KQUEUE
1745
+ if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
1746
+ #endif
1747
+ #if EV_USE_EPOLL
1748
+ if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
1749
+ #endif
1750
+ #if EV_USE_POLL
1751
+ if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
1752
+ #endif
1753
+ #if EV_USE_SELECT
1754
+ if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
1755
+ #endif
1756
+
1757
+ ev_prepare_init (&pending_w, pendingcb);
1758
+
1759
+ #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1760
+ ev_init (&pipe_w, pipecb);
1761
+ ev_set_priority (&pipe_w, EV_MAXPRI);
1762
+ #endif
1763
+ }
1764
+ }
1765
+
1766
+ /* free up a loop structure */
1767
+ void
1768
+ ev_loop_destroy (EV_P)
1769
+ {
1770
+ int i;
1771
+
1772
+ #if EV_MULTIPLICITY
1773
+ /* mimic free (0) */
1774
+ if (!EV_A)
1775
+ return;
1776
+ #endif
1777
+
1778
+ #if EV_CLEANUP_ENABLE
1779
+ /* queue cleanup watchers (and execute them) */
1780
+ if (expect_false (cleanupcnt))
1781
+ {
1782
+ queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
1783
+ EV_INVOKE_PENDING;
1784
+ }
1785
+ #endif
1786
+
1787
+ #if EV_CHILD_ENABLE
1788
+ if (ev_is_active (&childev))
1789
+ {
1790
+ ev_ref (EV_A); /* child watcher */
1791
+ ev_signal_stop (EV_A_ &childev);
1792
+ }
1793
+ #endif
1794
+
1795
+ if (ev_is_active (&pipe_w))
1796
+ {
1797
+ /*ev_ref (EV_A);*/
1798
+ /*ev_io_stop (EV_A_ &pipe_w);*/
1799
+
1800
+ #if EV_USE_EVENTFD
1801
+ if (evfd >= 0)
1802
+ close (evfd);
1803
+ #endif
1804
+
1805
+ if (evpipe [0] >= 0)
1806
+ {
1807
+ EV_WIN32_CLOSE_FD (evpipe [0]);
1808
+ EV_WIN32_CLOSE_FD (evpipe [1]);
1809
+ }
1810
+ }
1811
+
1812
+ #if EV_USE_SIGNALFD
1813
+ if (ev_is_active (&sigfd_w))
1814
+ close (sigfd);
1815
+ #endif
1816
+
1817
+ #if EV_USE_INOTIFY
1818
+ if (fs_fd >= 0)
1819
+ close (fs_fd);
1820
+ #endif
1821
+
1822
+ if (backend_fd >= 0)
1823
+ close (backend_fd);
1824
+
1825
+ #if EV_USE_IOCP
1826
+ if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
1827
+ #endif
1828
+ #if EV_USE_PORT
1829
+ if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
1830
+ #endif
1831
+ #if EV_USE_KQUEUE
1832
+ if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
1833
+ #endif
1834
+ #if EV_USE_EPOLL
1835
+ if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
1836
+ #endif
1837
+ #if EV_USE_POLL
1838
+ if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
1839
+ #endif
1840
+ #if EV_USE_SELECT
1841
+ if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
1842
+ #endif
1843
+
1844
+ for (i = NUMPRI; i--; )
1845
+ {
1846
+ array_free (pending, [i]);
1847
+ #if EV_IDLE_ENABLE
1848
+ array_free (idle, [i]);
1849
+ #endif
1850
+ }
1851
+
1852
+ ev_free (anfds); anfds = 0; anfdmax = 0;
1853
+
1854
+ /* have to use the microsoft-never-gets-it-right macro */
1855
+ array_free (rfeed, EMPTY);
1856
+ array_free (fdchange, EMPTY);
1857
+ array_free (timer, EMPTY);
1858
+ #if EV_PERIODIC_ENABLE
1859
+ array_free (periodic, EMPTY);
1860
+ #endif
1861
+ #if EV_FORK_ENABLE
1862
+ array_free (fork, EMPTY);
1863
+ #endif
1864
+ #if EV_CLEANUP_ENABLE
1865
+ array_free (cleanup, EMPTY);
1866
+ #endif
1867
+ array_free (prepare, EMPTY);
1868
+ array_free (check, EMPTY);
1869
+ #if EV_ASYNC_ENABLE
1870
+ array_free (async, EMPTY);
1871
+ #endif
1872
+
1873
+ backend = 0;
1874
+
1875
+ #if EV_MULTIPLICITY
1876
+ if (ev_is_default_loop (EV_A))
1877
+ #endif
1878
+ ev_default_loop_ptr = 0;
1879
+ #if EV_MULTIPLICITY
1880
+ else
1881
+ ev_free (EV_A);
1882
+ #endif
1883
+ }
1884
+
1885
+ #if EV_USE_INOTIFY
1886
+ inline_size void infy_fork (EV_P);
1887
+ #endif
1888
+
1889
+ inline_size void
1890
+ loop_fork (EV_P)
1891
+ {
1892
+ #if EV_USE_PORT
1893
+ if (backend == EVBACKEND_PORT ) port_fork (EV_A);
1894
+ #endif
1895
+ #if EV_USE_KQUEUE
1896
+ if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
1897
+ #endif
1898
+ #if EV_USE_EPOLL
1899
+ if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1900
+ #endif
1901
+ #if EV_USE_INOTIFY
1902
+ infy_fork (EV_A);
1903
+ #endif
1904
+
1905
+ if (ev_is_active (&pipe_w))
1906
+ {
1907
+ /* this "locks" the handlers against writing to the pipe */
1908
+ /* while we modify the fd vars */
1909
+ sig_pending = 1;
1910
+ #if EV_ASYNC_ENABLE
1911
+ async_pending = 1;
1912
+ #endif
1913
+
1914
+ ev_ref (EV_A);
1915
+ ev_io_stop (EV_A_ &pipe_w);
1916
+
1917
+ #if EV_USE_EVENTFD
1918
+ if (evfd >= 0)
1919
+ close (evfd);
1920
+ #endif
1921
+
1922
+ if (evpipe [0] >= 0)
1923
+ {
1924
+ EV_WIN32_CLOSE_FD (evpipe [0]);
1925
+ EV_WIN32_CLOSE_FD (evpipe [1]);
1926
+ }
1927
+
1928
+ #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
1929
+ evpipe_init (EV_A);
1930
+ /* now iterate over everything, in case we missed something */
1931
+ pipecb (EV_A_ &pipe_w, EV_READ);
1932
+ #endif
1933
+ }
1934
+
1935
+ postfork = 0;
1936
+ }
1937
+
1938
+ #if EV_MULTIPLICITY
1939
+
1940
+ struct ev_loop *
1941
+ ev_loop_new (unsigned int flags)
1942
+ {
1943
+ EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
1944
+
1945
+ memset (EV_A, 0, sizeof (struct ev_loop));
1946
+ loop_init (EV_A_ flags);
1947
+
1948
+ if (ev_backend (EV_A))
1949
+ return EV_A;
1950
+
1951
+ ev_free (EV_A);
1952
+ return 0;
1953
+ }
1954
+
1955
+ #endif /* multiplicity */
1956
+
1957
+ int
1958
+ ev_loop_refcount (EV_P)
1959
+ {
1960
+ return activecnt;
1961
+ }
1962
+
1963
+ #if EV_VERIFY
1964
+ static void noinline
1965
+ verify_watcher (EV_P_ W w)
1966
+ {
1967
+ assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1968
+
1969
+ if (w->pending)
1970
+ assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
1971
+ }
1972
+
1973
+ static void noinline
1974
+ verify_heap (EV_P_ ANHE *heap, int N)
1975
+ {
1976
+ int i;
1977
+
1978
+ for (i = HEAP0; i < N + HEAP0; ++i)
1979
+ {
1980
+ assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
1981
+ assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
1982
+ assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
1983
+
1984
+ verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
1985
+ }
1986
+ }
1987
+
1988
+ static void noinline
1989
+ array_verify (EV_P_ W *ws, int cnt)
1990
+ {
1991
+ while (cnt--)
1992
+ {
1993
+ assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
1994
+ verify_watcher (EV_A_ ws [cnt]);
1995
+ }
1996
+ }
1997
+ #endif
1998
+
1999
+ #if EV_FEATURE_API
2000
+ void
2001
+ ev_verify (EV_P)
2002
+ {
2003
+ #if EV_VERIFY
2004
+ int i;
2005
+ WL w;
2006
+
2007
+ assert (activecnt >= -1);
2008
+
2009
+ assert (fdchangemax >= fdchangecnt);
2010
+ for (i = 0; i < fdchangecnt; ++i)
2011
+ assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
2012
+
2013
+ assert (anfdmax >= 0);
2014
+ for (i = 0; i < anfdmax; ++i)
2015
+ for (w = anfds [i].head; w; w = w->next)
2016
+ {
2017
+ verify_watcher (EV_A_ (W)w);
2018
+ assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
2019
+ assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
2020
+ }
2021
+
2022
+ assert (timermax >= timercnt);
2023
+ verify_heap (EV_A_ timers, timercnt);
2024
+
2025
+ #if EV_PERIODIC_ENABLE
2026
+ assert (periodicmax >= periodiccnt);
2027
+ verify_heap (EV_A_ periodics, periodiccnt);
2028
+ #endif
2029
+
2030
+ for (i = NUMPRI; i--; )
2031
+ {
2032
+ assert (pendingmax [i] >= pendingcnt [i]);
2033
+ #if EV_IDLE_ENABLE
2034
+ assert (idleall >= 0);
2035
+ assert (idlemax [i] >= idlecnt [i]);
2036
+ array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
2037
+ #endif
2038
+ }
2039
+
2040
+ #if EV_FORK_ENABLE
2041
+ assert (forkmax >= forkcnt);
2042
+ array_verify (EV_A_ (W *)forks, forkcnt);
2043
+ #endif
2044
+
2045
+ #if EV_CLEANUP_ENABLE
2046
+ assert (cleanupmax >= cleanupcnt);
2047
+ array_verify (EV_A_ (W *)cleanups, cleanupcnt);
2048
+ #endif
2049
+
2050
+ #if EV_ASYNC_ENABLE
2051
+ assert (asyncmax >= asynccnt);
2052
+ array_verify (EV_A_ (W *)asyncs, asynccnt);
2053
+ #endif
2054
+
2055
+ #if EV_PREPARE_ENABLE
2056
+ assert (preparemax >= preparecnt);
2057
+ array_verify (EV_A_ (W *)prepares, preparecnt);
2058
+ #endif
2059
+
2060
+ #if EV_CHECK_ENABLE
2061
+ assert (checkmax >= checkcnt);
2062
+ array_verify (EV_A_ (W *)checks, checkcnt);
2063
+ #endif
2064
+
2065
+ # if 0
2066
+ #if EV_CHILD_ENABLE
2067
+ for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
2068
+ for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
2069
+ #endif
2070
+ # endif
2071
+ #endif
2072
+ }
2073
+ #endif
2074
+
2075
+ #if EV_MULTIPLICITY
2076
+ struct ev_loop *
2077
+ #else
2078
+ int
2079
+ #endif
2080
+ ev_default_loop (unsigned int flags)
2081
+ {
2082
+ if (!ev_default_loop_ptr)
2083
+ {
2084
+ #if EV_MULTIPLICITY
2085
+ EV_P = ev_default_loop_ptr = &default_loop_struct;
2086
+ #else
2087
+ ev_default_loop_ptr = 1;
2088
+ #endif
2089
+
2090
+ loop_init (EV_A_ flags);
2091
+
2092
+ if (ev_backend (EV_A))
2093
+ {
2094
+ #if EV_CHILD_ENABLE
2095
+ ev_signal_init (&childev, childcb, SIGCHLD);
2096
+ ev_set_priority (&childev, EV_MAXPRI);
2097
+ ev_signal_start (EV_A_ &childev);
2098
+ ev_unref (EV_A); /* child watcher should not keep loop alive */
2099
+ #endif
2100
+ }
2101
+ else
2102
+ ev_default_loop_ptr = 0;
2103
+ }
2104
+
2105
+ return ev_default_loop_ptr;
2106
+ }
2107
+
2108
+ void
2109
+ ev_loop_fork (EV_P)
2110
+ {
2111
+ postfork = 1; /* must be in line with ev_default_fork */
2112
+ }
2113
+
2114
+ /*****************************************************************************/
2115
+
2116
+ void
2117
+ ev_invoke (EV_P_ void *w, int revents)
2118
+ {
2119
+ EV_CB_INVOKE ((W)w, revents);
2120
+ }
2121
+
2122
+ unsigned int
2123
+ ev_pending_count (EV_P)
2124
+ {
2125
+ int pri;
2126
+ unsigned int count = 0;
2127
+
2128
+ for (pri = NUMPRI; pri--; )
2129
+ count += pendingcnt [pri];
2130
+
2131
+ return count;
2132
+ }
2133
+
2134
+ void noinline
2135
+ ev_invoke_pending (EV_P)
2136
+ {
2137
+ int pri;
2138
+
2139
+ for (pri = NUMPRI; pri--; )
2140
+ while (pendingcnt [pri])
2141
+ {
2142
+ ANPENDING *p = pendings [pri] + --pendingcnt [pri];
2143
+
2144
+ p->w->pending = 0;
2145
+ EV_CB_INVOKE (p->w, p->events);
2146
+ EV_FREQUENT_CHECK;
2147
+ }
2148
+ }
2149
+
2150
+ #if EV_IDLE_ENABLE
2151
+ /* make idle watchers pending. this handles the "call-idle */
2152
+ /* only when higher priorities are idle" logic */
2153
+ inline_size void
2154
+ idle_reify (EV_P)
2155
+ {
2156
+ if (expect_false (idleall))
2157
+ {
2158
+ int pri;
2159
+
2160
+ for (pri = NUMPRI; pri--; )
2161
+ {
2162
+ if (pendingcnt [pri])
2163
+ break;
2164
+
2165
+ if (idlecnt [pri])
2166
+ {
2167
+ queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
2168
+ break;
2169
+ }
2170
+ }
2171
+ }
2172
+ }
2173
+ #endif
2174
+
2175
+ /* make timers pending */
2176
+ inline_size void
2177
+ timers_reify (EV_P)
2178
+ {
2179
+ EV_FREQUENT_CHECK;
2180
+
2181
+ if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
2182
+ {
2183
+ do
2184
+ {
2185
+ ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
2186
+
2187
+ /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
2188
+
2189
+ /* first reschedule or stop timer */
2190
+ if (w->repeat)
2191
+ {
2192
+ ev_at (w) += w->repeat;
2193
+ if (ev_at (w) < mn_now)
2194
+ ev_at (w) = mn_now;
2195
+
2196
+ assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
2197
+
2198
+ ANHE_at_cache (timers [HEAP0]);
2199
+ downheap (timers, timercnt, HEAP0);
2200
+ }
2201
+ else
2202
+ ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
2203
+
2204
+ EV_FREQUENT_CHECK;
2205
+ feed_reverse (EV_A_ (W)w);
2206
+ }
2207
+ while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
2208
+
2209
+ feed_reverse_done (EV_A_ EV_TIMER);
2210
+ }
2211
+ }
2212
+
2213
+ #if EV_PERIODIC_ENABLE
2214
+
2215
+ inline_speed void
2216
+ periodic_recalc (EV_P_ ev_periodic *w)
2217
+ {
2218
+ /* TODO: use slow but potentially more correct incremental algo, */
2219
+ /* also do not rely on ceil */
2220
+ ev_at (w) = w->offset + ceil ((ev_rt_now - w->offset) / w->interval) * w->interval;
2221
+ }
2222
+
2223
+ /* make periodics pending */
2224
+ inline_size void
2225
+ periodics_reify (EV_P)
2226
+ {
2227
+ EV_FREQUENT_CHECK;
2228
+
2229
+ while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
2230
+ {
2231
+ int feed_count = 0;
2232
+
2233
+ do
2234
+ {
2235
+ ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
2236
+
2237
+ /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
2238
+
2239
+ /* first reschedule or stop timer */
2240
+ if (w->reschedule_cb)
2241
+ {
2242
+ ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2243
+
2244
+ assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
2245
+
2246
+ ANHE_at_cache (periodics [HEAP0]);
2247
+ downheap (periodics, periodiccnt, HEAP0);
2248
+ }
2249
+ else if (w->interval)
2250
+ {
2251
+ periodic_recalc (EV_A_ w);
2252
+
2253
+ /* if next trigger time is not sufficiently in the future, put it there */
2254
+ /* this might happen because of floating point inexactness */
2255
+ if (ev_at (w) - ev_rt_now < TIME_EPSILON)
2256
+ {
2257
+ ev_at (w) += w->interval;
2258
+
2259
+ /* if interval is unreasonably low we might still have a time in the past */
2260
+ /* so correct this. this will make the periodic very inexact, but the user */
2261
+ /* has effectively asked to get triggered more often than possible */
2262
+ if (ev_at (w) < ev_rt_now)
2263
+ ev_at (w) = ev_rt_now;
2264
+ }
2265
+
2266
+ ANHE_at_cache (periodics [HEAP0]);
2267
+ downheap (periodics, periodiccnt, HEAP0);
2268
+ }
2269
+ else
2270
+ ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
2271
+
2272
+ EV_FREQUENT_CHECK;
2273
+ feed_reverse (EV_A_ (W)w);
2274
+ }
2275
+ while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now);
2276
+
2277
+ feed_reverse_done (EV_A_ EV_PERIODIC);
2278
+ }
2279
+ }
2280
+
2281
+ /* simply recalculate all periodics */
2282
+ /* TODO: maybe ensure that at least one event happens when jumping forward? */
2283
+ static void noinline
2284
+ periodics_reschedule (EV_P)
2285
+ {
2286
+ int i;
2287
+
2288
+ /* adjust periodics after time jump */
2289
+ for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
2290
+ {
2291
+ ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
2292
+
2293
+ if (w->reschedule_cb)
2294
+ ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2295
+ else if (w->interval)
2296
+ periodic_recalc (EV_A_ w);
2297
+
2298
+ ANHE_at_cache (periodics [i]);
2299
+ }
2300
+
2301
+ reheap (periodics, periodiccnt);
2302
+ }
2303
+ #endif
2304
+
2305
+ /* adjust all timers by a given offset */
2306
+ static void noinline
2307
+ timers_reschedule (EV_P_ ev_tstamp adjust)
2308
+ {
2309
+ int i;
2310
+
2311
+ for (i = 0; i < timercnt; ++i)
2312
+ {
2313
+ ANHE *he = timers + i + HEAP0;
2314
+ ANHE_w (*he)->at += adjust;
2315
+ ANHE_at_cache (*he);
2316
+ }
2317
+ }
2318
+
2319
+ /* fetch new monotonic and realtime times from the kernel */
2320
+ /* also detect if there was a timejump, and act accordingly */
2321
+ inline_speed void
2322
+ time_update (EV_P_ ev_tstamp max_block)
2323
+ {
2324
+ #if EV_USE_MONOTONIC
2325
+ if (expect_true (have_monotonic))
2326
+ {
2327
+ int i;
2328
+ ev_tstamp odiff = rtmn_diff;
2329
+
2330
+ mn_now = get_clock ();
2331
+
2332
+ /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
2333
+ /* interpolate in the meantime */
2334
+ if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
2335
+ {
2336
+ ev_rt_now = rtmn_diff + mn_now;
2337
+ return;
2338
+ }
2339
+
2340
+ now_floor = mn_now;
2341
+ ev_rt_now = ev_time ();
2342
+
2343
+ /* loop a few times, before making important decisions.
2344
+ * on the choice of "4": one iteration isn't enough,
2345
+ * in case we get preempted during the calls to
2346
+ * ev_time and get_clock. a second call is almost guaranteed
2347
+ * to succeed in that case, though. and looping a few more times
2348
+ * doesn't hurt either as we only do this on time-jumps or
2349
+ * in the unlikely event of having been preempted here.
2350
+ */
2351
+ for (i = 4; --i; )
2352
+ {
2353
+ rtmn_diff = ev_rt_now - mn_now;
2354
+
2355
+ if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
2356
+ return; /* all is well */
2357
+
2358
+ ev_rt_now = ev_time ();
2359
+ mn_now = get_clock ();
2360
+ now_floor = mn_now;
2361
+ }
2362
+
2363
+ /* no timer adjustment, as the monotonic clock doesn't jump */
2364
+ /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
2365
+ # if EV_PERIODIC_ENABLE
2366
+ periodics_reschedule (EV_A);
2367
+ # endif
2368
+ }
2369
+ else
2370
+ #endif
2371
+ {
2372
+ ev_rt_now = ev_time ();
2373
+
2374
+ if (expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + MIN_TIMEJUMP))
2375
+ {
2376
+ /* adjust timers. this is easy, as the offset is the same for all of them */
2377
+ timers_reschedule (EV_A_ ev_rt_now - mn_now);
2378
+ #if EV_PERIODIC_ENABLE
2379
+ periodics_reschedule (EV_A);
2380
+ #endif
2381
+ }
2382
+
2383
+ mn_now = ev_rt_now;
2384
+ }
2385
+ }
2386
+
2387
+ void
2388
+ ev_run (EV_P_ int flags)
2389
+ {
2390
+ #if EV_FEATURE_API
2391
+ ++loop_depth;
2392
+ #endif
2393
+
2394
+ assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
2395
+
2396
+ loop_done = EVBREAK_CANCEL;
2397
+
2398
+ EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2399
+
2400
+ do
2401
+ {
2402
+ #if EV_VERIFY >= 2
2403
+ ev_verify (EV_A);
2404
+ #endif
2405
+
2406
+ #ifndef _WIN32
2407
+ if (expect_false (curpid)) /* penalise the forking check even more */
2408
+ if (expect_false (getpid () != curpid))
2409
+ {
2410
+ curpid = getpid ();
2411
+ postfork = 1;
2412
+ }
2413
+ #endif
2414
+
2415
+ #if EV_FORK_ENABLE
2416
+ /* we might have forked, so queue fork handlers */
2417
+ if (expect_false (postfork))
2418
+ if (forkcnt)
2419
+ {
2420
+ queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
2421
+ EV_INVOKE_PENDING;
2422
+ }
2423
+ #endif
2424
+
2425
+ #if EV_PREPARE_ENABLE
2426
+ /* queue prepare watchers (and execute them) */
2427
+ if (expect_false (preparecnt))
2428
+ {
2429
+ queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
2430
+ EV_INVOKE_PENDING;
2431
+ }
2432
+ #endif
2433
+
2434
+ if (expect_false (loop_done))
2435
+ break;
2436
+
2437
+ /* we might have forked, so reify kernel state if necessary */
2438
+ if (expect_false (postfork))
2439
+ loop_fork (EV_A);
2440
+
2441
+ /* update fd-related kernel structures */
2442
+ fd_reify (EV_A);
2443
+
2444
+ /* calculate blocking time */
2445
+ {
2446
+ ev_tstamp waittime = 0.;
2447
+ ev_tstamp sleeptime = 0.;
2448
+
2449
+ /* remember old timestamp for io_blocktime calculation */
2450
+ ev_tstamp prev_mn_now = mn_now;
2451
+
2452
+ /* update time to cancel out callback processing overhead */
2453
+ time_update (EV_A_ 1e100);
2454
+
2455
+ if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
2456
+ {
2457
+ waittime = MAX_BLOCKTIME;
2458
+
2459
+ if (timercnt)
2460
+ {
2461
+ ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2462
+ if (waittime > to) waittime = to;
2463
+ }
2464
+
2465
+ #if EV_PERIODIC_ENABLE
2466
+ if (periodiccnt)
2467
+ {
2468
+ ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now + backend_fudge;
2469
+ if (waittime > to) waittime = to;
2470
+ }
2471
+ #endif
2472
+
2473
+ /* don't let timeouts decrease the waittime below timeout_blocktime */
2474
+ if (expect_false (waittime < timeout_blocktime))
2475
+ waittime = timeout_blocktime;
2476
+
2477
+ /* extra check because io_blocktime is commonly 0 */
2478
+ if (expect_false (io_blocktime))
2479
+ {
2480
+ sleeptime = io_blocktime - (mn_now - prev_mn_now);
2481
+
2482
+ if (sleeptime > waittime - backend_fudge)
2483
+ sleeptime = waittime - backend_fudge;
2484
+
2485
+ if (expect_true (sleeptime > 0.))
2486
+ {
2487
+ ev_sleep (sleeptime);
2488
+ waittime -= sleeptime;
2489
+ }
2490
+ }
2491
+ }
2492
+
2493
+ #if EV_FEATURE_API
2494
+ ++loop_count;
2495
+ #endif
2496
+ assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2497
+ backend_poll (EV_A_ waittime);
2498
+ assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2499
+
2500
+ /* update ev_rt_now, do magic */
2501
+ time_update (EV_A_ waittime + sleeptime);
2502
+ }
2503
+
2504
+ /* queue pending timers and reschedule them */
2505
+ timers_reify (EV_A); /* relative timers called last */
2506
+ #if EV_PERIODIC_ENABLE
2507
+ periodics_reify (EV_A); /* absolute timers called first */
2508
+ #endif
2509
+
2510
+ #if EV_IDLE_ENABLE
2511
+ /* queue idle watchers unless other events are pending */
2512
+ idle_reify (EV_A);
2513
+ #endif
2514
+
2515
+ #if EV_CHECK_ENABLE
2516
+ /* queue check watchers, to be executed first */
2517
+ if (expect_false (checkcnt))
2518
+ queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
2519
+ #endif
2520
+
2521
+ EV_INVOKE_PENDING;
2522
+ }
2523
+ while (expect_true (
2524
+ activecnt
2525
+ && !loop_done
2526
+ && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2527
+ ));
2528
+
2529
+ if (loop_done == EVBREAK_ONE)
2530
+ loop_done = EVBREAK_CANCEL;
2531
+
2532
+ #if EV_FEATURE_API
2533
+ --loop_depth;
2534
+ #endif
2535
+ }
2536
+
2537
+ /* libuv special */
2538
+ void
2539
+ ev__run (EV_P_ ev_tstamp waittime)
2540
+ {
2541
+ fd_reify (EV_A);
2542
+ backend_poll (EV_A_ waittime);
2543
+ }
2544
+
2545
+ void
2546
+ ev_break (EV_P_ int how)
2547
+ {
2548
+ loop_done = how;
2549
+ }
2550
+
2551
+ void
2552
+ ev_ref (EV_P)
2553
+ {
2554
+ ++activecnt;
2555
+ }
2556
+
2557
+ void
2558
+ ev_unref (EV_P)
2559
+ {
2560
+ --activecnt;
2561
+ if (activecnt < 0) abort();
2562
+ }
2563
+
2564
+ void
2565
+ ev_now_update (EV_P)
2566
+ {
2567
+ time_update (EV_A_ 1e100);
2568
+ }
2569
+
2570
+ void
2571
+ ev_suspend (EV_P)
2572
+ {
2573
+ ev_now_update (EV_A);
2574
+ }
2575
+
2576
+ void
2577
+ ev_resume (EV_P)
2578
+ {
2579
+ ev_tstamp mn_prev = mn_now;
2580
+
2581
+ ev_now_update (EV_A);
2582
+ timers_reschedule (EV_A_ mn_now - mn_prev);
2583
+ #if EV_PERIODIC_ENABLE
2584
+ /* TODO: really do this? */
2585
+ periodics_reschedule (EV_A);
2586
+ #endif
2587
+ }
2588
+
2589
+ /*****************************************************************************/
2590
+ /* singly-linked list management, used when the expected list length is short */
2591
+
2592
+ inline_size void
2593
+ wlist_add (WL *head, WL elem)
2594
+ {
2595
+ elem->next = *head;
2596
+ *head = elem;
2597
+ }
2598
+
2599
+ inline_size void
2600
+ wlist_del (WL *head, WL elem)
2601
+ {
2602
+ while (*head)
2603
+ {
2604
+ if (expect_true (*head == elem))
2605
+ {
2606
+ *head = elem->next;
2607
+ break;
2608
+ }
2609
+
2610
+ head = &(*head)->next;
2611
+ }
2612
+ }
2613
+
2614
+ /* internal, faster, version of ev_clear_pending */
2615
+ inline_speed void
2616
+ clear_pending (EV_P_ W w)
2617
+ {
2618
+ if (w->pending)
2619
+ {
2620
+ pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
2621
+ w->pending = 0;
2622
+ }
2623
+ }
2624
+
2625
+ int
2626
+ ev_clear_pending (EV_P_ void *w)
2627
+ {
2628
+ W w_ = (W)w;
2629
+ int pending = w_->pending;
2630
+
2631
+ if (expect_true (pending))
2632
+ {
2633
+ ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
2634
+ p->w = (W)&pending_w;
2635
+ w_->pending = 0;
2636
+ return p->events;
2637
+ }
2638
+ else
2639
+ return 0;
2640
+ }
2641
+
2642
+ inline_size void
2643
+ pri_adjust (EV_P_ W w)
2644
+ {
2645
+ int pri = ev_priority (w);
2646
+ pri = pri < EV_MINPRI ? EV_MINPRI : pri;
2647
+ pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
2648
+ ev_set_priority (w, pri);
2649
+ }
2650
+
2651
+ inline_speed void
2652
+ ev_start (EV_P_ W w, int active)
2653
+ {
2654
+ pri_adjust (EV_A_ w);
2655
+ w->active = active;
2656
+ ev_ref (EV_A);
2657
+ }
2658
+
2659
+ inline_size void
2660
+ ev_stop (EV_P_ W w)
2661
+ {
2662
+ ev_unref (EV_A);
2663
+ w->active = 0;
2664
+ }
2665
+
2666
+ /*****************************************************************************/
2667
+
2668
+ void noinline
2669
+ ev_io_start (EV_P_ ev_io *w)
2670
+ {
2671
+ int fd = w->fd;
2672
+
2673
+ if (expect_false (ev_is_active (w)))
2674
+ return;
2675
+
2676
+ assert (("libev: ev_io_start called with negative fd", fd >= 0));
2677
+ assert (("libev: ev_io_start called with illegal event mask",
2678
+ !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE | EV_LIBUV_KQUEUE_HACK))));
2679
+
2680
+ EV_FREQUENT_CHECK;
2681
+
2682
+ ev_start (EV_A_ (W)w, 1);
2683
+ array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2684
+ wlist_add (&anfds[fd].head, (WL)w);
2685
+
2686
+ fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
2687
+ w->events &= ~EV__IOFDSET;
2688
+
2689
+ EV_FREQUENT_CHECK;
2690
+ }
2691
+
2692
+ void noinline
2693
+ ev_io_stop (EV_P_ ev_io *w)
2694
+ {
2695
+ clear_pending (EV_A_ (W)w);
2696
+ if (expect_false (!ev_is_active (w)))
2697
+ return;
2698
+
2699
+ assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
2700
+
2701
+ EV_FREQUENT_CHECK;
2702
+
2703
+ wlist_del (&anfds[w->fd].head, (WL)w);
2704
+ ev_stop (EV_A_ (W)w);
2705
+
2706
+ fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
2707
+
2708
+ EV_FREQUENT_CHECK;
2709
+ }
2710
+
2711
+ void noinline
2712
+ ev_timer_start (EV_P_ ev_timer *w)
2713
+ {
2714
+ if (expect_false (ev_is_active (w)))
2715
+ return;
2716
+
2717
+ ev_at (w) += mn_now;
2718
+
2719
+ assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
2720
+
2721
+ EV_FREQUENT_CHECK;
2722
+
2723
+ ++timercnt;
2724
+ ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1);
2725
+ array_needsize (ANHE, timers, timermax, ev_active (w) + 1, EMPTY2);
2726
+ ANHE_w (timers [ev_active (w)]) = (WT)w;
2727
+ ANHE_at_cache (timers [ev_active (w)]);
2728
+ upheap (timers, ev_active (w));
2729
+
2730
+ EV_FREQUENT_CHECK;
2731
+
2732
+ /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
2733
+ }
2734
+
2735
+ void noinline
2736
+ ev_timer_stop (EV_P_ ev_timer *w)
2737
+ {
2738
+ clear_pending (EV_A_ (W)w);
2739
+ if (expect_false (!ev_is_active (w)))
2740
+ return;
2741
+
2742
+ EV_FREQUENT_CHECK;
2743
+
2744
+ {
2745
+ int active = ev_active (w);
2746
+
2747
+ assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
2748
+
2749
+ --timercnt;
2750
+
2751
+ if (expect_true (active < timercnt + HEAP0))
2752
+ {
2753
+ timers [active] = timers [timercnt + HEAP0];
2754
+ adjustheap (timers, timercnt, active);
2755
+ }
2756
+ }
2757
+
2758
+ ev_at (w) -= mn_now;
2759
+
2760
+ ev_stop (EV_A_ (W)w);
2761
+
2762
+ EV_FREQUENT_CHECK;
2763
+ }
2764
+
2765
+ void noinline
2766
+ ev_timer_again (EV_P_ ev_timer *w)
2767
+ {
2768
+ EV_FREQUENT_CHECK;
2769
+
2770
+ if (ev_is_active (w))
2771
+ {
2772
+ if (w->repeat)
2773
+ {
2774
+ ev_at (w) = mn_now + w->repeat;
2775
+ ANHE_at_cache (timers [ev_active (w)]);
2776
+ adjustheap (timers, timercnt, ev_active (w));
2777
+ }
2778
+ else
2779
+ ev_timer_stop (EV_A_ w);
2780
+ }
2781
+ else if (w->repeat)
2782
+ {
2783
+ ev_at (w) = w->repeat;
2784
+ ev_timer_start (EV_A_ w);
2785
+ }
2786
+
2787
+ EV_FREQUENT_CHECK;
2788
+ }
2789
+
2790
+ ev_tstamp
2791
+ ev_timer_remaining (EV_P_ ev_timer *w)
2792
+ {
2793
+ return ev_at (w) - (ev_is_active (w) ? mn_now : 0.);
2794
+ }
2795
+
2796
+ #if EV_PERIODIC_ENABLE
2797
+ void noinline
2798
+ ev_periodic_start (EV_P_ ev_periodic *w)
2799
+ {
2800
+ if (expect_false (ev_is_active (w)))
2801
+ return;
2802
+
2803
+ if (w->reschedule_cb)
2804
+ ev_at (w) = w->reschedule_cb (w, ev_rt_now);
2805
+ else if (w->interval)
2806
+ {
2807
+ assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
2808
+ periodic_recalc (EV_A_ w);
2809
+ }
2810
+ else
2811
+ ev_at (w) = w->offset;
2812
+
2813
+ EV_FREQUENT_CHECK;
2814
+
2815
+ ++periodiccnt;
2816
+ ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1);
2817
+ array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, EMPTY2);
2818
+ ANHE_w (periodics [ev_active (w)]) = (WT)w;
2819
+ ANHE_at_cache (periodics [ev_active (w)]);
2820
+ upheap (periodics, ev_active (w));
2821
+
2822
+ EV_FREQUENT_CHECK;
2823
+
2824
+ /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
2825
+ }
2826
+
2827
+ void noinline
2828
+ ev_periodic_stop (EV_P_ ev_periodic *w)
2829
+ {
2830
+ clear_pending (EV_A_ (W)w);
2831
+ if (expect_false (!ev_is_active (w)))
2832
+ return;
2833
+
2834
+ EV_FREQUENT_CHECK;
2835
+
2836
+ {
2837
+ int active = ev_active (w);
2838
+
2839
+ assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
2840
+
2841
+ --periodiccnt;
2842
+
2843
+ if (expect_true (active < periodiccnt + HEAP0))
2844
+ {
2845
+ periodics [active] = periodics [periodiccnt + HEAP0];
2846
+ adjustheap (periodics, periodiccnt, active);
2847
+ }
2848
+ }
2849
+
2850
+ ev_stop (EV_A_ (W)w);
2851
+
2852
+ EV_FREQUENT_CHECK;
2853
+ }
2854
+
2855
+ void noinline
2856
+ ev_periodic_again (EV_P_ ev_periodic *w)
2857
+ {
2858
+ /* TODO: use adjustheap and recalculation */
2859
+ ev_periodic_stop (EV_A_ w);
2860
+ ev_periodic_start (EV_A_ w);
2861
+ }
2862
+ #endif
2863
+
2864
+ #ifndef SA_RESTART
2865
+ # define SA_RESTART 0
2866
+ #endif
2867
+
2868
+ #if EV_SIGNAL_ENABLE
2869
+
2870
+ void noinline
2871
+ ev_signal_start (EV_P_ ev_signal *w)
2872
+ {
2873
+ if (expect_false (ev_is_active (w)))
2874
+ return;
2875
+
2876
+ assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2877
+
2878
+ #if EV_MULTIPLICITY
2879
+ assert (("libev: a signal must not be attached to two different loops",
2880
+ !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2881
+
2882
+ signals [w->signum - 1].loop = EV_A;
2883
+ #endif
2884
+
2885
+ EV_FREQUENT_CHECK;
2886
+
2887
+ #if EV_USE_SIGNALFD
2888
+ if (sigfd == -2)
2889
+ {
2890
+ sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
2891
+ if (sigfd < 0 && errno == EINVAL)
2892
+ sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
2893
+
2894
+ if (sigfd >= 0)
2895
+ {
2896
+ fd_intern (sigfd); /* doing it twice will not hurt */
2897
+
2898
+ sigemptyset (&sigfd_set);
2899
+
2900
+ ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
2901
+ ev_set_priority (&sigfd_w, EV_MAXPRI);
2902
+ ev_io_start (EV_A_ &sigfd_w);
2903
+ ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
2904
+ }
2905
+ }
2906
+
2907
+ if (sigfd >= 0)
2908
+ {
2909
+ /* TODO: check .head */
2910
+ sigaddset (&sigfd_set, w->signum);
2911
+ sigprocmask (SIG_BLOCK, &sigfd_set, 0);
2912
+
2913
+ signalfd (sigfd, &sigfd_set, 0);
2914
+ }
2915
+ #endif
2916
+
2917
+ ev_start (EV_A_ (W)w, 1);
2918
+ wlist_add (&signals [w->signum - 1].head, (WL)w);
2919
+
2920
+ if (!((WL)w)->next)
2921
+ # if EV_USE_SIGNALFD
2922
+ if (sigfd < 0) /*TODO*/
2923
+ # endif
2924
+ {
2925
+ # ifdef _WIN32
2926
+ evpipe_init (EV_A);
2927
+
2928
+ signal (w->signum, ev_sighandler);
2929
+ # else
2930
+ struct sigaction sa;
2931
+
2932
+ evpipe_init (EV_A);
2933
+
2934
+ sa.sa_handler = ev_sighandler;
2935
+ sigfillset (&sa.sa_mask);
2936
+ sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
2937
+ sigaction (w->signum, &sa, 0);
2938
+
2939
+ if (origflags & EVFLAG_NOSIGMASK)
2940
+ {
2941
+ sigemptyset (&sa.sa_mask);
2942
+ sigaddset (&sa.sa_mask, w->signum);
2943
+ sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
2944
+ }
2945
+ #endif
2946
+ }
2947
+
2948
+ EV_FREQUENT_CHECK;
2949
+ }
2950
+
2951
+ void noinline
2952
+ ev_signal_stop (EV_P_ ev_signal *w)
2953
+ {
2954
+ clear_pending (EV_A_ (W)w);
2955
+ if (expect_false (!ev_is_active (w)))
2956
+ return;
2957
+
2958
+ EV_FREQUENT_CHECK;
2959
+
2960
+ wlist_del (&signals [w->signum - 1].head, (WL)w);
2961
+ ev_stop (EV_A_ (W)w);
2962
+
2963
+ if (!signals [w->signum - 1].head)
2964
+ {
2965
+ #if EV_MULTIPLICITY
2966
+ signals [w->signum - 1].loop = 0; /* unattach from signal */
2967
+ #endif
2968
+ #if EV_USE_SIGNALFD
2969
+ if (sigfd >= 0)
2970
+ {
2971
+ sigset_t ss;
2972
+
2973
+ sigemptyset (&ss);
2974
+ sigaddset (&ss, w->signum);
2975
+ sigdelset (&sigfd_set, w->signum);
2976
+
2977
+ signalfd (sigfd, &sigfd_set, 0);
2978
+ sigprocmask (SIG_UNBLOCK, &ss, 0);
2979
+ }
2980
+ else
2981
+ #endif
2982
+ signal (w->signum, SIG_DFL);
2983
+ }
2984
+
2985
+ EV_FREQUENT_CHECK;
2986
+ }
2987
+
2988
+ #endif
2989
+
2990
+ #if EV_CHILD_ENABLE
2991
+
2992
+ void
2993
+ ev_child_start (EV_P_ ev_child *w)
2994
+ {
2995
+ #if EV_MULTIPLICITY
2996
+ assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
2997
+ #endif
2998
+ if (expect_false (ev_is_active (w)))
2999
+ return;
3000
+
3001
+ EV_FREQUENT_CHECK;
3002
+
3003
+ ev_start (EV_A_ (W)w, 1);
3004
+ wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
3005
+
3006
+ EV_FREQUENT_CHECK;
3007
+ }
3008
+
3009
+ void
3010
+ ev_child_stop (EV_P_ ev_child *w)
3011
+ {
3012
+ clear_pending (EV_A_ (W)w);
3013
+ if (expect_false (!ev_is_active (w)))
3014
+ return;
3015
+
3016
+ EV_FREQUENT_CHECK;
3017
+
3018
+ wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
3019
+ ev_stop (EV_A_ (W)w);
3020
+
3021
+ EV_FREQUENT_CHECK;
3022
+ }
3023
+
3024
+ #endif
3025
+
3026
+ #if EV_STAT_ENABLE
3027
+
3028
+ # ifdef _WIN32
3029
+ # undef lstat
3030
+ # define lstat(a,b) _stati64 (a,b)
3031
+ # endif
3032
+
3033
+ #define DEF_STAT_INTERVAL 5.0074891
3034
+ #define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */
3035
+ #define MIN_STAT_INTERVAL 0.1074891
3036
+
3037
+ static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
3038
+
3039
+ #if EV_USE_INOTIFY
3040
+
3041
+ /* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
3042
+ # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
3043
+
3044
+ static void noinline
3045
+ infy_add (EV_P_ ev_stat *w)
3046
+ {
3047
+ w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD);
3048
+
3049
+ if (w->wd >= 0)
3050
+ {
3051
+ struct statfs sfs;
3052
+
3053
+ /* now local changes will be tracked by inotify, but remote changes won't */
3054
+ /* unless the filesystem is known to be local, we therefore still poll */
3055
+ /* also do poll on <2.6.25, but with normal frequency */
3056
+
3057
+ if (!fs_2625)
3058
+ w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3059
+ else if (!statfs (w->path, &sfs)
3060
+ && (sfs.f_type == 0x1373 /* devfs */
3061
+ || sfs.f_type == 0xEF53 /* ext2/3 */
3062
+ || sfs.f_type == 0x3153464a /* jfs */
3063
+ || sfs.f_type == 0x52654973 /* reiser3 */
3064
+ || sfs.f_type == 0x01021994 /* tempfs */
3065
+ || sfs.f_type == 0x58465342 /* xfs */))
3066
+ w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
3067
+ else
3068
+ w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
3069
+ }
3070
+ else
3071
+ {
3072
+ /* can't use inotify, continue to stat */
3073
+ w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3074
+
3075
+ /* if path is not there, monitor some parent directory for speedup hints */
3076
+ /* note that exceeding the hardcoded path limit is not a correctness issue, */
3077
+ /* but an efficiency issue only */
3078
+ if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
3079
+ {
3080
+ char path [4096];
3081
+ strcpy (path, w->path);
3082
+
3083
+ do
3084
+ {
3085
+ int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
3086
+ | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
3087
+
3088
+ char *pend = strrchr (path, '/');
3089
+
3090
+ if (!pend || pend == path)
3091
+ break;
3092
+
3093
+ *pend = 0;
3094
+ w->wd = inotify_add_watch (fs_fd, path, mask);
3095
+ }
3096
+ while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
3097
+ }
3098
+ }
3099
+
3100
+ if (w->wd >= 0)
3101
+ wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
3102
+
3103
+ /* now re-arm timer, if required */
3104
+ if (ev_is_active (&w->timer)) ev_ref (EV_A);
3105
+ ev_timer_again (EV_A_ &w->timer);
3106
+ if (ev_is_active (&w->timer)) ev_unref (EV_A);
3107
+ }
3108
+
3109
+ static void noinline
3110
+ infy_del (EV_P_ ev_stat *w)
3111
+ {
3112
+ int slot;
3113
+ int wd = w->wd;
3114
+
3115
+ if (wd < 0)
3116
+ return;
3117
+
3118
+ w->wd = -2;
3119
+ slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
3120
+ wlist_del (&fs_hash [slot].head, (WL)w);
3121
+
3122
+ /* remove this watcher, if others are watching it, they will rearm */
3123
+ inotify_rm_watch (fs_fd, wd);
3124
+ }
3125
+
3126
+ static void noinline
3127
+ infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
3128
+ {
3129
+ if (slot < 0)
3130
+ /* overflow, need to check for all hash slots */
3131
+ for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
3132
+ infy_wd (EV_A_ slot, wd, ev);
3133
+ else
3134
+ {
3135
+ WL w_;
3136
+
3137
+ for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
3138
+ {
3139
+ ev_stat *w = (ev_stat *)w_;
3140
+ w_ = w_->next; /* lets us remove this watcher and all before it */
3141
+
3142
+ if (w->wd == wd || wd == -1)
3143
+ {
3144
+ if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
3145
+ {
3146
+ wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
3147
+ w->wd = -1;
3148
+ infy_add (EV_A_ w); /* re-add, no matter what */
3149
+ }
3150
+
3151
+ stat_timer_cb (EV_A_ &w->timer, 0);
3152
+ }
3153
+ }
3154
+ }
3155
+ }
3156
+
3157
+ static void
3158
+ infy_cb (EV_P_ ev_io *w, int revents)
3159
+ {
3160
+ char buf [EV_INOTIFY_BUFSIZE];
3161
+ int ofs;
3162
+ int len = read (fs_fd, buf, sizeof (buf));
3163
+
3164
+ for (ofs = 0; ofs < len; )
3165
+ {
3166
+ struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
3167
+ infy_wd (EV_A_ ev->wd, ev->wd, ev);
3168
+ ofs += sizeof (struct inotify_event) + ev->len;
3169
+ }
3170
+ }
3171
+
3172
+ inline_size void
3173
+ ev_check_2625 (EV_P)
3174
+ {
3175
+ /* kernels < 2.6.25 are borked
3176
+ * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
3177
+ */
3178
+ if (ev_linux_version () < 0x020619)
3179
+ return;
3180
+
3181
+ fs_2625 = 1;
3182
+ }
3183
+
3184
+ inline_size int
3185
+ infy_newfd (void)
3186
+ {
3187
+ #if defined (IN_CLOEXEC) && defined (IN_NONBLOCK)
3188
+ int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
3189
+ if (fd >= 0)
3190
+ return fd;
3191
+ #endif
3192
+ return inotify_init ();
3193
+ }
3194
+
3195
+ inline_size void
3196
+ infy_init (EV_P)
3197
+ {
3198
+ if (fs_fd != -2)
3199
+ return;
3200
+
3201
+ fs_fd = -1;
3202
+
3203
+ ev_check_2625 (EV_A);
3204
+
3205
+ fs_fd = infy_newfd ();
3206
+
3207
+ if (fs_fd >= 0)
3208
+ {
3209
+ fd_intern (fs_fd);
3210
+ ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
3211
+ ev_set_priority (&fs_w, EV_MAXPRI);
3212
+ ev_io_start (EV_A_ &fs_w);
3213
+ ev_unref (EV_A);
3214
+ }
3215
+ }
3216
+
3217
+ inline_size void
3218
+ infy_fork (EV_P)
3219
+ {
3220
+ int slot;
3221
+
3222
+ if (fs_fd < 0)
3223
+ return;
3224
+
3225
+ ev_ref (EV_A);
3226
+ ev_io_stop (EV_A_ &fs_w);
3227
+ close (fs_fd);
3228
+ fs_fd = infy_newfd ();
3229
+
3230
+ if (fs_fd >= 0)
3231
+ {
3232
+ fd_intern (fs_fd);
3233
+ ev_io_set (&fs_w, fs_fd, EV_READ);
3234
+ ev_io_start (EV_A_ &fs_w);
3235
+ ev_unref (EV_A);
3236
+ }
3237
+
3238
+ for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
3239
+ {
3240
+ WL w_ = fs_hash [slot].head;
3241
+ fs_hash [slot].head = 0;
3242
+
3243
+ while (w_)
3244
+ {
3245
+ ev_stat *w = (ev_stat *)w_;
3246
+ w_ = w_->next; /* lets us add this watcher */
3247
+
3248
+ w->wd = -1;
3249
+
3250
+ if (fs_fd >= 0)
3251
+ infy_add (EV_A_ w); /* re-add, no matter what */
3252
+ else
3253
+ {
3254
+ w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
3255
+ if (ev_is_active (&w->timer)) ev_ref (EV_A);
3256
+ ev_timer_again (EV_A_ &w->timer);
3257
+ if (ev_is_active (&w->timer)) ev_unref (EV_A);
3258
+ }
3259
+ }
3260
+ }
3261
+ }
3262
+
3263
+ #endif
3264
+
3265
+ #ifdef _WIN32
3266
+ # define EV_LSTAT(p,b) _stati64 (p, b)
3267
+ #else
3268
+ # define EV_LSTAT(p,b) lstat (p, b)
3269
+ #endif
3270
+
3271
+ void
3272
+ ev_stat_stat (EV_P_ ev_stat *w)
3273
+ {
3274
+ if (lstat (w->path, &w->attr) < 0)
3275
+ w->attr.st_nlink = 0;
3276
+ else if (!w->attr.st_nlink)
3277
+ w->attr.st_nlink = 1;
3278
+ }
3279
+
3280
+ static void noinline
3281
+ stat_timer_cb (EV_P_ ev_timer *w_, int revents)
3282
+ {
3283
+ ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
3284
+
3285
+ ev_statdata prev = w->attr;
3286
+ ev_stat_stat (EV_A_ w);
3287
+
3288
+ /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
3289
+ if (
3290
+ prev.st_dev != w->attr.st_dev
3291
+ || prev.st_ino != w->attr.st_ino
3292
+ || prev.st_mode != w->attr.st_mode
3293
+ || prev.st_nlink != w->attr.st_nlink
3294
+ || prev.st_uid != w->attr.st_uid
3295
+ || prev.st_gid != w->attr.st_gid
3296
+ || prev.st_rdev != w->attr.st_rdev
3297
+ || prev.st_size != w->attr.st_size
3298
+ || prev.st_atime != w->attr.st_atime
3299
+ || prev.st_mtime != w->attr.st_mtime
3300
+ || prev.st_ctime != w->attr.st_ctime
3301
+ ) {
3302
+ /* we only update w->prev on actual differences */
3303
+ /* in case we test more often than invoke the callback, */
3304
+ /* to ensure that prev is always different to attr */
3305
+ w->prev = prev;
3306
+
3307
+ #if EV_USE_INOTIFY
3308
+ if (fs_fd >= 0)
3309
+ {
3310
+ infy_del (EV_A_ w);
3311
+ infy_add (EV_A_ w);
3312
+ ev_stat_stat (EV_A_ w); /* avoid race... */
3313
+ }
3314
+ #endif
3315
+
3316
+ ev_feed_event (EV_A_ w, EV_STAT);
3317
+ }
3318
+ }
3319
+
3320
+ void
3321
+ ev_stat_start (EV_P_ ev_stat *w)
3322
+ {
3323
+ if (expect_false (ev_is_active (w)))
3324
+ return;
3325
+
3326
+ ev_stat_stat (EV_A_ w);
3327
+
3328
+ if (w->interval < MIN_STAT_INTERVAL && w->interval)
3329
+ w->interval = MIN_STAT_INTERVAL;
3330
+
3331
+ ev_timer_init (&w->timer, stat_timer_cb, 0., w->interval ? w->interval : DEF_STAT_INTERVAL);
3332
+ ev_set_priority (&w->timer, ev_priority (w));
3333
+
3334
+ #if EV_USE_INOTIFY
3335
+ infy_init (EV_A);
3336
+
3337
+ if (fs_fd >= 0)
3338
+ infy_add (EV_A_ w);
3339
+ else
3340
+ #endif
3341
+ {
3342
+ ev_timer_again (EV_A_ &w->timer);
3343
+ ev_unref (EV_A);
3344
+ }
3345
+
3346
+ ev_start (EV_A_ (W)w, 1);
3347
+
3348
+ EV_FREQUENT_CHECK;
3349
+ }
3350
+
3351
+ void
3352
+ ev_stat_stop (EV_P_ ev_stat *w)
3353
+ {
3354
+ clear_pending (EV_A_ (W)w);
3355
+ if (expect_false (!ev_is_active (w)))
3356
+ return;
3357
+
3358
+ EV_FREQUENT_CHECK;
3359
+
3360
+ #if EV_USE_INOTIFY
3361
+ infy_del (EV_A_ w);
3362
+ #endif
3363
+
3364
+ if (ev_is_active (&w->timer))
3365
+ {
3366
+ ev_ref (EV_A);
3367
+ ev_timer_stop (EV_A_ &w->timer);
3368
+ }
3369
+
3370
+ ev_stop (EV_A_ (W)w);
3371
+
3372
+ EV_FREQUENT_CHECK;
3373
+ }
3374
+ #endif
3375
+
3376
+ #if EV_IDLE_ENABLE
3377
+ void
3378
+ ev_idle_start (EV_P_ ev_idle *w)
3379
+ {
3380
+ if (expect_false (ev_is_active (w)))
3381
+ return;
3382
+
3383
+ pri_adjust (EV_A_ (W)w);
3384
+
3385
+ EV_FREQUENT_CHECK;
3386
+
3387
+ {
3388
+ int active = ++idlecnt [ABSPRI (w)];
3389
+
3390
+ ++idleall;
3391
+ ev_start (EV_A_ (W)w, active);
3392
+
3393
+ array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
3394
+ idles [ABSPRI (w)][active - 1] = w;
3395
+ }
3396
+
3397
+ EV_FREQUENT_CHECK;
3398
+ }
3399
+
3400
+ void
3401
+ ev_idle_stop (EV_P_ ev_idle *w)
3402
+ {
3403
+ clear_pending (EV_A_ (W)w);
3404
+ if (expect_false (!ev_is_active (w)))
3405
+ return;
3406
+
3407
+ EV_FREQUENT_CHECK;
3408
+
3409
+ {
3410
+ int active = ev_active (w);
3411
+
3412
+ idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
3413
+ ev_active (idles [ABSPRI (w)][active - 1]) = active;
3414
+
3415
+ ev_stop (EV_A_ (W)w);
3416
+ --idleall;
3417
+ }
3418
+
3419
+ EV_FREQUENT_CHECK;
3420
+ }
3421
+ #endif
3422
+
3423
+ #if EV_PREPARE_ENABLE
3424
+ void
3425
+ ev_prepare_start (EV_P_ ev_prepare *w)
3426
+ {
3427
+ if (expect_false (ev_is_active (w)))
3428
+ return;
3429
+
3430
+ EV_FREQUENT_CHECK;
3431
+
3432
+ ev_start (EV_A_ (W)w, ++preparecnt);
3433
+ array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
3434
+ prepares [preparecnt - 1] = w;
3435
+
3436
+ EV_FREQUENT_CHECK;
3437
+ }
3438
+
3439
+ void
3440
+ ev_prepare_stop (EV_P_ ev_prepare *w)
3441
+ {
3442
+ clear_pending (EV_A_ (W)w);
3443
+ if (expect_false (!ev_is_active (w)))
3444
+ return;
3445
+
3446
+ EV_FREQUENT_CHECK;
3447
+
3448
+ {
3449
+ int active = ev_active (w);
3450
+
3451
+ prepares [active - 1] = prepares [--preparecnt];
3452
+ ev_active (prepares [active - 1]) = active;
3453
+ }
3454
+
3455
+ ev_stop (EV_A_ (W)w);
3456
+
3457
+ EV_FREQUENT_CHECK;
3458
+ }
3459
+ #endif
3460
+
3461
+ #if EV_CHECK_ENABLE
3462
+ void
3463
+ ev_check_start (EV_P_ ev_check *w)
3464
+ {
3465
+ if (expect_false (ev_is_active (w)))
3466
+ return;
3467
+
3468
+ EV_FREQUENT_CHECK;
3469
+
3470
+ ev_start (EV_A_ (W)w, ++checkcnt);
3471
+ array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
3472
+ checks [checkcnt - 1] = w;
3473
+
3474
+ EV_FREQUENT_CHECK;
3475
+ }
3476
+
3477
+ void
3478
+ ev_check_stop (EV_P_ ev_check *w)
3479
+ {
3480
+ clear_pending (EV_A_ (W)w);
3481
+ if (expect_false (!ev_is_active (w)))
3482
+ return;
3483
+
3484
+ EV_FREQUENT_CHECK;
3485
+
3486
+ {
3487
+ int active = ev_active (w);
3488
+
3489
+ checks [active - 1] = checks [--checkcnt];
3490
+ ev_active (checks [active - 1]) = active;
3491
+ }
3492
+
3493
+ ev_stop (EV_A_ (W)w);
3494
+
3495
+ EV_FREQUENT_CHECK;
3496
+ }
3497
+ #endif
3498
+
3499
+ #if EV_EMBED_ENABLE
3500
+ void noinline
3501
+ ev_embed_sweep (EV_P_ ev_embed *w)
3502
+ {
3503
+ ev_run (w->other, EVRUN_NOWAIT);
3504
+ }
3505
+
3506
+ static void
3507
+ embed_io_cb (EV_P_ ev_io *io, int revents)
3508
+ {
3509
+ ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3510
+
3511
+ if (ev_cb (w))
3512
+ ev_feed_event (EV_A_ (W)w, EV_EMBED);
3513
+ else
3514
+ ev_run (w->other, EVRUN_NOWAIT);
3515
+ }
3516
+
3517
+ static void
3518
+ embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3519
+ {
3520
+ ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
3521
+
3522
+ {
3523
+ EV_P = w->other;
3524
+
3525
+ while (fdchangecnt)
3526
+ {
3527
+ fd_reify (EV_A);
3528
+ ev_run (EV_A_ EVRUN_NOWAIT);
3529
+ }
3530
+ }
3531
+ }
3532
+
3533
+ static void
3534
+ embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
3535
+ {
3536
+ ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
3537
+
3538
+ ev_embed_stop (EV_A_ w);
3539
+
3540
+ {
3541
+ EV_P = w->other;
3542
+
3543
+ ev_loop_fork (EV_A);
3544
+ ev_run (EV_A_ EVRUN_NOWAIT);
3545
+ }
3546
+
3547
+ ev_embed_start (EV_A_ w);
3548
+ }
3549
+
3550
+ #if 0
3551
+ static void
3552
+ embed_idle_cb (EV_P_ ev_idle *idle, int revents)
3553
+ {
3554
+ ev_idle_stop (EV_A_ idle);
3555
+ }
3556
+ #endif
3557
+
3558
+ void
3559
+ ev_embed_start (EV_P_ ev_embed *w)
3560
+ {
3561
+ if (expect_false (ev_is_active (w)))
3562
+ return;
3563
+
3564
+ {
3565
+ EV_P = w->other;
3566
+ assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
3567
+ ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
3568
+ }
3569
+
3570
+ EV_FREQUENT_CHECK;
3571
+
3572
+ ev_set_priority (&w->io, ev_priority (w));
3573
+ ev_io_start (EV_A_ &w->io);
3574
+
3575
+ ev_prepare_init (&w->prepare, embed_prepare_cb);
3576
+ ev_set_priority (&w->prepare, EV_MINPRI);
3577
+ ev_prepare_start (EV_A_ &w->prepare);
3578
+
3579
+ ev_fork_init (&w->fork, embed_fork_cb);
3580
+ ev_fork_start (EV_A_ &w->fork);
3581
+
3582
+ /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
3583
+
3584
+ ev_start (EV_A_ (W)w, 1);
3585
+
3586
+ EV_FREQUENT_CHECK;
3587
+ }
3588
+
3589
+ void
3590
+ ev_embed_stop (EV_P_ ev_embed *w)
3591
+ {
3592
+ clear_pending (EV_A_ (W)w);
3593
+ if (expect_false (!ev_is_active (w)))
3594
+ return;
3595
+
3596
+ EV_FREQUENT_CHECK;
3597
+
3598
+ ev_io_stop (EV_A_ &w->io);
3599
+ ev_prepare_stop (EV_A_ &w->prepare);
3600
+ ev_fork_stop (EV_A_ &w->fork);
3601
+
3602
+ ev_stop (EV_A_ (W)w);
3603
+
3604
+ EV_FREQUENT_CHECK;
3605
+ }
3606
+ #endif
3607
+
3608
+ #if EV_FORK_ENABLE
3609
+ void
3610
+ ev_fork_start (EV_P_ ev_fork *w)
3611
+ {
3612
+ if (expect_false (ev_is_active (w)))
3613
+ return;
3614
+
3615
+ EV_FREQUENT_CHECK;
3616
+
3617
+ ev_start (EV_A_ (W)w, ++forkcnt);
3618
+ array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2);
3619
+ forks [forkcnt - 1] = w;
3620
+
3621
+ EV_FREQUENT_CHECK;
3622
+ }
3623
+
3624
+ void
3625
+ ev_fork_stop (EV_P_ ev_fork *w)
3626
+ {
3627
+ clear_pending (EV_A_ (W)w);
3628
+ if (expect_false (!ev_is_active (w)))
3629
+ return;
3630
+
3631
+ EV_FREQUENT_CHECK;
3632
+
3633
+ {
3634
+ int active = ev_active (w);
3635
+
3636
+ forks [active - 1] = forks [--forkcnt];
3637
+ ev_active (forks [active - 1]) = active;
3638
+ }
3639
+
3640
+ ev_stop (EV_A_ (W)w);
3641
+
3642
+ EV_FREQUENT_CHECK;
3643
+ }
3644
+ #endif
3645
+
3646
+ #if EV_CLEANUP_ENABLE
3647
+ void
3648
+ ev_cleanup_start (EV_P_ ev_cleanup *w)
3649
+ {
3650
+ if (expect_false (ev_is_active (w)))
3651
+ return;
3652
+
3653
+ EV_FREQUENT_CHECK;
3654
+
3655
+ ev_start (EV_A_ (W)w, ++cleanupcnt);
3656
+ array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, EMPTY2);
3657
+ cleanups [cleanupcnt - 1] = w;
3658
+
3659
+ /* cleanup watchers should never keep a refcount on the loop */
3660
+ ev_unref (EV_A);
3661
+ EV_FREQUENT_CHECK;
3662
+ }
3663
+
3664
+ void
3665
+ ev_cleanup_stop (EV_P_ ev_cleanup *w)
3666
+ {
3667
+ clear_pending (EV_A_ (W)w);
3668
+ if (expect_false (!ev_is_active (w)))
3669
+ return;
3670
+
3671
+ EV_FREQUENT_CHECK;
3672
+ ev_ref (EV_A);
3673
+
3674
+ {
3675
+ int active = ev_active (w);
3676
+
3677
+ cleanups [active - 1] = cleanups [--cleanupcnt];
3678
+ ev_active (cleanups [active - 1]) = active;
3679
+ }
3680
+
3681
+ ev_stop (EV_A_ (W)w);
3682
+
3683
+ EV_FREQUENT_CHECK;
3684
+ }
3685
+ #endif
3686
+
3687
+ #if EV_ASYNC_ENABLE
3688
+ void
3689
+ ev_async_start (EV_P_ ev_async *w)
3690
+ {
3691
+ if (expect_false (ev_is_active (w)))
3692
+ return;
3693
+
3694
+ w->sent = 0;
3695
+
3696
+ evpipe_init (EV_A);
3697
+
3698
+ EV_FREQUENT_CHECK;
3699
+
3700
+ ev_start (EV_A_ (W)w, ++asynccnt);
3701
+ array_needsize (ev_async *, asyncs, asyncmax, asynccnt, EMPTY2);
3702
+ asyncs [asynccnt - 1] = w;
3703
+
3704
+ EV_FREQUENT_CHECK;
3705
+ }
3706
+
3707
+ void
3708
+ ev_async_stop (EV_P_ ev_async *w)
3709
+ {
3710
+ clear_pending (EV_A_ (W)w);
3711
+ if (expect_false (!ev_is_active (w)))
3712
+ return;
3713
+
3714
+ EV_FREQUENT_CHECK;
3715
+
3716
+ {
3717
+ int active = ev_active (w);
3718
+
3719
+ asyncs [active - 1] = asyncs [--asynccnt];
3720
+ ev_active (asyncs [active - 1]) = active;
3721
+ }
3722
+
3723
+ ev_stop (EV_A_ (W)w);
3724
+
3725
+ EV_FREQUENT_CHECK;
3726
+ }
3727
+
3728
+ void
3729
+ ev_async_send (EV_P_ ev_async *w)
3730
+ {
3731
+ w->sent = 1;
3732
+ evpipe_write (EV_A_ &async_pending);
3733
+ }
3734
+ #endif
3735
+
3736
+ /*****************************************************************************/
3737
+
3738
+ struct ev_once
3739
+ {
3740
+ ev_io io;
3741
+ ev_timer to;
3742
+ void (*cb)(int revents, void *arg);
3743
+ void *arg;
3744
+ };
3745
+
3746
+ static void
3747
+ once_cb (EV_P_ struct ev_once *once, int revents)
3748
+ {
3749
+ void (*cb)(int revents, void *arg) = once->cb;
3750
+ void *arg = once->arg;
3751
+
3752
+ ev_io_stop (EV_A_ &once->io);
3753
+ ev_timer_stop (EV_A_ &once->to);
3754
+ ev_free (once);
3755
+
3756
+ cb (revents, arg);
3757
+ }
3758
+
3759
+ static void
3760
+ once_cb_io (EV_P_ ev_io *w, int revents)
3761
+ {
3762
+ struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io));
3763
+
3764
+ once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to));
3765
+ }
3766
+
3767
+ static void
3768
+ once_cb_to (EV_P_ ev_timer *w, int revents)
3769
+ {
3770
+ struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to));
3771
+
3772
+ once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
3773
+ }
3774
+
3775
+ void
3776
+ ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
3777
+ {
3778
+ struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
3779
+
3780
+ if (expect_false (!once))
3781
+ {
3782
+ cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMER, arg);
3783
+ return;
3784
+ }
3785
+
3786
+ once->cb = cb;
3787
+ once->arg = arg;
3788
+
3789
+ ev_init (&once->io, once_cb_io);
3790
+ if (fd >= 0)
3791
+ {
3792
+ ev_io_set (&once->io, fd, events);
3793
+ ev_io_start (EV_A_ &once->io);
3794
+ }
3795
+
3796
+ ev_init (&once->to, once_cb_to);
3797
+ if (timeout >= 0.)
3798
+ {
3799
+ ev_timer_set (&once->to, timeout, 0.);
3800
+ ev_timer_start (EV_A_ &once->to);
3801
+ }
3802
+ }
3803
+
3804
+ /*****************************************************************************/
3805
+
3806
+ #if EV_WALK_ENABLE
3807
+ void
3808
+ ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w))
3809
+ {
3810
+ int i, j;
3811
+ ev_watcher_list *wl, *wn;
3812
+
3813
+ if (types & (EV_IO | EV_EMBED))
3814
+ for (i = 0; i < anfdmax; ++i)
3815
+ for (wl = anfds [i].head; wl; )
3816
+ {
3817
+ wn = wl->next;
3818
+
3819
+ #if EV_EMBED_ENABLE
3820
+ if (ev_cb ((ev_io *)wl) == embed_io_cb)
3821
+ {
3822
+ if (types & EV_EMBED)
3823
+ cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io));
3824
+ }
3825
+ else
3826
+ #endif
3827
+ #if EV_USE_INOTIFY
3828
+ if (ev_cb ((ev_io *)wl) == infy_cb)
3829
+ ;
3830
+ else
3831
+ #endif
3832
+ if ((ev_io *)wl != &pipe_w)
3833
+ if (types & EV_IO)
3834
+ cb (EV_A_ EV_IO, wl);
3835
+
3836
+ wl = wn;
3837
+ }
3838
+
3839
+ if (types & (EV_TIMER | EV_STAT))
3840
+ for (i = timercnt + HEAP0; i-- > HEAP0; )
3841
+ #if EV_STAT_ENABLE
3842
+ /*TODO: timer is not always active*/
3843
+ if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb)
3844
+ {
3845
+ if (types & EV_STAT)
3846
+ cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer));
3847
+ }
3848
+ else
3849
+ #endif
3850
+ if (types & EV_TIMER)
3851
+ cb (EV_A_ EV_TIMER, ANHE_w (timers [i]));
3852
+
3853
+ #if EV_PERIODIC_ENABLE
3854
+ if (types & EV_PERIODIC)
3855
+ for (i = periodiccnt + HEAP0; i-- > HEAP0; )
3856
+ cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
3857
+ #endif
3858
+
3859
+ #if EV_IDLE_ENABLE
3860
+ if (types & EV_IDLE)
3861
+ for (j = NUMPRI; j--; )
3862
+ for (i = idlecnt [j]; i--; )
3863
+ cb (EV_A_ EV_IDLE, idles [j][i]);
3864
+ #endif
3865
+
3866
+ #if EV_FORK_ENABLE
3867
+ if (types & EV_FORK)
3868
+ for (i = forkcnt; i--; )
3869
+ if (ev_cb (forks [i]) != embed_fork_cb)
3870
+ cb (EV_A_ EV_FORK, forks [i]);
3871
+ #endif
3872
+
3873
+ #if EV_ASYNC_ENABLE
3874
+ if (types & EV_ASYNC)
3875
+ for (i = asynccnt; i--; )
3876
+ cb (EV_A_ EV_ASYNC, asyncs [i]);
3877
+ #endif
3878
+
3879
+ #if EV_PREPARE_ENABLE
3880
+ if (types & EV_PREPARE)
3881
+ for (i = preparecnt; i--; )
3882
+ # if EV_EMBED_ENABLE
3883
+ if (ev_cb (prepares [i]) != embed_prepare_cb)
3884
+ # endif
3885
+ cb (EV_A_ EV_PREPARE, prepares [i]);
3886
+ #endif
3887
+
3888
+ #if EV_CHECK_ENABLE
3889
+ if (types & EV_CHECK)
3890
+ for (i = checkcnt; i--; )
3891
+ cb (EV_A_ EV_CHECK, checks [i]);
3892
+ #endif
3893
+
3894
+ #if EV_SIGNAL_ENABLE
3895
+ if (types & EV_SIGNAL)
3896
+ for (i = 0; i < EV_NSIG - 1; ++i)
3897
+ for (wl = signals [i].head; wl; )
3898
+ {
3899
+ wn = wl->next;
3900
+ cb (EV_A_ EV_SIGNAL, wl);
3901
+ wl = wn;
3902
+ }
3903
+ #endif
3904
+
3905
+ #if EV_CHILD_ENABLE
3906
+ if (types & EV_CHILD)
3907
+ for (i = (EV_PID_HASHSIZE); i--; )
3908
+ for (wl = childs [i]; wl; )
3909
+ {
3910
+ wn = wl->next;
3911
+ cb (EV_A_ EV_CHILD, wl);
3912
+ wl = wn;
3913
+ }
3914
+ #endif
3915
+ /* EV_STAT 0x00001000 /* stat data changed */
3916
+ /* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
3917
+ }
3918
+ #endif
3919
+
3920
+ #if EV_MULTIPLICITY
3921
+ #include "ev_wrap.h"
3922
+ #endif
3923
+
3924
+ EV_CPP(})
3925
+