Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

5628 lines
148 KiB

  1. /*
  2. * libev event processing core, watcher management
  3. *
  4. * Copyright (c) 2007-2020 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. /* this big block deduces configuration from config.h */
  40. #ifndef EV_STANDALONE
  41. # ifdef EV_CONFIG_H
  42. # include EV_CONFIG_H
  43. # else
  44. # include "config.h"
  45. # endif
  46. # if HAVE_FLOOR
  47. # ifndef EV_USE_FLOOR
  48. # define EV_USE_FLOOR 1
  49. # endif
  50. # endif
  51. # if HAVE_CLOCK_SYSCALL
  52. # ifndef EV_USE_CLOCK_SYSCALL
  53. # define EV_USE_CLOCK_SYSCALL 1
  54. # ifndef EV_USE_REALTIME
  55. # define EV_USE_REALTIME 0
  56. # endif
  57. # ifndef EV_USE_MONOTONIC
  58. # define EV_USE_MONOTONIC 1
  59. # endif
  60. # endif
  61. # elif !defined EV_USE_CLOCK_SYSCALL
  62. # define EV_USE_CLOCK_SYSCALL 0
  63. # endif
  64. # if HAVE_CLOCK_GETTIME
  65. # ifndef EV_USE_MONOTONIC
  66. # define EV_USE_MONOTONIC 1
  67. # endif
  68. # ifndef EV_USE_REALTIME
  69. # define EV_USE_REALTIME 0
  70. # endif
  71. # else
  72. # ifndef EV_USE_MONOTONIC
  73. # define EV_USE_MONOTONIC 0
  74. # endif
  75. # ifndef EV_USE_REALTIME
  76. # define EV_USE_REALTIME 0
  77. # endif
  78. # endif
  79. # if HAVE_NANOSLEEP
  80. # ifndef EV_USE_NANOSLEEP
  81. # define EV_USE_NANOSLEEP EV_FEATURE_OS
  82. # endif
  83. # else
  84. # undef EV_USE_NANOSLEEP
  85. # define EV_USE_NANOSLEEP 0
  86. # endif
  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. # if HAVE_POLL && HAVE_POLL_H
  96. # ifndef EV_USE_POLL
  97. # define EV_USE_POLL EV_FEATURE_BACKENDS
  98. # endif
  99. # else
  100. # undef EV_USE_POLL
  101. # define EV_USE_POLL 0
  102. # endif
  103. # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
  104. # ifndef EV_USE_EPOLL
  105. # define EV_USE_EPOLL EV_FEATURE_BACKENDS
  106. # endif
  107. # else
  108. # undef EV_USE_EPOLL
  109. # define EV_USE_EPOLL 0
  110. # endif
  111. # if HAVE_LINUX_AIO_ABI_H
  112. # ifndef EV_USE_LINUXAIO
  113. # define EV_USE_LINUXAIO 0 /* was: EV_FEATURE_BACKENDS, always off by default */
  114. # endif
  115. # else
  116. # undef EV_USE_LINUXAIO
  117. # define EV_USE_LINUXAIO 0
  118. # endif
  119. # if HAVE_LINUX_FS_H && HAVE_SYS_TIMERFD_H && HAVE_KERNEL_RWF_T
  120. # ifndef EV_USE_IOURING
  121. # define EV_USE_IOURING EV_FEATURE_BACKENDS
  122. # endif
  123. # else
  124. # undef EV_USE_IOURING
  125. # define EV_USE_IOURING 0
  126. # endif
  127. # if HAVE_KQUEUE && HAVE_SYS_EVENT_H
  128. # ifndef EV_USE_KQUEUE
  129. # define EV_USE_KQUEUE EV_FEATURE_BACKENDS
  130. # endif
  131. # else
  132. # undef EV_USE_KQUEUE
  133. # define EV_USE_KQUEUE 0
  134. # endif
  135. # if HAVE_PORT_H && HAVE_PORT_CREATE
  136. # ifndef EV_USE_PORT
  137. # define EV_USE_PORT EV_FEATURE_BACKENDS
  138. # endif
  139. # else
  140. # undef EV_USE_PORT
  141. # define EV_USE_PORT 0
  142. # endif
  143. # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
  144. # ifndef EV_USE_INOTIFY
  145. # define EV_USE_INOTIFY EV_FEATURE_OS
  146. # endif
  147. # else
  148. # undef EV_USE_INOTIFY
  149. # define EV_USE_INOTIFY 0
  150. # endif
  151. # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
  152. # ifndef EV_USE_SIGNALFD
  153. # define EV_USE_SIGNALFD EV_FEATURE_OS
  154. # endif
  155. # else
  156. # undef EV_USE_SIGNALFD
  157. # define EV_USE_SIGNALFD 0
  158. # endif
  159. # if HAVE_EVENTFD
  160. # ifndef EV_USE_EVENTFD
  161. # define EV_USE_EVENTFD EV_FEATURE_OS
  162. # endif
  163. # else
  164. # undef EV_USE_EVENTFD
  165. # define EV_USE_EVENTFD 0
  166. # endif
  167. # if HAVE_SYS_TIMERFD_H
  168. # ifndef EV_USE_TIMERFD
  169. # define EV_USE_TIMERFD EV_FEATURE_OS
  170. # endif
  171. # else
  172. # undef EV_USE_TIMERFD
  173. # define EV_USE_TIMERFD 0
  174. # endif
  175. #endif
  176. /* OS X, in its infinite idiocy, actually HARDCODES
  177. * a limit of 1024 into their select. Where people have brains,
  178. * OS X engineers apparently have a vacuum. Or maybe they were
  179. * ordered to have a vacuum, or they do anything for money.
  180. * This might help. Or not.
  181. * Note that this must be defined early, as other include files
  182. * will rely on this define as well.
  183. */
  184. #define _DARWIN_UNLIMITED_SELECT 1
  185. #include <stdlib.h>
  186. #include <string.h>
  187. #include <fcntl.h>
  188. #include <stddef.h>
  189. #include <stdio.h>
  190. #include <assert.h>
  191. #include <errno.h>
  192. #include <sys/types.h>
  193. #include <time.h>
  194. #include <limits.h>
  195. #include <signal.h>
  196. #ifdef EV_H
  197. # include EV_H
  198. #else
  199. # include "ev.h"
  200. #endif
  201. #if EV_NO_THREADS
  202. # undef EV_NO_SMP
  203. # define EV_NO_SMP 1
  204. # undef ECB_NO_THREADS
  205. # define ECB_NO_THREADS 1
  206. #endif
  207. #if EV_NO_SMP
  208. # undef EV_NO_SMP
  209. # define ECB_NO_SMP 1
  210. #endif
  211. #ifndef _WIN32
  212. # include <sys/time.h>
  213. # include <sys/wait.h>
  214. # include <unistd.h>
  215. #else
  216. # include <io.h>
  217. # define WIN32_LEAN_AND_MEAN
  218. # include <winsock2.h>
  219. # include <windows.h>
  220. # ifndef EV_SELECT_IS_WINSOCKET
  221. # define EV_SELECT_IS_WINSOCKET 1
  222. # endif
  223. # undef EV_AVOID_STDIO
  224. #endif
  225. /* this block tries to deduce configuration from header-defined symbols and defaults */
  226. /* try to deduce the maximum number of signals on this platform */
  227. #if defined EV_NSIG
  228. /* use what's provided */
  229. #elif defined NSIG
  230. # define EV_NSIG (NSIG)
  231. #elif defined _NSIG
  232. # define EV_NSIG (_NSIG)
  233. #elif defined SIGMAX
  234. # define EV_NSIG (SIGMAX+1)
  235. #elif defined SIG_MAX
  236. # define EV_NSIG (SIG_MAX+1)
  237. #elif defined _SIG_MAX
  238. # define EV_NSIG (_SIG_MAX+1)
  239. #elif defined MAXSIG
  240. # define EV_NSIG (MAXSIG+1)
  241. #elif defined MAX_SIG
  242. # define EV_NSIG (MAX_SIG+1)
  243. #elif defined SIGARRAYSIZE
  244. # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
  245. #elif defined _sys_nsig
  246. # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
  247. #else
  248. # define EV_NSIG (8 * sizeof (sigset_t) + 1)
  249. #endif
  250. #ifndef EV_USE_FLOOR
  251. # define EV_USE_FLOOR 0
  252. #endif
  253. #ifndef EV_USE_CLOCK_SYSCALL
  254. # if __linux && __GLIBC__ == 2 && __GLIBC_MINOR__ < 17
  255. # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
  256. # else
  257. # define EV_USE_CLOCK_SYSCALL 0
  258. # endif
  259. #endif
  260. #if !(_POSIX_TIMERS > 0)
  261. # ifndef EV_USE_MONOTONIC
  262. # define EV_USE_MONOTONIC 0
  263. # endif
  264. # ifndef EV_USE_REALTIME
  265. # define EV_USE_REALTIME 0
  266. # endif
  267. #endif
  268. #ifndef EV_USE_MONOTONIC
  269. # if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
  270. # define EV_USE_MONOTONIC EV_FEATURE_OS
  271. # else
  272. # define EV_USE_MONOTONIC 0
  273. # endif
  274. #endif
  275. #ifndef EV_USE_REALTIME
  276. # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
  277. #endif
  278. #ifndef EV_USE_NANOSLEEP
  279. # if _POSIX_C_SOURCE >= 199309L
  280. # define EV_USE_NANOSLEEP EV_FEATURE_OS
  281. # else
  282. # define EV_USE_NANOSLEEP 0
  283. # endif
  284. #endif
  285. #ifndef EV_USE_SELECT
  286. # define EV_USE_SELECT EV_FEATURE_BACKENDS
  287. #endif
  288. #ifndef EV_USE_POLL
  289. # ifdef _WIN32
  290. # define EV_USE_POLL 0
  291. # else
  292. # define EV_USE_POLL EV_FEATURE_BACKENDS
  293. # endif
  294. #endif
  295. #ifndef EV_USE_EPOLL
  296. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
  297. # define EV_USE_EPOLL EV_FEATURE_BACKENDS
  298. # else
  299. # define EV_USE_EPOLL 0
  300. # endif
  301. #endif
  302. #ifndef EV_USE_KQUEUE
  303. # define EV_USE_KQUEUE 0
  304. #endif
  305. #ifndef EV_USE_PORT
  306. # define EV_USE_PORT 0
  307. #endif
  308. #ifndef EV_USE_LINUXAIO
  309. # if __linux /* libev currently assumes linux/aio_abi.h is always available on linux */
  310. # define EV_USE_LINUXAIO 0 /* was: 1, always off by default */
  311. # else
  312. # define EV_USE_LINUXAIO 0
  313. # endif
  314. #endif
  315. #ifndef EV_USE_IOURING
  316. # if __linux /* later checks might disable again */
  317. # define EV_USE_IOURING 1
  318. # else
  319. # define EV_USE_IOURING 0
  320. # endif
  321. #endif
  322. #ifndef EV_USE_INOTIFY
  323. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
  324. # define EV_USE_INOTIFY EV_FEATURE_OS
  325. # else
  326. # define EV_USE_INOTIFY 0
  327. # endif
  328. #endif
  329. #ifndef EV_PID_HASHSIZE
  330. # define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
  331. #endif
  332. #ifndef EV_INOTIFY_HASHSIZE
  333. # define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
  334. #endif
  335. #ifndef EV_USE_EVENTFD
  336. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
  337. # define EV_USE_EVENTFD EV_FEATURE_OS
  338. # else
  339. # define EV_USE_EVENTFD 0
  340. # endif
  341. #endif
  342. #ifndef EV_USE_SIGNALFD
  343. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
  344. # define EV_USE_SIGNALFD EV_FEATURE_OS
  345. # else
  346. # define EV_USE_SIGNALFD 0
  347. # endif
  348. #endif
  349. #ifndef EV_USE_TIMERFD
  350. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8))
  351. # define EV_USE_TIMERFD EV_FEATURE_OS
  352. # else
  353. # define EV_USE_TIMERFD 0
  354. # endif
  355. #endif
  356. #if 0 /* debugging */
  357. # define EV_VERIFY 3
  358. # define EV_USE_4HEAP 1
  359. # define EV_HEAP_CACHE_AT 1
  360. #endif
  361. #ifndef EV_VERIFY
  362. # define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
  363. #endif
  364. #ifndef EV_USE_4HEAP
  365. # define EV_USE_4HEAP EV_FEATURE_DATA
  366. #endif
  367. #ifndef EV_HEAP_CACHE_AT
  368. # define EV_HEAP_CACHE_AT EV_FEATURE_DATA
  369. #endif
  370. #ifdef __ANDROID__
  371. /* supposedly, android doesn't typedef fd_mask */
  372. # undef EV_USE_SELECT
  373. # define EV_USE_SELECT 0
  374. /* supposedly, we need to include syscall.h, not sys/syscall.h, so just disable */
  375. # undef EV_USE_CLOCK_SYSCALL
  376. # define EV_USE_CLOCK_SYSCALL 0
  377. #endif
  378. /* aix's poll.h seems to cause lots of trouble */
  379. #ifdef _AIX
  380. /* AIX has a completely broken poll.h header */
  381. # undef EV_USE_POLL
  382. # define EV_USE_POLL 0
  383. #endif
  384. /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
  385. /* which makes programs even slower. might work on other unices, too. */
  386. #if EV_USE_CLOCK_SYSCALL
  387. # include <sys/syscall.h>
  388. # ifdef SYS_clock_gettime
  389. # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
  390. # undef EV_USE_MONOTONIC
  391. # define EV_USE_MONOTONIC 1
  392. # define EV_NEED_SYSCALL 1
  393. # else
  394. # undef EV_USE_CLOCK_SYSCALL
  395. # define EV_USE_CLOCK_SYSCALL 0
  396. # endif
  397. #endif
  398. /* this block fixes any misconfiguration where we know we run into trouble otherwise */
  399. #ifndef CLOCK_MONOTONIC
  400. # undef EV_USE_MONOTONIC
  401. # define EV_USE_MONOTONIC 0
  402. #endif
  403. #ifndef CLOCK_REALTIME
  404. # undef EV_USE_REALTIME
  405. # define EV_USE_REALTIME 0
  406. #endif
  407. #if !EV_STAT_ENABLE
  408. # undef EV_USE_INOTIFY
  409. # define EV_USE_INOTIFY 0
  410. #endif
  411. #if __linux && EV_USE_IOURING
  412. # include <linux/version.h>
  413. # if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  414. # undef EV_USE_IOURING
  415. # define EV_USE_IOURING 0
  416. # endif
  417. #endif
  418. #if !EV_USE_NANOSLEEP
  419. /* hp-ux has it in sys/time.h, which we unconditionally include above */
  420. # if !defined _WIN32 && !defined __hpux
  421. # include <sys/select.h>
  422. # endif
  423. #endif
  424. #if EV_USE_LINUXAIO
  425. # include <sys/syscall.h>
  426. # if SYS_io_getevents && EV_USE_EPOLL /* linuxaio backend requires epoll backend */
  427. # define EV_NEED_SYSCALL 1
  428. # else
  429. # undef EV_USE_LINUXAIO
  430. # define EV_USE_LINUXAIO 0
  431. # endif
  432. #endif
  433. #if EV_USE_IOURING
  434. # include <sys/syscall.h>
  435. # if !SYS_io_uring_setup && __linux && !__alpha
  436. # define SYS_io_uring_setup 425
  437. # define SYS_io_uring_enter 426
  438. # define SYS_io_uring_wregister 427
  439. # endif
  440. # if SYS_io_uring_setup && EV_USE_EPOLL /* iouring backend requires epoll backend */
  441. # define EV_NEED_SYSCALL 1
  442. # else
  443. # undef EV_USE_IOURING
  444. # define EV_USE_IOURING 0
  445. # endif
  446. #endif
  447. #if EV_USE_INOTIFY
  448. # include <sys/statfs.h>
  449. # include <sys/inotify.h>
  450. /* some very old inotify.h headers don't have IN_DONT_FOLLOW */
  451. # ifndef IN_DONT_FOLLOW
  452. # undef EV_USE_INOTIFY
  453. # define EV_USE_INOTIFY 0
  454. # endif
  455. #endif
  456. #if EV_USE_EVENTFD
  457. /* our minimum requirement is glibc 2.7 which has the stub, but not the full header */
  458. # include <stdint.h>
  459. # ifndef EFD_NONBLOCK
  460. # define EFD_NONBLOCK O_NONBLOCK
  461. # endif
  462. # ifndef EFD_CLOEXEC
  463. # ifdef O_CLOEXEC
  464. # define EFD_CLOEXEC O_CLOEXEC
  465. # else
  466. # define EFD_CLOEXEC 02000000
  467. # endif
  468. # endif
  469. EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
  470. #endif
  471. #if EV_USE_SIGNALFD
  472. /* our minimum requirement is glibc 2.7 which has the stub, but not the full header */
  473. # include <stdint.h>
  474. # ifndef SFD_NONBLOCK
  475. # define SFD_NONBLOCK O_NONBLOCK
  476. # endif
  477. # ifndef SFD_CLOEXEC
  478. # ifdef O_CLOEXEC
  479. # define SFD_CLOEXEC O_CLOEXEC
  480. # else
  481. # define SFD_CLOEXEC 02000000
  482. # endif
  483. # endif
  484. EV_CPP (extern "C") int (signalfd) (int fd, const sigset_t *mask, int flags);
  485. struct signalfd_siginfo
  486. {
  487. uint32_t ssi_signo;
  488. char pad[128 - sizeof (uint32_t)];
  489. };
  490. #endif
  491. /* for timerfd, libev core requires TFD_TIMER_CANCEL_ON_SET &c */
  492. #if EV_USE_TIMERFD
  493. # include <sys/timerfd.h>
  494. /* timerfd is only used for periodics */
  495. # if !(defined (TFD_TIMER_CANCEL_ON_SET) && defined (TFD_CLOEXEC) && defined (TFD_NONBLOCK)) || !EV_PERIODIC_ENABLE
  496. # undef EV_USE_TIMERFD
  497. # define EV_USE_TIMERFD 0
  498. # endif
  499. #endif
  500. /*****************************************************************************/
  501. #if EV_VERIFY >= 3
  502. # define EV_FREQUENT_CHECK ev_verify (EV_A)
  503. #else
  504. # define EV_FREQUENT_CHECK do { } while (0)
  505. #endif
  506. /*
  507. * This is used to work around floating point rounding problems.
  508. * This value is good at least till the year 4000.
  509. */
  510. #define MIN_INTERVAL 0.0001220703125 /* 1/2**13, good till 4000 */
  511. /*#define MIN_INTERVAL 0.00000095367431640625 /* 1/2**20, good till 2200 */
  512. #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
  513. #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
  514. #define MAX_BLOCKTIME2 1500001.07 /* same, but when timerfd is used to detect jumps, also safe delay to not overflow */
  515. /* find a portable timestamp that is "always" in the future but fits into time_t.
  516. * this is quite hard, and we are mostly guessing - we handle 32 bit signed/unsigned time_t,
  517. * and sizes larger than 32 bit, and maybe the unlikely floating point time_t */
  518. #define EV_TSTAMP_HUGE \
  519. (sizeof (time_t) >= 8 ? 10000000000000. \
  520. : 0 < (time_t)4294967295 ? 4294967295. \
  521. : 2147483647.) \
  522. #ifndef EV_TS_CONST
  523. # define EV_TS_CONST(nv) nv
  524. # define EV_TS_TO_MSEC(a) a * 1e3 + 0.9999
  525. # define EV_TS_FROM_USEC(us) us * 1e-6
  526. # define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
  527. # define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
  528. # define EV_TV_GET(tv) ((tv).tv_sec + (tv).tv_usec * 1e-6)
  529. # define EV_TS_GET(ts) ((ts).tv_sec + (ts).tv_nsec * 1e-9)
  530. #endif
  531. /* the following is ecb.h embedded into libev - use update_ev_c to update from an external copy */
  532. /* ECB.H BEGIN */
  533. /*
  534. * libecb - http://software.schmorp.de/pkg/libecb
  535. *
  536. * Copyright (©) 2009-2015,2018-2020 Marc Alexander Lehmann <libecb@schmorp.de>
  537. * Copyright (©) 2011 Emanuele Giaquinta
  538. * All rights reserved.
  539. *
  540. * Redistribution and use in source and binary forms, with or without modifica-
  541. * tion, are permitted provided that the following conditions are met:
  542. *
  543. * 1. Redistributions of source code must retain the above copyright notice,
  544. * this list of conditions and the following disclaimer.
  545. *
  546. * 2. Redistributions in binary form must reproduce the above copyright
  547. * notice, this list of conditions and the following disclaimer in the
  548. * documentation and/or other materials provided with the distribution.
  549. *
  550. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  551. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  552. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  553. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  554. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  555. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  556. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  557. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  558. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  559. * OF THE POSSIBILITY OF SUCH DAMAGE.
  560. *
  561. * Alternatively, the contents of this file may be used under the terms of
  562. * the GNU General Public License ("GPL") version 2 or any later version,
  563. * in which case the provisions of the GPL are applicable instead of
  564. * the above. If you wish to allow the use of your version of this file
  565. * only under the terms of the GPL and not to allow others to use your
  566. * version of this file under the BSD license, indicate your decision
  567. * by deleting the provisions above and replace them with the notice
  568. * and other provisions required by the GPL. If you do not delete the
  569. * provisions above, a recipient may use your version of this file under
  570. * either the BSD or the GPL.
  571. */
  572. #ifndef ECB_H
  573. #define ECB_H
  574. /* 16 bits major, 16 bits minor */
  575. #define ECB_VERSION 0x00010008
  576. #include <string.h> /* for memcpy */
  577. #if defined (_WIN32) && !defined (__MINGW32__)
  578. typedef signed char int8_t;
  579. typedef unsigned char uint8_t;
  580. typedef signed char int_fast8_t;
  581. typedef unsigned char uint_fast8_t;
  582. typedef signed short int16_t;
  583. typedef unsigned short uint16_t;
  584. typedef signed int int_fast16_t;
  585. typedef unsigned int uint_fast16_t;
  586. typedef signed int int32_t;
  587. typedef unsigned int uint32_t;
  588. typedef signed int int_fast32_t;
  589. typedef unsigned int uint_fast32_t;
  590. #if __GNUC__
  591. typedef signed long long int64_t;
  592. typedef unsigned long long uint64_t;
  593. #else /* _MSC_VER || __BORLANDC__ */
  594. typedef signed __int64 int64_t;
  595. typedef unsigned __int64 uint64_t;
  596. #endif
  597. typedef int64_t int_fast64_t;
  598. typedef uint64_t uint_fast64_t;
  599. #ifdef _WIN64
  600. #define ECB_PTRSIZE 8
  601. typedef uint64_t uintptr_t;
  602. typedef int64_t intptr_t;
  603. #else
  604. #define ECB_PTRSIZE 4
  605. typedef uint32_t uintptr_t;
  606. typedef int32_t intptr_t;
  607. #endif
  608. #else
  609. #include <inttypes.h>
  610. #if (defined INTPTR_MAX ? INTPTR_MAX : ULONG_MAX) > 0xffffffffU
  611. #define ECB_PTRSIZE 8
  612. #else
  613. #define ECB_PTRSIZE 4
  614. #endif
  615. #endif
  616. #define ECB_GCC_AMD64 (__amd64 || __amd64__ || __x86_64 || __x86_64__)
  617. #define ECB_MSVC_AMD64 (_M_AMD64 || _M_X64)
  618. #ifndef ECB_OPTIMIZE_SIZE
  619. #if __OPTIMIZE_SIZE__
  620. #define ECB_OPTIMIZE_SIZE 1
  621. #else
  622. #define ECB_OPTIMIZE_SIZE 0
  623. #endif
  624. #endif
  625. /* work around x32 idiocy by defining proper macros */
  626. #if ECB_GCC_AMD64 || ECB_MSVC_AMD64
  627. #if _ILP32
  628. #define ECB_AMD64_X32 1
  629. #else
  630. #define ECB_AMD64 1
  631. #endif
  632. #endif
  633. /* many compilers define _GNUC_ to some versions but then only implement
  634. * what their idiot authors think are the "more important" extensions,
  635. * causing enormous grief in return for some better fake benchmark numbers.
  636. * or so.
  637. * we try to detect these and simply assume they are not gcc - if they have
  638. * an issue with that they should have done it right in the first place.
  639. */
  640. #if !defined __GNUC_MINOR__ || defined __INTEL_COMPILER || defined __SUNPRO_C || defined __SUNPRO_CC || defined __llvm__ || defined __clang__
  641. #define ECB_GCC_VERSION(major,minor) 0
  642. #else
  643. #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
  644. #endif
  645. #define ECB_CLANG_VERSION(major,minor) (__clang_major__ > (major) || (__clang_major__ == (major) && __clang_minor__ >= (minor)))
  646. #if __clang__ && defined __has_builtin
  647. #define ECB_CLANG_BUILTIN(x) __has_builtin (x)
  648. #else
  649. #define ECB_CLANG_BUILTIN(x) 0
  650. #endif
  651. #if __clang__ && defined __has_extension
  652. #define ECB_CLANG_EXTENSION(x) __has_extension (x)
  653. #else
  654. #define ECB_CLANG_EXTENSION(x) 0
  655. #endif
  656. #define ECB_CPP (__cplusplus+0)
  657. #define ECB_CPP11 (__cplusplus >= 201103L)
  658. #define ECB_CPP14 (__cplusplus >= 201402L)
  659. #define ECB_CPP17 (__cplusplus >= 201703L)
  660. #if ECB_CPP
  661. #define ECB_C 0
  662. #define ECB_STDC_VERSION 0
  663. #else
  664. #define ECB_C 1
  665. #define ECB_STDC_VERSION __STDC_VERSION__
  666. #endif
  667. #define ECB_C99 (ECB_STDC_VERSION >= 199901L)
  668. #define ECB_C11 (ECB_STDC_VERSION >= 201112L)
  669. #define ECB_C17 (ECB_STDC_VERSION >= 201710L)
  670. #if ECB_CPP
  671. #define ECB_EXTERN_C extern "C"
  672. #define ECB_EXTERN_C_BEG ECB_EXTERN_C {
  673. #define ECB_EXTERN_C_END }
  674. #else
  675. #define ECB_EXTERN_C extern
  676. #define ECB_EXTERN_C_BEG
  677. #define ECB_EXTERN_C_END
  678. #endif
  679. /*****************************************************************************/
  680. /* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
  681. /* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
  682. #if ECB_NO_THREADS
  683. #define ECB_NO_SMP 1
  684. #endif
  685. #if ECB_NO_SMP
  686. #define ECB_MEMORY_FENCE do { } while (0)
  687. #endif
  688. /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/compiler_ref/compiler_builtins.html */
  689. #if __xlC__ && ECB_CPP
  690. #include <builtins.h>
  691. #endif
  692. #if 1400 <= _MSC_VER
  693. #include <intrin.h> /* fence functions _ReadBarrier, also bit search functions _BitScanReverse */
  694. #endif
  695. #ifndef ECB_MEMORY_FENCE
  696. #if ECB_GCC_VERSION(2,5) || defined __INTEL_COMPILER || (__llvm__ && __GNUC__) || __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
  697. #define ECB_MEMORY_FENCE_RELAXED __asm__ __volatile__ ("" : : : "memory")
  698. #if __i386 || __i386__
  699. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
  700. #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
  701. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory")
  702. #elif ECB_GCC_AMD64
  703. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
  704. #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("" : : : "memory")
  705. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("" : : : "memory")
  706. #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__
  707. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory")
  708. #elif defined __ARM_ARCH_2__ \
  709. || defined __ARM_ARCH_3__ || defined __ARM_ARCH_3M__ \
  710. || defined __ARM_ARCH_4__ || defined __ARM_ARCH_4T__ \
  711. || defined __ARM_ARCH_5__ || defined __ARM_ARCH_5E__ \
  712. || defined __ARM_ARCH_5T__ || defined __ARM_ARCH_5TE__ \
  713. || defined __ARM_ARCH_5TEJ__
  714. /* should not need any, unless running old code on newer cpu - arm doesn't support that */
  715. #elif defined __ARM_ARCH_6__ || defined __ARM_ARCH_6J__ \
  716. || defined __ARM_ARCH_6K__ || defined __ARM_ARCH_6ZK__ \
  717. || defined __ARM_ARCH_6T2__
  718. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory")
  719. #elif defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
  720. || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__
  721. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory")
  722. #elif __aarch64__
  723. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb ish" : : : "memory")
  724. #elif (__sparc || __sparc__) && !(__sparc_v8__ || defined __sparcv8)
  725. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad | #StoreStore | #StoreLoad" : : : "memory")
  726. #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("membar #LoadStore | #LoadLoad" : : : "memory")
  727. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("membar #LoadStore | #StoreStore")
  728. #elif defined __s390__ || defined __s390x__
  729. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("bcr 15,0" : : : "memory")
  730. #elif defined __mips__
  731. /* GNU/Linux emulates sync on mips1 architectures, so we force its use */
  732. /* anybody else who still uses mips1 is supposed to send in their version, with detection code. */
  733. #define ECB_MEMORY_FENCE __asm__ __volatile__ (".set mips2; sync; .set mips0" : : : "memory")
  734. #elif defined __alpha__
  735. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mb" : : : "memory")
  736. #elif defined __hppa__
  737. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
  738. #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("")
  739. #elif defined __ia64__
  740. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mf" : : : "memory")
  741. #elif defined __m68k__
  742. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
  743. #elif defined __m88k__
  744. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("tb1 0,%%r0,128" : : : "memory")
  745. #elif defined __sh__
  746. #define ECB_MEMORY_FENCE __asm__ __volatile__ ("" : : : "memory")
  747. #endif
  748. #endif
  749. #endif
  750. #ifndef ECB_MEMORY_FENCE
  751. #if ECB_GCC_VERSION(4,7)
  752. /* see comment below (stdatomic.h) about the C11 memory model. */
  753. #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
  754. #define ECB_MEMORY_FENCE_ACQUIRE __atomic_thread_fence (__ATOMIC_ACQUIRE)
  755. #define ECB_MEMORY_FENCE_RELEASE __atomic_thread_fence (__ATOMIC_RELEASE)
  756. #define ECB_MEMORY_FENCE_RELAXED __atomic_thread_fence (__ATOMIC_RELAXED)
  757. #elif ECB_CLANG_EXTENSION(c_atomic)
  758. /* see comment below (stdatomic.h) about the C11 memory model. */
  759. #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
  760. #define ECB_MEMORY_FENCE_ACQUIRE __c11_atomic_thread_fence (__ATOMIC_ACQUIRE)
  761. #define ECB_MEMORY_FENCE_RELEASE __c11_atomic_thread_fence (__ATOMIC_RELEASE)
  762. #define ECB_MEMORY_FENCE_RELAXED __c11_atomic_thread_fence (__ATOMIC_RELAXED)
  763. #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
  764. #define ECB_MEMORY_FENCE __sync_synchronize ()
  765. #elif _MSC_VER >= 1500 /* VC++ 2008 */
  766. /* apparently, microsoft broke all the memory barrier stuff in Visual Studio 2008... */
  767. #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
  768. #define ECB_MEMORY_FENCE _ReadWriteBarrier (); MemoryBarrier()
  769. #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier (); MemoryBarrier() /* according to msdn, _ReadBarrier is not a load fence */
  770. #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier (); MemoryBarrier()
  771. #elif _MSC_VER >= 1400 /* VC++ 2005 */
  772. #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
  773. #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
  774. #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
  775. #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
  776. #elif defined _WIN32
  777. #include <WinNT.h>
  778. #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */
  779. #elif __SUNPRO_C >= 0x5110 || __SUNPRO_CC >= 0x5110
  780. #include <mbarrier.h>
  781. #define ECB_MEMORY_FENCE __machine_rw_barrier ()
  782. #define ECB_MEMORY_FENCE_ACQUIRE __machine_acq_barrier ()
  783. #define ECB_MEMORY_FENCE_RELEASE __machine_rel_barrier ()
  784. #define ECB_MEMORY_FENCE_RELAXED __compiler_barrier ()
  785. #elif __xlC__
  786. #define ECB_MEMORY_FENCE __sync ()
  787. #endif
  788. #endif
  789. #ifndef ECB_MEMORY_FENCE
  790. #if ECB_C11 && !defined __STDC_NO_ATOMICS__
  791. /* we assume that these memory fences work on all variables/all memory accesses, */
  792. /* not just C11 atomics and atomic accesses */
  793. #include <stdatomic.h>
  794. #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
  795. #define ECB_MEMORY_FENCE_ACQUIRE atomic_thread_fence (memory_order_acquire)
  796. #define ECB_MEMORY_FENCE_RELEASE atomic_thread_fence (memory_order_release)
  797. #endif
  798. #endif
  799. #ifndef ECB_MEMORY_FENCE
  800. #if !ECB_AVOID_PTHREADS
  801. /*
  802. * if you get undefined symbol references to pthread_mutex_lock,
  803. * or failure to find pthread.h, then you should implement
  804. * the ECB_MEMORY_FENCE operations for your cpu/compiler
  805. * OR provide pthread.h and link against the posix thread library
  806. * of your system.
  807. */
  808. #include <pthread.h>
  809. #define ECB_NEEDS_PTHREADS 1
  810. #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1
  811. static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
  812. #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
  813. #endif
  814. #endif
  815. #if !defined ECB_MEMORY_FENCE_ACQUIRE && defined ECB_MEMORY_FENCE
  816. #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
  817. #endif
  818. #if !defined ECB_MEMORY_FENCE_RELEASE && defined ECB_MEMORY_FENCE
  819. #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
  820. #endif
  821. #if !defined ECB_MEMORY_FENCE_RELAXED && defined ECB_MEMORY_FENCE
  822. #define ECB_MEMORY_FENCE_RELAXED ECB_MEMORY_FENCE /* very heavy-handed */
  823. #endif
  824. /*****************************************************************************/
  825. #if ECB_CPP
  826. #define ecb_inline static inline
  827. #elif ECB_GCC_VERSION(2,5)
  828. #define ecb_inline static __inline__
  829. #elif ECB_C99
  830. #define ecb_inline static inline
  831. #else
  832. #define ecb_inline static
  833. #endif
  834. #if ECB_GCC_VERSION(3,3)
  835. #define ecb_restrict __restrict__
  836. #elif ECB_C99
  837. #define ecb_restrict restrict
  838. #else
  839. #define ecb_restrict
  840. #endif
  841. typedef int ecb_bool;
  842. #define ECB_CONCAT_(a, b) a ## b
  843. #define ECB_CONCAT(a, b) ECB_CONCAT_(a, b)
  844. #define ECB_STRINGIFY_(a) # a
  845. #define ECB_STRINGIFY(a) ECB_STRINGIFY_(a)
  846. #define ECB_STRINGIFY_EXPR(expr) ((expr), ECB_STRINGIFY_ (expr))
  847. #define ecb_function_ ecb_inline
  848. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_VERSION(2,8)
  849. #define ecb_attribute(attrlist) __attribute__ (attrlist)
  850. #else
  851. #define ecb_attribute(attrlist)
  852. #endif
  853. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_constant_p)
  854. #define ecb_is_constant(expr) __builtin_constant_p (expr)
  855. #else
  856. /* possible C11 impl for integral types
  857. typedef struct ecb_is_constant_struct ecb_is_constant_struct;
  858. #define ecb_is_constant(expr) _Generic ((1 ? (struct ecb_is_constant_struct *)0 : (void *)((expr) - (expr)), ecb_is_constant_struct *: 0, default: 1)) */
  859. #define ecb_is_constant(expr) 0
  860. #endif
  861. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_expect)
  862. #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
  863. #else
  864. #define ecb_expect(expr,value) (expr)
  865. #endif
  866. #if ECB_GCC_VERSION(3,1) || ECB_CLANG_BUILTIN(__builtin_prefetch)
  867. #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
  868. #else
  869. #define ecb_prefetch(addr,rw,locality)
  870. #endif
  871. /* no emulation for ecb_decltype */
  872. #if ECB_CPP11
  873. // older implementations might have problems with decltype(x)::type, work around it
  874. template<class T> struct ecb_decltype_t { typedef T type; };
  875. #define ecb_decltype(x) ecb_decltype_t<decltype (x)>::type
  876. #elif ECB_GCC_VERSION(3,0) || ECB_CLANG_VERSION(2,8)
  877. #define ecb_decltype(x) __typeof__ (x)
  878. #endif
  879. #if _MSC_VER >= 1300
  880. #define ecb_deprecated __declspec (deprecated)
  881. #else
  882. #define ecb_deprecated ecb_attribute ((__deprecated__))
  883. #endif
  884. #if _MSC_VER >= 1500
  885. #define ecb_deprecated_message(msg) __declspec (deprecated (msg))
  886. #elif ECB_GCC_VERSION(4,5)
  887. #define ecb_deprecated_message(msg) ecb_attribute ((__deprecated__ (msg))
  888. #else
  889. #define ecb_deprecated_message(msg) ecb_deprecated
  890. #endif
  891. #if _MSC_VER >= 1400
  892. #define ecb_noinline __declspec (noinline)
  893. #else
  894. #define ecb_noinline ecb_attribute ((__noinline__))
  895. #endif
  896. #define ecb_unused ecb_attribute ((__unused__))
  897. #define ecb_const ecb_attribute ((__const__))
  898. #define ecb_pure ecb_attribute ((__pure__))
  899. #if ECB_C11 || __IBMC_NORETURN
  900. /* http://www-01.ibm.com/support/knowledgecenter/SSGH3R_13.1.0/com.ibm.xlcpp131.aix.doc/language_ref/noreturn.html */
  901. #define ecb_noreturn _Noreturn
  902. #elif ECB_CPP11
  903. #define ecb_noreturn [[noreturn]]
  904. #elif _MSC_VER >= 1200
  905. /* http://msdn.microsoft.com/en-us/library/k6ktzx3s.aspx */
  906. #define ecb_noreturn __declspec (noreturn)
  907. #else
  908. #define ecb_noreturn ecb_attribute ((__noreturn__))
  909. #endif
  910. #if ECB_GCC_VERSION(4,3)
  911. #define ecb_artificial ecb_attribute ((__artificial__))
  912. #define ecb_hot ecb_attribute ((__hot__))
  913. #define ecb_cold ecb_attribute ((__cold__))
  914. #else
  915. #define ecb_artificial
  916. #define ecb_hot
  917. #define ecb_cold
  918. #endif
  919. /* put around conditional expressions if you are very sure that the */
  920. /* expression is mostly true or mostly false. note that these return */
  921. /* booleans, not the expression. */
  922. #define ecb_expect_false(expr) ecb_expect (!!(expr), 0)
  923. #define ecb_expect_true(expr) ecb_expect (!!(expr), 1)
  924. /* for compatibility to the rest of the world */
  925. #define ecb_likely(expr) ecb_expect_true (expr)
  926. #define ecb_unlikely(expr) ecb_expect_false (expr)
  927. /* count trailing zero bits and count # of one bits */
  928. #if ECB_GCC_VERSION(3,4) \
  929. || (ECB_CLANG_BUILTIN(__builtin_clz) && ECB_CLANG_BUILTIN(__builtin_clzll) \
  930. && ECB_CLANG_BUILTIN(__builtin_ctz) && ECB_CLANG_BUILTIN(__builtin_ctzll) \
  931. && ECB_CLANG_BUILTIN(__builtin_popcount))
  932. /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */
  933. #define ecb_ld32(x) (__builtin_clz (x) ^ 31)
  934. #define ecb_ld64(x) (__builtin_clzll (x) ^ 63)
  935. #define ecb_ctz32(x) __builtin_ctz (x)
  936. #define ecb_ctz64(x) __builtin_ctzll (x)
  937. #define ecb_popcount32(x) __builtin_popcount (x)
  938. /* no popcountll */
  939. #else
  940. ecb_function_ ecb_const int ecb_ctz32 (uint32_t x);
  941. ecb_function_ ecb_const int
  942. ecb_ctz32 (uint32_t x)
  943. {
  944. #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
  945. unsigned long r;
  946. _BitScanForward (&r, x);
  947. return (int)r;
  948. #else
  949. int r = 0;
  950. x &= ~x + 1; /* this isolates the lowest bit */
  951. #if ECB_branchless_on_i386
  952. r += !!(x & 0xaaaaaaaa) << 0;
  953. r += !!(x & 0xcccccccc) << 1;
  954. r += !!(x & 0xf0f0f0f0) << 2;
  955. r += !!(x & 0xff00ff00) << 3;
  956. r += !!(x & 0xffff0000) << 4;
  957. #else
  958. if (x & 0xaaaaaaaa) r += 1;
  959. if (x & 0xcccccccc) r += 2;
  960. if (x & 0xf0f0f0f0) r += 4;
  961. if (x & 0xff00ff00) r += 8;
  962. if (x & 0xffff0000) r += 16;
  963. #endif
  964. return r;
  965. #endif
  966. }
  967. ecb_function_ ecb_const int ecb_ctz64 (uint64_t x);
  968. ecb_function_ ecb_const int
  969. ecb_ctz64 (uint64_t x)
  970. {
  971. #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
  972. unsigned long r;
  973. _BitScanForward64 (&r, x);
  974. return (int)r;
  975. #else
  976. int shift = x & 0xffffffff ? 0 : 32;
  977. return ecb_ctz32 (x >> shift) + shift;
  978. #endif
  979. }
  980. ecb_function_ ecb_const int ecb_popcount32 (uint32_t x);
  981. ecb_function_ ecb_const int
  982. ecb_popcount32 (uint32_t x)
  983. {
  984. x -= (x >> 1) & 0x55555555;
  985. x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
  986. x = ((x >> 4) + x) & 0x0f0f0f0f;
  987. x *= 0x01010101;
  988. return x >> 24;
  989. }
  990. ecb_function_ ecb_const int ecb_ld32 (uint32_t x);
  991. ecb_function_ ecb_const int ecb_ld32 (uint32_t x)
  992. {
  993. #if 1400 <= _MSC_VER && (_M_IX86 || _M_X64 || _M_IA64 || _M_ARM)
  994. unsigned long r;
  995. _BitScanReverse (&r, x);
  996. return (int)r;
  997. #else
  998. int r = 0;
  999. if (x >> 16) { x >>= 16; r += 16; }
  1000. if (x >> 8) { x >>= 8; r += 8; }
  1001. if (x >> 4) { x >>= 4; r += 4; }
  1002. if (x >> 2) { x >>= 2; r += 2; }
  1003. if (x >> 1) { r += 1; }
  1004. return r;
  1005. #endif
  1006. }
  1007. ecb_function_ ecb_const int ecb_ld64 (uint64_t x);
  1008. ecb_function_ ecb_const int ecb_ld64 (uint64_t x)
  1009. {
  1010. #if 1400 <= _MSC_VER && (_M_X64 || _M_IA64 || _M_ARM)
  1011. unsigned long r;
  1012. _BitScanReverse64 (&r, x);
  1013. return (int)r;
  1014. #else
  1015. int r = 0;
  1016. if (x >> 32) { x >>= 32; r += 32; }
  1017. return r + ecb_ld32 (x);
  1018. #endif
  1019. }
  1020. #endif
  1021. ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x);
  1022. ecb_function_ ecb_const ecb_bool ecb_is_pot32 (uint32_t x) { return !(x & (x - 1)); }
  1023. ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x);
  1024. ecb_function_ ecb_const ecb_bool ecb_is_pot64 (uint64_t x) { return !(x & (x - 1)); }
  1025. ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x);
  1026. ecb_function_ ecb_const uint8_t ecb_bitrev8 (uint8_t x)
  1027. {
  1028. return ( (x * 0x0802U & 0x22110U)
  1029. | (x * 0x8020U & 0x88440U)) * 0x10101U >> 16;
  1030. }
  1031. ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x);
  1032. ecb_function_ ecb_const uint16_t ecb_bitrev16 (uint16_t x)
  1033. {
  1034. x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
  1035. x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
  1036. x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
  1037. x = ( x >> 8 ) | ( x << 8);
  1038. return x;
  1039. }
  1040. ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x);
  1041. ecb_function_ ecb_const uint32_t ecb_bitrev32 (uint32_t x)
  1042. {
  1043. x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
  1044. x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
  1045. x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
  1046. x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
  1047. x = ( x >> 16 ) | ( x << 16);
  1048. return x;
  1049. }
  1050. /* popcount64 is only available on 64 bit cpus as gcc builtin */
  1051. /* so for this version we are lazy */
  1052. ecb_function_ ecb_const int ecb_popcount64 (uint64_t x);
  1053. ecb_function_ ecb_const int
  1054. ecb_popcount64 (uint64_t x)
  1055. {
  1056. return ecb_popcount32 (x) + ecb_popcount32 (x >> 32);
  1057. }
  1058. ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count);
  1059. ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count);
  1060. ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count);
  1061. ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count);
  1062. ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count);
  1063. ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count);
  1064. ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count);
  1065. ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count);
  1066. ecb_inline ecb_const uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); }
  1067. ecb_inline ecb_const uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); }
  1068. ecb_inline ecb_const uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); }
  1069. ecb_inline ecb_const uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); }
  1070. ecb_inline ecb_const uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); }
  1071. ecb_inline ecb_const uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); }
  1072. ecb_inline ecb_const uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); }
  1073. ecb_inline ecb_const uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); }
  1074. #if ECB_CPP
  1075. inline uint8_t ecb_ctz (uint8_t v) { return ecb_ctz32 (v); }
  1076. inline uint16_t ecb_ctz (uint16_t v) { return ecb_ctz32 (v); }
  1077. inline uint32_t ecb_ctz (uint32_t v) { return ecb_ctz32 (v); }
  1078. inline uint64_t ecb_ctz (uint64_t v) { return ecb_ctz64 (v); }
  1079. inline bool ecb_is_pot (uint8_t v) { return ecb_is_pot32 (v); }
  1080. inline bool ecb_is_pot (uint16_t v) { return ecb_is_pot32 (v); }
  1081. inline bool ecb_is_pot (uint32_t v) { return ecb_is_pot32 (v); }
  1082. inline bool ecb_is_pot (uint64_t v) { return ecb_is_pot64 (v); }
  1083. inline int ecb_ld (uint8_t v) { return ecb_ld32 (v); }
  1084. inline int ecb_ld (uint16_t v) { return ecb_ld32 (v); }
  1085. inline int ecb_ld (uint32_t v) { return ecb_ld32 (v); }
  1086. inline int ecb_ld (uint64_t v) { return ecb_ld64 (v); }
  1087. inline int ecb_popcount (uint8_t v) { return ecb_popcount32 (v); }
  1088. inline int ecb_popcount (uint16_t v) { return ecb_popcount32 (v); }
  1089. inline int ecb_popcount (uint32_t v) { return ecb_popcount32 (v); }
  1090. inline int ecb_popcount (uint64_t v) { return ecb_popcount64 (v); }
  1091. inline uint8_t ecb_bitrev (uint8_t v) { return ecb_bitrev8 (v); }
  1092. inline uint16_t ecb_bitrev (uint16_t v) { return ecb_bitrev16 (v); }
  1093. inline uint32_t ecb_bitrev (uint32_t v) { return ecb_bitrev32 (v); }
  1094. inline uint8_t ecb_rotl (uint8_t v, unsigned int count) { return ecb_rotl8 (v, count); }
  1095. inline uint16_t ecb_rotl (uint16_t v, unsigned int count) { return ecb_rotl16 (v, count); }
  1096. inline uint32_t ecb_rotl (uint32_t v, unsigned int count) { return ecb_rotl32 (v, count); }
  1097. inline uint64_t ecb_rotl (uint64_t v, unsigned int count) { return ecb_rotl64 (v, count); }
  1098. inline uint8_t ecb_rotr (uint8_t v, unsigned int count) { return ecb_rotr8 (v, count); }
  1099. inline uint16_t ecb_rotr (uint16_t v, unsigned int count) { return ecb_rotr16 (v, count); }
  1100. inline uint32_t ecb_rotr (uint32_t v, unsigned int count) { return ecb_rotr32 (v, count); }
  1101. inline uint64_t ecb_rotr (uint64_t v, unsigned int count) { return ecb_rotr64 (v, count); }
  1102. #endif
  1103. #if ECB_GCC_VERSION(4,3) || (ECB_CLANG_BUILTIN(__builtin_bswap32) && ECB_CLANG_BUILTIN(__builtin_bswap64))
  1104. #if ECB_GCC_VERSION(4,8) || ECB_CLANG_BUILTIN(__builtin_bswap16)
  1105. #define ecb_bswap16(x) __builtin_bswap16 (x)
  1106. #else
  1107. #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16)
  1108. #endif
  1109. #define ecb_bswap32(x) __builtin_bswap32 (x)
  1110. #define ecb_bswap64(x) __builtin_bswap64 (x)
  1111. #elif _MSC_VER
  1112. #include <stdlib.h>
  1113. #define ecb_bswap16(x) ((uint16_t)_byteswap_ushort ((uint16_t)(x)))
  1114. #define ecb_bswap32(x) ((uint32_t)_byteswap_ulong ((uint32_t)(x)))
  1115. #define ecb_bswap64(x) ((uint64_t)_byteswap_uint64 ((uint64_t)(x)))
  1116. #else
  1117. ecb_function_ ecb_const uint16_t ecb_bswap16 (uint16_t x);
  1118. ecb_function_ ecb_const uint16_t
  1119. ecb_bswap16 (uint16_t x)
  1120. {
  1121. return ecb_rotl16 (x, 8);
  1122. }
  1123. ecb_function_ ecb_const uint32_t ecb_bswap32 (uint32_t x);
  1124. ecb_function_ ecb_const uint32_t
  1125. ecb_bswap32 (uint32_t x)
  1126. {
  1127. return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16);
  1128. }
  1129. ecb_function_ ecb_const uint64_t ecb_bswap64 (uint64_t x);
  1130. ecb_function_ ecb_const uint64_t
  1131. ecb_bswap64 (uint64_t x)
  1132. {
  1133. return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32);
  1134. }
  1135. #endif
  1136. #if ECB_GCC_VERSION(4,5) || ECB_CLANG_BUILTIN(__builtin_unreachable)
  1137. #define ecb_unreachable() __builtin_unreachable ()
  1138. #else
  1139. /* this seems to work fine, but gcc always emits a warning for it :/ */
  1140. ecb_inline ecb_noreturn void ecb_unreachable (void);
  1141. ecb_inline ecb_noreturn void ecb_unreachable (void) { }
  1142. #endif
  1143. /* try to tell the compiler that some condition is definitely true */
  1144. #define ecb_assume(cond) if (!(cond)) ecb_unreachable (); else 0
  1145. ecb_inline ecb_const uint32_t ecb_byteorder_helper (void);
  1146. ecb_inline ecb_const uint32_t
  1147. ecb_byteorder_helper (void)
  1148. {
  1149. /* the union code still generates code under pressure in gcc, */
  1150. /* but less than using pointers, and always seems to */
  1151. /* successfully return a constant. */
  1152. /* the reason why we have this horrible preprocessor mess */
  1153. /* is to avoid it in all cases, at least on common architectures */
  1154. /* or when using a recent enough gcc version (>= 4.6) */
  1155. #if (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
  1156. || ((__i386 || __i386__ || _M_IX86 || ECB_GCC_AMD64 || ECB_MSVC_AMD64) && !__VOS__)
  1157. #define ECB_LITTLE_ENDIAN 1
  1158. return 0x44332211;
  1159. #elif (defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) \
  1160. || ((__AARCH64EB__ || __MIPSEB__ || __ARMEB__) && !__VOS__)
  1161. #define ECB_BIG_ENDIAN 1
  1162. return 0x11223344;
  1163. #else
  1164. union
  1165. {
  1166. uint8_t c[4];
  1167. uint32_t u;
  1168. } u = { 0x11, 0x22, 0x33, 0x44 };
  1169. return u.u;
  1170. #endif
  1171. }
  1172. ecb_inline ecb_const ecb_bool ecb_big_endian (void);
  1173. ecb_inline ecb_const ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11223344; }
  1174. ecb_inline ecb_const ecb_bool ecb_little_endian (void);
  1175. ecb_inline ecb_const ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44332211; }
  1176. /*****************************************************************************/
  1177. /* unaligned load/store */
  1178. ecb_inline uint_fast16_t ecb_be_u16_to_host (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; }
  1179. ecb_inline uint_fast32_t ecb_be_u32_to_host (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; }
  1180. ecb_inline uint_fast64_t ecb_be_u64_to_host (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; }
  1181. ecb_inline uint_fast16_t ecb_le_u16_to_host (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; }
  1182. ecb_inline uint_fast32_t ecb_le_u32_to_host (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; }
  1183. ecb_inline uint_fast64_t ecb_le_u64_to_host (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; }
  1184. ecb_inline uint_fast16_t ecb_peek_u16_u (const void *ptr) { uint16_t v; memcpy (&v, ptr, sizeof (v)); return v; }
  1185. ecb_inline uint_fast32_t ecb_peek_u32_u (const void *ptr) { uint32_t v; memcpy (&v, ptr, sizeof (v)); return v; }
  1186. ecb_inline uint_fast64_t ecb_peek_u64_u (const void *ptr) { uint64_t v; memcpy (&v, ptr, sizeof (v)); return v; }
  1187. ecb_inline uint_fast16_t ecb_peek_be_u16_u (const void *ptr) { return ecb_be_u16_to_host (ecb_peek_u16_u (ptr)); }
  1188. ecb_inline uint_fast32_t ecb_peek_be_u32_u (const void *ptr) { return ecb_be_u32_to_host (ecb_peek_u32_u (ptr)); }
  1189. ecb_inline uint_fast64_t ecb_peek_be_u64_u (const void *ptr) { return ecb_be_u64_to_host (ecb_peek_u64_u (ptr)); }
  1190. ecb_inline uint_fast16_t ecb_peek_le_u16_u (const void *ptr) { return ecb_le_u16_to_host (ecb_peek_u16_u (ptr)); }
  1191. ecb_inline uint_fast32_t ecb_peek_le_u32_u (const void *ptr) { return ecb_le_u32_to_host (ecb_peek_u32_u (ptr)); }
  1192. ecb_inline uint_fast64_t ecb_peek_le_u64_u (const void *ptr) { return ecb_le_u64_to_host (ecb_peek_u64_u (ptr)); }
  1193. ecb_inline uint_fast16_t ecb_host_to_be_u16 (uint_fast16_t v) { return ecb_little_endian () ? ecb_bswap16 (v) : v; }
  1194. ecb_inline uint_fast32_t ecb_host_to_be_u32 (uint_fast32_t v) { return ecb_little_endian () ? ecb_bswap32 (v) : v; }
  1195. ecb_inline uint_fast64_t ecb_host_to_be_u64 (uint_fast64_t v) { return ecb_little_endian () ? ecb_bswap64 (v) : v; }
  1196. ecb_inline uint_fast16_t ecb_host_to_le_u16 (uint_fast16_t v) { return ecb_big_endian () ? ecb_bswap16 (v) : v; }
  1197. ecb_inline uint_fast32_t ecb_host_to_le_u32 (uint_fast32_t v) { return ecb_big_endian () ? ecb_bswap32 (v) : v; }
  1198. ecb_inline uint_fast64_t ecb_host_to_le_u64 (uint_fast64_t v) { return ecb_big_endian () ? ecb_bswap64 (v) : v; }
  1199. ecb_inline void ecb_poke_u16_u (void *ptr, uint16_t v) { memcpy (ptr, &v, sizeof (v)); }
  1200. ecb_inline void ecb_poke_u32_u (void *ptr, uint32_t v) { memcpy (ptr, &v, sizeof (v)); }
  1201. ecb_inline void ecb_poke_u64_u (void *ptr, uint64_t v) { memcpy (ptr, &v, sizeof (v)); }
  1202. ecb_inline void ecb_poke_be_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_be_u16 (v)); }
  1203. ecb_inline void ecb_poke_be_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_be_u32 (v)); }
  1204. ecb_inline void ecb_poke_be_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_be_u64 (v)); }
  1205. ecb_inline void ecb_poke_le_u16_u (void *ptr, uint_fast16_t v) { ecb_poke_u16_u (ptr, ecb_host_to_le_u16 (v)); }
  1206. ecb_inline void ecb_poke_le_u32_u (void *ptr, uint_fast32_t v) { ecb_poke_u32_u (ptr, ecb_host_to_le_u32 (v)); }
  1207. ecb_inline void ecb_poke_le_u64_u (void *ptr, uint_fast64_t v) { ecb_poke_u64_u (ptr, ecb_host_to_le_u64 (v)); }
  1208. #if ECB_CPP
  1209. inline uint8_t ecb_bswap (uint8_t v) { return v; }
  1210. inline uint16_t ecb_bswap (uint16_t v) { return ecb_bswap16 (v); }
  1211. inline uint32_t ecb_bswap (uint32_t v) { return ecb_bswap32 (v); }
  1212. inline uint64_t ecb_bswap (uint64_t v) { return ecb_bswap64 (v); }
  1213. template<typename T> inline T ecb_be_to_host (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; }
  1214. template<typename T> inline T ecb_le_to_host (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; }
  1215. template<typename T> inline T ecb_peek (const void *ptr) { return *(const T *)ptr; }
  1216. template<typename T> inline T ecb_peek_be (const void *ptr) { return ecb_be_to_host (ecb_peek <T> (ptr)); }
  1217. template<typename T> inline T ecb_peek_le (const void *ptr) { return ecb_le_to_host (ecb_peek <T> (ptr)); }
  1218. template<typename T> inline T ecb_peek_u (const void *ptr) { T v; memcpy (&v, ptr, sizeof (v)); return v; }
  1219. template<typename T> inline T ecb_peek_be_u (const void *ptr) { return ecb_be_to_host (ecb_peek_u<T> (ptr)); }
  1220. template<typename T> inline T ecb_peek_le_u (const void *ptr) { return ecb_le_to_host (ecb_peek_u<T> (ptr)); }
  1221. template<typename T> inline T ecb_host_to_be (T v) { return ecb_little_endian () ? ecb_bswap (v) : v; }
  1222. template<typename T> inline T ecb_host_to_le (T v) { return ecb_big_endian () ? ecb_bswap (v) : v; }
  1223. template<typename T> inline void ecb_poke (void *ptr, T v) { *(T *)ptr = v; }
  1224. template<typename T> inline void ecb_poke_be (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_be (v)); }
  1225. template<typename T> inline void ecb_poke_le (void *ptr, T v) { return ecb_poke <T> (ptr, ecb_host_to_le (v)); }
  1226. template<typename T> inline void ecb_poke_u (void *ptr, T v) { memcpy (ptr, &v, sizeof (v)); }
  1227. template<typename T> inline void ecb_poke_be_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_be (v)); }
  1228. template<typename T> inline void ecb_poke_le_u (void *ptr, T v) { return ecb_poke_u<T> (ptr, ecb_host_to_le (v)); }
  1229. #endif
  1230. /*****************************************************************************/
  1231. #if ECB_GCC_VERSION(3,0) || ECB_C99
  1232. #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0))
  1233. #else
  1234. #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n)))
  1235. #endif
  1236. #if ECB_CPP
  1237. template<typename T>
  1238. static inline T ecb_div_rd (T val, T div)
  1239. {
  1240. return val < 0 ? - ((-val + div - 1) / div) : (val ) / div;
  1241. }
  1242. template<typename T>
  1243. static inline T ecb_div_ru (T val, T div)
  1244. {
  1245. return val < 0 ? - ((-val ) / div) : (val + div - 1) / div;
  1246. }
  1247. #else
  1248. #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div))
  1249. #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div))
  1250. #endif
  1251. #if ecb_cplusplus_does_not_suck
  1252. /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */
  1253. template<typename T, int N>
  1254. static inline int ecb_array_length (const T (&arr)[N])
  1255. {
  1256. return N;
  1257. }
  1258. #else
  1259. #define ecb_array_length(name) (sizeof (name) / sizeof (name [0]))
  1260. #endif
  1261. /*****************************************************************************/
  1262. ecb_function_ ecb_const uint32_t ecb_binary16_to_binary32 (uint32_t x);
  1263. ecb_function_ ecb_const uint32_t
  1264. ecb_binary16_to_binary32 (uint32_t x)
  1265. {
  1266. unsigned int s = (x & 0x8000) << (31 - 15);
  1267. int e = (x >> 10) & 0x001f;
  1268. unsigned int m = x & 0x03ff;
  1269. if (ecb_expect_false (e == 31))
  1270. /* infinity or NaN */
  1271. e = 255 - (127 - 15);
  1272. else if (ecb_expect_false (!e))
  1273. {
  1274. if (ecb_expect_true (!m))
  1275. /* zero, handled by code below by forcing e to 0 */
  1276. e = 0 - (127 - 15);
  1277. else
  1278. {
  1279. /* subnormal, renormalise */
  1280. unsigned int s = 10 - ecb_ld32 (m);
  1281. m = (m << s) & 0x3ff; /* mask implicit bit */
  1282. e -= s - 1;
  1283. }
  1284. }
  1285. /* e and m now are normalised, or zero, (or inf or nan) */
  1286. e += 127 - 15;
  1287. return s | (e << 23) | (m << (23 - 10));
  1288. }
  1289. ecb_function_ ecb_const uint16_t ecb_binary32_to_binary16 (uint32_t x);
  1290. ecb_function_ ecb_const uint16_t
  1291. ecb_binary32_to_binary16 (uint32_t x)
  1292. {
  1293. unsigned int s = (x >> 16) & 0x00008000; /* sign bit, the easy part */
  1294. unsigned int e = ((x >> 23) & 0x000000ff) - (127 - 15); /* the desired exponent */
  1295. unsigned int m = x & 0x007fffff;
  1296. x &= 0x7fffffff;
  1297. /* if it's within range of binary16 normals, use fast path */
  1298. if (ecb_expect_true (0x38800000 <= x && x <= 0x477fefff))
  1299. {
  1300. /* mantissa round-to-even */
  1301. m += 0x00000fff + ((m >> (23 - 10)) & 1);
  1302. /* handle overflow */
  1303. if (ecb_expect_false (m >= 0x00800000))
  1304. {
  1305. m >>= 1;
  1306. e += 1;
  1307. }
  1308. return s | (e << 10) | (m >> (23 - 10));
  1309. }
  1310. /* handle large numbers and infinity */
  1311. if (ecb_expect_true (0x477fefff < x && x <= 0x7f800000))
  1312. return s | 0x7c00;
  1313. /* handle zero, subnormals and small numbers */
  1314. if (ecb_expect_true (x < 0x38800000))
  1315. {
  1316. /* zero */
  1317. if (ecb_expect_true (!x))
  1318. return s;
  1319. /* handle subnormals */
  1320. /* too small, will be zero */
  1321. if (e < (14 - 24)) /* might not be sharp, but is good enough */
  1322. return s;
  1323. m |= 0x00800000; /* make implicit bit explicit */
  1324. /* very tricky - we need to round to the nearest e (+10) bit value */
  1325. {
  1326. unsigned int bits = 14 - e;
  1327. unsigned int half = (1 << (bits - 1)) - 1;
  1328. unsigned int even = (m >> bits) & 1;
  1329. /* if this overflows, we will end up with a normalised number */
  1330. m = (m + half + even) >> bits;
  1331. }
  1332. return s | m;
  1333. }
  1334. /* handle NaNs, preserve leftmost nan bits, but make sure we don't turn them into infinities */
  1335. m >>= 13;
  1336. return s | 0x7c00 | m | !m;
  1337. }
  1338. /*******************************************************************************/
  1339. /* floating point stuff, can be disabled by defining ECB_NO_LIBM */
  1340. /* basically, everything uses "ieee pure-endian" floating point numbers */
  1341. /* the only noteworthy exception is ancient armle, which uses order 43218765 */
  1342. #if 0 \
  1343. || __i386 || __i386__ \
  1344. || ECB_GCC_AMD64 \
  1345. || __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ \
  1346. || defined __s390__ || defined __s390x__ \
  1347. || defined __mips__ \
  1348. || defined __alpha__ \
  1349. || defined __hppa__ \
  1350. || defined __ia64__ \
  1351. || defined __m68k__ \
  1352. || defined __m88k__ \
  1353. || defined __sh__ \
  1354. || defined _M_IX86 || defined ECB_MSVC_AMD64 || defined _M_IA64 \
  1355. || (defined __arm__ && (defined __ARM_EABI__ || defined __EABI__ || defined __VFP_FP__ || defined _WIN32_WCE || defined __ANDROID__)) \
  1356. || defined __aarch64__
  1357. #define ECB_STDFP 1
  1358. #else
  1359. #define ECB_STDFP 0
  1360. #endif
  1361. #ifndef ECB_NO_LIBM
  1362. #include <math.h> /* for frexp*, ldexp*, INFINITY, NAN */
  1363. /* only the oldest of old doesn't have this one. solaris. */
  1364. #ifdef INFINITY
  1365. #define ECB_INFINITY INFINITY
  1366. #else
  1367. #define ECB_INFINITY HUGE_VAL
  1368. #endif
  1369. #ifdef NAN
  1370. #define ECB_NAN NAN
  1371. #else
  1372. #define ECB_NAN ECB_INFINITY
  1373. #endif
  1374. #if ECB_C99 || _XOPEN_VERSION >= 600 || _POSIX_VERSION >= 200112L
  1375. #define ecb_ldexpf(x,e) ldexpf ((x), (e))
  1376. #define ecb_frexpf(x,e) frexpf ((x), (e))
  1377. #else
  1378. #define ecb_ldexpf(x,e) (float) ldexp ((double) (x), (e))
  1379. #define ecb_frexpf(x,e) (float) frexp ((double) (x), (e))
  1380. #endif
  1381. /* convert a float to ieee single/binary32 */
  1382. ecb_function_ ecb_const uint32_t ecb_float_to_binary32 (float x);
  1383. ecb_function_ ecb_const uint32_t
  1384. ecb_float_to_binary32 (float x)
  1385. {
  1386. uint32_t r;
  1387. #if ECB_STDFP
  1388. memcpy (&r, &x, 4);
  1389. #else
  1390. /* slow emulation, works for anything but -0 */
  1391. uint32_t m;
  1392. int e;
  1393. if (x == 0e0f ) return 0x00000000U;
  1394. if (x > +3.40282346638528860e+38f) return 0x7f800000U;
  1395. if (x < -3.40282346638528860e+38f) return 0xff800000U;
  1396. if (x != x ) return 0x7fbfffffU;
  1397. m = ecb_frexpf (x, &e) * 0x1000000U;
  1398. r = m & 0x80000000U;
  1399. if (r)
  1400. m = -m;
  1401. if (e <= -126)
  1402. {
  1403. m &= 0xffffffU;
  1404. m >>= (-125 - e);
  1405. e = -126;
  1406. }
  1407. r |= (e + 126) << 23;
  1408. r |= m & 0x7fffffU;
  1409. #endif
  1410. return r;
  1411. }
  1412. /* converts an ieee single/binary32 to a float */
  1413. ecb_function_ ecb_const float ecb_binary32_to_float (uint32_t x);
  1414. ecb_function_ ecb_const float
  1415. ecb_binary32_to_float (uint32_t x)
  1416. {
  1417. float r;
  1418. #if ECB_STDFP
  1419. memcpy (&r, &x, 4);
  1420. #else
  1421. /* emulation, only works for normals and subnormals and +0 */
  1422. int neg = x >> 31;
  1423. int e = (x >> 23) & 0xffU;
  1424. x &= 0x7fffffU;
  1425. if (e)
  1426. x |= 0x800000U;
  1427. else
  1428. e = 1;
  1429. /* we distrust ldexpf a bit and do the 2**-24 scaling by an extra multiply */
  1430. r = ecb_ldexpf (x * (0.5f / 0x800000U), e - 126);
  1431. r = neg ? -r : r;
  1432. #endif
  1433. return r;
  1434. }
  1435. /* convert a double to ieee double/binary64 */
  1436. ecb_function_ ecb_const uint64_t ecb_double_to_binary64 (double x);
  1437. ecb_function_ ecb_const uint64_t
  1438. ecb_double_to_binary64 (double x)
  1439. {
  1440. uint64_t r;
  1441. #if ECB_STDFP
  1442. memcpy (&r, &x, 8);
  1443. #else
  1444. /* slow emulation, works for anything but -0 */
  1445. uint64_t m;
  1446. int e;
  1447. if (x == 0e0 ) return 0x0000000000000000U;
  1448. if (x > +1.79769313486231470e+308) return 0x7ff0000000000000U;
  1449. if (x < -1.79769313486231470e+308) return 0xfff0000000000000U;
  1450. if (x != x ) return 0X7ff7ffffffffffffU;
  1451. m = frexp (x, &e) * 0x20000000000000U;
  1452. r = m & 0x8000000000000000;;
  1453. if (r)
  1454. m = -m;
  1455. if (e <= -1022)
  1456. {
  1457. m &= 0x1fffffffffffffU;
  1458. m >>= (-1021 - e);
  1459. e = -1022;
  1460. }
  1461. r |= ((uint64_t)(e + 1022)) << 52;
  1462. r |= m & 0xfffffffffffffU;
  1463. #endif
  1464. return r;
  1465. }
  1466. /* converts an ieee double/binary64 to a double */
  1467. ecb_function_ ecb_const double ecb_binary64_to_double (uint64_t x);
  1468. ecb_function_ ecb_const double
  1469. ecb_binary64_to_double (uint64_t x)
  1470. {
  1471. double r;
  1472. #if ECB_STDFP
  1473. memcpy (&r, &x, 8);
  1474. #else
  1475. /* emulation, only works for normals and subnormals and +0 */
  1476. int neg = x >> 63;
  1477. int e = (x >> 52) & 0x7ffU;
  1478. x &= 0xfffffffffffffU;
  1479. if (e)
  1480. x |= 0x10000000000000U;
  1481. else
  1482. e = 1;
  1483. /* we distrust ldexp a bit and do the 2**-53 scaling by an extra multiply */
  1484. r = ldexp (x * (0.5 / 0x10000000000000U), e - 1022);
  1485. r = neg ? -r : r;
  1486. #endif
  1487. return r;
  1488. }
  1489. /* convert a float to ieee half/binary16 */
  1490. ecb_function_ ecb_const uint16_t ecb_float_to_binary16 (float x);
  1491. ecb_function_ ecb_const uint16_t
  1492. ecb_float_to_binary16 (float x)
  1493. {
  1494. return ecb_binary32_to_binary16 (ecb_float_to_binary32 (x));
  1495. }
  1496. /* convert an ieee half/binary16 to float */
  1497. ecb_function_ ecb_const float ecb_binary16_to_float (uint16_t x);
  1498. ecb_function_ ecb_const float
  1499. ecb_binary16_to_float (uint16_t x)
  1500. {
  1501. return ecb_binary32_to_float (ecb_binary16_to_binary32 (x));
  1502. }
  1503. #endif
  1504. #endif
  1505. /* ECB.H END */
  1506. #if ECB_MEMORY_FENCE_NEEDS_PTHREADS
  1507. /* if your architecture doesn't need memory fences, e.g. because it is
  1508. * single-cpu/core, or if you use libev in a project that doesn't use libev
  1509. * from multiple threads, then you can define ECB_NO_THREADS when compiling
  1510. * libev, in which cases the memory fences become nops.
  1511. * alternatively, you can remove this #error and link against libpthread,
  1512. * which will then provide the memory fences.
  1513. */
  1514. # error "memory fences not defined for your architecture, please report"
  1515. #endif
  1516. #ifndef ECB_MEMORY_FENCE
  1517. # define ECB_MEMORY_FENCE do { } while (0)
  1518. # define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
  1519. # define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
  1520. #endif
  1521. #define inline_size ecb_inline
  1522. #if EV_FEATURE_CODE
  1523. # define inline_speed ecb_inline
  1524. #else
  1525. # define inline_speed ecb_noinline static
  1526. #endif
  1527. /*****************************************************************************/
  1528. /* raw syscall wrappers */
  1529. #if EV_NEED_SYSCALL
  1530. #include <sys/syscall.h>
  1531. /*
  1532. * define some syscall wrappers for common architectures
  1533. * this is mostly for nice looks during debugging, not performance.
  1534. * our syscalls return < 0, not == -1, on error. which is good
  1535. * enough for linux aio.
  1536. * TODO: arm is also common nowadays, maybe even mips and x86
  1537. * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove...
  1538. */
  1539. #if __GNUC__ && __linux && ECB_AMD64 && !EV_FEATURE_CODE
  1540. /* the costly errno access probably kills this for size optimisation */
  1541. #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5,arg6) \
  1542. ({ \
  1543. long res; \
  1544. register unsigned long r6 __asm__ ("r9" ); \
  1545. register unsigned long r5 __asm__ ("r8" ); \
  1546. register unsigned long r4 __asm__ ("r10"); \
  1547. register unsigned long r3 __asm__ ("rdx"); \
  1548. register unsigned long r2 __asm__ ("rsi"); \
  1549. register unsigned long r1 __asm__ ("rdi"); \
  1550. if (narg >= 6) r6 = (unsigned long)(arg6); \
  1551. if (narg >= 5) r5 = (unsigned long)(arg5); \
  1552. if (narg >= 4) r4 = (unsigned long)(arg4); \
  1553. if (narg >= 3) r3 = (unsigned long)(arg3); \
  1554. if (narg >= 2) r2 = (unsigned long)(arg2); \
  1555. if (narg >= 1) r1 = (unsigned long)(arg1); \
  1556. __asm__ __volatile__ ( \
  1557. "syscall\n\t" \
  1558. : "=a" (res) \
  1559. : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \
  1560. : "cc", "r11", "cx", "memory"); \
  1561. errno = -res; \
  1562. res; \
  1563. })
  1564. #endif
  1565. #ifdef ev_syscall
  1566. #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0, 0)
  1567. #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0, 0)
  1568. #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0, 0)
  1569. #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0, 0)
  1570. #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0, 0)
  1571. #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5, 0)
  1572. #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) ev_syscall (nr, 6, arg1, arg2, arg3, arg4, arg5,arg6)
  1573. #else
  1574. #define ev_syscall0(nr) syscall (nr)
  1575. #define ev_syscall1(nr,arg1) syscall (nr, arg1)
  1576. #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2)
  1577. #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3)
  1578. #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4)
  1579. #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5)
  1580. #define ev_syscall6(nr,arg1,arg2,arg3,arg4,arg5,arg6) syscall (nr, arg1, arg2, arg3, arg4, arg5,arg6)
  1581. #endif
  1582. #endif
  1583. /*****************************************************************************/
  1584. #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
  1585. #if EV_MINPRI == EV_MAXPRI
  1586. # define ABSPRI(w) (((W)w), 0)
  1587. #else
  1588. # define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
  1589. #endif
  1590. #define EMPTY /* required for microsofts broken pseudo-c compiler */
  1591. typedef ev_watcher *W;
  1592. typedef ev_watcher_list *WL;
  1593. typedef ev_watcher_time *WT;
  1594. #define ev_active(w) ((W)(w))->active
  1595. #define ev_at(w) ((WT)(w))->at
  1596. #if EV_USE_REALTIME
  1597. /* sig_atomic_t is used to avoid per-thread variables or locking but still */
  1598. /* giving it a reasonably high chance of working on typical architectures */
  1599. static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
  1600. #endif
  1601. #if EV_USE_MONOTONIC
  1602. static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
  1603. #endif
  1604. #ifndef EV_FD_TO_WIN32_HANDLE
  1605. # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
  1606. #endif
  1607. #ifndef EV_WIN32_HANDLE_TO_FD
  1608. # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
  1609. #endif
  1610. #ifndef EV_WIN32_CLOSE_FD
  1611. # define EV_WIN32_CLOSE_FD(fd) close (fd)
  1612. #endif
  1613. #ifdef _WIN32
  1614. # include "ev_win32.c"
  1615. #endif
  1616. /*****************************************************************************/
  1617. #if EV_USE_LINUXAIO
  1618. # include <linux/aio_abi.h> /* probably only needed for aio_context_t */
  1619. #endif
  1620. /* define a suitable floor function (only used by periodics atm) */
  1621. #if EV_USE_FLOOR
  1622. # include <math.h>
  1623. # define ev_floor(v) floor (v)
  1624. #else
  1625. #include <float.h>
  1626. /* a floor() replacement function, should be independent of ev_tstamp type */
  1627. ecb_noinline
  1628. static ev_tstamp
  1629. ev_floor (ev_tstamp v)
  1630. {
  1631. /* the choice of shift factor is not terribly important */
  1632. #if FLT_RADIX != 2 /* assume FLT_RADIX == 10 */
  1633. const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 10000000000000000000. : 1000000000.;
  1634. #else
  1635. const ev_tstamp shift = sizeof (unsigned long) >= 8 ? 18446744073709551616. : 4294967296.;
  1636. #endif
  1637. /* special treatment for negative arguments */
  1638. if (ecb_expect_false (v < 0.))
  1639. {
  1640. ev_tstamp f = -ev_floor (-v);
  1641. return f - (f == v ? 0 : 1);
  1642. }
  1643. /* argument too large for an unsigned long? then reduce it */
  1644. if (ecb_expect_false (v >= shift))
  1645. {
  1646. ev_tstamp f;
  1647. if (v == v - 1.)
  1648. return v; /* very large numbers are assumed to be integer */
  1649. f = shift * ev_floor (v * (1. / shift));
  1650. return f + ev_floor (v - f);
  1651. }
  1652. /* fits into an unsigned long */
  1653. return (unsigned long)v;
  1654. }
  1655. #endif
  1656. /*****************************************************************************/
  1657. #ifdef __linux
  1658. # include <sys/utsname.h>
  1659. #endif
  1660. ecb_noinline ecb_cold
  1661. static unsigned int
  1662. ev_linux_version (void)
  1663. {
  1664. #ifdef __linux
  1665. unsigned int v = 0;
  1666. struct utsname buf;
  1667. int i;
  1668. char *p = buf.release;
  1669. if (uname (&buf))
  1670. return 0;
  1671. for (i = 3+1; --i; )
  1672. {
  1673. unsigned int c = 0;
  1674. for (;;)
  1675. {
  1676. if (*p >= '0' && *p <= '9')
  1677. c = c * 10 + *p++ - '0';
  1678. else
  1679. {
  1680. p += *p == '.';
  1681. break;
  1682. }
  1683. }
  1684. v = (v << 8) | c;
  1685. }
  1686. return v;
  1687. #else
  1688. return 0;
  1689. #endif
  1690. }
  1691. /*****************************************************************************/
  1692. #if EV_AVOID_STDIO
  1693. ecb_noinline ecb_cold
  1694. static void
  1695. ev_printerr (const char *msg)
  1696. {
  1697. write (STDERR_FILENO, msg, strlen (msg));
  1698. }
  1699. #endif
  1700. static void (*syserr_cb)(const char *msg) EV_NOEXCEPT;
  1701. ecb_cold
  1702. void
  1703. ev_set_syserr_cb (void (*cb)(const char *msg) EV_NOEXCEPT) EV_NOEXCEPT
  1704. {
  1705. syserr_cb = cb;
  1706. }
  1707. ecb_noinline ecb_cold
  1708. static void
  1709. ev_syserr (const char *msg)
  1710. {
  1711. if (!msg)
  1712. msg = "(libev) system error";
  1713. if (syserr_cb)
  1714. syserr_cb (msg);
  1715. else
  1716. {
  1717. #if EV_AVOID_STDIO
  1718. ev_printerr (msg);
  1719. ev_printerr (": ");
  1720. ev_printerr (strerror (errno));
  1721. ev_printerr ("\n");
  1722. #else
  1723. perror (msg);
  1724. #endif
  1725. abort ();
  1726. }
  1727. }
  1728. static void *
  1729. ev_realloc_emul (void *ptr, long size) EV_NOEXCEPT
  1730. {
  1731. /* some systems, notably openbsd and darwin, fail to properly
  1732. * implement realloc (x, 0) (as required by both ansi c-89 and
  1733. * the single unix specification, so work around them here.
  1734. * recently, also (at least) fedora and debian started breaking it,
  1735. * despite documenting it otherwise.
  1736. */
  1737. if (size)
  1738. return realloc (ptr, size);
  1739. free (ptr);
  1740. return 0;
  1741. }
  1742. static void *(*alloc)(void *ptr, long size) EV_NOEXCEPT = ev_realloc_emul;
  1743. ecb_cold
  1744. void
  1745. ev_set_allocator (void *(*cb)(void *ptr, long size) EV_NOEXCEPT) EV_NOEXCEPT
  1746. {
  1747. alloc = cb;
  1748. }
  1749. inline_speed void *
  1750. ev_realloc (void *ptr, long size)
  1751. {
  1752. ptr = alloc (ptr, size);
  1753. if (!ptr && size)
  1754. {
  1755. #if EV_AVOID_STDIO
  1756. ev_printerr ("(libev) memory allocation failed, aborting.\n");
  1757. #else
  1758. fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
  1759. #endif
  1760. abort ();
  1761. }
  1762. return ptr;
  1763. }
  1764. #define ev_malloc(size) ev_realloc (0, (size))
  1765. #define ev_free(ptr) ev_realloc ((ptr), 0)
  1766. /*****************************************************************************/
  1767. /* set in reify when reification needed */
  1768. #define EV_ANFD_REIFY 1
  1769. /* file descriptor info structure */
  1770. typedef struct
  1771. {
  1772. WL head;
  1773. unsigned char events; /* the events watched for */
  1774. unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
  1775. unsigned char emask; /* some backends store the actual kernel mask in here */
  1776. unsigned char eflags; /* flags field for use by backends */
  1777. #if EV_USE_EPOLL
  1778. unsigned int egen; /* generation counter to counter epoll bugs */
  1779. #endif
  1780. #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  1781. SOCKET handle;
  1782. #endif
  1783. #if EV_USE_IOCP
  1784. OVERLAPPED or, ow;
  1785. #endif
  1786. } ANFD;
  1787. /* stores the pending event set for a given watcher */
  1788. typedef struct
  1789. {
  1790. W w;
  1791. int events; /* the pending event set for the given watcher */
  1792. } ANPENDING;
  1793. #if EV_USE_INOTIFY
  1794. /* hash table entry per inotify-id */
  1795. typedef struct
  1796. {
  1797. WL head;
  1798. } ANFS;
  1799. #endif
  1800. /* Heap Entry */
  1801. #if EV_HEAP_CACHE_AT
  1802. /* a heap element */
  1803. typedef struct {
  1804. ev_tstamp at;
  1805. WT w;
  1806. } ANHE;
  1807. #define ANHE_w(he) (he).w /* access watcher, read-write */
  1808. #define ANHE_at(he) (he).at /* access cached at, read-only */
  1809. #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
  1810. #else
  1811. /* a heap element */
  1812. typedef WT ANHE;
  1813. #define ANHE_w(he) (he)
  1814. #define ANHE_at(he) (he)->at
  1815. #define ANHE_at_cache(he)
  1816. #endif
  1817. #if EV_MULTIPLICITY
  1818. struct ev_loop
  1819. {
  1820. ev_tstamp ev_rt_now;
  1821. #define ev_rt_now ((loop)->ev_rt_now)
  1822. #define VAR(name,decl) decl;
  1823. #include "ev_vars.h"
  1824. #undef VAR
  1825. };
  1826. #include "ev_wrap.h"
  1827. static struct ev_loop default_loop_struct;
  1828. EV_API_DECL struct ev_loop *ev_default_loop_ptr = 0; /* needs to be initialised to make it a definition despite extern */
  1829. #else
  1830. EV_API_DECL ev_tstamp ev_rt_now = EV_TS_CONST (0.); /* needs to be initialised to make it a definition despite extern */
  1831. #define VAR(name,decl) static decl;
  1832. #include "ev_vars.h"
  1833. #undef VAR
  1834. static int ev_default_loop_ptr;
  1835. #endif
  1836. #if EV_FEATURE_API
  1837. # define EV_RELEASE_CB if (ecb_expect_false (release_cb)) release_cb (EV_A)
  1838. # define EV_ACQUIRE_CB if (ecb_expect_false (acquire_cb)) acquire_cb (EV_A)
  1839. # define EV_INVOKE_PENDING invoke_cb (EV_A)
  1840. #else
  1841. # define EV_RELEASE_CB (void)0
  1842. # define EV_ACQUIRE_CB (void)0
  1843. # define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
  1844. #endif
  1845. #define EVBREAK_RECURSE 0x80
  1846. /*****************************************************************************/
  1847. #ifndef EV_HAVE_EV_TIME
  1848. ev_tstamp
  1849. ev_time (void) EV_NOEXCEPT
  1850. {
  1851. #if EV_USE_REALTIME
  1852. if (ecb_expect_true (have_realtime))
  1853. {
  1854. struct timespec ts;
  1855. clock_gettime (CLOCK_REALTIME, &ts);
  1856. return EV_TS_GET (ts);
  1857. }
  1858. #endif
  1859. {
  1860. struct timeval tv;
  1861. gettimeofday (&tv, 0);
  1862. return EV_TV_GET (tv);
  1863. }
  1864. }
  1865. #endif
  1866. inline_size ev_tstamp
  1867. get_clock (void)
  1868. {
  1869. #if EV_USE_MONOTONIC
  1870. if (ecb_expect_true (have_monotonic))
  1871. {
  1872. struct timespec ts;
  1873. clock_gettime (CLOCK_MONOTONIC, &ts);
  1874. return EV_TS_GET (ts);
  1875. }
  1876. #endif
  1877. return ev_time ();
  1878. }
  1879. #if EV_MULTIPLICITY
  1880. ev_tstamp
  1881. ev_now (EV_P) EV_NOEXCEPT
  1882. {
  1883. return ev_rt_now;
  1884. }
  1885. #endif
  1886. void
  1887. ev_sleep (ev_tstamp delay) EV_NOEXCEPT
  1888. {
  1889. if (delay > EV_TS_CONST (0.))
  1890. {
  1891. #if EV_USE_NANOSLEEP
  1892. struct timespec ts;
  1893. EV_TS_SET (ts, delay);
  1894. nanosleep (&ts, 0);
  1895. #elif defined _WIN32
  1896. /* maybe this should round up, as ms is very low resolution */
  1897. /* compared to select (µs) or nanosleep (ns) */
  1898. Sleep ((unsigned long)(EV_TS_TO_MSEC (delay)));
  1899. #else
  1900. struct timeval tv;
  1901. /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
  1902. /* something not guaranteed by newer posix versions, but guaranteed */
  1903. /* by older ones */
  1904. EV_TV_SET (tv, delay);
  1905. select (0, 0, 0, 0, &tv);
  1906. #endif
  1907. }
  1908. }
  1909. /*****************************************************************************/
  1910. #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
  1911. /* find a suitable new size for the given array, */
  1912. /* hopefully by rounding to a nice-to-malloc size */
  1913. inline_size int
  1914. array_nextsize (int elem, int cur, int cnt)
  1915. {
  1916. int ncur = cur + 1;
  1917. do
  1918. ncur <<= 1;
  1919. while (cnt > ncur);
  1920. /* if size is large, round to MALLOC_ROUND - 4 * longs to accommodate malloc overhead */
  1921. if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
  1922. {
  1923. ncur *= elem;
  1924. ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
  1925. ncur = ncur - sizeof (void *) * 4;
  1926. ncur /= elem;
  1927. }
  1928. return ncur;
  1929. }
  1930. ecb_noinline ecb_cold
  1931. static void *
  1932. array_realloc (int elem, void *base, int *cur, int cnt)
  1933. {
  1934. *cur = array_nextsize (elem, *cur, cnt);
  1935. return ev_realloc (base, elem * *cur);
  1936. }
  1937. #define array_needsize_noinit(base,offset,count)
  1938. #define array_needsize_zerofill(base,offset,count) \
  1939. memset ((void *)(base + offset), 0, sizeof (*(base)) * (count))
  1940. #define array_needsize(type,base,cur,cnt,init) \
  1941. if (ecb_expect_false ((cnt) > (cur))) \
  1942. { \
  1943. ecb_unused int ocur_ = (cur); \
  1944. (base) = (type *)array_realloc \
  1945. (sizeof (type), (base), &(cur), (cnt)); \
  1946. init ((base), ocur_, ((cur) - ocur_)); \
  1947. }
  1948. #if 0
  1949. #define array_slim(type,stem) \
  1950. if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
  1951. { \
  1952. stem ## max = array_roundsize (stem ## cnt >> 1); \
  1953. base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
  1954. fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
  1955. }
  1956. #endif
  1957. #define array_free(stem, idx) \
  1958. ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
  1959. /*****************************************************************************/
  1960. /* dummy callback for pending events */
  1961. ecb_noinline
  1962. static void
  1963. pendingcb (EV_P_ ev_prepare *w, int revents)
  1964. {
  1965. }
  1966. ecb_noinline
  1967. void
  1968. ev_feed_event (EV_P_ void *w, int revents) EV_NOEXCEPT
  1969. {
  1970. W w_ = (W)w;
  1971. int pri = ABSPRI (w_);
  1972. if (ecb_expect_false (w_->pending))
  1973. pendings [pri][w_->pending - 1].events |= revents;
  1974. else
  1975. {
  1976. w_->pending = ++pendingcnt [pri];
  1977. array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, array_needsize_noinit);
  1978. pendings [pri][w_->pending - 1].w = w_;
  1979. pendings [pri][w_->pending - 1].events = revents;
  1980. }
  1981. pendingpri = NUMPRI - 1;
  1982. }
  1983. inline_speed void
  1984. feed_reverse (EV_P_ W w)
  1985. {
  1986. array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, array_needsize_noinit);
  1987. rfeeds [rfeedcnt++] = w;
  1988. }
  1989. inline_size void
  1990. feed_reverse_done (EV_P_ int revents)
  1991. {
  1992. do
  1993. ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
  1994. while (rfeedcnt);
  1995. }
  1996. inline_speed void
  1997. queue_events (EV_P_ W *events, int eventcnt, int type)
  1998. {
  1999. int i;
  2000. for (i = 0; i < eventcnt; ++i)
  2001. ev_feed_event (EV_A_ events [i], type);
  2002. }
  2003. /*****************************************************************************/
  2004. inline_speed void
  2005. fd_event_nocheck (EV_P_ int fd, int revents)
  2006. {
  2007. ANFD *anfd = anfds + fd;
  2008. ev_io *w;
  2009. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  2010. {
  2011. int ev = w->events & revents;
  2012. if (ev)
  2013. ev_feed_event (EV_A_ (W)w, ev);
  2014. }
  2015. }
  2016. /* do not submit kernel events for fds that have reify set */
  2017. /* because that means they changed while we were polling for new events */
  2018. inline_speed void
  2019. fd_event (EV_P_ int fd, int revents)
  2020. {
  2021. ANFD *anfd = anfds + fd;
  2022. if (ecb_expect_true (!anfd->reify))
  2023. fd_event_nocheck (EV_A_ fd, revents);
  2024. }
  2025. void
  2026. ev_feed_fd_event (EV_P_ int fd, int revents) EV_NOEXCEPT
  2027. {
  2028. if (fd >= 0 && fd < anfdmax)
  2029. fd_event_nocheck (EV_A_ fd, revents);
  2030. }
  2031. /* make sure the external fd watch events are in-sync */
  2032. /* with the kernel/libev internal state */
  2033. inline_size void
  2034. fd_reify (EV_P)
  2035. {
  2036. int i;
  2037. /* most backends do not modify the fdchanges list in backend_modfiy.
  2038. * except io_uring, which has fixed-size buffers which might force us
  2039. * to handle events in backend_modify, causing fdchanges to be amended,
  2040. * which could result in an endless loop.
  2041. * to avoid this, we do not dynamically handle fds that were added
  2042. * during fd_reify. that means that for those backends, fdchangecnt
  2043. * might be non-zero during poll, which must cause them to not block.
  2044. * to not put too much of a burden on other backends, this detail
  2045. * needs to be handled in the backend.
  2046. */
  2047. int changecnt = fdchangecnt;
  2048. #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  2049. for (i = 0; i < changecnt; ++i)
  2050. {
  2051. int fd = fdchanges [i];
  2052. ANFD *anfd = anfds + fd;
  2053. if (anfd->reify & EV__IOFDSET && anfd->head)
  2054. {
  2055. SOCKET handle = EV_FD_TO_WIN32_HANDLE (fd);
  2056. if (handle != anfd->handle)
  2057. {
  2058. unsigned long arg;
  2059. assert (("libev: only socket fds supported in this configuration", ioctlsocket (handle, FIONREAD, &arg) == 0));
  2060. /* handle changed, but fd didn't - we need to do it in two steps */
  2061. backend_modify (EV_A_ fd, anfd->events, 0);
  2062. anfd->events = 0;
  2063. anfd->handle = handle;
  2064. }
  2065. }
  2066. }
  2067. #endif
  2068. for (i = 0; i < changecnt; ++i)
  2069. {
  2070. int fd = fdchanges [i];
  2071. ANFD *anfd = anfds + fd;
  2072. ev_io *w;
  2073. unsigned char o_events = anfd->events;
  2074. unsigned char o_reify = anfd->reify;
  2075. anfd->reify = 0;
  2076. /*if (ecb_expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
  2077. {
  2078. anfd->events = 0;
  2079. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  2080. anfd->events |= (unsigned char)w->events;
  2081. if (o_events != anfd->events)
  2082. o_reify = EV__IOFDSET; /* actually |= */
  2083. }
  2084. if (o_reify & EV__IOFDSET)
  2085. backend_modify (EV_A_ fd, o_events, anfd->events);
  2086. }
  2087. /* normally, fdchangecnt hasn't changed. if it has, then new fds have been added.
  2088. * this is a rare case (see beginning comment in this function), so we copy them to the
  2089. * front and hope the backend handles this case.
  2090. */
  2091. if (ecb_expect_false (fdchangecnt != changecnt))
  2092. memmove (fdchanges, fdchanges + changecnt, (fdchangecnt - changecnt) * sizeof (*fdchanges));
  2093. fdchangecnt -= changecnt;
  2094. }
  2095. /* something about the given fd changed */
  2096. inline_size
  2097. void
  2098. fd_change (EV_P_ int fd, int flags)
  2099. {
  2100. unsigned char reify = anfds [fd].reify;
  2101. anfds [fd].reify = reify | flags;
  2102. if (ecb_expect_true (!reify))
  2103. {
  2104. ++fdchangecnt;
  2105. array_needsize (int, fdchanges, fdchangemax, fdchangecnt, array_needsize_noinit);
  2106. fdchanges [fdchangecnt - 1] = fd;
  2107. }
  2108. }
  2109. /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
  2110. inline_speed ecb_cold void
  2111. fd_kill (EV_P_ int fd)
  2112. {
  2113. ev_io *w;
  2114. while ((w = (ev_io *)anfds [fd].head))
  2115. {
  2116. ev_io_stop (EV_A_ w);
  2117. ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
  2118. }
  2119. }
  2120. /* check whether the given fd is actually valid, for error recovery */
  2121. inline_size ecb_cold int
  2122. fd_valid (int fd)
  2123. {
  2124. #ifdef _WIN32
  2125. return EV_FD_TO_WIN32_HANDLE (fd) != -1;
  2126. #else
  2127. return fcntl (fd, F_GETFD) != -1;
  2128. #endif
  2129. }
  2130. /* called on EBADF to verify fds */
  2131. ecb_noinline ecb_cold
  2132. static void
  2133. fd_ebadf (EV_P)
  2134. {
  2135. int fd;
  2136. for (fd = 0; fd < anfdmax; ++fd)
  2137. if (anfds [fd].events)
  2138. if (!fd_valid (fd) && errno == EBADF)
  2139. fd_kill (EV_A_ fd);
  2140. }
  2141. /* called on ENOMEM in select/poll to kill some fds and retry */
  2142. ecb_noinline ecb_cold
  2143. static void
  2144. fd_enomem (EV_P)
  2145. {
  2146. int fd;
  2147. for (fd = anfdmax; fd--; )
  2148. if (anfds [fd].events)
  2149. {
  2150. fd_kill (EV_A_ fd);
  2151. break;
  2152. }
  2153. }
  2154. /* usually called after fork if backend needs to re-arm all fds from scratch */
  2155. ecb_noinline
  2156. static void
  2157. fd_rearm_all (EV_P)
  2158. {
  2159. int fd;
  2160. for (fd = 0; fd < anfdmax; ++fd)
  2161. if (anfds [fd].events)
  2162. {
  2163. anfds [fd].events = 0;
  2164. anfds [fd].emask = 0;
  2165. fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
  2166. }
  2167. }
  2168. /* used to prepare libev internal fd's */
  2169. /* this is not fork-safe */
  2170. inline_speed void
  2171. fd_intern (int fd)
  2172. {
  2173. #ifdef _WIN32
  2174. unsigned long arg = 1;
  2175. ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
  2176. #else
  2177. fcntl (fd, F_SETFD, FD_CLOEXEC);
  2178. fcntl (fd, F_SETFL, O_NONBLOCK);
  2179. #endif
  2180. }
  2181. /*****************************************************************************/
  2182. /*
  2183. * the heap functions want a real array index. array index 0 is guaranteed to not
  2184. * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
  2185. * the branching factor of the d-tree.
  2186. */
  2187. /*
  2188. * at the moment we allow libev the luxury of two heaps,
  2189. * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
  2190. * which is more cache-efficient.
  2191. * the difference is about 5% with 50000+ watchers.
  2192. */
  2193. #if EV_USE_4HEAP
  2194. #define DHEAP 4
  2195. #define HEAP0 (DHEAP - 1) /* index of first element in heap */
  2196. #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
  2197. #define UPHEAP_DONE(p,k) ((p) == (k))
  2198. /* away from the root */
  2199. inline_speed void
  2200. downheap (ANHE *heap, int N, int k)
  2201. {
  2202. ANHE he = heap [k];
  2203. ANHE *E = heap + N + HEAP0;
  2204. for (;;)
  2205. {
  2206. ev_tstamp minat;
  2207. ANHE *minpos;
  2208. ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
  2209. /* find minimum child */
  2210. if (ecb_expect_true (pos + DHEAP - 1 < E))
  2211. {
  2212. /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
  2213. if ( minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos));
  2214. if ( minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos));
  2215. if ( minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos));
  2216. }
  2217. else if (pos < E)
  2218. {
  2219. /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
  2220. if (pos + 1 < E && minat > ANHE_at (pos [1])) (minpos = pos + 1), (minat = ANHE_at (*minpos));
  2221. if (pos + 2 < E && minat > ANHE_at (pos [2])) (minpos = pos + 2), (minat = ANHE_at (*minpos));
  2222. if (pos + 3 < E && minat > ANHE_at (pos [3])) (minpos = pos + 3), (minat = ANHE_at (*minpos));
  2223. }
  2224. else
  2225. break;
  2226. if (ANHE_at (he) <= minat)
  2227. break;
  2228. heap [k] = *minpos;
  2229. ev_active (ANHE_w (*minpos)) = k;
  2230. k = minpos - heap;
  2231. }
  2232. heap [k] = he;
  2233. ev_active (ANHE_w (he)) = k;
  2234. }
  2235. #else /* not 4HEAP */
  2236. #define HEAP0 1
  2237. #define HPARENT(k) ((k) >> 1)
  2238. #define UPHEAP_DONE(p,k) (!(p))
  2239. /* away from the root */
  2240. inline_speed void
  2241. downheap (ANHE *heap, int N, int k)
  2242. {
  2243. ANHE he = heap [k];
  2244. for (;;)
  2245. {
  2246. int c = k << 1;
  2247. if (c >= N + HEAP0)
  2248. break;
  2249. c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
  2250. ? 1 : 0;
  2251. if (ANHE_at (he) <= ANHE_at (heap [c]))
  2252. break;
  2253. heap [k] = heap [c];
  2254. ev_active (ANHE_w (heap [k])) = k;
  2255. k = c;
  2256. }
  2257. heap [k] = he;
  2258. ev_active (ANHE_w (he)) = k;
  2259. }
  2260. #endif
  2261. /* towards the root */
  2262. inline_speed void
  2263. upheap (ANHE *heap, int k)
  2264. {
  2265. ANHE he = heap [k];
  2266. for (;;)
  2267. {
  2268. int p = HPARENT (k);
  2269. if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he))
  2270. break;
  2271. heap [k] = heap [p];
  2272. ev_active (ANHE_w (heap [k])) = k;
  2273. k = p;
  2274. }
  2275. heap [k] = he;
  2276. ev_active (ANHE_w (he)) = k;
  2277. }
  2278. /* move an element suitably so it is in a correct place */
  2279. inline_size void
  2280. adjustheap (ANHE *heap, int N, int k)
  2281. {
  2282. if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
  2283. upheap (heap, k);
  2284. else
  2285. downheap (heap, N, k);
  2286. }
  2287. /* rebuild the heap: this function is used only once and executed rarely */
  2288. inline_size void
  2289. reheap (ANHE *heap, int N)
  2290. {
  2291. int i;
  2292. /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
  2293. /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
  2294. for (i = 0; i < N; ++i)
  2295. upheap (heap, i + HEAP0);
  2296. }
  2297. /*****************************************************************************/
  2298. /* associate signal watchers to a signal */
  2299. typedef struct
  2300. {
  2301. EV_ATOMIC_T pending;
  2302. #if EV_MULTIPLICITY
  2303. EV_P;
  2304. #endif
  2305. WL head;
  2306. } ANSIG;
  2307. static ANSIG signals [EV_NSIG - 1];
  2308. /*****************************************************************************/
  2309. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  2310. ecb_noinline ecb_cold
  2311. static void
  2312. evpipe_init (EV_P)
  2313. {
  2314. if (!ev_is_active (&pipe_w))
  2315. {
  2316. int fds [2];
  2317. # if EV_USE_EVENTFD
  2318. fds [0] = -1;
  2319. fds [1] = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
  2320. if (fds [1] < 0 && errno == EINVAL)
  2321. fds [1] = eventfd (0, 0);
  2322. if (fds [1] < 0)
  2323. # endif
  2324. {
  2325. while (pipe (fds))
  2326. ev_syserr ("(libev) error creating signal/async pipe");
  2327. fd_intern (fds [0]);
  2328. }
  2329. evpipe [0] = fds [0];
  2330. if (evpipe [1] < 0)
  2331. evpipe [1] = fds [1]; /* first call, set write fd */
  2332. else
  2333. {
  2334. /* on subsequent calls, do not change evpipe [1] */
  2335. /* so that evpipe_write can always rely on its value. */
  2336. /* this branch does not do anything sensible on windows, */
  2337. /* so must not be executed on windows */
  2338. dup2 (fds [1], evpipe [1]);
  2339. close (fds [1]);
  2340. }
  2341. fd_intern (evpipe [1]);
  2342. ev_io_set (&pipe_w, evpipe [0] < 0 ? evpipe [1] : evpipe [0], EV_READ);
  2343. ev_io_start (EV_A_ &pipe_w);
  2344. ev_unref (EV_A); /* watcher should not keep loop alive */
  2345. }
  2346. }
  2347. inline_speed void
  2348. evpipe_write (EV_P_ EV_ATOMIC_T *flag)
  2349. {
  2350. ECB_MEMORY_FENCE; /* push out the write before this function was called, acquire flag */
  2351. if (ecb_expect_true (*flag))
  2352. return;
  2353. *flag = 1;
  2354. ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
  2355. pipe_write_skipped = 1;
  2356. ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
  2357. if (pipe_write_wanted)
  2358. {
  2359. int old_errno;
  2360. pipe_write_skipped = 0;
  2361. ECB_MEMORY_FENCE_RELEASE;
  2362. old_errno = errno; /* save errno because write will clobber it */
  2363. #if EV_USE_EVENTFD
  2364. if (evpipe [0] < 0)
  2365. {
  2366. uint64_t counter = 1;
  2367. write (evpipe [1], &counter, sizeof (uint64_t));
  2368. }
  2369. else
  2370. #endif
  2371. {
  2372. #ifdef _WIN32
  2373. WSABUF buf;
  2374. DWORD sent;
  2375. buf.buf = (char *)&buf;
  2376. buf.len = 1;
  2377. WSASend (EV_FD_TO_WIN32_HANDLE (evpipe [1]), &buf, 1, &sent, 0, 0, 0);
  2378. #else
  2379. write (evpipe [1], &(evpipe [1]), 1);
  2380. #endif
  2381. }
  2382. errno = old_errno;
  2383. }
  2384. }
  2385. /* called whenever the libev signal pipe */
  2386. /* got some events (signal, async) */
  2387. static void
  2388. pipecb (EV_P_ ev_io *iow, int revents)
  2389. {
  2390. int i;
  2391. if (revents & EV_READ)
  2392. {
  2393. #if EV_USE_EVENTFD
  2394. if (evpipe [0] < 0)
  2395. {
  2396. uint64_t counter;
  2397. read (evpipe [1], &counter, sizeof (uint64_t));
  2398. }
  2399. else
  2400. #endif
  2401. {
  2402. char dummy[4];
  2403. #ifdef _WIN32
  2404. WSABUF buf;
  2405. DWORD recvd;
  2406. DWORD flags = 0;
  2407. buf.buf = dummy;
  2408. buf.len = sizeof (dummy);
  2409. WSARecv (EV_FD_TO_WIN32_HANDLE (evpipe [0]), &buf, 1, &recvd, &flags, 0, 0);
  2410. #else
  2411. read (evpipe [0], &dummy, sizeof (dummy));
  2412. #endif
  2413. }
  2414. }
  2415. pipe_write_skipped = 0;
  2416. ECB_MEMORY_FENCE; /* push out skipped, acquire flags */
  2417. #if EV_SIGNAL_ENABLE
  2418. if (sig_pending)
  2419. {
  2420. sig_pending = 0;
  2421. ECB_MEMORY_FENCE;
  2422. for (i = EV_NSIG - 1; i--; )
  2423. if (ecb_expect_false (signals [i].pending))
  2424. ev_feed_signal_event (EV_A_ i + 1);
  2425. }
  2426. #endif
  2427. #if EV_ASYNC_ENABLE
  2428. if (async_pending)
  2429. {
  2430. async_pending = 0;
  2431. ECB_MEMORY_FENCE;
  2432. for (i = asynccnt; i--; )
  2433. if (asyncs [i]->sent)
  2434. {
  2435. asyncs [i]->sent = 0;
  2436. ECB_MEMORY_FENCE_RELEASE;
  2437. ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
  2438. }
  2439. }
  2440. #endif
  2441. }
  2442. /*****************************************************************************/
  2443. void
  2444. ev_feed_signal (int signum) EV_NOEXCEPT
  2445. {
  2446. #if EV_MULTIPLICITY
  2447. EV_P;
  2448. ECB_MEMORY_FENCE_ACQUIRE;
  2449. EV_A = signals [signum - 1].loop;
  2450. if (!EV_A)
  2451. return;
  2452. #endif
  2453. signals [signum - 1].pending = 1;
  2454. evpipe_write (EV_A_ &sig_pending);
  2455. }
  2456. static void
  2457. ev_sighandler (int signum)
  2458. {
  2459. #ifdef _WIN32
  2460. signal (signum, ev_sighandler);
  2461. #endif
  2462. ev_feed_signal (signum);
  2463. }
  2464. ecb_noinline
  2465. void
  2466. ev_feed_signal_event (EV_P_ int signum) EV_NOEXCEPT
  2467. {
  2468. WL w;
  2469. if (ecb_expect_false (signum <= 0 || signum >= EV_NSIG))
  2470. return;
  2471. --signum;
  2472. #if EV_MULTIPLICITY
  2473. /* it is permissible to try to feed a signal to the wrong loop */
  2474. /* or, likely more useful, feeding a signal nobody is waiting for */
  2475. if (ecb_expect_false (signals [signum].loop != EV_A))
  2476. return;
  2477. #endif
  2478. signals [signum].pending = 0;
  2479. ECB_MEMORY_FENCE_RELEASE;
  2480. for (w = signals [signum].head; w; w = w->next)
  2481. ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
  2482. }
  2483. #if EV_USE_SIGNALFD
  2484. static void
  2485. sigfdcb (EV_P_ ev_io *iow, int revents)
  2486. {
  2487. struct signalfd_siginfo si[2], *sip; /* these structs are big */
  2488. for (;;)
  2489. {
  2490. ssize_t res = read (sigfd, si, sizeof (si));
  2491. /* not ISO-C, as res might be -1, but works with SuS */
  2492. for (sip = si; (char *)sip < (char *)si + res; ++sip)
  2493. ev_feed_signal_event (EV_A_ sip->ssi_signo);
  2494. if (res < (ssize_t)sizeof (si))
  2495. break;
  2496. }
  2497. }
  2498. #endif
  2499. #endif
  2500. /*****************************************************************************/
  2501. #if EV_CHILD_ENABLE
  2502. static WL childs [EV_PID_HASHSIZE];
  2503. static ev_signal childev;
  2504. #ifndef WIFCONTINUED
  2505. # define WIFCONTINUED(status) 0
  2506. #endif
  2507. /* handle a single child status event */
  2508. inline_speed void
  2509. child_reap (EV_P_ int chain, int pid, int status)
  2510. {
  2511. ev_child *w;
  2512. int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
  2513. for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
  2514. {
  2515. if ((w->pid == pid || !w->pid)
  2516. && (!traced || (w->flags & 1)))
  2517. {
  2518. ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
  2519. w->rpid = pid;
  2520. w->rstatus = status;
  2521. ev_feed_event (EV_A_ (W)w, EV_CHILD);
  2522. }
  2523. }
  2524. }
  2525. #ifndef WCONTINUED
  2526. # define WCONTINUED 0
  2527. #endif
  2528. /* called on sigchld etc., calls waitpid */
  2529. static void
  2530. childcb (EV_P_ ev_signal *sw, int revents)
  2531. {
  2532. int pid, status;
  2533. /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
  2534. if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
  2535. if (!WCONTINUED
  2536. || errno != EINVAL
  2537. || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
  2538. return;
  2539. /* make sure we are called again until all children have been reaped */
  2540. /* we need to do it this way so that the callback gets called before we continue */
  2541. ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
  2542. child_reap (EV_A_ pid, pid, status);
  2543. if ((EV_PID_HASHSIZE) > 1)
  2544. child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
  2545. }
  2546. #endif
  2547. /*****************************************************************************/
  2548. #if EV_USE_TIMERFD
  2549. static void periodics_reschedule (EV_P);
  2550. static void
  2551. timerfdcb (EV_P_ ev_io *iow, int revents)
  2552. {
  2553. struct itimerspec its = { 0 };
  2554. its.it_value.tv_sec = ev_rt_now + (int)MAX_BLOCKTIME2;
  2555. timerfd_settime (timerfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &its, 0);
  2556. ev_rt_now = ev_time ();
  2557. /* periodics_reschedule only needs ev_rt_now */
  2558. /* but maybe in the future we want the full treatment. */
  2559. /*
  2560. now_floor = EV_TS_CONST (0.);
  2561. time_update (EV_A_ EV_TSTAMP_HUGE);
  2562. */
  2563. #if EV_PERIODIC_ENABLE
  2564. periodics_reschedule (EV_A);
  2565. #endif
  2566. }
  2567. ecb_noinline ecb_cold
  2568. static void
  2569. evtimerfd_init (EV_P)
  2570. {
  2571. if (!ev_is_active (&timerfd_w))
  2572. {
  2573. timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
  2574. if (timerfd >= 0)
  2575. {
  2576. fd_intern (timerfd); /* just to be sure */
  2577. ev_io_init (&timerfd_w, timerfdcb, timerfd, EV_READ);
  2578. ev_set_priority (&timerfd_w, EV_MINPRI);
  2579. ev_io_start (EV_A_ &timerfd_w);
  2580. ev_unref (EV_A); /* watcher should not keep loop alive */
  2581. /* (re-) arm timer */
  2582. timerfdcb (EV_A_ 0, 0);
  2583. }
  2584. }
  2585. }
  2586. #endif
  2587. /*****************************************************************************/
  2588. #if EV_USE_IOCP
  2589. # include "ev_iocp.c"
  2590. #endif
  2591. #if EV_USE_PORT
  2592. # include "ev_port.c"
  2593. #endif
  2594. #if EV_USE_KQUEUE
  2595. # include "ev_kqueue.c"
  2596. #endif
  2597. #if EV_USE_EPOLL
  2598. # include "ev_epoll.c"
  2599. #endif
  2600. #if EV_USE_LINUXAIO
  2601. # include "ev_linuxaio.c"
  2602. #endif
  2603. #if EV_USE_IOURING
  2604. # include "ev_iouring.c"
  2605. #endif
  2606. #if EV_USE_POLL
  2607. # include "ev_poll.c"
  2608. #endif
  2609. #if EV_USE_SELECT
  2610. # include "ev_select.c"
  2611. #endif
  2612. ecb_cold int
  2613. ev_version_major (void) EV_NOEXCEPT
  2614. {
  2615. return EV_VERSION_MAJOR;
  2616. }
  2617. ecb_cold int
  2618. ev_version_minor (void) EV_NOEXCEPT
  2619. {
  2620. return EV_VERSION_MINOR;
  2621. }
  2622. /* return true if we are running with elevated privileges and should ignore env variables */
  2623. inline_size ecb_cold int
  2624. enable_secure (void)
  2625. {
  2626. #ifdef _WIN32
  2627. return 0;
  2628. #else
  2629. return getuid () != geteuid ()
  2630. || getgid () != getegid ();
  2631. #endif
  2632. }
  2633. ecb_cold
  2634. unsigned int
  2635. ev_supported_backends (void) EV_NOEXCEPT
  2636. {
  2637. unsigned int flags = 0;
  2638. if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
  2639. if (EV_USE_KQUEUE ) flags |= EVBACKEND_KQUEUE;
  2640. if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
  2641. if (EV_USE_LINUXAIO ) flags |= EVBACKEND_LINUXAIO;
  2642. if (EV_USE_IOURING && ev_linux_version () >= 0x050601) flags |= EVBACKEND_IOURING; /* 5.6.1+ */
  2643. if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
  2644. if (EV_USE_SELECT ) flags |= EVBACKEND_SELECT;
  2645. return flags;
  2646. }
  2647. ecb_cold
  2648. unsigned int
  2649. ev_recommended_backends (void) EV_NOEXCEPT
  2650. {
  2651. unsigned int flags = ev_supported_backends ();
  2652. #ifndef __NetBSD__
  2653. /* kqueue is borked on everything but netbsd apparently */
  2654. /* it usually doesn't work correctly on anything but sockets and pipes */
  2655. flags &= ~EVBACKEND_KQUEUE;
  2656. #endif
  2657. #ifdef __APPLE__
  2658. /* only select works correctly on that "unix-certified" platform */
  2659. flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
  2660. flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
  2661. #endif
  2662. #ifdef __FreeBSD__
  2663. flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
  2664. #endif
  2665. /* TODO: linuxaio is very experimental */
  2666. #if !EV_RECOMMEND_LINUXAIO
  2667. flags &= ~EVBACKEND_LINUXAIO;
  2668. #endif
  2669. /* TODO: iouring is super experimental */
  2670. #if !EV_RECOMMEND_IOURING
  2671. flags &= ~EVBACKEND_IOURING;
  2672. #endif
  2673. return flags;
  2674. }
  2675. ecb_cold
  2676. unsigned int
  2677. ev_embeddable_backends (void) EV_NOEXCEPT
  2678. {
  2679. int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT | EVBACKEND_IOURING;
  2680. /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
  2681. if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
  2682. flags &= ~EVBACKEND_EPOLL;
  2683. /* EVBACKEND_LINUXAIO is theoretically embeddable, but suffers from a performance overhead */
  2684. return flags;
  2685. }
  2686. unsigned int
  2687. ev_backend (EV_P) EV_NOEXCEPT
  2688. {
  2689. return backend;
  2690. }
  2691. #if EV_FEATURE_API
  2692. unsigned int
  2693. ev_iteration (EV_P) EV_NOEXCEPT
  2694. {
  2695. return loop_count;
  2696. }
  2697. unsigned int
  2698. ev_depth (EV_P) EV_NOEXCEPT
  2699. {
  2700. return loop_depth;
  2701. }
  2702. void
  2703. ev_set_io_collect_interval (EV_P_ ev_tstamp interval) EV_NOEXCEPT
  2704. {
  2705. io_blocktime = interval;
  2706. }
  2707. void
  2708. ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_NOEXCEPT
  2709. {
  2710. timeout_blocktime = interval;
  2711. }
  2712. void
  2713. ev_set_userdata (EV_P_ void *data) EV_NOEXCEPT
  2714. {
  2715. userdata = data;
  2716. }
  2717. void *
  2718. ev_userdata (EV_P) EV_NOEXCEPT
  2719. {
  2720. return userdata;
  2721. }
  2722. void
  2723. ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_NOEXCEPT
  2724. {
  2725. invoke_cb = invoke_pending_cb;
  2726. }
  2727. void
  2728. ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_NOEXCEPT, void (*acquire)(EV_P) EV_NOEXCEPT) EV_NOEXCEPT
  2729. {
  2730. release_cb = release;
  2731. acquire_cb = acquire;
  2732. }
  2733. #endif
  2734. /* initialise a loop structure, must be zero-initialised */
  2735. ecb_noinline ecb_cold
  2736. static void
  2737. loop_init (EV_P_ unsigned int flags) EV_NOEXCEPT
  2738. {
  2739. if (!backend)
  2740. {
  2741. origflags = flags;
  2742. #if EV_USE_REALTIME
  2743. if (!have_realtime)
  2744. {
  2745. struct timespec ts;
  2746. if (!clock_gettime (CLOCK_REALTIME, &ts))
  2747. have_realtime = 1;
  2748. }
  2749. #endif
  2750. #if EV_USE_MONOTONIC
  2751. if (!have_monotonic)
  2752. {
  2753. struct timespec ts;
  2754. if (!clock_gettime (CLOCK_MONOTONIC, &ts))
  2755. have_monotonic = 1;
  2756. }
  2757. #endif
  2758. /* pid check not overridable via env */
  2759. #ifndef _WIN32
  2760. if (flags & EVFLAG_FORKCHECK)
  2761. curpid = getpid ();
  2762. #endif
  2763. if (!(flags & EVFLAG_NOENV)
  2764. && !enable_secure ()
  2765. && getenv ("LIBEV_FLAGS"))
  2766. flags = atoi (getenv ("LIBEV_FLAGS"));
  2767. ev_rt_now = ev_time ();
  2768. mn_now = get_clock ();
  2769. now_floor = mn_now;
  2770. rtmn_diff = ev_rt_now - mn_now;
  2771. #if EV_FEATURE_API
  2772. invoke_cb = ev_invoke_pending;
  2773. #endif
  2774. io_blocktime = 0.;
  2775. timeout_blocktime = 0.;
  2776. backend = 0;
  2777. backend_fd = -1;
  2778. sig_pending = 0;
  2779. #if EV_ASYNC_ENABLE
  2780. async_pending = 0;
  2781. #endif
  2782. pipe_write_skipped = 0;
  2783. pipe_write_wanted = 0;
  2784. evpipe [0] = -1;
  2785. evpipe [1] = -1;
  2786. #if EV_USE_INOTIFY
  2787. fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
  2788. #endif
  2789. #if EV_USE_SIGNALFD
  2790. sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
  2791. #endif
  2792. #if EV_USE_TIMERFD
  2793. timerfd = flags & EVFLAG_NOTIMERFD ? -1 : -2;
  2794. #endif
  2795. if (!(flags & EVBACKEND_MASK))
  2796. flags |= ev_recommended_backends ();
  2797. #if EV_USE_IOCP
  2798. if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
  2799. #endif
  2800. #if EV_USE_PORT
  2801. if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
  2802. #endif
  2803. #if EV_USE_KQUEUE
  2804. if (!backend && (flags & EVBACKEND_KQUEUE )) backend = kqueue_init (EV_A_ flags);
  2805. #endif
  2806. #if EV_USE_IOURING
  2807. if (!backend && (flags & EVBACKEND_IOURING )) backend = iouring_init (EV_A_ flags);
  2808. #endif
  2809. #if EV_USE_LINUXAIO
  2810. if (!backend && (flags & EVBACKEND_LINUXAIO)) backend = linuxaio_init (EV_A_ flags);
  2811. #endif
  2812. #if EV_USE_EPOLL
  2813. if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
  2814. #endif
  2815. #if EV_USE_POLL
  2816. if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
  2817. #endif
  2818. #if EV_USE_SELECT
  2819. if (!backend && (flags & EVBACKEND_SELECT )) backend = select_init (EV_A_ flags);
  2820. #endif
  2821. ev_prepare_init (&pending_w, pendingcb);
  2822. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  2823. ev_init (&pipe_w, pipecb);
  2824. ev_set_priority (&pipe_w, EV_MAXPRI);
  2825. #endif
  2826. }
  2827. }
  2828. /* free up a loop structure */
  2829. ecb_cold
  2830. void
  2831. ev_loop_destroy (EV_P)
  2832. {
  2833. int i;
  2834. #if EV_MULTIPLICITY
  2835. /* mimic free (0) */
  2836. if (!EV_A)
  2837. return;
  2838. #endif
  2839. #if EV_CLEANUP_ENABLE
  2840. /* queue cleanup watchers (and execute them) */
  2841. if (ecb_expect_false (cleanupcnt))
  2842. {
  2843. queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
  2844. EV_INVOKE_PENDING;
  2845. }
  2846. #endif
  2847. #if EV_CHILD_ENABLE
  2848. if (ev_is_default_loop (EV_A) && ev_is_active (&childev))
  2849. {
  2850. ev_ref (EV_A); /* child watcher */
  2851. ev_signal_stop (EV_A_ &childev);
  2852. }
  2853. #endif
  2854. if (ev_is_active (&pipe_w))
  2855. {
  2856. /*ev_ref (EV_A);*/
  2857. /*ev_io_stop (EV_A_ &pipe_w);*/
  2858. if (evpipe [0] >= 0) EV_WIN32_CLOSE_FD (evpipe [0]);
  2859. if (evpipe [1] >= 0) EV_WIN32_CLOSE_FD (evpipe [1]);
  2860. }
  2861. #if EV_USE_SIGNALFD
  2862. if (ev_is_active (&sigfd_w))
  2863. close (sigfd);
  2864. #endif
  2865. #if EV_USE_TIMERFD
  2866. if (ev_is_active (&timerfd_w))
  2867. close (timerfd);
  2868. #endif
  2869. #if EV_USE_INOTIFY
  2870. if (fs_fd >= 0)
  2871. close (fs_fd);
  2872. #endif
  2873. if (backend_fd >= 0)
  2874. close (backend_fd);
  2875. #if EV_USE_IOCP
  2876. if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
  2877. #endif
  2878. #if EV_USE_PORT
  2879. if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
  2880. #endif
  2881. #if EV_USE_KQUEUE
  2882. if (backend == EVBACKEND_KQUEUE ) kqueue_destroy (EV_A);
  2883. #endif
  2884. #if EV_USE_IOURING
  2885. if (backend == EVBACKEND_IOURING ) iouring_destroy (EV_A);
  2886. #endif
  2887. #if EV_USE_LINUXAIO
  2888. if (backend == EVBACKEND_LINUXAIO) linuxaio_destroy (EV_A);
  2889. #endif
  2890. #if EV_USE_EPOLL
  2891. if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
  2892. #endif
  2893. #if EV_USE_POLL
  2894. if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
  2895. #endif
  2896. #if EV_USE_SELECT
  2897. if (backend == EVBACKEND_SELECT ) select_destroy (EV_A);
  2898. #endif
  2899. for (i = NUMPRI; i--; )
  2900. {
  2901. array_free (pending, [i]);
  2902. #if EV_IDLE_ENABLE
  2903. array_free (idle, [i]);
  2904. #endif
  2905. }
  2906. ev_free (anfds); anfds = 0; anfdmax = 0;
  2907. /* have to use the microsoft-never-gets-it-right macro */
  2908. array_free (rfeed, EMPTY);
  2909. array_free (fdchange, EMPTY);
  2910. array_free (timer, EMPTY);
  2911. #if EV_PERIODIC_ENABLE
  2912. array_free (periodic, EMPTY);
  2913. #endif
  2914. #if EV_FORK_ENABLE
  2915. array_free (fork, EMPTY);
  2916. #endif
  2917. #if EV_CLEANUP_ENABLE
  2918. array_free (cleanup, EMPTY);
  2919. #endif
  2920. array_free (prepare, EMPTY);
  2921. array_free (check, EMPTY);
  2922. #if EV_ASYNC_ENABLE
  2923. array_free (async, EMPTY);
  2924. #endif
  2925. backend = 0;
  2926. #if EV_MULTIPLICITY
  2927. if (ev_is_default_loop (EV_A))
  2928. #endif
  2929. ev_default_loop_ptr = 0;
  2930. #if EV_MULTIPLICITY
  2931. else
  2932. ev_free (EV_A);
  2933. #endif
  2934. }
  2935. #if EV_USE_INOTIFY
  2936. inline_size void infy_fork (EV_P);
  2937. #endif
  2938. inline_size void
  2939. loop_fork (EV_P)
  2940. {
  2941. #if EV_USE_PORT
  2942. if (backend == EVBACKEND_PORT ) port_fork (EV_A);
  2943. #endif
  2944. #if EV_USE_KQUEUE
  2945. if (backend == EVBACKEND_KQUEUE ) kqueue_fork (EV_A);
  2946. #endif
  2947. #if EV_USE_IOURING
  2948. if (backend == EVBACKEND_IOURING ) iouring_fork (EV_A);
  2949. #endif
  2950. #if EV_USE_LINUXAIO
  2951. if (backend == EVBACKEND_LINUXAIO) linuxaio_fork (EV_A);
  2952. #endif
  2953. #if EV_USE_EPOLL
  2954. if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
  2955. #endif
  2956. #if EV_USE_INOTIFY
  2957. infy_fork (EV_A);
  2958. #endif
  2959. if (postfork != 2)
  2960. {
  2961. #if EV_USE_SIGNALFD
  2962. /* surprisingly, nothing needs to be done for signalfd, accoridng to docs, it does the right thing on fork */
  2963. #endif
  2964. #if EV_USE_TIMERFD
  2965. if (ev_is_active (&timerfd_w))
  2966. {
  2967. ev_ref (EV_A);
  2968. ev_io_stop (EV_A_ &timerfd_w);
  2969. close (timerfd);
  2970. timerfd = -2;
  2971. evtimerfd_init (EV_A);
  2972. /* reschedule periodics, in case we missed something */
  2973. ev_feed_event (EV_A_ &timerfd_w, EV_CUSTOM);
  2974. }
  2975. #endif
  2976. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  2977. if (ev_is_active (&pipe_w))
  2978. {
  2979. /* pipe_write_wanted must be false now, so modifying fd vars should be safe */
  2980. ev_ref (EV_A);
  2981. ev_io_stop (EV_A_ &pipe_w);
  2982. if (evpipe [0] >= 0)
  2983. EV_WIN32_CLOSE_FD (evpipe [0]);
  2984. evpipe_init (EV_A);
  2985. /* iterate over everything, in case we missed something before */
  2986. ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
  2987. }
  2988. #endif
  2989. }
  2990. postfork = 0;
  2991. }
  2992. #if EV_MULTIPLICITY
  2993. ecb_cold
  2994. struct ev_loop *
  2995. ev_loop_new (unsigned int flags) EV_NOEXCEPT
  2996. {
  2997. EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
  2998. memset (EV_A, 0, sizeof (struct ev_loop));
  2999. loop_init (EV_A_ flags);
  3000. if (ev_backend (EV_A))
  3001. return EV_A;
  3002. ev_free (EV_A);
  3003. return 0;
  3004. }
  3005. #endif /* multiplicity */
  3006. #if EV_VERIFY
  3007. ecb_noinline ecb_cold
  3008. static void
  3009. verify_watcher (EV_P_ W w)
  3010. {
  3011. assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
  3012. if (w->pending)
  3013. assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
  3014. }
  3015. ecb_noinline ecb_cold
  3016. static void
  3017. verify_heap (EV_P_ ANHE *heap, int N)
  3018. {
  3019. int i;
  3020. for (i = HEAP0; i < N + HEAP0; ++i)
  3021. {
  3022. assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
  3023. assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
  3024. assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
  3025. verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
  3026. }
  3027. }
  3028. ecb_noinline ecb_cold
  3029. static void
  3030. array_verify (EV_P_ W *ws, int cnt)
  3031. {
  3032. while (cnt--)
  3033. {
  3034. assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
  3035. verify_watcher (EV_A_ ws [cnt]);
  3036. }
  3037. }
  3038. #endif
  3039. #if EV_FEATURE_API
  3040. void ecb_cold
  3041. ev_verify (EV_P) EV_NOEXCEPT
  3042. {
  3043. #if EV_VERIFY
  3044. int i;
  3045. WL w, w2;
  3046. assert (activecnt >= -1);
  3047. assert (fdchangemax >= fdchangecnt);
  3048. for (i = 0; i < fdchangecnt; ++i)
  3049. assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
  3050. assert (anfdmax >= 0);
  3051. for (i = 0; i < anfdmax; ++i)
  3052. {
  3053. int j = 0;
  3054. for (w = w2 = anfds [i].head; w; w = w->next)
  3055. {
  3056. verify_watcher (EV_A_ (W)w);
  3057. if (j++ & 1)
  3058. {
  3059. assert (("libev: io watcher list contains a loop", w != w2));
  3060. w2 = w2->next;
  3061. }
  3062. assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
  3063. assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
  3064. }
  3065. }
  3066. assert (timermax >= timercnt);
  3067. verify_heap (EV_A_ timers, timercnt);
  3068. #if EV_PERIODIC_ENABLE
  3069. assert (periodicmax >= periodiccnt);
  3070. verify_heap (EV_A_ periodics, periodiccnt);
  3071. #endif
  3072. for (i = NUMPRI; i--; )
  3073. {
  3074. assert (pendingmax [i] >= pendingcnt [i]);
  3075. #if EV_IDLE_ENABLE
  3076. assert (idleall >= 0);
  3077. assert (idlemax [i] >= idlecnt [i]);
  3078. array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
  3079. #endif
  3080. }
  3081. #if EV_FORK_ENABLE
  3082. assert (forkmax >= forkcnt);
  3083. array_verify (EV_A_ (W *)forks, forkcnt);
  3084. #endif
  3085. #if EV_CLEANUP_ENABLE
  3086. assert (cleanupmax >= cleanupcnt);
  3087. array_verify (EV_A_ (W *)cleanups, cleanupcnt);
  3088. #endif
  3089. #if EV_ASYNC_ENABLE
  3090. assert (asyncmax >= asynccnt);
  3091. array_verify (EV_A_ (W *)asyncs, asynccnt);
  3092. #endif
  3093. #if EV_PREPARE_ENABLE
  3094. assert (preparemax >= preparecnt);
  3095. array_verify (EV_A_ (W *)prepares, preparecnt);
  3096. #endif
  3097. #if EV_CHECK_ENABLE
  3098. assert (checkmax >= checkcnt);
  3099. array_verify (EV_A_ (W *)checks, checkcnt);
  3100. #endif
  3101. # if 0
  3102. #if EV_CHILD_ENABLE
  3103. for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
  3104. for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
  3105. #endif
  3106. # endif
  3107. #endif
  3108. }
  3109. #endif
  3110. #if EV_MULTIPLICITY
  3111. ecb_cold
  3112. struct ev_loop *
  3113. #else
  3114. int
  3115. #endif
  3116. ev_default_loop (unsigned int flags) EV_NOEXCEPT
  3117. {
  3118. if (!ev_default_loop_ptr)
  3119. {
  3120. #if EV_MULTIPLICITY
  3121. EV_P = ev_default_loop_ptr = &default_loop_struct;
  3122. #else
  3123. ev_default_loop_ptr = 1;
  3124. #endif
  3125. loop_init (EV_A_ flags);
  3126. if (ev_backend (EV_A))
  3127. {
  3128. #if EV_CHILD_ENABLE
  3129. ev_signal_init (&childev, childcb, SIGCHLD);
  3130. ev_set_priority (&childev, EV_MAXPRI);
  3131. ev_signal_start (EV_A_ &childev);
  3132. ev_unref (EV_A); /* child watcher should not keep loop alive */
  3133. #endif
  3134. }
  3135. else
  3136. ev_default_loop_ptr = 0;
  3137. }
  3138. return ev_default_loop_ptr;
  3139. }
  3140. void
  3141. ev_loop_fork (EV_P) EV_NOEXCEPT
  3142. {
  3143. postfork = 1;
  3144. }
  3145. /*****************************************************************************/
  3146. void
  3147. ev_invoke (EV_P_ void *w, int revents)
  3148. {
  3149. EV_CB_INVOKE ((W)w, revents);
  3150. }
  3151. unsigned int
  3152. ev_pending_count (EV_P) EV_NOEXCEPT
  3153. {
  3154. int pri;
  3155. unsigned int count = 0;
  3156. for (pri = NUMPRI; pri--; )
  3157. count += pendingcnt [pri];
  3158. return count;
  3159. }
  3160. ecb_noinline
  3161. void
  3162. ev_invoke_pending (EV_P)
  3163. {
  3164. pendingpri = NUMPRI;
  3165. do
  3166. {
  3167. --pendingpri;
  3168. /* pendingpri possibly gets modified in the inner loop */
  3169. while (pendingcnt [pendingpri])
  3170. {
  3171. ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
  3172. p->w->pending = 0;
  3173. EV_CB_INVOKE (p->w, p->events);
  3174. EV_FREQUENT_CHECK;
  3175. }
  3176. }
  3177. while (pendingpri);
  3178. }
  3179. #if EV_IDLE_ENABLE
  3180. /* make idle watchers pending. this handles the "call-idle */
  3181. /* only when higher priorities are idle" logic */
  3182. inline_size void
  3183. idle_reify (EV_P)
  3184. {
  3185. if (ecb_expect_false (idleall))
  3186. {
  3187. int pri;
  3188. for (pri = NUMPRI; pri--; )
  3189. {
  3190. if (pendingcnt [pri])
  3191. break;
  3192. if (idlecnt [pri])
  3193. {
  3194. queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
  3195. break;
  3196. }
  3197. }
  3198. }
  3199. }
  3200. #endif
  3201. /* make timers pending */
  3202. inline_size void
  3203. timers_reify (EV_P)
  3204. {
  3205. EV_FREQUENT_CHECK;
  3206. if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
  3207. {
  3208. do
  3209. {
  3210. ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
  3211. /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
  3212. /* first reschedule or stop timer */
  3213. if (w->repeat)
  3214. {
  3215. ev_at (w) += w->repeat;
  3216. if (ev_at (w) < mn_now)
  3217. ev_at (w) = mn_now;
  3218. assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > EV_TS_CONST (0.)));
  3219. ANHE_at_cache (timers [HEAP0]);
  3220. downheap (timers, timercnt, HEAP0);
  3221. }
  3222. else
  3223. ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
  3224. EV_FREQUENT_CHECK;
  3225. feed_reverse (EV_A_ (W)w);
  3226. }
  3227. while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
  3228. feed_reverse_done (EV_A_ EV_TIMER);
  3229. }
  3230. }
  3231. #if EV_PERIODIC_ENABLE
  3232. ecb_noinline
  3233. static void
  3234. periodic_recalc (EV_P_ ev_periodic *w)
  3235. {
  3236. ev_tstamp interval = w->interval > MIN_INTERVAL ? w->interval : MIN_INTERVAL;
  3237. ev_tstamp at = w->offset + interval * ev_floor ((ev_rt_now - w->offset) / interval);
  3238. /* the above almost always errs on the low side */
  3239. while (at <= ev_rt_now)
  3240. {
  3241. ev_tstamp nat = at + w->interval;
  3242. /* when resolution fails us, we use ev_rt_now */
  3243. if (ecb_expect_false (nat == at))
  3244. {
  3245. at = ev_rt_now;
  3246. break;
  3247. }
  3248. at = nat;
  3249. }
  3250. ev_at (w) = at;
  3251. }
  3252. /* make periodics pending */
  3253. inline_size void
  3254. periodics_reify (EV_P)
  3255. {
  3256. EV_FREQUENT_CHECK;
  3257. while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now)
  3258. {
  3259. do
  3260. {
  3261. ev_periodic *w = (ev_periodic *)ANHE_w (periodics [HEAP0]);
  3262. /*assert (("libev: inactive timer on periodic heap detected", ev_is_active (w)));*/
  3263. /* first reschedule or stop timer */
  3264. if (w->reschedule_cb)
  3265. {
  3266. ev_at (w) = w->reschedule_cb (w, ev_rt_now);
  3267. assert (("libev: ev_periodic reschedule callback returned time in the past", ev_at (w) >= ev_rt_now));
  3268. ANHE_at_cache (periodics [HEAP0]);
  3269. downheap (periodics, periodiccnt, HEAP0);
  3270. }
  3271. else if (w->interval)
  3272. {
  3273. periodic_recalc (EV_A_ w);
  3274. ANHE_at_cache (periodics [HEAP0]);
  3275. downheap (periodics, periodiccnt, HEAP0);
  3276. }
  3277. else
  3278. ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
  3279. EV_FREQUENT_CHECK;
  3280. feed_reverse (EV_A_ (W)w);
  3281. }
  3282. while (periodiccnt && ANHE_at (periodics [HEAP0]) < ev_rt_now);
  3283. feed_reverse_done (EV_A_ EV_PERIODIC);
  3284. }
  3285. }
  3286. /* simply recalculate all periodics */
  3287. /* TODO: maybe ensure that at least one event happens when jumping forward? */
  3288. ecb_noinline ecb_cold
  3289. static void
  3290. periodics_reschedule (EV_P)
  3291. {
  3292. int i;
  3293. /* adjust periodics after time jump */
  3294. for (i = HEAP0; i < periodiccnt + HEAP0; ++i)
  3295. {
  3296. ev_periodic *w = (ev_periodic *)ANHE_w (periodics [i]);
  3297. if (w->reschedule_cb)
  3298. ev_at (w) = w->reschedule_cb (w, ev_rt_now);
  3299. else if (w->interval)
  3300. periodic_recalc (EV_A_ w);
  3301. ANHE_at_cache (periodics [i]);
  3302. }
  3303. reheap (periodics, periodiccnt);
  3304. }
  3305. #endif
  3306. /* adjust all timers by a given offset */
  3307. ecb_noinline ecb_cold
  3308. static void
  3309. timers_reschedule (EV_P_ ev_tstamp adjust)
  3310. {
  3311. int i;
  3312. for (i = 0; i < timercnt; ++i)
  3313. {
  3314. ANHE *he = timers + i + HEAP0;
  3315. ANHE_w (*he)->at += adjust;
  3316. ANHE_at_cache (*he);
  3317. }
  3318. }
  3319. /* fetch new monotonic and realtime times from the kernel */
  3320. /* also detect if there was a timejump, and act accordingly */
  3321. inline_speed void
  3322. time_update (EV_P_ ev_tstamp max_block)
  3323. {
  3324. #if EV_USE_MONOTONIC
  3325. if (ecb_expect_true (have_monotonic))
  3326. {
  3327. int i;
  3328. ev_tstamp odiff = rtmn_diff;
  3329. mn_now = get_clock ();
  3330. /* only fetch the realtime clock every 0.5*MIN_TIMEJUMP seconds */
  3331. /* interpolate in the meantime */
  3332. if (ecb_expect_true (mn_now - now_floor < EV_TS_CONST (MIN_TIMEJUMP * .5)))
  3333. {
  3334. ev_rt_now = rtmn_diff + mn_now;
  3335. return;
  3336. }
  3337. now_floor = mn_now;
  3338. ev_rt_now = ev_time ();
  3339. /* loop a few times, before making important decisions.
  3340. * on the choice of "4": one iteration isn't enough,
  3341. * in case we get preempted during the calls to
  3342. * ev_time and get_clock. a second call is almost guaranteed
  3343. * to succeed in that case, though. and looping a few more times
  3344. * doesn't hurt either as we only do this on time-jumps or
  3345. * in the unlikely event of having been preempted here.
  3346. */
  3347. for (i = 4; --i; )
  3348. {
  3349. ev_tstamp diff;
  3350. rtmn_diff = ev_rt_now - mn_now;
  3351. diff = odiff - rtmn_diff;
  3352. if (ecb_expect_true ((diff < EV_TS_CONST (0.) ? -diff : diff) < EV_TS_CONST (MIN_TIMEJUMP)))
  3353. return; /* all is well */
  3354. ev_rt_now = ev_time ();
  3355. mn_now = get_clock ();
  3356. now_floor = mn_now;
  3357. }
  3358. /* no timer adjustment, as the monotonic clock doesn't jump */
  3359. /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
  3360. # if EV_PERIODIC_ENABLE
  3361. periodics_reschedule (EV_A);
  3362. # endif
  3363. }
  3364. else
  3365. #endif
  3366. {
  3367. ev_rt_now = ev_time ();
  3368. if (ecb_expect_false (mn_now > ev_rt_now || ev_rt_now > mn_now + max_block + EV_TS_CONST (MIN_TIMEJUMP)))
  3369. {
  3370. /* adjust timers. this is easy, as the offset is the same for all of them */
  3371. timers_reschedule (EV_A_ ev_rt_now - mn_now);
  3372. #if EV_PERIODIC_ENABLE
  3373. periodics_reschedule (EV_A);
  3374. #endif
  3375. }
  3376. mn_now = ev_rt_now;
  3377. }
  3378. }
  3379. int
  3380. ev_run (EV_P_ int flags)
  3381. {
  3382. #if EV_FEATURE_API
  3383. ++loop_depth;
  3384. #endif
  3385. assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
  3386. loop_done = EVBREAK_CANCEL;
  3387. EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
  3388. do
  3389. {
  3390. #if EV_VERIFY >= 2
  3391. ev_verify (EV_A);
  3392. #endif
  3393. #ifndef _WIN32
  3394. if (ecb_expect_false (curpid)) /* penalise the forking check even more */
  3395. if (ecb_expect_false (getpid () != curpid))
  3396. {
  3397. curpid = getpid ();
  3398. postfork = 1;
  3399. }
  3400. #endif
  3401. #if EV_FORK_ENABLE
  3402. /* we might have forked, so queue fork handlers */
  3403. if (ecb_expect_false (postfork))
  3404. if (forkcnt)
  3405. {
  3406. queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
  3407. EV_INVOKE_PENDING;
  3408. }
  3409. #endif
  3410. #if EV_PREPARE_ENABLE
  3411. /* queue prepare watchers (and execute them) */
  3412. if (ecb_expect_false (preparecnt))
  3413. {
  3414. queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
  3415. EV_INVOKE_PENDING;
  3416. }
  3417. #endif
  3418. if (ecb_expect_false (loop_done))
  3419. break;
  3420. /* we might have forked, so reify kernel state if necessary */
  3421. if (ecb_expect_false (postfork))
  3422. loop_fork (EV_A);
  3423. /* update fd-related kernel structures */
  3424. fd_reify (EV_A);
  3425. /* calculate blocking time */
  3426. {
  3427. ev_tstamp waittime = 0.;
  3428. ev_tstamp sleeptime = 0.;
  3429. /* remember old timestamp for io_blocktime calculation */
  3430. ev_tstamp prev_mn_now = mn_now;
  3431. /* update time to cancel out callback processing overhead */
  3432. time_update (EV_A_ EV_TS_CONST (EV_TSTAMP_HUGE));
  3433. /* from now on, we want a pipe-wake-up */
  3434. pipe_write_wanted = 1;
  3435. ECB_MEMORY_FENCE; /* make sure pipe_write_wanted is visible before we check for potential skips */
  3436. if (ecb_expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
  3437. {
  3438. waittime = EV_TS_CONST (MAX_BLOCKTIME);
  3439. #if EV_USE_TIMERFD
  3440. /* sleep a lot longer when we can reliably detect timejumps */
  3441. if (ecb_expect_true (timerfd >= 0))
  3442. waittime = EV_TS_CONST (MAX_BLOCKTIME2);
  3443. #endif
  3444. #if !EV_PERIODIC_ENABLE
  3445. /* without periodics but with monotonic clock there is no need */
  3446. /* for any time jump detection, so sleep longer */
  3447. if (ecb_expect_true (have_monotonic))
  3448. waittime = EV_TS_CONST (MAX_BLOCKTIME2);
  3449. #endif
  3450. if (timercnt)
  3451. {
  3452. ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now;
  3453. if (waittime > to) waittime = to;
  3454. }
  3455. #if EV_PERIODIC_ENABLE
  3456. if (periodiccnt)
  3457. {
  3458. ev_tstamp to = ANHE_at (periodics [HEAP0]) - ev_rt_now;
  3459. if (waittime > to) waittime = to;
  3460. }
  3461. #endif
  3462. /* don't let timeouts decrease the waittime below timeout_blocktime */
  3463. if (ecb_expect_false (waittime < timeout_blocktime))
  3464. waittime = timeout_blocktime;
  3465. /* now there are two more special cases left, either we have
  3466. * already-expired timers, so we should not sleep, or we have timers
  3467. * that expire very soon, in which case we need to wait for a minimum
  3468. * amount of time for some event loop backends.
  3469. */
  3470. if (ecb_expect_false (waittime < backend_mintime))
  3471. waittime = waittime <= EV_TS_CONST (0.)
  3472. ? EV_TS_CONST (0.)
  3473. : backend_mintime;
  3474. /* extra check because io_blocktime is commonly 0 */
  3475. if (ecb_expect_false (io_blocktime))
  3476. {
  3477. sleeptime = io_blocktime - (mn_now - prev_mn_now);
  3478. if (sleeptime > waittime - backend_mintime)
  3479. sleeptime = waittime - backend_mintime;
  3480. if (ecb_expect_true (sleeptime > EV_TS_CONST (0.)))
  3481. {
  3482. ev_sleep (sleeptime);
  3483. waittime -= sleeptime;
  3484. }
  3485. }
  3486. }
  3487. #if EV_FEATURE_API
  3488. ++loop_count;
  3489. #endif
  3490. assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
  3491. backend_poll (EV_A_ waittime);
  3492. assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
  3493. pipe_write_wanted = 0; /* just an optimisation, no fence needed */
  3494. ECB_MEMORY_FENCE_ACQUIRE;
  3495. if (pipe_write_skipped)
  3496. {
  3497. assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
  3498. ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
  3499. }
  3500. /* update ev_rt_now, do magic */
  3501. time_update (EV_A_ waittime + sleeptime);
  3502. }
  3503. /* queue pending timers and reschedule them */
  3504. timers_reify (EV_A); /* relative timers called last */
  3505. #if EV_PERIODIC_ENABLE
  3506. periodics_reify (EV_A); /* absolute timers called first */
  3507. #endif
  3508. #if EV_IDLE_ENABLE
  3509. /* queue idle watchers unless other events are pending */
  3510. idle_reify (EV_A);
  3511. #endif
  3512. #if EV_CHECK_ENABLE
  3513. /* queue check watchers, to be executed first */
  3514. if (ecb_expect_false (checkcnt))
  3515. queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
  3516. #endif
  3517. EV_INVOKE_PENDING;
  3518. }
  3519. while (ecb_expect_true (
  3520. activecnt
  3521. && !loop_done
  3522. && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
  3523. ));
  3524. if (loop_done == EVBREAK_ONE)
  3525. loop_done = EVBREAK_CANCEL;
  3526. #if EV_FEATURE_API
  3527. --loop_depth;
  3528. #endif
  3529. return activecnt;
  3530. }
  3531. void
  3532. ev_break (EV_P_ int how) EV_NOEXCEPT
  3533. {
  3534. loop_done = how;
  3535. }
  3536. void
  3537. ev_ref (EV_P) EV_NOEXCEPT
  3538. {
  3539. ++activecnt;
  3540. }
  3541. void
  3542. ev_unref (EV_P) EV_NOEXCEPT
  3543. {
  3544. --activecnt;
  3545. }
  3546. void
  3547. ev_now_update (EV_P) EV_NOEXCEPT
  3548. {
  3549. time_update (EV_A_ EV_TSTAMP_HUGE);
  3550. }
  3551. void
  3552. ev_suspend (EV_P) EV_NOEXCEPT
  3553. {
  3554. ev_now_update (EV_A);
  3555. }
  3556. void
  3557. ev_resume (EV_P) EV_NOEXCEPT
  3558. {
  3559. ev_tstamp mn_prev = mn_now;
  3560. ev_now_update (EV_A);
  3561. timers_reschedule (EV_A_ mn_now - mn_prev);
  3562. #if EV_PERIODIC_ENABLE
  3563. /* TODO: really do this? */
  3564. periodics_reschedule (EV_A);
  3565. #endif
  3566. }
  3567. /*****************************************************************************/
  3568. /* singly-linked list management, used when the expected list length is short */
  3569. inline_size void
  3570. wlist_add (WL *head, WL elem)
  3571. {
  3572. elem->next = *head;
  3573. *head = elem;
  3574. }
  3575. inline_size void
  3576. wlist_del (WL *head, WL elem)
  3577. {
  3578. while (*head)
  3579. {
  3580. if (ecb_expect_true (*head == elem))
  3581. {
  3582. *head = elem->next;
  3583. break;
  3584. }
  3585. head = &(*head)->next;
  3586. }
  3587. }
  3588. /* internal, faster, version of ev_clear_pending */
  3589. inline_speed void
  3590. clear_pending (EV_P_ W w)
  3591. {
  3592. if (w->pending)
  3593. {
  3594. pendings [ABSPRI (w)][w->pending - 1].w = (W)&pending_w;
  3595. w->pending = 0;
  3596. }
  3597. }
  3598. int
  3599. ev_clear_pending (EV_P_ void *w) EV_NOEXCEPT
  3600. {
  3601. W w_ = (W)w;
  3602. int pending = w_->pending;
  3603. if (ecb_expect_true (pending))
  3604. {
  3605. ANPENDING *p = pendings [ABSPRI (w_)] + pending - 1;
  3606. p->w = (W)&pending_w;
  3607. w_->pending = 0;
  3608. return p->events;
  3609. }
  3610. else
  3611. return 0;
  3612. }
  3613. inline_size void
  3614. pri_adjust (EV_P_ W w)
  3615. {
  3616. int pri = ev_priority (w);
  3617. pri = pri < EV_MINPRI ? EV_MINPRI : pri;
  3618. pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
  3619. ev_set_priority (w, pri);
  3620. }
  3621. inline_speed void
  3622. ev_start (EV_P_ W w, int active)
  3623. {
  3624. pri_adjust (EV_A_ w);
  3625. w->active = active;
  3626. ev_ref (EV_A);
  3627. }
  3628. inline_size void
  3629. ev_stop (EV_P_ W w)
  3630. {
  3631. ev_unref (EV_A);
  3632. w->active = 0;
  3633. }
  3634. /*****************************************************************************/
  3635. ecb_noinline
  3636. void
  3637. ev_io_start (EV_P_ ev_io *w) EV_NOEXCEPT
  3638. {
  3639. int fd = w->fd;
  3640. if (ecb_expect_false (ev_is_active (w)))
  3641. return;
  3642. assert (("libev: ev_io_start called with negative fd", fd >= 0));
  3643. assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
  3644. #if EV_VERIFY >= 2
  3645. assert (("libev: ev_io_start called on watcher with invalid fd", fd_valid (fd)));
  3646. #endif
  3647. EV_FREQUENT_CHECK;
  3648. ev_start (EV_A_ (W)w, 1);
  3649. array_needsize (ANFD, anfds, anfdmax, fd + 1, array_needsize_zerofill);
  3650. wlist_add (&anfds[fd].head, (WL)w);
  3651. /* common bug, apparently */
  3652. assert (("libev: ev_io_start called with corrupted watcher", ((WL)w)->next != (WL)w));
  3653. fd_change (EV_A_ fd, w->events & EV__IOFDSET | EV_ANFD_REIFY);
  3654. w->events &= ~EV__IOFDSET;
  3655. EV_FREQUENT_CHECK;
  3656. }
  3657. ecb_noinline
  3658. void
  3659. ev_io_stop (EV_P_ ev_io *w) EV_NOEXCEPT
  3660. {
  3661. clear_pending (EV_A_ (W)w);
  3662. if (ecb_expect_false (!ev_is_active (w)))
  3663. return;
  3664. assert (("libev: ev_io_stop called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
  3665. #if EV_VERIFY >= 2
  3666. assert (("libev: ev_io_stop called on watcher with invalid fd", fd_valid (w->fd)));
  3667. #endif
  3668. EV_FREQUENT_CHECK;
  3669. wlist_del (&anfds[w->fd].head, (WL)w);
  3670. ev_stop (EV_A_ (W)w);
  3671. fd_change (EV_A_ w->fd, EV_ANFD_REIFY);
  3672. EV_FREQUENT_CHECK;
  3673. }
  3674. ecb_noinline
  3675. void
  3676. ev_timer_start (EV_P_ ev_timer *w) EV_NOEXCEPT
  3677. {
  3678. if (ecb_expect_false (ev_is_active (w)))
  3679. return;
  3680. ev_at (w) += mn_now;
  3681. assert (("libev: ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
  3682. EV_FREQUENT_CHECK;
  3683. ++timercnt;
  3684. ev_start (EV_A_ (W)w, timercnt + HEAP0 - 1);
  3685. array_needsize (ANHE, timers, timermax, ev_active (w) + 1, array_needsize_noinit);
  3686. ANHE_w (timers [ev_active (w)]) = (WT)w;
  3687. ANHE_at_cache (timers [ev_active (w)]);
  3688. upheap (timers, ev_active (w));
  3689. EV_FREQUENT_CHECK;
  3690. /*assert (("libev: internal timer heap corruption", timers [ev_active (w)] == (WT)w));*/
  3691. }
  3692. ecb_noinline
  3693. void
  3694. ev_timer_stop (EV_P_ ev_timer *w) EV_NOEXCEPT
  3695. {
  3696. clear_pending (EV_A_ (W)w);
  3697. if (ecb_expect_false (!ev_is_active (w)))
  3698. return;
  3699. EV_FREQUENT_CHECK;
  3700. {
  3701. int active = ev_active (w);
  3702. assert (("libev: internal timer heap corruption", ANHE_w (timers [active]) == (WT)w));
  3703. --timercnt;
  3704. if (ecb_expect_true (active < timercnt + HEAP0))
  3705. {
  3706. timers [active] = timers [timercnt + HEAP0];
  3707. adjustheap (timers, timercnt, active);
  3708. }
  3709. }
  3710. ev_at (w) -= mn_now;
  3711. ev_stop (EV_A_ (W)w);
  3712. EV_FREQUENT_CHECK;
  3713. }
  3714. ecb_noinline
  3715. void
  3716. ev_timer_again (EV_P_ ev_timer *w) EV_NOEXCEPT
  3717. {
  3718. EV_FREQUENT_CHECK;
  3719. clear_pending (EV_A_ (W)w);
  3720. if (ev_is_active (w))
  3721. {
  3722. if (w->repeat)
  3723. {
  3724. ev_at (w) = mn_now + w->repeat;
  3725. ANHE_at_cache (timers [ev_active (w)]);
  3726. adjustheap (timers, timercnt, ev_active (w));
  3727. }
  3728. else
  3729. ev_timer_stop (EV_A_ w);
  3730. }
  3731. else if (w->repeat)
  3732. {
  3733. ev_at (w) = w->repeat;
  3734. ev_timer_start (EV_A_ w);
  3735. }
  3736. EV_FREQUENT_CHECK;
  3737. }
  3738. ev_tstamp
  3739. ev_timer_remaining (EV_P_ ev_timer *w) EV_NOEXCEPT
  3740. {
  3741. return ev_at (w) - (ev_is_active (w) ? mn_now : EV_TS_CONST (0.));
  3742. }
  3743. #if EV_PERIODIC_ENABLE
  3744. ecb_noinline
  3745. void
  3746. ev_periodic_start (EV_P_ ev_periodic *w) EV_NOEXCEPT
  3747. {
  3748. if (ecb_expect_false (ev_is_active (w)))
  3749. return;
  3750. #if EV_USE_TIMERFD
  3751. if (timerfd == -2)
  3752. evtimerfd_init (EV_A);
  3753. #endif
  3754. if (w->reschedule_cb)
  3755. ev_at (w) = w->reschedule_cb (w, ev_rt_now);
  3756. else if (w->interval)
  3757. {
  3758. assert (("libev: ev_periodic_start called with negative interval value", w->interval >= 0.));
  3759. periodic_recalc (EV_A_ w);
  3760. }
  3761. else
  3762. ev_at (w) = w->offset;
  3763. EV_FREQUENT_CHECK;
  3764. ++periodiccnt;
  3765. ev_start (EV_A_ (W)w, periodiccnt + HEAP0 - 1);
  3766. array_needsize (ANHE, periodics, periodicmax, ev_active (w) + 1, array_needsize_noinit);
  3767. ANHE_w (periodics [ev_active (w)]) = (WT)w;
  3768. ANHE_at_cache (periodics [ev_active (w)]);
  3769. upheap (periodics, ev_active (w));
  3770. EV_FREQUENT_CHECK;
  3771. /*assert (("libev: internal periodic heap corruption", ANHE_w (periodics [ev_active (w)]) == (WT)w));*/
  3772. }
  3773. ecb_noinline
  3774. void
  3775. ev_periodic_stop (EV_P_ ev_periodic *w) EV_NOEXCEPT
  3776. {
  3777. clear_pending (EV_A_ (W)w);
  3778. if (ecb_expect_false (!ev_is_active (w)))
  3779. return;
  3780. EV_FREQUENT_CHECK;
  3781. {
  3782. int active = ev_active (w);
  3783. assert (("libev: internal periodic heap corruption", ANHE_w (periodics [active]) == (WT)w));
  3784. --periodiccnt;
  3785. if (ecb_expect_true (active < periodiccnt + HEAP0))
  3786. {
  3787. periodics [active] = periodics [periodiccnt + HEAP0];
  3788. adjustheap (periodics, periodiccnt, active);
  3789. }
  3790. }
  3791. ev_stop (EV_A_ (W)w);
  3792. EV_FREQUENT_CHECK;
  3793. }
  3794. ecb_noinline
  3795. void
  3796. ev_periodic_again (EV_P_ ev_periodic *w) EV_NOEXCEPT
  3797. {
  3798. /* TODO: use adjustheap and recalculation */
  3799. ev_periodic_stop (EV_A_ w);
  3800. ev_periodic_start (EV_A_ w);
  3801. }
  3802. #endif
  3803. #ifndef SA_RESTART
  3804. # define SA_RESTART 0
  3805. #endif
  3806. #if EV_SIGNAL_ENABLE
  3807. ecb_noinline
  3808. void
  3809. ev_signal_start (EV_P_ ev_signal *w) EV_NOEXCEPT
  3810. {
  3811. if (ecb_expect_false (ev_is_active (w)))
  3812. return;
  3813. assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
  3814. #if EV_MULTIPLICITY
  3815. assert (("libev: a signal must not be attached to two different loops",
  3816. !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
  3817. signals [w->signum - 1].loop = EV_A;
  3818. ECB_MEMORY_FENCE_RELEASE;
  3819. #endif
  3820. EV_FREQUENT_CHECK;
  3821. #if EV_USE_SIGNALFD
  3822. if (sigfd == -2)
  3823. {
  3824. sigfd = signalfd (-1, &sigfd_set, SFD_NONBLOCK | SFD_CLOEXEC);
  3825. if (sigfd < 0 && errno == EINVAL)
  3826. sigfd = signalfd (-1, &sigfd_set, 0); /* retry without flags */
  3827. if (sigfd >= 0)
  3828. {
  3829. fd_intern (sigfd); /* doing it twice will not hurt */
  3830. sigemptyset (&sigfd_set);
  3831. ev_io_init (&sigfd_w, sigfdcb, sigfd, EV_READ);
  3832. ev_set_priority (&sigfd_w, EV_MAXPRI);
  3833. ev_io_start (EV_A_ &sigfd_w);
  3834. ev_unref (EV_A); /* signalfd watcher should not keep loop alive */
  3835. }
  3836. }
  3837. if (sigfd >= 0)
  3838. {
  3839. /* TODO: check .head */
  3840. sigaddset (&sigfd_set, w->signum);
  3841. sigprocmask (SIG_BLOCK, &sigfd_set, 0);
  3842. signalfd (sigfd, &sigfd_set, 0);
  3843. }
  3844. #endif
  3845. ev_start (EV_A_ (W)w, 1);
  3846. wlist_add (&signals [w->signum - 1].head, (WL)w);
  3847. if (!((WL)w)->next)
  3848. # if EV_USE_SIGNALFD
  3849. if (sigfd < 0) /*TODO*/
  3850. # endif
  3851. {
  3852. # ifdef _WIN32
  3853. evpipe_init (EV_A);
  3854. signal (w->signum, ev_sighandler);
  3855. # else
  3856. struct sigaction sa;
  3857. evpipe_init (EV_A);
  3858. sa.sa_handler = ev_sighandler;
  3859. sigfillset (&sa.sa_mask);
  3860. sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
  3861. sigaction (w->signum, &sa, 0);
  3862. if (origflags & EVFLAG_NOSIGMASK)
  3863. {
  3864. sigemptyset (&sa.sa_mask);
  3865. sigaddset (&sa.sa_mask, w->signum);
  3866. sigprocmask (SIG_UNBLOCK, &sa.sa_mask, 0);
  3867. }
  3868. #endif
  3869. }
  3870. EV_FREQUENT_CHECK;
  3871. }
  3872. ecb_noinline
  3873. void
  3874. ev_signal_stop (EV_P_ ev_signal *w) EV_NOEXCEPT
  3875. {
  3876. clear_pending (EV_A_ (W)w);
  3877. if (ecb_expect_false (!ev_is_active (w)))
  3878. return;
  3879. EV_FREQUENT_CHECK;
  3880. wlist_del (&signals [w->signum - 1].head, (WL)w);
  3881. ev_stop (EV_A_ (W)w);
  3882. if (!signals [w->signum - 1].head)
  3883. {
  3884. #if EV_MULTIPLICITY
  3885. signals [w->signum - 1].loop = 0; /* unattach from signal */
  3886. #endif
  3887. #if EV_USE_SIGNALFD
  3888. if (sigfd >= 0)
  3889. {
  3890. sigset_t ss;
  3891. sigemptyset (&ss);
  3892. sigaddset (&ss, w->signum);
  3893. sigdelset (&sigfd_set, w->signum);
  3894. signalfd (sigfd, &sigfd_set, 0);
  3895. sigprocmask (SIG_UNBLOCK, &ss, 0);
  3896. }
  3897. else
  3898. #endif
  3899. signal (w->signum, SIG_DFL);
  3900. }
  3901. EV_FREQUENT_CHECK;
  3902. }
  3903. #endif
  3904. #if EV_CHILD_ENABLE
  3905. void
  3906. ev_child_start (EV_P_ ev_child *w) EV_NOEXCEPT
  3907. {
  3908. #if EV_MULTIPLICITY
  3909. assert (("libev: child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
  3910. #endif
  3911. if (ecb_expect_false (ev_is_active (w)))
  3912. return;
  3913. EV_FREQUENT_CHECK;
  3914. ev_start (EV_A_ (W)w, 1);
  3915. wlist_add (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
  3916. EV_FREQUENT_CHECK;
  3917. }
  3918. void
  3919. ev_child_stop (EV_P_ ev_child *w) EV_NOEXCEPT
  3920. {
  3921. clear_pending (EV_A_ (W)w);
  3922. if (ecb_expect_false (!ev_is_active (w)))
  3923. return;
  3924. EV_FREQUENT_CHECK;
  3925. wlist_del (&childs [w->pid & ((EV_PID_HASHSIZE) - 1)], (WL)w);
  3926. ev_stop (EV_A_ (W)w);
  3927. EV_FREQUENT_CHECK;
  3928. }
  3929. #endif
  3930. #if EV_STAT_ENABLE
  3931. # ifdef _WIN32
  3932. # undef lstat
  3933. # define lstat(a,b) _stati64 (a,b)
  3934. # endif
  3935. #define DEF_STAT_INTERVAL 5.0074891
  3936. #define NFS_STAT_INTERVAL 30.1074891 /* for filesystems potentially failing inotify */
  3937. #define MIN_STAT_INTERVAL 0.1074891
  3938. ecb_noinline static void stat_timer_cb (EV_P_ ev_timer *w_, int revents);
  3939. #if EV_USE_INOTIFY
  3940. /* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
  3941. # define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
  3942. ecb_noinline
  3943. static void
  3944. infy_add (EV_P_ ev_stat *w)
  3945. {
  3946. w->wd = inotify_add_watch (fs_fd, w->path,
  3947. IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY
  3948. | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
  3949. | IN_DONT_FOLLOW | IN_MASK_ADD);
  3950. if (w->wd >= 0)
  3951. {
  3952. struct statfs sfs;
  3953. /* now local changes will be tracked by inotify, but remote changes won't */
  3954. /* unless the filesystem is known to be local, we therefore still poll */
  3955. /* also do poll on <2.6.25, but with normal frequency */
  3956. if (!fs_2625)
  3957. w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
  3958. else if (!statfs (w->path, &sfs)
  3959. && (sfs.f_type == 0x1373 /* devfs */
  3960. || sfs.f_type == 0x4006 /* fat */
  3961. || sfs.f_type == 0x4d44 /* msdos */
  3962. || sfs.f_type == 0xEF53 /* ext2/3 */
  3963. || sfs.f_type == 0x72b6 /* jffs2 */
  3964. || sfs.f_type == 0x858458f6 /* ramfs */
  3965. || sfs.f_type == 0x5346544e /* ntfs */
  3966. || sfs.f_type == 0x3153464a /* jfs */
  3967. || sfs.f_type == 0x9123683e /* btrfs */
  3968. || sfs.f_type == 0x52654973 /* reiser3 */
  3969. || sfs.f_type == 0x01021994 /* tmpfs */
  3970. || sfs.f_type == 0x58465342 /* xfs */))
  3971. w->timer.repeat = 0.; /* filesystem is local, kernel new enough */
  3972. else
  3973. w->timer.repeat = w->interval ? w->interval : NFS_STAT_INTERVAL; /* remote, use reduced frequency */
  3974. }
  3975. else
  3976. {
  3977. /* can't use inotify, continue to stat */
  3978. w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
  3979. /* if path is not there, monitor some parent directory for speedup hints */
  3980. /* note that exceeding the hardcoded path limit is not a correctness issue, */
  3981. /* but an efficiency issue only */
  3982. if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
  3983. {
  3984. char path [4096];
  3985. strcpy (path, w->path);
  3986. do
  3987. {
  3988. int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
  3989. | (errno == EACCES ? IN_ATTRIB : IN_CREATE | IN_MOVED_TO);
  3990. char *pend = strrchr (path, '/');
  3991. if (!pend || pend == path)
  3992. break;
  3993. *pend = 0;
  3994. w->wd = inotify_add_watch (fs_fd, path, mask);
  3995. }
  3996. while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
  3997. }
  3998. }
  3999. if (w->wd >= 0)
  4000. wlist_add (&fs_hash [w->wd & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
  4001. /* now re-arm timer, if required */
  4002. if (ev_is_active (&w->timer)) ev_ref (EV_A);
  4003. ev_timer_again (EV_A_ &w->timer);
  4004. if (ev_is_active (&w->timer)) ev_unref (EV_A);
  4005. }
  4006. ecb_noinline
  4007. static void
  4008. infy_del (EV_P_ ev_stat *w)
  4009. {
  4010. int slot;
  4011. int wd = w->wd;
  4012. if (wd < 0)
  4013. return;
  4014. w->wd = -2;
  4015. slot = wd & ((EV_INOTIFY_HASHSIZE) - 1);
  4016. wlist_del (&fs_hash [slot].head, (WL)w);
  4017. /* remove this watcher, if others are watching it, they will rearm */
  4018. inotify_rm_watch (fs_fd, wd);
  4019. }
  4020. ecb_noinline
  4021. static void
  4022. infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
  4023. {
  4024. if (slot < 0)
  4025. /* overflow, need to check for all hash slots */
  4026. for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
  4027. infy_wd (EV_A_ slot, wd, ev);
  4028. else
  4029. {
  4030. WL w_;
  4031. for (w_ = fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head; w_; )
  4032. {
  4033. ev_stat *w = (ev_stat *)w_;
  4034. w_ = w_->next; /* lets us remove this watcher and all before it */
  4035. if (w->wd == wd || wd == -1)
  4036. {
  4037. if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
  4038. {
  4039. wlist_del (&fs_hash [slot & ((EV_INOTIFY_HASHSIZE) - 1)].head, (WL)w);
  4040. w->wd = -1;
  4041. infy_add (EV_A_ w); /* re-add, no matter what */
  4042. }
  4043. stat_timer_cb (EV_A_ &w->timer, 0);
  4044. }
  4045. }
  4046. }
  4047. }
  4048. static void
  4049. infy_cb (EV_P_ ev_io *w, int revents)
  4050. {
  4051. char buf [EV_INOTIFY_BUFSIZE];
  4052. int ofs;
  4053. int len = read (fs_fd, buf, sizeof (buf));
  4054. for (ofs = 0; ofs < len; )
  4055. {
  4056. struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
  4057. infy_wd (EV_A_ ev->wd, ev->wd, ev);
  4058. ofs += sizeof (struct inotify_event) + ev->len;
  4059. }
  4060. }
  4061. inline_size ecb_cold
  4062. void
  4063. ev_check_2625 (EV_P)
  4064. {
  4065. /* kernels < 2.6.25 are borked
  4066. * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
  4067. */
  4068. if (ev_linux_version () < 0x020619)
  4069. return;
  4070. fs_2625 = 1;
  4071. }
  4072. inline_size int
  4073. infy_newfd (void)
  4074. {
  4075. #if defined IN_CLOEXEC && defined IN_NONBLOCK
  4076. int fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK);
  4077. if (fd >= 0)
  4078. return fd;
  4079. #endif
  4080. return inotify_init ();
  4081. }
  4082. inline_size void
  4083. infy_init (EV_P)
  4084. {
  4085. if (fs_fd != -2)
  4086. return;
  4087. fs_fd = -1;
  4088. ev_check_2625 (EV_A);
  4089. fs_fd = infy_newfd ();
  4090. if (fs_fd >= 0)
  4091. {
  4092. fd_intern (fs_fd);
  4093. ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
  4094. ev_set_priority (&fs_w, EV_MAXPRI);
  4095. ev_io_start (EV_A_ &fs_w);
  4096. ev_unref (EV_A);
  4097. }
  4098. }
  4099. inline_size void
  4100. infy_fork (EV_P)
  4101. {
  4102. int slot;
  4103. if (fs_fd < 0)
  4104. return;
  4105. ev_ref (EV_A);
  4106. ev_io_stop (EV_A_ &fs_w);
  4107. close (fs_fd);
  4108. fs_fd = infy_newfd ();
  4109. if (fs_fd >= 0)
  4110. {
  4111. fd_intern (fs_fd);
  4112. ev_io_set (&fs_w, fs_fd, EV_READ);
  4113. ev_io_start (EV_A_ &fs_w);
  4114. ev_unref (EV_A);
  4115. }
  4116. for (slot = 0; slot < (EV_INOTIFY_HASHSIZE); ++slot)
  4117. {
  4118. WL w_ = fs_hash [slot].head;
  4119. fs_hash [slot].head = 0;
  4120. while (w_)
  4121. {
  4122. ev_stat *w = (ev_stat *)w_;
  4123. w_ = w_->next; /* lets us add this watcher */
  4124. w->wd = -1;
  4125. if (fs_fd >= 0)
  4126. infy_add (EV_A_ w); /* re-add, no matter what */
  4127. else
  4128. {
  4129. w->timer.repeat = w->interval ? w->interval : DEF_STAT_INTERVAL;
  4130. if (ev_is_active (&w->timer)) ev_ref (EV_A);
  4131. ev_timer_again (EV_A_ &w->timer);
  4132. if (ev_is_active (&w->timer)) ev_unref (EV_A);
  4133. }
  4134. }
  4135. }
  4136. }
  4137. #endif
  4138. #ifdef _WIN32
  4139. # define EV_LSTAT(p,b) _stati64 (p, b)
  4140. #else
  4141. # define EV_LSTAT(p,b) lstat (p, b)
  4142. #endif
  4143. void
  4144. ev_stat_stat (EV_P_ ev_stat *w) EV_NOEXCEPT
  4145. {
  4146. if (lstat (w->path, &w->attr) < 0)
  4147. w->attr.st_nlink = 0;
  4148. else if (!w->attr.st_nlink)
  4149. w->attr.st_nlink = 1;
  4150. }
  4151. ecb_noinline
  4152. static void
  4153. stat_timer_cb (EV_P_ ev_timer *w_, int revents)
  4154. {
  4155. ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
  4156. ev_statdata prev = w->attr;
  4157. ev_stat_stat (EV_A_ w);
  4158. /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
  4159. if (
  4160. prev.st_dev != w->attr.st_dev
  4161. || prev.st_ino != w->attr.st_ino
  4162. || prev.st_mode != w->attr.st_mode
  4163. || prev.st_nlink != w->attr.st_nlink
  4164. || prev.st_uid != w->attr.st_uid
  4165. || prev.st_gid != w->attr.st_gid
  4166. || prev.st_rdev != w->attr.st_rdev
  4167. || prev.st_size != w->attr.st_size
  4168. || prev.st_atime != w->attr.st_atime
  4169. || prev.st_mtime != w->attr.st_mtime
  4170. || prev.st_ctime != w->attr.st_ctime
  4171. ) {
  4172. /* we only update w->prev on actual differences */
  4173. /* in case we test more often than invoke the callback, */
  4174. /* to ensure that prev is always different to attr */
  4175. w->prev = prev;
  4176. #if EV_USE_INOTIFY
  4177. if (fs_fd >= 0)
  4178. {
  4179. infy_del (EV_A_ w);
  4180. infy_add (EV_A_ w);
  4181. ev_stat_stat (EV_A_ w); /* avoid race... */
  4182. }
  4183. #endif
  4184. ev_feed_event (EV_A_ w, EV_STAT);
  4185. }
  4186. }
  4187. void
  4188. ev_stat_start (EV_P_ ev_stat *w) EV_NOEXCEPT
  4189. {
  4190. if (ecb_expect_false (ev_is_active (w)))
  4191. return;
  4192. ev_stat_stat (EV_A_ w);
  4193. if (w->interval < MIN_STAT_INTERVAL && w->interval)
  4194. w->interval = MIN_STAT_INTERVAL;
  4195. ev_timer_init (&w->timer, stat_timer_cb, 0., w->interval ? w->interval : DEF_STAT_INTERVAL);
  4196. ev_set_priority (&w->timer, ev_priority (w));
  4197. #if EV_USE_INOTIFY
  4198. infy_init (EV_A);
  4199. if (fs_fd >= 0)
  4200. infy_add (EV_A_ w);
  4201. else
  4202. #endif
  4203. {
  4204. ev_timer_again (EV_A_ &w->timer);
  4205. ev_unref (EV_A);
  4206. }
  4207. ev_start (EV_A_ (W)w, 1);
  4208. EV_FREQUENT_CHECK;
  4209. }
  4210. void
  4211. ev_stat_stop (EV_P_ ev_stat *w) EV_NOEXCEPT
  4212. {
  4213. clear_pending (EV_A_ (W)w);
  4214. if (ecb_expect_false (!ev_is_active (w)))
  4215. return;
  4216. EV_FREQUENT_CHECK;
  4217. #if EV_USE_INOTIFY
  4218. infy_del (EV_A_ w);
  4219. #endif
  4220. if (ev_is_active (&w->timer))
  4221. {
  4222. ev_ref (EV_A);
  4223. ev_timer_stop (EV_A_ &w->timer);
  4224. }
  4225. ev_stop (EV_A_ (W)w);
  4226. EV_FREQUENT_CHECK;
  4227. }
  4228. #endif
  4229. #if EV_IDLE_ENABLE
  4230. void
  4231. ev_idle_start (EV_P_ ev_idle *w) EV_NOEXCEPT
  4232. {
  4233. if (ecb_expect_false (ev_is_active (w)))
  4234. return;
  4235. pri_adjust (EV_A_ (W)w);
  4236. EV_FREQUENT_CHECK;
  4237. {
  4238. int active = ++idlecnt [ABSPRI (w)];
  4239. ++idleall;
  4240. ev_start (EV_A_ (W)w, active);
  4241. array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, array_needsize_noinit);
  4242. idles [ABSPRI (w)][active - 1] = w;
  4243. }
  4244. EV_FREQUENT_CHECK;
  4245. }
  4246. void
  4247. ev_idle_stop (EV_P_ ev_idle *w) EV_NOEXCEPT
  4248. {
  4249. clear_pending (EV_A_ (W)w);
  4250. if (ecb_expect_false (!ev_is_active (w)))
  4251. return;
  4252. EV_FREQUENT_CHECK;
  4253. {
  4254. int active = ev_active (w);
  4255. idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
  4256. ev_active (idles [ABSPRI (w)][active - 1]) = active;
  4257. ev_stop (EV_A_ (W)w);
  4258. --idleall;
  4259. }
  4260. EV_FREQUENT_CHECK;
  4261. }
  4262. #endif
  4263. #if EV_PREPARE_ENABLE
  4264. void
  4265. ev_prepare_start (EV_P_ ev_prepare *w) EV_NOEXCEPT
  4266. {
  4267. if (ecb_expect_false (ev_is_active (w)))
  4268. return;
  4269. EV_FREQUENT_CHECK;
  4270. ev_start (EV_A_ (W)w, ++preparecnt);
  4271. array_needsize (ev_prepare *, prepares, preparemax, preparecnt, array_needsize_noinit);
  4272. prepares [preparecnt - 1] = w;
  4273. EV_FREQUENT_CHECK;
  4274. }
  4275. void
  4276. ev_prepare_stop (EV_P_ ev_prepare *w) EV_NOEXCEPT
  4277. {
  4278. clear_pending (EV_A_ (W)w);
  4279. if (ecb_expect_false (!ev_is_active (w)))
  4280. return;
  4281. EV_FREQUENT_CHECK;
  4282. {
  4283. int active = ev_active (w);
  4284. prepares [active - 1] = prepares [--preparecnt];
  4285. ev_active (prepares [active - 1]) = active;
  4286. }
  4287. ev_stop (EV_A_ (W)w);
  4288. EV_FREQUENT_CHECK;
  4289. }
  4290. #endif
  4291. #if EV_CHECK_ENABLE
  4292. void
  4293. ev_check_start (EV_P_ ev_check *w) EV_NOEXCEPT
  4294. {
  4295. if (ecb_expect_false (ev_is_active (w)))
  4296. return;
  4297. EV_FREQUENT_CHECK;
  4298. ev_start (EV_A_ (W)w, ++checkcnt);
  4299. array_needsize (ev_check *, checks, checkmax, checkcnt, array_needsize_noinit);
  4300. checks [checkcnt - 1] = w;
  4301. EV_FREQUENT_CHECK;
  4302. }
  4303. void
  4304. ev_check_stop (EV_P_ ev_check *w) EV_NOEXCEPT
  4305. {
  4306. clear_pending (EV_A_ (W)w);
  4307. if (ecb_expect_false (!ev_is_active (w)))
  4308. return;
  4309. EV_FREQUENT_CHECK;
  4310. {
  4311. int active = ev_active (w);
  4312. checks [active - 1] = checks [--checkcnt];
  4313. ev_active (checks [active - 1]) = active;
  4314. }
  4315. ev_stop (EV_A_ (W)w);
  4316. EV_FREQUENT_CHECK;
  4317. }
  4318. #endif
  4319. #if EV_EMBED_ENABLE
  4320. ecb_noinline
  4321. void
  4322. ev_embed_sweep (EV_P_ ev_embed *w) EV_NOEXCEPT
  4323. {
  4324. ev_run (w->other, EVRUN_NOWAIT);
  4325. }
  4326. static void
  4327. embed_io_cb (EV_P_ ev_io *io, int revents)
  4328. {
  4329. ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
  4330. if (ev_cb (w))
  4331. ev_feed_event (EV_A_ (W)w, EV_EMBED);
  4332. else
  4333. ev_run (w->other, EVRUN_NOWAIT);
  4334. }
  4335. static void
  4336. embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
  4337. {
  4338. ev_embed *w = (ev_embed *)(((char *)prepare) - offsetof (ev_embed, prepare));
  4339. {
  4340. EV_P = w->other;
  4341. while (fdchangecnt)
  4342. {
  4343. fd_reify (EV_A);
  4344. ev_run (EV_A_ EVRUN_NOWAIT);
  4345. }
  4346. }
  4347. }
  4348. #if EV_FORK_ENABLE
  4349. static void
  4350. embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
  4351. {
  4352. ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
  4353. ev_embed_stop (EV_A_ w);
  4354. {
  4355. EV_P = w->other;
  4356. ev_loop_fork (EV_A);
  4357. ev_run (EV_A_ EVRUN_NOWAIT);
  4358. }
  4359. ev_embed_start (EV_A_ w);
  4360. }
  4361. #endif
  4362. #if 0
  4363. static void
  4364. embed_idle_cb (EV_P_ ev_idle *idle, int revents)
  4365. {
  4366. ev_idle_stop (EV_A_ idle);
  4367. }
  4368. #endif
  4369. void
  4370. ev_embed_start (EV_P_ ev_embed *w) EV_NOEXCEPT
  4371. {
  4372. if (ecb_expect_false (ev_is_active (w)))
  4373. return;
  4374. {
  4375. EV_P = w->other;
  4376. assert (("libev: loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
  4377. ev_io_init (&w->io, embed_io_cb, backend_fd, EV_READ);
  4378. }
  4379. EV_FREQUENT_CHECK;
  4380. ev_set_priority (&w->io, ev_priority (w));
  4381. ev_io_start (EV_A_ &w->io);
  4382. ev_prepare_init (&w->prepare, embed_prepare_cb);
  4383. ev_set_priority (&w->prepare, EV_MINPRI);
  4384. ev_prepare_start (EV_A_ &w->prepare);
  4385. #if EV_FORK_ENABLE
  4386. ev_fork_init (&w->fork, embed_fork_cb);
  4387. ev_fork_start (EV_A_ &w->fork);
  4388. #endif
  4389. /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
  4390. ev_start (EV_A_ (W)w, 1);
  4391. EV_FREQUENT_CHECK;
  4392. }
  4393. void
  4394. ev_embed_stop (EV_P_ ev_embed *w) EV_NOEXCEPT
  4395. {
  4396. clear_pending (EV_A_ (W)w);
  4397. if (ecb_expect_false (!ev_is_active (w)))
  4398. return;
  4399. EV_FREQUENT_CHECK;
  4400. ev_io_stop (EV_A_ &w->io);
  4401. ev_prepare_stop (EV_A_ &w->prepare);
  4402. #if EV_FORK_ENABLE
  4403. ev_fork_stop (EV_A_ &w->fork);
  4404. #endif
  4405. ev_stop (EV_A_ (W)w);
  4406. EV_FREQUENT_CHECK;
  4407. }
  4408. #endif
  4409. #if EV_FORK_ENABLE
  4410. void
  4411. ev_fork_start (EV_P_ ev_fork *w) EV_NOEXCEPT
  4412. {
  4413. if (ecb_expect_false (ev_is_active (w)))
  4414. return;
  4415. EV_FREQUENT_CHECK;
  4416. ev_start (EV_A_ (W)w, ++forkcnt);
  4417. array_needsize (ev_fork *, forks, forkmax, forkcnt, array_needsize_noinit);
  4418. forks [forkcnt - 1] = w;
  4419. EV_FREQUENT_CHECK;
  4420. }
  4421. void
  4422. ev_fork_stop (EV_P_ ev_fork *w) EV_NOEXCEPT
  4423. {
  4424. clear_pending (EV_A_ (W)w);
  4425. if (ecb_expect_false (!ev_is_active (w)))
  4426. return;
  4427. EV_FREQUENT_CHECK;
  4428. {
  4429. int active = ev_active (w);
  4430. forks [active - 1] = forks [--forkcnt];
  4431. ev_active (forks [active - 1]) = active;
  4432. }
  4433. ev_stop (EV_A_ (W)w);
  4434. EV_FREQUENT_CHECK;
  4435. }
  4436. #endif
  4437. #if EV_CLEANUP_ENABLE
  4438. void
  4439. ev_cleanup_start (EV_P_ ev_cleanup *w) EV_NOEXCEPT
  4440. {
  4441. if (ecb_expect_false (ev_is_active (w)))
  4442. return;
  4443. EV_FREQUENT_CHECK;
  4444. ev_start (EV_A_ (W)w, ++cleanupcnt);
  4445. array_needsize (ev_cleanup *, cleanups, cleanupmax, cleanupcnt, array_needsize_noinit);
  4446. cleanups [cleanupcnt - 1] = w;
  4447. /* cleanup watchers should never keep a refcount on the loop */
  4448. ev_unref (EV_A);
  4449. EV_FREQUENT_CHECK;
  4450. }
  4451. void
  4452. ev_cleanup_stop (EV_P_ ev_cleanup *w) EV_NOEXCEPT
  4453. {
  4454. clear_pending (EV_A_ (W)w);
  4455. if (ecb_expect_false (!ev_is_active (w)))
  4456. return;
  4457. EV_FREQUENT_CHECK;
  4458. ev_ref (EV_A);
  4459. {
  4460. int active = ev_active (w);
  4461. cleanups [active - 1] = cleanups [--cleanupcnt];
  4462. ev_active (cleanups [active - 1]) = active;
  4463. }
  4464. ev_stop (EV_A_ (W)w);
  4465. EV_FREQUENT_CHECK;
  4466. }
  4467. #endif
  4468. #if EV_ASYNC_ENABLE
  4469. void
  4470. ev_async_start (EV_P_ ev_async *w) EV_NOEXCEPT
  4471. {
  4472. if (ecb_expect_false (ev_is_active (w)))
  4473. return;
  4474. w->sent = 0;
  4475. evpipe_init (EV_A);
  4476. EV_FREQUENT_CHECK;
  4477. ev_start (EV_A_ (W)w, ++asynccnt);
  4478. array_needsize (ev_async *, asyncs, asyncmax, asynccnt, array_needsize_noinit);
  4479. asyncs [asynccnt - 1] = w;
  4480. EV_FREQUENT_CHECK;
  4481. }
  4482. void
  4483. ev_async_stop (EV_P_ ev_async *w) EV_NOEXCEPT
  4484. {
  4485. clear_pending (EV_A_ (W)w);
  4486. if (ecb_expect_false (!ev_is_active (w)))
  4487. return;
  4488. EV_FREQUENT_CHECK;
  4489. {
  4490. int active = ev_active (w);
  4491. asyncs [active - 1] = asyncs [--asynccnt];
  4492. ev_active (asyncs [active - 1]) = active;
  4493. }
  4494. ev_stop (EV_A_ (W)w);
  4495. EV_FREQUENT_CHECK;
  4496. }
  4497. void
  4498. ev_async_send (EV_P_ ev_async *w) EV_NOEXCEPT
  4499. {
  4500. w->sent = 1;
  4501. evpipe_write (EV_A_ &async_pending);
  4502. }
  4503. #endif
  4504. /*****************************************************************************/
  4505. struct ev_once
  4506. {
  4507. ev_io io;
  4508. ev_timer to;
  4509. void (*cb)(int revents, void *arg);
  4510. void *arg;
  4511. };
  4512. static void
  4513. once_cb (EV_P_ struct ev_once *once, int revents)
  4514. {
  4515. void (*cb)(int revents, void *arg) = once->cb;
  4516. void *arg = once->arg;
  4517. ev_io_stop (EV_A_ &once->io);
  4518. ev_timer_stop (EV_A_ &once->to);
  4519. ev_free (once);
  4520. cb (revents, arg);
  4521. }
  4522. static void
  4523. once_cb_io (EV_P_ ev_io *w, int revents)
  4524. {
  4525. struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io));
  4526. once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to));
  4527. }
  4528. static void
  4529. once_cb_to (EV_P_ ev_timer *w, int revents)
  4530. {
  4531. struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to));
  4532. once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
  4533. }
  4534. void
  4535. ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_NOEXCEPT
  4536. {
  4537. struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
  4538. once->cb = cb;
  4539. once->arg = arg;
  4540. ev_init (&once->io, once_cb_io);
  4541. if (fd >= 0)
  4542. {
  4543. ev_io_set (&once->io, fd, events);
  4544. ev_io_start (EV_A_ &once->io);
  4545. }
  4546. ev_init (&once->to, once_cb_to);
  4547. if (timeout >= 0.)
  4548. {
  4549. ev_timer_set (&once->to, timeout, 0.);
  4550. ev_timer_start (EV_A_ &once->to);
  4551. }
  4552. }
  4553. /*****************************************************************************/
  4554. #if EV_WALK_ENABLE
  4555. ecb_cold
  4556. void
  4557. ev_walk (EV_P_ int types, void (*cb)(EV_P_ int type, void *w)) EV_NOEXCEPT
  4558. {
  4559. int i, j;
  4560. ev_watcher_list *wl, *wn;
  4561. if (types & (EV_IO | EV_EMBED))
  4562. for (i = 0; i < anfdmax; ++i)
  4563. for (wl = anfds [i].head; wl; )
  4564. {
  4565. wn = wl->next;
  4566. #if EV_EMBED_ENABLE
  4567. if (ev_cb ((ev_io *)wl) == embed_io_cb)
  4568. {
  4569. if (types & EV_EMBED)
  4570. cb (EV_A_ EV_EMBED, ((char *)wl) - offsetof (struct ev_embed, io));
  4571. }
  4572. else
  4573. #endif
  4574. #if EV_USE_INOTIFY
  4575. if (ev_cb ((ev_io *)wl) == infy_cb)
  4576. ;
  4577. else
  4578. #endif
  4579. if ((ev_io *)wl != &pipe_w)
  4580. if (types & EV_IO)
  4581. cb (EV_A_ EV_IO, wl);
  4582. wl = wn;
  4583. }
  4584. if (types & (EV_TIMER | EV_STAT))
  4585. for (i = timercnt + HEAP0; i-- > HEAP0; )
  4586. #if EV_STAT_ENABLE
  4587. /*TODO: timer is not always active*/
  4588. if (ev_cb ((ev_timer *)ANHE_w (timers [i])) == stat_timer_cb)
  4589. {
  4590. if (types & EV_STAT)
  4591. cb (EV_A_ EV_STAT, ((char *)ANHE_w (timers [i])) - offsetof (struct ev_stat, timer));
  4592. }
  4593. else
  4594. #endif
  4595. if (types & EV_TIMER)
  4596. cb (EV_A_ EV_TIMER, ANHE_w (timers [i]));
  4597. #if EV_PERIODIC_ENABLE
  4598. if (types & EV_PERIODIC)
  4599. for (i = periodiccnt + HEAP0; i-- > HEAP0; )
  4600. cb (EV_A_ EV_PERIODIC, ANHE_w (periodics [i]));
  4601. #endif
  4602. #if EV_IDLE_ENABLE
  4603. if (types & EV_IDLE)
  4604. for (j = NUMPRI; j--; )
  4605. for (i = idlecnt [j]; i--; )
  4606. cb (EV_A_ EV_IDLE, idles [j][i]);
  4607. #endif
  4608. #if EV_FORK_ENABLE
  4609. if (types & EV_FORK)
  4610. for (i = forkcnt; i--; )
  4611. if (ev_cb (forks [i]) != embed_fork_cb)
  4612. cb (EV_A_ EV_FORK, forks [i]);
  4613. #endif
  4614. #if EV_ASYNC_ENABLE
  4615. if (types & EV_ASYNC)
  4616. for (i = asynccnt; i--; )
  4617. cb (EV_A_ EV_ASYNC, asyncs [i]);
  4618. #endif
  4619. #if EV_PREPARE_ENABLE
  4620. if (types & EV_PREPARE)
  4621. for (i = preparecnt; i--; )
  4622. # if EV_EMBED_ENABLE
  4623. if (ev_cb (prepares [i]) != embed_prepare_cb)
  4624. # endif
  4625. cb (EV_A_ EV_PREPARE, prepares [i]);
  4626. #endif
  4627. #if EV_CHECK_ENABLE
  4628. if (types & EV_CHECK)
  4629. for (i = checkcnt; i--; )
  4630. cb (EV_A_ EV_CHECK, checks [i]);
  4631. #endif
  4632. #if EV_SIGNAL_ENABLE
  4633. if (types & EV_SIGNAL)
  4634. for (i = 0; i < EV_NSIG - 1; ++i)
  4635. for (wl = signals [i].head; wl; )
  4636. {
  4637. wn = wl->next;
  4638. cb (EV_A_ EV_SIGNAL, wl);
  4639. wl = wn;
  4640. }
  4641. #endif
  4642. #if EV_CHILD_ENABLE
  4643. if (types & EV_CHILD)
  4644. for (i = (EV_PID_HASHSIZE); i--; )
  4645. for (wl = childs [i]; wl; )
  4646. {
  4647. wn = wl->next;
  4648. cb (EV_A_ EV_CHILD, wl);
  4649. wl = wn;
  4650. }
  4651. #endif
  4652. /* EV_STAT 0x00001000 /* stat data changed */
  4653. /* EV_EMBED 0x00010000 /* embedded event loop needs sweep */
  4654. }
  4655. #endif
  4656. #if EV_MULTIPLICITY
  4657. #include "ev_wrap.h"
  4658. #endif