/src/libev/ev.c

http://github.com/jacksonh/manos · C · 3878 lines · 2930 code · 703 blank · 245 comment · 505 complexity · 8beea16dda02c1d1b8b9d1c7d1de7e57 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * libev event processing core, watcher management
  3. *
  4. * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modifica-
  8. * tion, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
  19. * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
  21. * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
  25. * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  26. * OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * the GNU General Public License ("GPL") version 2 or any later version,
  30. * in which case the provisions of the GPL are applicable instead of
  31. * the above. If you wish to allow the use of your version of this file
  32. * only under the terms of the GPL and not to allow others to use your
  33. * version of this file under the BSD license, indicate your decision
  34. * by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL. If you do not delete the
  36. * provisions above, a recipient may use your version of this file under
  37. * either the BSD or the GPL.
  38. */
  39. /* 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_CLOCK_SYSCALL
  47. # ifndef EV_USE_CLOCK_SYSCALL
  48. # define EV_USE_CLOCK_SYSCALL 1
  49. # ifndef EV_USE_REALTIME
  50. # define EV_USE_REALTIME 0
  51. # endif
  52. # ifndef EV_USE_MONOTONIC
  53. # define EV_USE_MONOTONIC 1
  54. # endif
  55. # endif
  56. # elif !defined(EV_USE_CLOCK_SYSCALL)
  57. # define EV_USE_CLOCK_SYSCALL 0
  58. # endif
  59. # if HAVE_CLOCK_GETTIME
  60. # ifndef EV_USE_MONOTONIC
  61. # define EV_USE_MONOTONIC 1
  62. # endif
  63. # ifndef EV_USE_REALTIME
  64. # define EV_USE_REALTIME 0
  65. # endif
  66. # else
  67. # ifndef EV_USE_MONOTONIC
  68. # define EV_USE_MONOTONIC 0
  69. # endif
  70. # ifndef EV_USE_REALTIME
  71. # define EV_USE_REALTIME 0
  72. # endif
  73. # endif
  74. # if HAVE_NANOSLEEP
  75. # ifndef EV_USE_NANOSLEEP
  76. # define EV_USE_NANOSLEEP EV_FEATURE_OS
  77. # endif
  78. # else
  79. # undef EV_USE_NANOSLEEP
  80. # define EV_USE_NANOSLEEP 0
  81. # endif
  82. # if HAVE_SELECT && HAVE_SYS_SELECT_H
  83. # ifndef EV_USE_SELECT
  84. # define EV_USE_SELECT EV_FEATURE_BACKENDS
  85. # endif
  86. # else
  87. # undef EV_USE_SELECT
  88. # define EV_USE_SELECT 0
  89. # endif
  90. # if HAVE_POLL && HAVE_POLL_H
  91. # ifndef EV_USE_POLL
  92. # define EV_USE_POLL EV_FEATURE_BACKENDS
  93. # endif
  94. # else
  95. # undef EV_USE_POLL
  96. # define EV_USE_POLL 0
  97. # endif
  98. # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
  99. # ifndef EV_USE_EPOLL
  100. # define EV_USE_EPOLL EV_FEATURE_BACKENDS
  101. # endif
  102. # else
  103. # undef EV_USE_EPOLL
  104. # define EV_USE_EPOLL 0
  105. # endif
  106. # if HAVE_KQUEUE && HAVE_SYS_EVENT_H
  107. # ifndef EV_USE_KQUEUE
  108. # define EV_USE_KQUEUE EV_FEATURE_BACKENDS
  109. # endif
  110. # else
  111. # undef EV_USE_KQUEUE
  112. # define EV_USE_KQUEUE 0
  113. # endif
  114. # if HAVE_PORT_H && HAVE_PORT_CREATE
  115. # ifndef EV_USE_PORT
  116. # define EV_USE_PORT EV_FEATURE_BACKENDS
  117. # endif
  118. # else
  119. # undef EV_USE_PORT
  120. # define EV_USE_PORT 0
  121. # endif
  122. # if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
  123. # ifndef EV_USE_INOTIFY
  124. # define EV_USE_INOTIFY EV_FEATURE_OS
  125. # endif
  126. # else
  127. # undef EV_USE_INOTIFY
  128. # define EV_USE_INOTIFY 0
  129. # endif
  130. # if HAVE_SIGNALFD && HAVE_SYS_SIGNALFD_H
  131. # ifndef EV_USE_SIGNALFD
  132. # define EV_USE_SIGNALFD EV_FEATURE_OS
  133. # endif
  134. # else
  135. # undef EV_USE_SIGNALFD
  136. # define EV_USE_SIGNALFD 0
  137. # endif
  138. # if HAVE_EVENTFD
  139. # ifndef EV_USE_EVENTFD
  140. # define EV_USE_EVENTFD EV_FEATURE_OS
  141. # endif
  142. # else
  143. # undef EV_USE_EVENTFD
  144. # define EV_USE_EVENTFD 0
  145. # endif
  146. #endif
  147. #include <math.h>
  148. #include <stdlib.h>
  149. #include <string.h>
  150. #include <fcntl.h>
  151. #include <stddef.h>
  152. #include <stdio.h>
  153. #include <assert.h>
  154. #include <errno.h>
  155. #include <sys/types.h>
  156. #include <time.h>
  157. #include <limits.h>
  158. #include <signal.h>
  159. #ifdef EV_H
  160. # include EV_H
  161. #else
  162. # include "ev.h"
  163. #endif
  164. EV_CPP(extern "C" {)
  165. #ifndef _WIN32
  166. # include <sys/time.h>
  167. # include <sys/wait.h>
  168. # include <unistd.h>
  169. #else
  170. # include <io.h>
  171. # define WIN32_LEAN_AND_MEAN
  172. # include <windows.h>
  173. # ifndef EV_SELECT_IS_WINSOCKET
  174. # define EV_SELECT_IS_WINSOCKET 1
  175. # endif
  176. # undef EV_AVOID_STDIO
  177. #endif
  178. /* OS X, in its infinite idiocy, actually HARDCODES
  179. * a limit of 1024 into their select. Where people have brains,
  180. * OS X engineers apparently have a vacuum. Or maybe they were
  181. * ordered to have a vacuum, or they do anything for money.
  182. * This might help. Or not.
  183. */
  184. #define _DARWIN_UNLIMITED_SELECT 1
  185. /* this block tries to deduce configuration from header-defined symbols and defaults */
  186. /* try to deduce the maximum number of signals on this platform */
  187. #if defined (EV_NSIG)
  188. /* use what's provided */
  189. #elif defined (NSIG)
  190. # define EV_NSIG (NSIG)
  191. #elif defined(_NSIG)
  192. # define EV_NSIG (_NSIG)
  193. #elif defined (SIGMAX)
  194. # define EV_NSIG (SIGMAX+1)
  195. #elif defined (SIG_MAX)
  196. # define EV_NSIG (SIG_MAX+1)
  197. #elif defined (_SIG_MAX)
  198. # define EV_NSIG (_SIG_MAX+1)
  199. #elif defined (MAXSIG)
  200. # define EV_NSIG (MAXSIG+1)
  201. #elif defined (MAX_SIG)
  202. # define EV_NSIG (MAX_SIG+1)
  203. #elif defined (SIGARRAYSIZE)
  204. # define EV_NSIG (SIGARRAYSIZE) /* Assume ary[SIGARRAYSIZE] */
  205. #elif defined (_sys_nsig)
  206. # define EV_NSIG (_sys_nsig) /* Solaris 2.5 */
  207. #else
  208. # error "unable to find value for NSIG, please report"
  209. /* to make it compile regardless, just remove the above line, */
  210. /* but consider reporting it, too! :) */
  211. # define EV_NSIG 65
  212. #endif
  213. #ifndef EV_USE_CLOCK_SYSCALL
  214. # if __linux && __GLIBC__ >= 2
  215. # define EV_USE_CLOCK_SYSCALL EV_FEATURE_OS
  216. # else
  217. # define EV_USE_CLOCK_SYSCALL 0
  218. # endif
  219. #endif
  220. #ifndef EV_USE_MONOTONIC
  221. # if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
  222. # define EV_USE_MONOTONIC EV_FEATURE_OS
  223. # else
  224. # define EV_USE_MONOTONIC 0
  225. # endif
  226. #endif
  227. #ifndef EV_USE_REALTIME
  228. # define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
  229. #endif
  230. #ifndef EV_USE_NANOSLEEP
  231. # if _POSIX_C_SOURCE >= 199309L
  232. # define EV_USE_NANOSLEEP EV_FEATURE_OS
  233. # else
  234. # define EV_USE_NANOSLEEP 0
  235. # endif
  236. #endif
  237. #ifndef EV_USE_SELECT
  238. # define EV_USE_SELECT EV_FEATURE_BACKENDS
  239. #endif
  240. #ifndef EV_USE_POLL
  241. # ifdef _WIN32
  242. # define EV_USE_POLL 0
  243. # else
  244. # define EV_USE_POLL EV_FEATURE_BACKENDS
  245. # endif
  246. #endif
  247. #ifndef EV_USE_EPOLL
  248. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
  249. # define EV_USE_EPOLL EV_FEATURE_BACKENDS
  250. # else
  251. # define EV_USE_EPOLL 0
  252. # endif
  253. #endif
  254. #ifndef EV_USE_KQUEUE
  255. # define EV_USE_KQUEUE 0
  256. #endif
  257. #ifndef EV_USE_PORT
  258. # define EV_USE_PORT 0
  259. #endif
  260. #ifndef EV_USE_INOTIFY
  261. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
  262. # define EV_USE_INOTIFY EV_FEATURE_OS
  263. # else
  264. # define EV_USE_INOTIFY 0
  265. # endif
  266. #endif
  267. #ifndef EV_PID_HASHSIZE
  268. # define EV_PID_HASHSIZE EV_FEATURE_DATA ? 16 : 1
  269. #endif
  270. #ifndef EV_INOTIFY_HASHSIZE
  271. # define EV_INOTIFY_HASHSIZE EV_FEATURE_DATA ? 16 : 1
  272. #endif
  273. #ifndef EV_USE_EVENTFD
  274. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
  275. # define EV_USE_EVENTFD EV_FEATURE_OS
  276. # else
  277. # define EV_USE_EVENTFD 0
  278. # endif
  279. #endif
  280. #ifndef EV_USE_SIGNALFD
  281. # if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
  282. # define EV_USE_SIGNALFD EV_FEATURE_OS
  283. # else
  284. # define EV_USE_SIGNALFD 0
  285. # endif
  286. #endif
  287. #if 0 /* debugging */
  288. # define EV_VERIFY 3
  289. # define EV_USE_4HEAP 1
  290. # define EV_HEAP_CACHE_AT 1
  291. #endif
  292. #ifndef EV_VERIFY
  293. # define EV_VERIFY (EV_FEATURE_API ? 1 : 0)
  294. #endif
  295. #ifndef EV_USE_4HEAP
  296. # define EV_USE_4HEAP EV_FEATURE_DATA
  297. #endif
  298. #ifndef EV_HEAP_CACHE_AT
  299. # define EV_HEAP_CACHE_AT EV_FEATURE_DATA
  300. #endif
  301. /* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
  302. /* which makes programs even slower. might work on other unices, too. */
  303. #if EV_USE_CLOCK_SYSCALL
  304. # include <syscall.h>
  305. # ifdef SYS_clock_gettime
  306. # define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
  307. # undef EV_USE_MONOTONIC
  308. # define EV_USE_MONOTONIC 1
  309. # else
  310. # undef EV_USE_CLOCK_SYSCALL
  311. # define EV_USE_CLOCK_SYSCALL 0
  312. # endif
  313. #endif
  314. /* this block fixes any misconfiguration where we know we run into trouble otherwise */
  315. #ifdef _AIX
  316. /* AIX has a completely broken poll.h header */
  317. # undef EV_USE_POLL
  318. # define EV_USE_POLL 0
  319. #endif
  320. #ifndef CLOCK_MONOTONIC
  321. # undef EV_USE_MONOTONIC
  322. # define EV_USE_MONOTONIC 0
  323. #endif
  324. #ifndef CLOCK_REALTIME
  325. # undef EV_USE_REALTIME
  326. # define EV_USE_REALTIME 0
  327. #endif
  328. #if !EV_STAT_ENABLE
  329. # undef EV_USE_INOTIFY
  330. # define EV_USE_INOTIFY 0
  331. #endif
  332. #if !EV_USE_NANOSLEEP
  333. # ifndef _WIN32
  334. # include <sys/select.h>
  335. # endif
  336. #endif
  337. #if EV_USE_INOTIFY
  338. # include <sys/statfs.h>
  339. # include <sys/inotify.h>
  340. /* some very old inotify.h headers don't have IN_DONT_FOLLOW */
  341. # ifndef IN_DONT_FOLLOW
  342. # undef EV_USE_INOTIFY
  343. # define EV_USE_INOTIFY 0
  344. # endif
  345. #endif
  346. #if EV_SELECT_IS_WINSOCKET
  347. # include <winsock.h>
  348. #endif
  349. #if EV_USE_EVENTFD
  350. /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
  351. # include <stdint.h>
  352. # ifndef EFD_NONBLOCK
  353. # define EFD_NONBLOCK O_NONBLOCK
  354. # endif
  355. # ifndef EFD_CLOEXEC
  356. # ifdef O_CLOEXEC
  357. # define EFD_CLOEXEC O_CLOEXEC
  358. # else
  359. # define EFD_CLOEXEC 02000000
  360. # endif
  361. # endif
  362. EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
  363. #endif
  364. #if EV_USE_SIGNALFD
  365. /* our minimum requirement is glibc 2.7 which has the stub, but not the header */
  366. # include <stdint.h>
  367. # ifndef SFD_NONBLOCK
  368. # define SFD_NONBLOCK O_NONBLOCK
  369. # endif
  370. # ifndef SFD_CLOEXEC
  371. # ifdef O_CLOEXEC
  372. # define SFD_CLOEXEC O_CLOEXEC
  373. # else
  374. # define SFD_CLOEXEC 02000000
  375. # endif
  376. # endif
  377. EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
  378. struct signalfd_siginfo
  379. {
  380. uint32_t ssi_signo;
  381. char pad[128 - sizeof (uint32_t)];
  382. };
  383. #endif
  384. /**/
  385. #if EV_VERIFY >= 3
  386. # define EV_FREQUENT_CHECK ev_verify (EV_A)
  387. #else
  388. # define EV_FREQUENT_CHECK do { } while (0)
  389. #endif
  390. /*
  391. * This is used to avoid floating point rounding problems.
  392. * It is added to ev_rt_now when scheduling periodics
  393. * to ensure progress, time-wise, even when rounding
  394. * errors are against us.
  395. * This value is good at least till the year 4000.
  396. * Better solutions welcome.
  397. */
  398. #define TIME_EPSILON 0.0001220703125 /* 1/8192 */
  399. #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
  400. #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
  401. #define EV_TV_SET(tv,t) do { tv.tv_sec = (long)t; tv.tv_usec = (long)((t - tv.tv_sec) * 1e6); } while (0)
  402. #define EV_TS_SET(ts,t) do { ts.tv_sec = (long)t; ts.tv_nsec = (long)((t - ts.tv_sec) * 1e9); } while (0)
  403. #if __GNUC__ >= 4
  404. # define expect(expr,value) __builtin_expect ((expr),(value))
  405. # define noinline __attribute__ ((noinline))
  406. #else
  407. # define expect(expr,value) (expr)
  408. # define noinline
  409. # if __STDC_VERSION__ < 199901L && __GNUC__ < 2
  410. # define inline
  411. # endif
  412. #endif
  413. #define expect_false(expr) expect ((expr) != 0, 0)
  414. #define expect_true(expr) expect ((expr) != 0, 1)
  415. #define inline_size static inline
  416. #if EV_FEATURE_CODE
  417. # define inline_speed static inline
  418. #else
  419. # define inline_speed static noinline
  420. #endif
  421. #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
  422. #if EV_MINPRI == EV_MAXPRI
  423. # define ABSPRI(w) (((W)w), 0)
  424. #else
  425. # define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
  426. #endif
  427. #define EMPTY /* required for microsofts broken pseudo-c compiler */
  428. #define EMPTY2(a,b) /* used to suppress some warnings */
  429. typedef ev_watcher *W;
  430. typedef ev_watcher_list *WL;
  431. typedef ev_watcher_time *WT;
  432. #define ev_active(w) ((W)(w))->active
  433. #define ev_at(w) ((WT)(w))->at
  434. #if EV_USE_REALTIME
  435. /* sig_atomic_t is used to avoid per-thread variables or locking but still */
  436. /* giving it a reasonably high chance of working on typical architectures */
  437. static EV_ATOMIC_T have_realtime; /* did clock_gettime (CLOCK_REALTIME) work? */
  438. #endif
  439. #if EV_USE_MONOTONIC
  440. static EV_ATOMIC_T have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
  441. #endif
  442. #ifndef EV_FD_TO_WIN32_HANDLE
  443. # define EV_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd)
  444. #endif
  445. #ifndef EV_WIN32_HANDLE_TO_FD
  446. # define EV_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0)
  447. #endif
  448. #ifndef EV_WIN32_CLOSE_FD
  449. # define EV_WIN32_CLOSE_FD(fd) close (fd)
  450. #endif
  451. #ifdef _WIN32
  452. # include "ev_win32.c"
  453. #endif
  454. /*****************************************************************************/
  455. #ifdef __linux
  456. # include <sys/utsname.h>
  457. #endif
  458. static unsigned int noinline
  459. ev_linux_version (void)
  460. {
  461. #ifdef __linux
  462. unsigned int v = 0;
  463. struct utsname buf;
  464. int i;
  465. char *p = buf.release;
  466. if (uname (&buf))
  467. return 0;
  468. for (i = 3+1; --i; )
  469. {
  470. unsigned int c = 0;
  471. for (;;)
  472. {
  473. if (*p >= '0' && *p <= '9')
  474. c = c * 10 + *p++ - '0';
  475. else
  476. {
  477. p += *p == '.';
  478. break;
  479. }
  480. }
  481. v = (v << 8) | c;
  482. }
  483. return v;
  484. #else
  485. return 0;
  486. #endif
  487. }
  488. /*****************************************************************************/
  489. #if EV_AVOID_STDIO
  490. static void noinline
  491. ev_printerr (const char *msg)
  492. {
  493. write (STDERR_FILENO, msg, strlen (msg));
  494. }
  495. #endif
  496. static void (*syserr_cb)(const char *msg);
  497. void
  498. ev_set_syserr_cb (void (*cb)(const char *msg))
  499. {
  500. syserr_cb = cb;
  501. }
  502. static void noinline
  503. ev_syserr (const char *msg)
  504. {
  505. if (!msg)
  506. msg = "(libev) system error";
  507. if (syserr_cb)
  508. syserr_cb (msg);
  509. else
  510. {
  511. #if EV_AVOID_STDIO
  512. ev_printerr (msg);
  513. ev_printerr (": ");
  514. ev_printerr (strerror (errno));
  515. ev_printerr ("\n");
  516. #else
  517. perror (msg);
  518. #endif
  519. abort ();
  520. }
  521. }
  522. static void *
  523. ev_realloc_emul (void *ptr, long size)
  524. {
  525. #if __GLIBC__
  526. return realloc (ptr, size);
  527. #else
  528. /* some systems, notably openbsd and darwin, fail to properly
  529. * implement realloc (x, 0) (as required by both ansi c-89 and
  530. * the single unix specification, so work around them here.
  531. */
  532. if (size)
  533. return realloc (ptr, size);
  534. free (ptr);
  535. return 0;
  536. #endif
  537. }
  538. static void *(*alloc)(void *ptr, long size) = ev_realloc_emul;
  539. void
  540. ev_set_allocator (void *(*cb)(void *ptr, long size))
  541. {
  542. alloc = cb;
  543. }
  544. inline_speed void *
  545. ev_realloc (void *ptr, long size)
  546. {
  547. ptr = alloc (ptr, size);
  548. if (!ptr && size)
  549. {
  550. #if EV_AVOID_STDIO
  551. ev_printerr ("(libev) memory allocation failed, aborting.\n");
  552. #else
  553. fprintf (stderr, "(libev) cannot allocate %ld bytes, aborting.", size);
  554. #endif
  555. abort ();
  556. }
  557. return ptr;
  558. }
  559. #define ev_malloc(size) ev_realloc (0, (size))
  560. #define ev_free(ptr) ev_realloc ((ptr), 0)
  561. /*****************************************************************************/
  562. /* set in reify when reification needed */
  563. #define EV_ANFD_REIFY 1
  564. /* file descriptor info structure */
  565. typedef struct
  566. {
  567. WL head;
  568. unsigned char events; /* the events watched for */
  569. unsigned char reify; /* flag set when this ANFD needs reification (EV_ANFD_REIFY, EV__IOFDSET) */
  570. unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
  571. unsigned char unused;
  572. #if EV_USE_EPOLL
  573. unsigned int egen; /* generation counter to counter epoll bugs */
  574. #endif
  575. #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  576. SOCKET handle;
  577. #endif
  578. #if EV_USE_IOCP
  579. OVERLAPPED or, ow;
  580. #endif
  581. } ANFD;
  582. /* stores the pending event set for a given watcher */
  583. typedef struct
  584. {
  585. W w;
  586. int events; /* the pending event set for the given watcher */
  587. } ANPENDING;
  588. #if EV_USE_INOTIFY
  589. /* hash table entry per inotify-id */
  590. typedef struct
  591. {
  592. WL head;
  593. } ANFS;
  594. #endif
  595. /* Heap Entry */
  596. #if EV_HEAP_CACHE_AT
  597. /* a heap element */
  598. typedef struct {
  599. ev_tstamp at;
  600. WT w;
  601. } ANHE;
  602. #define ANHE_w(he) (he).w /* access watcher, read-write */
  603. #define ANHE_at(he) (he).at /* access cached at, read-only */
  604. #define ANHE_at_cache(he) (he).at = (he).w->at /* update at from watcher */
  605. #else
  606. /* a heap element */
  607. typedef WT ANHE;
  608. #define ANHE_w(he) (he)
  609. #define ANHE_at(he) (he)->at
  610. #define ANHE_at_cache(he)
  611. #endif
  612. #if EV_MULTIPLICITY
  613. struct ev_loop
  614. {
  615. ev_tstamp ev_rt_now;
  616. #define ev_rt_now ((loop)->ev_rt_now)
  617. #define VAR(name,decl) decl;
  618. #include "ev_vars.h"
  619. #undef VAR
  620. };
  621. #include "ev_wrap.h"
  622. static struct ev_loop default_loop_struct;
  623. struct ev_loop *ev_default_loop_ptr;
  624. #else
  625. ev_tstamp ev_rt_now;
  626. #define VAR(name,decl) static decl;
  627. #include "ev_vars.h"
  628. #undef VAR
  629. static int ev_default_loop_ptr;
  630. #endif
  631. #if EV_FEATURE_API
  632. # define EV_RELEASE_CB if (expect_false (release_cb)) release_cb (EV_A)
  633. # define EV_ACQUIRE_CB if (expect_false (acquire_cb)) acquire_cb (EV_A)
  634. # define EV_INVOKE_PENDING invoke_cb (EV_A)
  635. #else
  636. # define EV_RELEASE_CB (void)0
  637. # define EV_ACQUIRE_CB (void)0
  638. # define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
  639. #endif
  640. #define EVBREAK_RECURSE 0x80
  641. /*****************************************************************************/
  642. #ifndef EV_HAVE_EV_TIME
  643. ev_tstamp
  644. ev_time (void)
  645. {
  646. #if EV_USE_REALTIME
  647. if (expect_true (have_realtime))
  648. {
  649. struct timespec ts;
  650. clock_gettime (CLOCK_REALTIME, &ts);
  651. return ts.tv_sec + ts.tv_nsec * 1e-9;
  652. }
  653. #endif
  654. struct timeval tv;
  655. gettimeofday (&tv, 0);
  656. return tv.tv_sec + tv.tv_usec * 1e-6;
  657. }
  658. #endif
  659. inline_size ev_tstamp
  660. get_clock (void)
  661. {
  662. #if EV_USE_MONOTONIC
  663. if (expect_true (have_monotonic))
  664. {
  665. struct timespec ts;
  666. clock_gettime (CLOCK_MONOTONIC, &ts);
  667. return ts.tv_sec + ts.tv_nsec * 1e-9;
  668. }
  669. #endif
  670. return ev_time ();
  671. }
  672. #if EV_MULTIPLICITY
  673. ev_tstamp
  674. ev_now (EV_P)
  675. {
  676. return ev_rt_now;
  677. }
  678. #endif
  679. void
  680. ev_sleep (ev_tstamp delay)
  681. {
  682. if (delay > 0.)
  683. {
  684. #if EV_USE_NANOSLEEP
  685. struct timespec ts;
  686. EV_TS_SET (ts, delay);
  687. nanosleep (&ts, 0);
  688. #elif defined(_WIN32)
  689. Sleep ((unsigned long)(delay * 1e3));
  690. #else
  691. struct timeval tv;
  692. /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
  693. /* something not guaranteed by newer posix versions, but guaranteed */
  694. /* by older ones */
  695. EV_TV_SET (tv, delay);
  696. select (0, 0, 0, 0, &tv);
  697. #endif
  698. }
  699. }
  700. /*****************************************************************************/
  701. #define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
  702. /* find a suitable new size for the given array, */
  703. /* hopefully by rounding to a nice-to-malloc size */
  704. inline_size int
  705. array_nextsize (int elem, int cur, int cnt)
  706. {
  707. int ncur = cur + 1;
  708. do
  709. ncur <<= 1;
  710. while (cnt > ncur);
  711. /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
  712. if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
  713. {
  714. ncur *= elem;
  715. ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
  716. ncur = ncur - sizeof (void *) * 4;
  717. ncur /= elem;
  718. }
  719. return ncur;
  720. }
  721. static noinline void *
  722. array_realloc (int elem, void *base, int *cur, int cnt)
  723. {
  724. *cur = array_nextsize (elem, *cur, cnt);
  725. return ev_realloc (base, elem * *cur);
  726. }
  727. #define array_init_zero(base,count) \
  728. memset ((void *)(base), 0, sizeof (*(base)) * (count))
  729. #define array_needsize(type,base,cur,cnt,init) \
  730. if (expect_false ((cnt) > (cur))) \
  731. { \
  732. int ocur_ = (cur); \
  733. (base) = (type *)array_realloc \
  734. (sizeof (type), (base), &(cur), (cnt)); \
  735. init ((base) + (ocur_), (cur) - ocur_); \
  736. }
  737. #if 0
  738. #define array_slim(type,stem) \
  739. if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
  740. { \
  741. stem ## max = array_roundsize (stem ## cnt >> 1); \
  742. base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
  743. fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
  744. }
  745. #endif
  746. #define array_free(stem, idx) \
  747. ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; stem ## s idx = 0
  748. /*****************************************************************************/
  749. /* dummy callback for pending events */
  750. static void noinline
  751. pendingcb (EV_P_ ev_prepare *w, int revents)
  752. {
  753. }
  754. void noinline
  755. ev_feed_event (EV_P_ void *w, int revents)
  756. {
  757. W w_ = (W)w;
  758. int pri = ABSPRI (w_);
  759. if (expect_false (w_->pending))
  760. pendings [pri][w_->pending - 1].events |= revents;
  761. else
  762. {
  763. w_->pending = ++pendingcnt [pri];
  764. array_needsize (ANPENDING, pendings [pri], pendingmax [pri], w_->pending, EMPTY2);
  765. pendings [pri][w_->pending - 1].w = w_;
  766. pendings [pri][w_->pending - 1].events = revents;
  767. }
  768. }
  769. inline_speed void
  770. feed_reverse (EV_P_ W w)
  771. {
  772. array_needsize (W, rfeeds, rfeedmax, rfeedcnt + 1, EMPTY2);
  773. rfeeds [rfeedcnt++] = w;
  774. }
  775. inline_size void
  776. feed_reverse_done (EV_P_ int revents)
  777. {
  778. do
  779. ev_feed_event (EV_A_ rfeeds [--rfeedcnt], revents);
  780. while (rfeedcnt);
  781. }
  782. inline_speed void
  783. queue_events (EV_P_ W *events, int eventcnt, int type)
  784. {
  785. int i;
  786. for (i = 0; i < eventcnt; ++i)
  787. ev_feed_event (EV_A_ events [i], type);
  788. }
  789. /*****************************************************************************/
  790. inline_speed void
  791. fd_event_nocheck (EV_P_ int fd, int revents)
  792. {
  793. ANFD *anfd = anfds + fd;
  794. ev_io *w;
  795. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  796. {
  797. int ev = w->events & revents;
  798. if (ev)
  799. ev_feed_event (EV_A_ (W)w, ev);
  800. }
  801. }
  802. /* do not submit kernel events for fds that have reify set */
  803. /* because that means they changed while we were polling for new events */
  804. inline_speed void
  805. fd_event (EV_P_ int fd, int revents)
  806. {
  807. ANFD *anfd = anfds + fd;
  808. if (expect_true (!anfd->reify))
  809. fd_event_nocheck (EV_A_ fd, revents);
  810. }
  811. void
  812. ev_feed_fd_event (EV_P_ int fd, int revents)
  813. {
  814. if (fd >= 0 && fd < anfdmax)
  815. fd_event_nocheck (EV_A_ fd, revents);
  816. }
  817. /* make sure the external fd watch events are in-sync */
  818. /* with the kernel/libev internal state */
  819. inline_size void
  820. fd_reify (EV_P)
  821. {
  822. int i;
  823. for (i = 0; i < fdchangecnt; ++i)
  824. {
  825. int fd = fdchanges [i];
  826. ANFD *anfd = anfds + fd;
  827. ev_io *w;
  828. unsigned char o_events = anfd->events;
  829. unsigned char o_reify = anfd->reify;
  830. anfd->reify = 0;
  831. #if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
  832. if (o_reify & EV__IOFDSET)
  833. {
  834. unsigned long arg;
  835. anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
  836. assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
  837. printf ("oi %d %x\n", fd, anfd->handle);//D
  838. }
  839. #endif
  840. /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
  841. {
  842. anfd->events = 0;
  843. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  844. anfd->events |= (unsigned char)w->events;
  845. if (o_events != anfd->events)
  846. o_reify = EV__IOFDSET; /* actually |= */
  847. }
  848. if (o_reify & EV__IOFDSET)
  849. backend_modify (EV_A_ fd, o_events, anfd->events);
  850. }
  851. fdchangecnt = 0;
  852. }
  853. /* something about the given fd changed */
  854. inline_size void
  855. fd_change (EV_P_ int fd, int flags)
  856. {
  857. unsigned char reify = anfds [fd].reify;
  858. anfds [fd].reify |= flags;
  859. if (expect_true (!reify))
  860. {
  861. ++fdchangecnt;
  862. array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
  863. fdchanges [fdchangecnt - 1] = fd;
  864. }
  865. }
  866. /* the given fd is invalid/unusable, so make sure it doesn't hurt us anymore */
  867. inline_speed void
  868. fd_kill (EV_P_ int fd)
  869. {
  870. ev_io *w;
  871. while ((w = (ev_io *)anfds [fd].head))
  872. {
  873. ev_io_stop (EV_A_ w);
  874. ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
  875. }
  876. }
  877. /* check whether the given fd is actually valid, for error recovery */
  878. inline_size int
  879. fd_valid (int fd)
  880. {
  881. #ifdef _WIN32
  882. return EV_FD_TO_WIN32_HANDLE (fd) != -1;
  883. #else
  884. return fcntl (fd, F_GETFD) != -1;
  885. #endif
  886. }
  887. /* called on EBADF to verify fds */
  888. static void noinline
  889. fd_ebadf (EV_P)
  890. {
  891. int fd;
  892. for (fd = 0; fd < anfdmax; ++fd)
  893. if (anfds [fd].events)
  894. if (!fd_valid (fd) && errno == EBADF)
  895. fd_kill (EV_A_ fd);
  896. }
  897. /* called on ENOMEM in select/poll to kill some fds and retry */
  898. static void noinline
  899. fd_enomem (EV_P)
  900. {
  901. int fd;
  902. for (fd = anfdmax; fd--; )
  903. if (anfds [fd].events)
  904. {
  905. fd_kill (EV_A_ fd);
  906. break;
  907. }
  908. }
  909. /* usually called after fork if backend needs to re-arm all fds from scratch */
  910. static void noinline
  911. fd_rearm_all (EV_P)
  912. {
  913. int fd;
  914. for (fd = 0; fd < anfdmax; ++fd)
  915. if (anfds [fd].events)
  916. {
  917. anfds [fd].events = 0;
  918. anfds [fd].emask = 0;
  919. fd_change (EV_A_ fd, EV__IOFDSET | EV_ANFD_REIFY);
  920. }
  921. }
  922. /* used to prepare libev internal fd's */
  923. /* this is not fork-safe */
  924. inline_speed void
  925. fd_intern (int fd)
  926. {
  927. #ifdef _WIN32
  928. unsigned long arg = 1;
  929. ioctlsocket (EV_FD_TO_WIN32_HANDLE (fd), FIONBIO, &arg);
  930. #else
  931. fcntl (fd, F_SETFD, FD_CLOEXEC);
  932. fcntl (fd, F_SETFL, O_NONBLOCK);
  933. #endif
  934. }
  935. /*****************************************************************************/
  936. /*
  937. * the heap functions want a real array index. array index 0 is guaranteed to not
  938. * be in-use at any time. the first heap entry is at array [HEAP0]. DHEAP gives
  939. * the branching factor of the d-tree.
  940. */
  941. /*
  942. * at the moment we allow libev the luxury of two heaps,
  943. * a small-code-size 2-heap one and a ~1.5kb larger 4-heap
  944. * which is more cache-efficient.
  945. * the difference is about 5% with 50000+ watchers.
  946. */
  947. #if EV_USE_4HEAP
  948. #define DHEAP 4
  949. #define HEAP0 (DHEAP - 1) /* index of first element in heap */
  950. #define HPARENT(k) ((((k) - HEAP0 - 1) / DHEAP) + HEAP0)
  951. #define UPHEAP_DONE(p,k) ((p) == (k))
  952. /* away from the root */
  953. inline_speed void
  954. downheap (ANHE *heap, int N, int k)
  955. {
  956. ANHE he = heap [k];
  957. ANHE *E = heap + N + HEAP0;
  958. for (;;)
  959. {
  960. ev_tstamp minat;
  961. ANHE *minpos;
  962. ANHE *pos = heap + DHEAP * (k - HEAP0) + HEAP0 + 1;
  963. /* find minimum child */
  964. if (expect_true (pos + DHEAP - 1 < E))
  965. {
  966. /* fast path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
  967. if ( ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
  968. if ( ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
  969. if ( ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
  970. }
  971. else if (pos < E)
  972. {
  973. /* slow path */ (minpos = pos + 0), (minat = ANHE_at (*minpos));
  974. if (pos + 1 < E && ANHE_at (pos [1]) < minat) (minpos = pos + 1), (minat = ANHE_at (*minpos));
  975. if (pos + 2 < E && ANHE_at (pos [2]) < minat) (minpos = pos + 2), (minat = ANHE_at (*minpos));
  976. if (pos + 3 < E && ANHE_at (pos [3]) < minat) (minpos = pos + 3), (minat = ANHE_at (*minpos));
  977. }
  978. else
  979. break;
  980. if (ANHE_at (he) <= minat)
  981. break;
  982. heap [k] = *minpos;
  983. ev_active (ANHE_w (*minpos)) = k;
  984. k = minpos - heap;
  985. }
  986. heap [k] = he;
  987. ev_active (ANHE_w (he)) = k;
  988. }
  989. #else /* 4HEAP */
  990. #define HEAP0 1
  991. #define HPARENT(k) ((k) >> 1)
  992. #define UPHEAP_DONE(p,k) (!(p))
  993. /* away from the root */
  994. inline_speed void
  995. downheap (ANHE *heap, int N, int k)
  996. {
  997. ANHE he = heap [k];
  998. for (;;)
  999. {
  1000. int c = k << 1;
  1001. if (c >= N + HEAP0)
  1002. break;
  1003. c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
  1004. ? 1 : 0;
  1005. if (ANHE_at (he) <= ANHE_at (heap [c]))
  1006. break;
  1007. heap [k] = heap [c];
  1008. ev_active (ANHE_w (heap [k])) = k;
  1009. k = c;
  1010. }
  1011. heap [k] = he;
  1012. ev_active (ANHE_w (he)) = k;
  1013. }
  1014. #endif
  1015. /* towards the root */
  1016. inline_speed void
  1017. upheap (ANHE *heap, int k)
  1018. {
  1019. ANHE he = heap [k];
  1020. for (;;)
  1021. {
  1022. int p = HPARENT (k);
  1023. if (UPHEAP_DONE (p, k) || ANHE_at (heap [p]) <= ANHE_at (he))
  1024. break;
  1025. heap [k] = heap [p];
  1026. ev_active (ANHE_w (heap [k])) = k;
  1027. k = p;
  1028. }
  1029. heap [k] = he;
  1030. ev_active (ANHE_w (he)) = k;
  1031. }
  1032. /* move an element suitably so it is in a correct place */
  1033. inline_size void
  1034. adjustheap (ANHE *heap, int N, int k)
  1035. {
  1036. if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
  1037. upheap (heap, k);
  1038. else
  1039. downheap (heap, N, k);
  1040. }
  1041. /* rebuild the heap: this function is used only once and executed rarely */
  1042. inline_size void
  1043. reheap (ANHE *heap, int N)
  1044. {
  1045. int i;
  1046. /* we don't use floyds algorithm, upheap is simpler and is more cache-efficient */
  1047. /* also, this is easy to implement and correct for both 2-heaps and 4-heaps */
  1048. for (i = 0; i < N; ++i)
  1049. upheap (heap, i + HEAP0);
  1050. }
  1051. /*****************************************************************************/
  1052. /* associate signal watchers to a signal signal */
  1053. typedef struct
  1054. {
  1055. EV_ATOMIC_T pending;
  1056. #if EV_MULTIPLICITY
  1057. EV_P;
  1058. #endif
  1059. WL head;
  1060. } ANSIG;
  1061. static ANSIG signals [EV_NSIG - 1];
  1062. /*****************************************************************************/
  1063. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  1064. static void noinline
  1065. evpipe_init (EV_P)
  1066. {
  1067. if (!ev_is_active (&pipe_w))
  1068. {
  1069. # if EV_USE_EVENTFD
  1070. evfd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC);
  1071. if (evfd < 0 && errno == EINVAL)
  1072. evfd = eventfd (0, 0);
  1073. if (evfd >= 0)
  1074. {
  1075. evpipe [0] = -1;
  1076. fd_intern (evfd); /* doing it twice doesn't hurt */
  1077. ev_io_set (&pipe_w, evfd, EV_READ);
  1078. }
  1079. else
  1080. # endif
  1081. {
  1082. while (pipe (evpipe))
  1083. ev_syserr ("(libev) error creating signal/async pipe");
  1084. fd_intern (evpipe [0]);
  1085. fd_intern (evpipe [1]);
  1086. ev_io_set (&pipe_w, evpipe [0], EV_READ);
  1087. }
  1088. ev_io_start (EV_A_ &pipe_w);
  1089. ev_unref (EV_A); /* watcher should not keep loop alive */
  1090. }
  1091. }
  1092. inline_size void
  1093. evpipe_write (EV_P_ EV_ATOMIC_T *flag)
  1094. {
  1095. if (!*flag)
  1096. {
  1097. int old_errno = errno; /* save errno because write might clobber it */
  1098. char dummy;
  1099. *flag = 1;
  1100. #if EV_USE_EVENTFD
  1101. if (evfd >= 0)
  1102. {
  1103. uint64_t counter = 1;
  1104. write (evfd, &counter, sizeof (uint64_t));
  1105. }
  1106. else
  1107. #endif
  1108. /* win32 people keep sending patches that change this write() to send() */
  1109. /* and then run away. but send() is wrong, it wants a socket handle on win32 */
  1110. /* so when you think this write should be a send instead, please find out */
  1111. /* where your send() is from - it's definitely not the microsoft send, and */
  1112. /* tell me. thank you. */
  1113. write (evpipe [1], &dummy, 1);
  1114. errno = old_errno;
  1115. }
  1116. }
  1117. /* called whenever the libev signal pipe */
  1118. /* got some events (signal, async) */
  1119. static void
  1120. pipecb (EV_P_ ev_io *iow, int revents)
  1121. {
  1122. int i;
  1123. #if EV_USE_EVENTFD
  1124. if (evfd >= 0)
  1125. {
  1126. uint64_t counter;
  1127. read (evfd, &counter, sizeof (uint64_t));
  1128. }
  1129. else
  1130. #endif
  1131. {
  1132. char dummy;
  1133. /* see discussion in evpipe_write when you think this read should be recv in win32 */
  1134. read (evpipe [0], &dummy, 1);
  1135. }
  1136. if (sig_pending)
  1137. {
  1138. sig_pending = 0;
  1139. for (i = EV_NSIG - 1; i--; )
  1140. if (expect_false (signals [i].pending))
  1141. ev_feed_signal_event (EV_A_ i + 1);
  1142. }
  1143. #if EV_ASYNC_ENABLE
  1144. if (async_pending)
  1145. {
  1146. async_pending = 0;
  1147. for (i = asynccnt; i--; )
  1148. if (asyncs [i]->sent)
  1149. {
  1150. asyncs [i]->sent = 0;
  1151. ev_feed_event (EV_A_ asyncs [i], EV_ASYNC);
  1152. }
  1153. }
  1154. #endif
  1155. }
  1156. /*****************************************************************************/
  1157. void
  1158. ev_feed_signal (int signum)
  1159. {
  1160. #if EV_MULTIPLICITY
  1161. EV_P = signals [signum - 1].loop;
  1162. if (!EV_A)
  1163. return;
  1164. #endif
  1165. signals [signum - 1].pending = 1;
  1166. evpipe_write (EV_A_ &sig_pending);
  1167. }
  1168. static void
  1169. ev_sighandler (int signum)
  1170. {
  1171. #ifdef _WIN32
  1172. signal (signum, ev_sighandler);
  1173. #endif
  1174. ev_feed_signal (signum);
  1175. }
  1176. void noinline
  1177. ev_feed_signal_event (EV_P_ int signum)
  1178. {
  1179. WL w;
  1180. if (expect_false (signum <= 0 || signum > EV_NSIG))
  1181. return;
  1182. --signum;
  1183. #if EV_MULTIPLICITY
  1184. /* it is permissible to try to feed a signal to the wrong loop */
  1185. /* or, likely more useful, feeding a signal nobody is waiting for */
  1186. if (expect_false (signals [signum].loop != EV_A))
  1187. return;
  1188. #endif
  1189. signals [signum].pending = 0;
  1190. for (w = signals [signum].head; w; w = w->next)
  1191. ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
  1192. }
  1193. #if EV_USE_SIGNALFD
  1194. static void
  1195. sigfdcb (EV_P_ ev_io *iow, int revents)
  1196. {
  1197. struct signalfd_siginfo si[2], *sip; /* these structs are big */
  1198. for (;;)
  1199. {
  1200. ssize_t res = read (sigfd, si, sizeof (si));
  1201. /* not ISO-C, as res might be -1, but works with SuS */
  1202. for (sip = si; (char *)sip < (char *)si + res; ++sip)
  1203. ev_feed_signal_event (EV_A_ sip->ssi_signo);
  1204. if (res < (ssize_t)sizeof (si))
  1205. break;
  1206. }
  1207. }
  1208. #endif
  1209. #endif
  1210. /*****************************************************************************/
  1211. #if EV_CHILD_ENABLE
  1212. static WL childs [EV_PID_HASHSIZE];
  1213. static ev_signal childev;
  1214. #ifndef WIFCONTINUED
  1215. # define WIFCONTINUED(status) 0
  1216. #endif
  1217. /* handle a single child status event */
  1218. inline_speed void
  1219. child_reap (EV_P_ int chain, int pid, int status)
  1220. {
  1221. ev_child *w;
  1222. int traced = WIFSTOPPED (status) || WIFCONTINUED (status);
  1223. for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
  1224. {
  1225. if ((w->pid == pid || !w->pid)
  1226. && (!traced || (w->flags & 1)))
  1227. {
  1228. ev_set_priority (w, EV_MAXPRI); /* need to do it *now*, this *must* be the same prio as the signal watcher itself */
  1229. w->rpid = pid;
  1230. w->rstatus = status;
  1231. ev_feed_event (EV_A_ (W)w, EV_CHILD);
  1232. }
  1233. }
  1234. }
  1235. #ifndef WCONTINUED
  1236. # define WCONTINUED 0
  1237. #endif
  1238. /* called on sigchld etc., calls waitpid */
  1239. static void
  1240. childcb (EV_P_ ev_signal *sw, int revents)
  1241. {
  1242. int pid, status;
  1243. /* some systems define WCONTINUED but then fail to support it (linux 2.4) */
  1244. if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
  1245. if (!WCONTINUED
  1246. || errno != EINVAL
  1247. || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED)))
  1248. return;
  1249. /* make sure we are called again until all children have been reaped */
  1250. /* we need to do it this way so that the callback gets called before we continue */
  1251. ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
  1252. child_reap (EV_A_ pid, pid, status);
  1253. if ((EV_PID_HASHSIZE) > 1)
  1254. child_reap (EV_A_ 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
  1255. }
  1256. #endif
  1257. /*****************************************************************************/
  1258. #if EV_USE_IOCP
  1259. # include "ev_iocp.c"
  1260. #endif
  1261. #if EV_USE_PORT
  1262. # include "ev_port.c"
  1263. #endif
  1264. #if EV_USE_KQUEUE
  1265. # include "ev_kqueue.c"
  1266. #endif
  1267. #if EV_USE_EPOLL
  1268. # include "ev_epoll.c"
  1269. #endif
  1270. #if EV_USE_POLL
  1271. # include "ev_poll.c"
  1272. #endif
  1273. #if EV_USE_SELECT
  1274. # include "ev_select.c"
  1275. #endif
  1276. int
  1277. ev_version_major (void)
  1278. {
  1279. return EV_VERSION_MAJOR;
  1280. }
  1281. int
  1282. ev_version_minor (void)
  1283. {
  1284. return EV_VERSION_MINOR;
  1285. }
  1286. /* return true if we are running with elevated privileges and should ignore env variables */
  1287. int inline_size
  1288. enable_secure (void)
  1289. {
  1290. #ifdef _WIN32
  1291. return 0;
  1292. #else
  1293. return getuid () != geteuid ()
  1294. || getgid () != getegid ();
  1295. #endif
  1296. }
  1297. unsigned int
  1298. ev_supported_backends (void)
  1299. {
  1300. unsigned int flags = 0;
  1301. if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
  1302. if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
  1303. if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
  1304. if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
  1305. if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
  1306. return flags;
  1307. }
  1308. unsigned int
  1309. ev_recommended_backends (void)
  1310. {
  1311. unsigned int flags = ev_supported_backends ();
  1312. #ifndef __NetBSD__
  1313. /* kqueue is borked on everything but netbsd apparently */
  1314. /* it usually doesn't work correctly on anything but sockets and pipes */
  1315. flags &= ~EVBACKEND_KQUEUE;
  1316. #endif
  1317. #ifdef __APPLE__
  1318. /* only select works correctly on that "unix-certified" platform */
  1319. flags &= ~EVBACKEND_KQUEUE; /* horribly broken, even for sockets */
  1320. flags &= ~EVBACKEND_POLL; /* poll is based on kqueue from 10.5 onwards */
  1321. #endif
  1322. #ifdef __FreeBSD__
  1323. flags &= ~EVBACKEND_POLL; /* poll return value is unusable (http://forums.freebsd.org/archive/index.php/t-10270.html) */
  1324. #endif
  1325. return flags;
  1326. }
  1327. unsigned int
  1328. ev_embeddable_backends (void)
  1329. {
  1330. int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
  1331. /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
  1332. if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
  1333. flags &= ~EVBACKEND_EPOLL;
  1334. return flags;
  1335. }
  1336. unsigned int
  1337. ev_backend (EV_P)
  1338. {
  1339. return backend;
  1340. }
  1341. #if EV_FEATURE_API
  1342. unsigned int
  1343. ev_iteration (EV_P)
  1344. {
  1345. return loop_count;
  1346. }
  1347. unsigned int
  1348. ev_depth (EV_P)
  1349. {
  1350. return loop_depth;
  1351. }
  1352. void
  1353. ev_set_io_collect_interval (EV_P_ ev_tstamp interval)
  1354. {
  1355. io_blocktime = interval;
  1356. }
  1357. void
  1358. ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval)
  1359. {
  1360. timeout_blocktime = interval;
  1361. }
  1362. void
  1363. ev_set_userdata (EV_P_ void *data)
  1364. {
  1365. userdata = data;
  1366. }
  1367. void *
  1368. ev_userdata (EV_P)
  1369. {
  1370. return userdata;
  1371. }
  1372. void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P))
  1373. {
  1374. invoke_cb = invoke_pending_cb;
  1375. }
  1376. void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P))
  1377. {
  1378. release_cb = release;
  1379. acquire_cb = acquire;
  1380. }
  1381. #endif
  1382. /* initialise a loop structure, must be zero-initialised */
  1383. static void noinline
  1384. loop_init (EV_P_ unsigned int flags)
  1385. {
  1386. if (!backend)
  1387. {
  1388. origflags = flags;
  1389. #if EV_USE_REALTIME
  1390. if (!have_realtime)
  1391. {
  1392. struct timespec ts;
  1393. if (!clock_gettime (CLOCK_REALTIME, &ts))
  1394. have_realtime = 1;
  1395. }
  1396. #endif
  1397. #if EV_USE_MONOTONIC
  1398. if (!have_monotonic)
  1399. {
  1400. struct timespec ts;
  1401. if (!clock_gettime (CLOCK_MONOTONIC, &ts))
  1402. have_monotonic = 1;
  1403. }
  1404. #endif
  1405. /* pid check not overridable via env */
  1406. #ifndef _WIN32
  1407. if (flags & EVFLAG_FORKCHECK)
  1408. curpid = getpid ();
  1409. #endif
  1410. if (!(flags & EVFLAG_NOENV)
  1411. && !enable_secure ()
  1412. && getenv ("LIBEV_FLAGS"))
  1413. flags = atoi (getenv ("LIBEV_FLAGS"));
  1414. ev_rt_now = ev_time ();
  1415. mn_now = get_clock ();
  1416. now_floor = mn_now;
  1417. rtmn_diff = ev_rt_now - mn_now;
  1418. #if EV_FEATURE_API
  1419. invoke_cb = ev_invoke_pending;
  1420. #endif
  1421. io_blocktime = 0.;
  1422. timeout_blocktime = 0.;
  1423. backend = 0;
  1424. backend_fd = -1;
  1425. sig_pending = 0;
  1426. #if EV_ASYNC_ENABLE
  1427. async_pending = 0;
  1428. #endif
  1429. #if EV_USE_INOTIFY
  1430. fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
  1431. #endif
  1432. #if EV_USE_SIGNALFD
  1433. sigfd = flags & EVFLAG_SIGNALFD ? -2 : -1;
  1434. #endif
  1435. if (!(flags & EVBACKEND_MASK))
  1436. flags |= ev_recommended_backends ();
  1437. #if EV_USE_IOCP
  1438. if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
  1439. #endif
  1440. #if EV_USE_PORT
  1441. if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
  1442. #endif
  1443. #if EV_USE_KQUEUE
  1444. if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
  1445. #endif
  1446. #if EV_USE_EPOLL
  1447. if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
  1448. #endif
  1449. #if EV_USE_POLL
  1450. if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
  1451. #endif
  1452. #if EV_USE_SELECT
  1453. if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
  1454. #endif
  1455. ev_prepare_init (&pending_w, pendingcb);
  1456. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  1457. ev_init (&pipe_w, pipecb);
  1458. ev_set_priority (&pipe_w, EV_MAXPRI);
  1459. #endif
  1460. }
  1461. }
  1462. /* free up a loop structure */
  1463. void
  1464. ev_loop_destroy (EV_P)
  1465. {
  1466. int i;
  1467. #if EV_MULTIPLICITY
  1468. /* mimic free (0) */
  1469. if (!EV_A)
  1470. return;
  1471. #endif
  1472. #if EV_CLEANUP_ENABLE
  1473. /* queue cleanup watchers (and execute them) */
  1474. if (expect_false (cleanupcnt))
  1475. {
  1476. queue_events (EV_A_ (W *)cleanups, cleanupcnt, EV_CLEANUP);
  1477. EV_INVOKE_PENDING;
  1478. }
  1479. #endif
  1480. #if EV_CHILD_ENABLE
  1481. if (ev_is_active (&childev))
  1482. {
  1483. ev_ref (EV_A); /* child watcher */
  1484. ev_signal_stop (EV_A_ &childev);
  1485. }
  1486. #endif
  1487. if (ev_is_active (&pipe_w))
  1488. {
  1489. /*ev_ref (EV_A);*/
  1490. /*ev_io_stop (EV_A_ &pipe_w);*/
  1491. #if EV_USE_EVENTFD
  1492. if (evfd >= 0)
  1493. close (evfd);
  1494. #endif
  1495. if (evpipe [0] >= 0)
  1496. {
  1497. EV_WIN32_CLOSE_FD (evpipe [0]);
  1498. EV_WIN32_CLOSE_FD (evpipe [1]);
  1499. }
  1500. }
  1501. #if EV_USE_SIGNALFD
  1502. if (ev_is_active (&sigfd_w))
  1503. close (sigfd);
  1504. #endif
  1505. #if EV_USE_INOTIFY
  1506. if (fs_fd >= 0)
  1507. close (fs_fd);
  1508. #endif
  1509. if (backend_fd >= 0)
  1510. close (backend_fd);
  1511. #if EV_USE_IOCP
  1512. if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
  1513. #endif
  1514. #if EV_USE_PORT
  1515. if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
  1516. #endif
  1517. #if EV_USE_KQUEUE
  1518. if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
  1519. #endif
  1520. #if EV_USE_EPOLL
  1521. if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
  1522. #endif
  1523. #if EV_USE_POLL
  1524. if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
  1525. #endif
  1526. #if EV_USE_SELECT
  1527. if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
  1528. #endif
  1529. for (i = NUMPRI; i--; )
  1530. {
  1531. array_free (pending, [i]);
  1532. #if EV_IDLE_ENABLE
  1533. array_free (idle, [i]);
  1534. #endif
  1535. }
  1536. ev_free (anfds); anfds = 0; anfdmax = 0;
  1537. /* have to use the microsoft-never-gets-it-right macro */
  1538. array_free (rfeed, EMPTY);
  1539. array_free (fdchange, EMPTY);
  1540. array_free (timer, EMPTY);
  1541. #if EV_PERIODIC_ENABLE
  1542. array_free (periodic, EMPTY);
  1543. #endif
  1544. #if EV_FORK_ENABLE
  1545. array_free (fork, EMPTY);
  1546. #endif
  1547. #if EV_CLEANUP_ENABLE
  1548. array_free (cleanup, EMPTY);
  1549. #endif
  1550. array_free (prepare, EMPTY);
  1551. array_free (check, EMPTY);
  1552. #if EV_ASYNC_ENABLE
  1553. array_free (async, EMPTY);
  1554. #endif
  1555. backend = 0;
  1556. #if EV_MULTIPLICITY
  1557. if (ev_is_default_loop (EV_A))
  1558. #endif
  1559. ev_default_loop_ptr = 0;
  1560. #if EV_MULTIPLICITY
  1561. else
  1562. ev_free (EV_A);
  1563. #endif
  1564. }
  1565. #if EV_USE_INOTIFY
  1566. inline_size void infy_fork (EV_P);
  1567. #endif
  1568. inline_size void
  1569. loop_fork (EV_P)
  1570. {
  1571. #if EV_USE_PORT
  1572. if (backend == EVBACKEND_PORT ) port_fork (EV_A);
  1573. #endif
  1574. #if EV_USE_KQUEUE
  1575. if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
  1576. #endif
  1577. #if EV_USE_EPOLL
  1578. if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
  1579. #endif
  1580. #if EV_USE_INOTIFY
  1581. infy_fork (EV_A);
  1582. #endif
  1583. if (ev_is_active (&pipe_w))
  1584. {
  1585. /* this "locks" the handlers against writing to the pipe */
  1586. /* while we modify the fd vars */
  1587. sig_pending = 1;
  1588. #if EV_ASYNC_ENABLE
  1589. async_pending = 1;
  1590. #endif
  1591. ev_ref (EV_A);
  1592. ev_io_stop (EV_A_ &pipe_w);
  1593. #if EV_USE_EVENTFD
  1594. if (evfd >= 0)
  1595. close (evfd);
  1596. #endif
  1597. if (evpipe [0] >= 0)
  1598. {
  1599. EV_WIN32_CLOSE_FD (evpipe [0]);
  1600. EV_WIN32_CLOSE_FD (evpipe [1]);
  1601. }
  1602. #if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
  1603. evpipe_init (EV_A);
  1604. /* now iterate over everything, in case we missed something */
  1605. pipecb (EV_A_ &pipe_w, EV_READ);
  1606. #endif
  1607. }
  1608. postfork = 0;
  1609. }
  1610. #if EV_MULTIPLICITY
  1611. struct ev_loop *
  1612. ev_loop_new (unsigned int flags)
  1613. {
  1614. EV_P = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
  1615. memset (EV_A, 0, sizeof (struct ev_loop));
  1616. loop_init (EV_A_ flags);
  1617. if (ev_backend (EV_A))
  1618. return EV_A;
  1619. ev_free (EV_A);
  1620. return 0;
  1621. }
  1622. #endif /* multiplicity */
  1623. #if EV_VERIFY
  1624. static void noinline
  1625. verify_watcher (EV_P_ W w)
  1626. {
  1627. assert (("libev: watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
  1628. if (w->pending)
  1629. assert (("libev: pending watcher not on pending queue", pendings [ABSPRI (w)][w->pending - 1].w == w));
  1630. }
  1631. static void noinline
  1632. verify_heap (EV_P_ ANHE *heap, int N)
  1633. {
  1634. int i;
  1635. for (i = HEAP0; i < N + HEAP0; ++i)
  1636. {
  1637. assert (("libev: active index mismatch in heap", ev_active (ANHE_w (heap [i])) == i));
  1638. assert (("libev: heap condition violated", i == HEAP0 || ANHE_at (heap [HPARENT (i)]) <= ANHE_at (heap [i])));
  1639. assert (("libev: heap at cache mismatch", ANHE_at (heap [i]) == ev_at (ANHE_w (heap [i]))));
  1640. verify_watcher (EV_A_ (W)ANHE_w (heap [i]));
  1641. }
  1642. }
  1643. static void noinline
  1644. array_verify (EV_P_ W *ws, int cnt)
  1645. {
  1646. while (cnt--)
  1647. {
  1648. assert (("libev: active index mismatch", ev_active (ws [cnt]) == cnt + 1));
  1649. verify_watcher (EV_A_ ws [cnt]);
  1650. }
  1651. }
  1652. #endif
  1653. #if EV_FEATURE_API
  1654. void
  1655. ev_verify (EV_P)
  1656. {
  1657. #if EV_VERIFY
  1658. int i;
  1659. WL w;
  1660. assert (activecnt >= -1);
  1661. assert (fdchangemax >= fdchangecnt);
  1662. for (i = 0; i < fdchangecnt; ++i)
  1663. assert (("libev: negative fd in fdchanges", fdchanges [i] >= 0));
  1664. assert (anfdmax >= 0);
  1665. for (i = 0; i < anfdmax; ++i)
  1666. for (w = anfds [i].head; w; w = w->next)
  1667. {
  1668. verify_watcher (EV_A_ (W)w);
  1669. assert (("libev: inactive fd watcher on anfd list", ev_active (w) == 1));
  1670. assert (("libev: fd mismatch between watcher and anfd", ((ev_io *)w)->fd == i));
  1671. }
  1672. assert (timermax >= timercnt);
  1673. verify_heap (EV_A_ timers, timercnt);
  1674. #if EV_PERIODIC_ENABLE
  1675. assert (periodicmax >= periodiccnt);
  1676. verify_heap (EV_A_ periodics, periodiccnt);
  1677. #endif
  1678. for (i = NUMPRI; i--; )
  1679. {
  1680. assert (pendingmax [i] >= pendingcnt [i]);
  1681. #if EV_IDLE_ENABLE
  1682. assert (idleall >= 0);
  1683. assert (idlemax [i] >= idlecnt [i]);
  1684. array_verify (EV_A_ (W *)idles [i], idlecnt [i]);
  1685. #endif
  1686. }
  1687. #if EV_FORK_ENABLE
  1688. assert (forkmax >= forkcnt);
  1689. array_verify (EV_A_ (W *)forks, forkcnt);
  1690. #endif
  1691. #if EV_CLEANUP_ENABLE
  1692. assert (cleanupmax >= cleanupcnt);
  1693. array_verify (EV_A_ (W *)cleanups, cleanupcnt);
  1694. #endif
  1695. #if EV_ASYNC_ENABLE
  1696. assert (asyncmax >= asynccnt);
  1697. array_verify (EV_A_ (W *)asyncs, asynccnt);
  1698. #endif
  1699. #if EV_PREPARE_ENABLE
  1700. assert (preparemax >= preparecnt);
  1701. array_verify (EV_A_ (W *)prepares, preparecnt);
  1702. #endif
  1703. #if EV_CHECK_ENABLE
  1704. assert (checkmax >= checkcnt);
  1705. array_verify (EV_A_ (W *)checks, checkcnt);
  1706. #endif
  1707. # if 0
  1708. #if EV_CHILD_ENABLE
  1709. for (w = (ev_child *)childs [chain & ((EV_PID_HASHSIZE) - 1)]; w; w = (ev_child *)((WL)w)->next)
  1710. for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
  1711. #endif
  1712. # endif
  1713. #endif
  1714. }
  1715. #endif
  1716. #if EV_MULTIPLICITY
  1717. struct ev_loop *
  1718. #else
  1719. int
  1720. #endif
  1721. ev_default_loop (unsigned int flags)
  1722. {
  1723. if (!ev_default_loop_ptr)
  1724. {
  1725. #if EV_MULTIPLICITY
  1726. EV_P = ev_default_loop_ptr = &default_loop_struct;
  1727. #else
  1728. ev_default_loop_ptr = 1;
  1729. #endif
  1730. loop_init (EV_A_ flags);
  1731. if (ev_backend (EV_A))
  1732. {
  1733. #if EV_CHILD_ENABLE
  1734. ev_signal_init (&childev, childcb, SIGCHLD);
  1735. ev_set_priority (&childev, EV_MAXPRI);
  1736. ev_signal_start (EV_A_ &childev);
  1737. ev_unref (EV_A); /* child watcher should not keep loop alive */
  1738. #endif
  1739. }
  1740. else
  1741. ev_default_loop_ptr = 0;
  1742. }
  1743. return ev_default_loop_ptr;
  1744. }
  1745. void
  1746. ev_loop_fork (EV_P)
  1747. {
  1748. postfork = 1; /* must be in line with ev_default_fork */
  1749. }
  1750. /*****************************************************************************/
  1751. void
  1752. ev_invoke (EV_P_ void *w, int revents)
  1753. {
  1754. EV_CB_INVOKE ((W)w, revents);
  1755. }
  1756. unsigned int
  1757. ev_pending_count (EV_P)
  1758. {
  1759. int pri;
  1760. unsigned int count = 0;
  1761. for (pri = NUMPRI; pri--; )
  1762. count += pendingcnt [pri];
  1763. return count;
  1764. }
  1765. void noinline
  1766. ev_invoke_pending (EV_P)
  1767. {
  1768. int pri;
  1769. for (pri = NUMPRI; pri--; )
  1770. while (pendingcnt [pri])
  1771. {
  1772. ANPENDING *p = pendings [pri] + --pendingcnt [pri];
  1773. p->w->pending = 0;
  1774. EV_CB_INVOKE (p->w, p->events);
  1775. EV_FREQUENT_CHECK;
  1776. }
  1777. }
  1778. #if EV_IDLE_ENABLE
  1779. /* make idle watchers pending. this handles the "call-idle */
  1780. /* only when higher priorities are idle" logic */
  1781. inline_size void
  1782. idle_reify (EV_P)
  1783. {
  1784. if (expect_false (idleall))
  1785. {
  1786. int pri;
  1787. for (pri = NUMPRI; pri--; )
  1788. {
  1789. if (pendingcnt [pri])
  1790. break;
  1791. if (idlecnt [pri])
  1792. {
  1793. queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
  1794. break;
  1795. }
  1796. }
  1797. }
  1798. }
  1799. #endif
  1800. /* make timers pending */
  1801. inline_size void
  1802. timers_reify (EV_P)
  1803. {
  1804. EV_FREQUENT_CHECK;
  1805. if (timercnt && ANHE_at (timers [HEAP0]) < mn_now)
  1806. {
  1807. do
  1808. {
  1809. ev_timer *w = (ev_timer *)ANHE_w (timers [HEAP0]);
  1810. /*assert (("libev: inactive timer on timer heap detected", ev_is_active (w)));*/
  1811. /* first reschedule or stop timer */
  1812. if (w->repeat)
  1813. {
  1814. ev_at (w) += w->repeat;
  1815. if (ev_at (w) < mn_now)
  1816. ev_at (w) = mn_now;
  1817. assert (("libev: negative ev_timer repeat value found while processing timers", w->repeat > 0.));
  1818. ANHE_at_cache (timers [HEAP0]);
  1819. downheap (timers, timercnt, HEAP0);
  1820. }
  1821. else
  1822. ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
  1823. EV_FREQUENT_CHECK;
  1824. feed_reverse (EV_A_ (W)w);
  1825. }
  1826. while (timercnt && ANHE_at (timers [HEAP0]) < mn_now);
  1827. feed_reverse_done (EV_A_ EV_TIMER);
  1828. }
  1829. }
  1830. #if EV_PERIODIC_ENABLE
  1831. /* make periodics pending */
  1832. inline_size void
  1833. periodics_reify (EV_P)
  1834. {
  1835. EV_FREQUENT_CHECK;
  1836. while (periodiccnt && ANHE_at (period