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.

1789 lines
37 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * libev event processing core, watcher management
  3. *
  4. * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #ifndef EV_STANDALONE
  35. # ifdef EV_CONFIG_H
  36. # include EV_CONFIG_H
  37. # else
  38. # include "config.h"
  39. # endif
  40. # if HAVE_CLOCK_GETTIME
  41. # ifndef EV_USE_MONOTONIC
  42. # define EV_USE_MONOTONIC 1
  43. # endif
  44. # ifndef EV_USE_REALTIME
  45. # define EV_USE_REALTIME 1
  46. # endif
  47. # else
  48. # ifndef EV_USE_MONOTONIC
  49. # define EV_USE_MONOTONIC 0
  50. # endif
  51. # ifndef EV_USE_REALTIME
  52. # define EV_USE_REALTIME 0
  53. # endif
  54. # endif
  55. # ifndef EV_USE_SELECT
  56. # if HAVE_SELECT && HAVE_SYS_SELECT_H
  57. # define EV_USE_SELECT 1
  58. # else
  59. # define EV_USE_SELECT 0
  60. # endif
  61. # endif
  62. # ifndef EV_USE_POLL
  63. # if HAVE_POLL && HAVE_POLL_H
  64. # define EV_USE_POLL 1
  65. # else
  66. # define EV_USE_POLL 0
  67. # endif
  68. # endif
  69. # ifndef EV_USE_EPOLL
  70. # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
  71. # define EV_USE_EPOLL 1
  72. # else
  73. # define EV_USE_EPOLL 0
  74. # endif
  75. # endif
  76. # ifndef EV_USE_KQUEUE
  77. # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
  78. # define EV_USE_KQUEUE 1
  79. # else
  80. # define EV_USE_KQUEUE 0
  81. # endif
  82. # endif
  83. # ifndef EV_USE_PORT
  84. # if HAVE_PORT_H && HAVE_PORT_CREATE
  85. # define EV_USE_PORT 1
  86. # else
  87. # define EV_USE_PORT 0
  88. # endif
  89. # endif
  90. #endif
  91. #include <math.h>
  92. #include <stdlib.h>
  93. #include <fcntl.h>
  94. #include <stddef.h>
  95. #include <stdio.h>
  96. #include <assert.h>
  97. #include <errno.h>
  98. #include <sys/types.h>
  99. #include <time.h>
  100. #include <signal.h>
  101. #ifndef _WIN32
  102. # include <unistd.h>
  103. # include <sys/time.h>
  104. # include <sys/wait.h>
  105. #else
  106. # define WIN32_LEAN_AND_MEAN
  107. # include <windows.h>
  108. # ifndef EV_SELECT_IS_WINSOCKET
  109. # define EV_SELECT_IS_WINSOCKET 1
  110. # endif
  111. #endif
  112. /**/
  113. #ifndef EV_USE_MONOTONIC
  114. # define EV_USE_MONOTONIC 0
  115. #endif
  116. #ifndef EV_USE_REALTIME
  117. # define EV_USE_REALTIME 0
  118. #endif
  119. #ifndef EV_USE_SELECT
  120. # define EV_USE_SELECT 1
  121. #endif
  122. #ifndef EV_USE_POLL
  123. # ifdef _WIN32
  124. # define EV_USE_POLL 0
  125. # else
  126. # define EV_USE_POLL 1
  127. # endif
  128. #endif
  129. #ifndef EV_USE_EPOLL
  130. # define EV_USE_EPOLL 0
  131. #endif
  132. #ifndef EV_USE_KQUEUE
  133. # define EV_USE_KQUEUE 0
  134. #endif
  135. #ifndef EV_USE_PORT
  136. # define EV_USE_PORT 0
  137. #endif
  138. /**/
  139. #ifndef CLOCK_MONOTONIC
  140. # undef EV_USE_MONOTONIC
  141. # define EV_USE_MONOTONIC 0
  142. #endif
  143. #ifndef CLOCK_REALTIME
  144. # undef EV_USE_REALTIME
  145. # define EV_USE_REALTIME 0
  146. #endif
  147. #if EV_SELECT_IS_WINSOCKET
  148. # include <winsock.h>
  149. #endif
  150. /**/
  151. #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
  152. #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */
  153. #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
  154. /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
  155. #ifdef EV_H
  156. # include EV_H
  157. #else
  158. # include "ev.h"
  159. #endif
  160. #if __GNUC__ >= 3
  161. # define expect(expr,value) __builtin_expect ((expr),(value))
  162. # define inline static inline
  163. #else
  164. # define expect(expr,value) (expr)
  165. # define inline static
  166. #endif
  167. #define expect_false(expr) expect ((expr) != 0, 0)
  168. #define expect_true(expr) expect ((expr) != 0, 1)
  169. #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
  170. #define ABSPRI(w) ((w)->priority - EV_MINPRI)
  171. #define EMPTY0 /* required for microsofts broken pseudo-c compiler */
  172. #define EMPTY2(a,b) /* used to suppress some warnings */
  173. typedef ev_watcher *W;
  174. typedef ev_watcher_list *WL;
  175. typedef ev_watcher_time *WT;
  176. static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
  177. #ifdef _WIN32
  178. # include "ev_win32.c"
  179. #endif
  180. /*****************************************************************************/
  181. static void (*syserr_cb)(const char *msg);
  182. void ev_set_syserr_cb (void (*cb)(const char *msg))
  183. {
  184. syserr_cb = cb;
  185. }
  186. static void
  187. syserr (const char *msg)
  188. {
  189. if (!msg)
  190. msg = "(libev) system error";
  191. if (syserr_cb)
  192. syserr_cb (msg);
  193. else
  194. {
  195. perror (msg);
  196. abort ();
  197. }
  198. }
  199. static void *(*alloc)(void *ptr, long size);
  200. void ev_set_allocator (void *(*cb)(void *ptr, long size))
  201. {
  202. alloc = cb;
  203. }
  204. static void *
  205. ev_realloc (void *ptr, long size)
  206. {
  207. ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
  208. if (!ptr && size)
  209. {
  210. fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
  211. abort ();
  212. }
  213. return ptr;
  214. }
  215. #define ev_malloc(size) ev_realloc (0, (size))
  216. #define ev_free(ptr) ev_realloc ((ptr), 0)
  217. /*****************************************************************************/
  218. typedef struct
  219. {
  220. WL head;
  221. unsigned char events;
  222. unsigned char reify;
  223. #if EV_SELECT_IS_WINSOCKET
  224. SOCKET handle;
  225. #endif
  226. } ANFD;
  227. typedef struct
  228. {
  229. W w;
  230. int events;
  231. } ANPENDING;
  232. #if EV_MULTIPLICITY
  233. struct ev_loop
  234. {
  235. ev_tstamp ev_rt_now;
  236. #define ev_rt_now ((loop)->ev_rt_now)
  237. #define VAR(name,decl) decl;
  238. #include "ev_vars.h"
  239. #undef VAR
  240. };
  241. #include "ev_wrap.h"
  242. static struct ev_loop default_loop_struct;
  243. struct ev_loop *ev_default_loop_ptr;
  244. #else
  245. ev_tstamp ev_rt_now;
  246. #define VAR(name,decl) static decl;
  247. #include "ev_vars.h"
  248. #undef VAR
  249. static int ev_default_loop_ptr;
  250. #endif
  251. /*****************************************************************************/
  252. ev_tstamp
  253. ev_time (void)
  254. {
  255. #if EV_USE_REALTIME
  256. struct timespec ts;
  257. clock_gettime (CLOCK_REALTIME, &ts);
  258. return ts.tv_sec + ts.tv_nsec * 1e-9;
  259. #else
  260. struct timeval tv;
  261. gettimeofday (&tv, 0);
  262. return tv.tv_sec + tv.tv_usec * 1e-6;
  263. #endif
  264. }
  265. inline ev_tstamp
  266. get_clock (void)
  267. {
  268. #if EV_USE_MONOTONIC
  269. if (expect_true (have_monotonic))
  270. {
  271. struct timespec ts;
  272. clock_gettime (CLOCK_MONOTONIC, &ts);
  273. return ts.tv_sec + ts.tv_nsec * 1e-9;
  274. }
  275. #endif
  276. return ev_time ();
  277. }
  278. #if EV_MULTIPLICITY
  279. ev_tstamp
  280. ev_now (EV_P)
  281. {
  282. return ev_rt_now;
  283. }
  284. #endif
  285. #define array_roundsize(type,n) (((n) | 4) & ~3)
  286. #define array_needsize(type,base,cur,cnt,init) \
  287. if (expect_false ((cnt) > cur)) \
  288. { \
  289. int newcnt = cur; \
  290. do \
  291. { \
  292. newcnt = array_roundsize (type, newcnt << 1); \
  293. } \
  294. while ((cnt) > newcnt); \
  295. \
  296. base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
  297. init (base + cur, newcnt - cur); \
  298. cur = newcnt; \
  299. }
  300. #define array_slim(type,stem) \
  301. if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
  302. { \
  303. stem ## max = array_roundsize (stem ## cnt >> 1); \
  304. base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
  305. fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
  306. }
  307. #define array_free(stem, idx) \
  308. ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
  309. /*****************************************************************************/
  310. static void
  311. anfds_init (ANFD *base, int count)
  312. {
  313. while (count--)
  314. {
  315. base->head = 0;
  316. base->events = EV_NONE;
  317. base->reify = 0;
  318. ++base;
  319. }
  320. }
  321. void
  322. ev_feed_event (EV_P_ void *w, int revents)
  323. {
  324. W w_ = (W)w;
  325. if (expect_false (w_->pending))
  326. {
  327. pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
  328. return;
  329. }
  330. w_->pending = ++pendingcnt [ABSPRI (w_)];
  331. array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
  332. pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
  333. pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
  334. }
  335. static void
  336. queue_events (EV_P_ W *events, int eventcnt, int type)
  337. {
  338. int i;
  339. for (i = 0; i < eventcnt; ++i)
  340. ev_feed_event (EV_A_ events [i], type);
  341. }
  342. inline void
  343. fd_event (EV_P_ int fd, int revents)
  344. {
  345. ANFD *anfd = anfds + fd;
  346. ev_io *w;
  347. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  348. {
  349. int ev = w->events & revents;
  350. if (ev)
  351. ev_feed_event (EV_A_ (W)w, ev);
  352. }
  353. }
  354. void
  355. ev_feed_fd_event (EV_P_ int fd, int revents)
  356. {
  357. fd_event (EV_A_ fd, revents);
  358. }
  359. /*****************************************************************************/
  360. inline void
  361. fd_reify (EV_P)
  362. {
  363. int i;
  364. for (i = 0; i < fdchangecnt; ++i)
  365. {
  366. int fd = fdchanges [i];
  367. ANFD *anfd = anfds + fd;
  368. ev_io *w;
  369. int events = 0;
  370. for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
  371. events |= w->events;
  372. #if EV_SELECT_IS_WINSOCKET
  373. if (events)
  374. {
  375. unsigned long argp;
  376. anfd->handle = _get_osfhandle (fd);
  377. assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
  378. }
  379. #endif
  380. anfd->reify = 0;
  381. backend_modify (EV_A_ fd, anfd->events, events);
  382. anfd->events = events;
  383. }
  384. fdchangecnt = 0;
  385. }
  386. static void
  387. fd_change (EV_P_ int fd)
  388. {
  389. if (expect_false (anfds [fd].reify))
  390. return;
  391. anfds [fd].reify = 1;
  392. ++fdchangecnt;
  393. array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
  394. fdchanges [fdchangecnt - 1] = fd;
  395. }
  396. static void
  397. fd_kill (EV_P_ int fd)
  398. {
  399. ev_io *w;
  400. while ((w = (ev_io *)anfds [fd].head))
  401. {
  402. ev_io_stop (EV_A_ w);
  403. ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
  404. }
  405. }
  406. inline int
  407. fd_valid (int fd)
  408. {
  409. #ifdef _WIN32
  410. return _get_osfhandle (fd) != -1;
  411. #else
  412. return fcntl (fd, F_GETFD) != -1;
  413. #endif
  414. }
  415. /* called on EBADF to verify fds */
  416. static void
  417. fd_ebadf (EV_P)
  418. {
  419. int fd;
  420. for (fd = 0; fd < anfdmax; ++fd)
  421. if (anfds [fd].events)
  422. if (!fd_valid (fd) == -1 && errno == EBADF)
  423. fd_kill (EV_A_ fd);
  424. }
  425. /* called on ENOMEM in select/poll to kill some fds and retry */
  426. static void
  427. fd_enomem (EV_P)
  428. {
  429. int fd;
  430. for (fd = anfdmax; fd--; )
  431. if (anfds [fd].events)
  432. {
  433. fd_kill (EV_A_ fd);
  434. return;
  435. }
  436. }
  437. /* usually called after fork if backend needs to re-arm all fds from scratch */
  438. static void
  439. fd_rearm_all (EV_P)
  440. {
  441. int fd;
  442. /* this should be highly optimised to not do anything but set a flag */
  443. for (fd = 0; fd < anfdmax; ++fd)
  444. if (anfds [fd].events)
  445. {
  446. anfds [fd].events = 0;
  447. fd_change (EV_A_ fd);
  448. }
  449. }
  450. /*****************************************************************************/
  451. static void
  452. upheap (WT *heap, int k)
  453. {
  454. WT w = heap [k];
  455. while (k && heap [k >> 1]->at > w->at)
  456. {
  457. heap [k] = heap [k >> 1];
  458. ((W)heap [k])->active = k + 1;
  459. k >>= 1;
  460. }
  461. heap [k] = w;
  462. ((W)heap [k])->active = k + 1;
  463. }
  464. static void
  465. downheap (WT *heap, int N, int k)
  466. {
  467. WT w = heap [k];
  468. while (k < (N >> 1))
  469. {
  470. int j = k << 1;
  471. if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
  472. ++j;
  473. if (w->at <= heap [j]->at)
  474. break;
  475. heap [k] = heap [j];
  476. ((W)heap [k])->active = k + 1;
  477. k = j;
  478. }
  479. heap [k] = w;
  480. ((W)heap [k])->active = k + 1;
  481. }
  482. inline void
  483. adjustheap (WT *heap, int N, int k)
  484. {
  485. upheap (heap, k);
  486. downheap (heap, N, k);
  487. }
  488. /*****************************************************************************/
  489. typedef struct
  490. {
  491. WL head;
  492. sig_atomic_t volatile gotsig;
  493. } ANSIG;
  494. static ANSIG *signals;
  495. static int signalmax;
  496. static int sigpipe [2];
  497. static sig_atomic_t volatile gotsig;
  498. static ev_io sigev;
  499. static void
  500. signals_init (ANSIG *base, int count)
  501. {
  502. while (count--)
  503. {
  504. base->head = 0;
  505. base->gotsig = 0;
  506. ++base;
  507. }
  508. }
  509. static void
  510. sighandler (int signum)
  511. {
  512. #if _WIN32
  513. signal (signum, sighandler);
  514. #endif
  515. signals [signum - 1].gotsig = 1;
  516. if (!gotsig)
  517. {
  518. int old_errno = errno;
  519. gotsig = 1;
  520. write (sigpipe [1], &signum, 1);
  521. errno = old_errno;
  522. }
  523. }
  524. void
  525. ev_feed_signal_event (EV_P_ int signum)
  526. {
  527. WL w;
  528. #if EV_MULTIPLICITY
  529. assert (("feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr));
  530. #endif
  531. --signum;
  532. if (signum < 0 || signum >= signalmax)
  533. return;
  534. signals [signum].gotsig = 0;
  535. for (w = signals [signum].head; w; w = w->next)
  536. ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
  537. }
  538. static void
  539. sigcb (EV_P_ ev_io *iow, int revents)
  540. {
  541. int signum;
  542. read (sigpipe [0], &revents, 1);
  543. gotsig = 0;
  544. for (signum = signalmax; signum--; )
  545. if (signals [signum].gotsig)
  546. ev_feed_signal_event (EV_A_ signum + 1);
  547. }
  548. static void
  549. fd_intern (int fd)
  550. {
  551. #ifdef _WIN32
  552. int arg = 1;
  553. ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
  554. #else
  555. fcntl (fd, F_SETFD, FD_CLOEXEC);
  556. fcntl (fd, F_SETFL, O_NONBLOCK);
  557. #endif
  558. }
  559. static void
  560. siginit (EV_P)
  561. {
  562. fd_intern (sigpipe [0]);
  563. fd_intern (sigpipe [1]);
  564. ev_io_set (&sigev, sigpipe [0], EV_READ);
  565. ev_io_start (EV_A_ &sigev);
  566. ev_unref (EV_A); /* child watcher should not keep loop alive */
  567. }
  568. /*****************************************************************************/
  569. static ev_child *childs [PID_HASHSIZE];
  570. #ifndef _WIN32
  571. static ev_signal childev;
  572. #ifndef WCONTINUED
  573. # define WCONTINUED 0
  574. #endif
  575. static void
  576. child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
  577. {
  578. ev_child *w;
  579. for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
  580. if (w->pid == pid || !w->pid)
  581. {
  582. ev_priority (w) = ev_priority (sw); /* need to do it *now* */
  583. w->rpid = pid;
  584. w->rstatus = status;
  585. ev_feed_event (EV_A_ (W)w, EV_CHILD);
  586. }
  587. }
  588. static void
  589. childcb (EV_P_ ev_signal *sw, int revents)
  590. {
  591. int pid, status;
  592. if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
  593. {
  594. /* make sure we are called again until all childs have been reaped */
  595. /* we need to do it this way so that the callback gets called before we continue */
  596. ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
  597. child_reap (EV_A_ sw, pid, pid, status);
  598. child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
  599. }
  600. }
  601. #endif
  602. /*****************************************************************************/
  603. #if EV_USE_PORT
  604. # include "ev_port.c"
  605. #endif
  606. #if EV_USE_KQUEUE
  607. # include "ev_kqueue.c"
  608. #endif
  609. #if EV_USE_EPOLL
  610. # include "ev_epoll.c"
  611. #endif
  612. #if EV_USE_POLL
  613. # include "ev_poll.c"
  614. #endif
  615. #if EV_USE_SELECT
  616. # include "ev_select.c"
  617. #endif
  618. int
  619. ev_version_major (void)
  620. {
  621. return EV_VERSION_MAJOR;
  622. }
  623. int
  624. ev_version_minor (void)
  625. {
  626. return EV_VERSION_MINOR;
  627. }
  628. /* return true if we are running with elevated privileges and should ignore env variables */
  629. static int
  630. enable_secure (void)
  631. {
  632. #ifdef _WIN32
  633. return 0;
  634. #else
  635. return getuid () != geteuid ()
  636. || getgid () != getegid ();
  637. #endif
  638. }
  639. unsigned int
  640. ev_supported_backends (void)
  641. {
  642. unsigned int flags = 0;
  643. if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
  644. if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
  645. if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
  646. if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
  647. if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
  648. return flags;
  649. }
  650. unsigned int
  651. ev_recommended_backends (void)
  652. {
  653. unsigned int flags = ev_supported_backends ();
  654. #ifndef __NetBSD__
  655. /* kqueue is borked on everything but netbsd apparently */
  656. /* it usually doesn't work correctly on anything but sockets and pipes */
  657. flags &= ~EVBACKEND_KQUEUE;
  658. #endif
  659. #ifdef __APPLE__
  660. // flags &= ~EVBACKEND_KQUEUE; for documentation
  661. flags &= ~EVBACKEND_POLL;
  662. #endif
  663. return flags;
  664. }
  665. unsigned int
  666. ev_embeddable_backends (void)
  667. {
  668. return EVBACKEND_EPOLL
  669. | EVBACKEND_KQUEUE
  670. | EVBACKEND_PORT;
  671. }
  672. unsigned int
  673. ev_backend (EV_P)
  674. {
  675. return backend;
  676. }
  677. static void
  678. loop_init (EV_P_ unsigned int flags)
  679. {
  680. if (!backend)
  681. {
  682. #if EV_USE_MONOTONIC
  683. {
  684. struct timespec ts;
  685. if (!clock_gettime (CLOCK_MONOTONIC, &ts))
  686. have_monotonic = 1;
  687. }
  688. #endif
  689. ev_rt_now = ev_time ();
  690. mn_now = get_clock ();
  691. now_floor = mn_now;
  692. rtmn_diff = ev_rt_now - mn_now;
  693. if (!(flags & EVFLAG_NOENV)
  694. && !enable_secure ()
  695. && getenv ("LIBEV_FLAGS"))
  696. flags = atoi (getenv ("LIBEV_FLAGS"));
  697. if (!(flags & 0x0000ffffUL))
  698. flags |= ev_recommended_backends ();
  699. backend = 0;
  700. #if EV_USE_PORT
  701. if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
  702. #endif
  703. #if EV_USE_KQUEUE
  704. if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
  705. #endif
  706. #if EV_USE_EPOLL
  707. if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags);
  708. #endif
  709. #if EV_USE_POLL
  710. if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags);
  711. #endif
  712. #if EV_USE_SELECT
  713. if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags);
  714. #endif
  715. ev_init (&sigev, sigcb);
  716. ev_set_priority (&sigev, EV_MAXPRI);
  717. }
  718. }
  719. static void
  720. loop_destroy (EV_P)
  721. {
  722. int i;
  723. #if EV_USE_PORT
  724. if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
  725. #endif
  726. #if EV_USE_KQUEUE
  727. if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
  728. #endif
  729. #if EV_USE_EPOLL
  730. if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
  731. #endif
  732. #if EV_USE_POLL
  733. if (backend == EVBACKEND_POLL ) poll_destroy (EV_A);
  734. #endif
  735. #if EV_USE_SELECT
  736. if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
  737. #endif
  738. for (i = NUMPRI; i--; )
  739. array_free (pending, [i]);
  740. /* have to use the microsoft-never-gets-it-right macro */
  741. array_free (fdchange, EMPTY0);
  742. array_free (timer, EMPTY0);
  743. #if EV_PERIODICS
  744. array_free (periodic, EMPTY0);
  745. #endif
  746. array_free (idle, EMPTY0);
  747. array_free (prepare, EMPTY0);
  748. array_free (check, EMPTY0);
  749. backend = 0;
  750. }
  751. static void
  752. loop_fork (EV_P)
  753. {
  754. #if EV_USE_PORT
  755. if (backend == EVBACKEND_PORT ) port_fork (EV_A);
  756. #endif
  757. #if EV_USE_KQUEUE
  758. if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
  759. #endif
  760. #if EV_USE_EPOLL
  761. if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
  762. #endif
  763. if (ev_is_active (&sigev))
  764. {
  765. /* default loop */
  766. ev_ref (EV_A);
  767. ev_io_stop (EV_A_ &sigev);
  768. close (sigpipe [0]);
  769. close (sigpipe [1]);
  770. while (pipe (sigpipe))
  771. syserr ("(libev) error creating pipe");
  772. siginit (EV_A);
  773. }
  774. postfork = 0;
  775. }
  776. #if EV_MULTIPLICITY
  777. struct ev_loop *
  778. ev_loop_new (unsigned int flags)
  779. {
  780. struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
  781. memset (loop, 0, sizeof (struct ev_loop));
  782. loop_init (EV_A_ flags);
  783. if (ev_backend (EV_A))
  784. return loop;
  785. return 0;
  786. }
  787. void
  788. ev_loop_destroy (EV_P)
  789. {
  790. loop_destroy (EV_A);
  791. ev_free (loop);
  792. }
  793. void
  794. ev_loop_fork (EV_P)
  795. {
  796. postfork = 1;
  797. }
  798. #endif
  799. #if EV_MULTIPLICITY
  800. struct ev_loop *
  801. ev_default_loop_init (unsigned int flags)
  802. #else
  803. int
  804. ev_default_loop (unsigned int flags)
  805. #endif
  806. {
  807. if (sigpipe [0] == sigpipe [1])
  808. if (pipe (sigpipe))
  809. return 0;
  810. if (!ev_default_loop_ptr)
  811. {
  812. #if EV_MULTIPLICITY
  813. struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
  814. #else
  815. ev_default_loop_ptr = 1;
  816. #endif
  817. loop_init (EV_A_ flags);
  818. if (ev_backend (EV_A))
  819. {
  820. siginit (EV_A);
  821. #ifndef _WIN32
  822. ev_signal_init (&childev, childcb, SIGCHLD);
  823. ev_set_priority (&childev, EV_MAXPRI);
  824. ev_signal_start (EV_A_ &childev);
  825. ev_unref (EV_A); /* child watcher should not keep loop alive */
  826. #endif
  827. }
  828. else
  829. ev_default_loop_ptr = 0;
  830. }
  831. return ev_default_loop_ptr;
  832. }
  833. void
  834. ev_default_destroy (void)
  835. {
  836. #if EV_MULTIPLICITY
  837. struct ev_loop *loop = ev_default_loop_ptr;
  838. #endif
  839. #ifndef _WIN32
  840. ev_ref (EV_A); /* child watcher */
  841. ev_signal_stop (EV_A_ &childev);
  842. #endif
  843. ev_ref (EV_A); /* signal watcher */
  844. ev_io_stop (EV_A_ &sigev);
  845. close (sigpipe [0]); sigpipe [0] = 0;
  846. close (sigpipe [1]); sigpipe [1] = 0;
  847. loop_destroy (EV_A);
  848. }
  849. void
  850. ev_default_fork (void)
  851. {
  852. #if EV_MULTIPLICITY
  853. struct ev_loop *loop = ev_default_loop_ptr;
  854. #endif
  855. if (backend)
  856. postfork = 1;
  857. }
  858. /*****************************************************************************/
  859. static int
  860. any_pending (EV_P)
  861. {
  862. int pri;
  863. for (pri = NUMPRI; pri--; )
  864. if (pendingcnt [pri])
  865. return 1;
  866. return 0;
  867. }
  868. inline void
  869. call_pending (EV_P)
  870. {
  871. int pri;
  872. for (pri = NUMPRI; pri--; )
  873. while (pendingcnt [pri])
  874. {
  875. ANPENDING *p = pendings [pri] + --pendingcnt [pri];
  876. if (expect_true (p->w))
  877. {
  878. p->w->pending = 0;
  879. EV_CB_INVOKE (p->w, p->events);
  880. }
  881. }
  882. }
  883. inline void
  884. timers_reify (EV_P)
  885. {
  886. while (timercnt && ((WT)timers [0])->at <= mn_now)
  887. {
  888. ev_timer *w = timers [0];
  889. assert (("inactive timer on timer heap detected", ev_is_active (w)));
  890. /* first reschedule or stop timer */
  891. if (w->repeat)
  892. {
  893. assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
  894. ((WT)w)->at += w->repeat;
  895. if (((WT)w)->at < mn_now)
  896. ((WT)w)->at = mn_now;
  897. downheap ((WT *)timers, timercnt, 0);
  898. }
  899. else
  900. ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
  901. ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
  902. }
  903. }
  904. #if EV_PERIODICS
  905. inline void
  906. periodics_reify (EV_P)
  907. {
  908. while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
  909. {
  910. ev_periodic *w = periodics [0];
  911. assert (("inactive timer on periodic heap detected", ev_is_active (w)));
  912. /* first reschedule or stop timer */
  913. if (w->reschedule_cb)
  914. {
  915. ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
  916. assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
  917. downheap ((WT *)periodics, periodiccnt, 0);
  918. }
  919. else if (w->interval)
  920. {
  921. ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
  922. assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
  923. downheap ((WT *)periodics, periodiccnt, 0);
  924. }
  925. else
  926. ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
  927. ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
  928. }
  929. }
  930. static void
  931. periodics_reschedule (EV_P)
  932. {
  933. int i;
  934. /* adjust periodics after time jump */
  935. for (i = 0; i < periodiccnt; ++i)
  936. {
  937. ev_periodic *w = periodics [i];
  938. if (w->reschedule_cb)
  939. ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
  940. else if (w->interval)
  941. ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
  942. }
  943. /* now rebuild the heap */
  944. for (i = periodiccnt >> 1; i--; )
  945. downheap ((WT *)periodics, periodiccnt, i);
  946. }
  947. #endif
  948. inline int
  949. time_update_monotonic (EV_P)
  950. {
  951. mn_now = get_clock ();
  952. if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
  953. {
  954. ev_rt_now = rtmn_diff + mn_now;
  955. return 0;
  956. }
  957. else
  958. {
  959. now_floor = mn_now;
  960. ev_rt_now = ev_time ();
  961. return 1;
  962. }
  963. }
  964. inline void
  965. time_update (EV_P)
  966. {
  967. int i;
  968. #if EV_USE_MONOTONIC
  969. if (expect_true (have_monotonic))
  970. {
  971. if (time_update_monotonic (EV_A))
  972. {
  973. ev_tstamp odiff = rtmn_diff;
  974. for (i = 4; --i; ) /* loop a few times, before making important decisions */
  975. {
  976. rtmn_diff = ev_rt_now - mn_now;
  977. if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
  978. return; /* all is well */
  979. ev_rt_now = ev_time ();
  980. mn_now = get_clock ();
  981. now_floor = mn_now;
  982. }
  983. # if EV_PERIODICS
  984. periodics_reschedule (EV_A);
  985. # endif
  986. /* no timer adjustment, as the monotonic clock doesn't jump */
  987. /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
  988. }
  989. }
  990. else
  991. #endif
  992. {
  993. ev_rt_now = ev_time ();
  994. if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
  995. {
  996. #if EV_PERIODICS
  997. periodics_reschedule (EV_A);
  998. #endif
  999. /* adjust timers. this is easy, as the offset is the same for all */
  1000. for (i = 0; i < timercnt; ++i)
  1001. ((WT)timers [i])->at += ev_rt_now - mn_now;
  1002. }
  1003. mn_now = ev_rt_now;
  1004. }
  1005. }
  1006. void
  1007. ev_ref (EV_P)
  1008. {
  1009. ++activecnt;
  1010. }
  1011. void
  1012. ev_unref (EV_P)
  1013. {
  1014. --activecnt;
  1015. }
  1016. static int loop_done;
  1017. void
  1018. ev_loop (EV_P_ int flags)
  1019. {
  1020. loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
  1021. ? EVUNLOOP_ONE
  1022. : EVUNLOOP_CANCEL;
  1023. while (activecnt)
  1024. {
  1025. /* queue check watchers (and execute them) */
  1026. if (expect_false (preparecnt))
  1027. {
  1028. queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
  1029. call_pending (EV_A);
  1030. }
  1031. /* we might have forked, so reify kernel state if necessary */
  1032. if (expect_false (postfork))
  1033. loop_fork (EV_A);
  1034. /* update fd-related kernel structures */
  1035. fd_reify (EV_A);
  1036. /* calculate blocking time */
  1037. {
  1038. double block;
  1039. if (flags & EVLOOP_NONBLOCK || idlecnt)
  1040. block = 0.; /* do not block at all */
  1041. else
  1042. {
  1043. /* update time to cancel out callback processing overhead */
  1044. #if EV_USE_MONOTONIC
  1045. if (expect_true (have_monotonic))
  1046. time_update_monotonic (EV_A);
  1047. else
  1048. #endif
  1049. {
  1050. ev_rt_now = ev_time ();
  1051. mn_now = ev_rt_now;
  1052. }
  1053. block = MAX_BLOCKTIME;
  1054. if (timercnt)
  1055. {
  1056. ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
  1057. if (block > to) block = to;
  1058. }
  1059. #if EV_PERIODICS
  1060. if (periodiccnt)
  1061. {
  1062. ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
  1063. if (block > to) block = to;
  1064. }
  1065. #endif
  1066. if (expect_false (block < 0.)) block = 0.;
  1067. }
  1068. backend_poll (EV_A_ block);
  1069. }
  1070. /* update ev_rt_now, do magic */
  1071. time_update (EV_A);
  1072. /* queue pending timers and reschedule them */
  1073. timers_reify (EV_A); /* relative timers called last */
  1074. #if EV_PERIODICS
  1075. periodics_reify (EV_A); /* absolute timers called first */
  1076. #endif
  1077. /* queue idle watchers unless other events are pending */
  1078. if (idlecnt && !any_pending (EV_A))
  1079. queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
  1080. /* queue check watchers, to be executed first */
  1081. if (expect_false (checkcnt))
  1082. queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
  1083. call_pending (EV_A);
  1084. if (expect_false (loop_done))
  1085. break;
  1086. }
  1087. if (loop_done == EVUNLOOP_ONE)
  1088. loop_done = EVUNLOOP_CANCEL;
  1089. }
  1090. void
  1091. ev_unloop (EV_P_ int how)
  1092. {
  1093. loop_done = how;
  1094. }
  1095. /*****************************************************************************/
  1096. inline void
  1097. wlist_add (WL *head, WL elem)
  1098. {
  1099. elem->next = *head;
  1100. *head = elem;
  1101. }
  1102. inline void
  1103. wlist_del (WL *head, WL elem)
  1104. {
  1105. while (*head)
  1106. {
  1107. if (*head == elem)
  1108. {
  1109. *head = elem->next;
  1110. return;
  1111. }
  1112. head = &(*head)->next;
  1113. }
  1114. }
  1115. inline void
  1116. ev_clear_pending (EV_P_ W w)
  1117. {
  1118. if (w->pending)
  1119. {
  1120. pendings [ABSPRI (w)][w->pending - 1].w = 0;
  1121. w->pending = 0;
  1122. }
  1123. }
  1124. inline void
  1125. ev_start (EV_P_ W w, int active)
  1126. {
  1127. if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
  1128. if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
  1129. w->active = active;
  1130. ev_ref (EV_A);
  1131. }
  1132. inline void
  1133. ev_stop (EV_P_ W w)
  1134. {
  1135. ev_unref (EV_A);
  1136. w->active = 0;
  1137. }
  1138. /*****************************************************************************/
  1139. void
  1140. ev_io_start (EV_P_ ev_io *w)
  1141. {
  1142. int fd = w->fd;
  1143. if (expect_false (ev_is_active (w)))
  1144. return;
  1145. assert (("ev_io_start called with negative fd", fd >= 0));
  1146. ev_start (EV_A_ (W)w, 1);
  1147. array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
  1148. wlist_add ((WL *)&anfds[fd].head, (WL)w);
  1149. fd_change (EV_A_ fd);
  1150. }
  1151. void
  1152. ev_io_stop (EV_P_ ev_io *w)
  1153. {
  1154. ev_clear_pending (EV_A_ (W)w);
  1155. if (expect_false (!ev_is_active (w)))
  1156. return;
  1157. assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
  1158. wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
  1159. ev_stop (EV_A_ (W)w);
  1160. fd_change (EV_A_ w->fd);
  1161. }
  1162. void
  1163. ev_timer_start (EV_P_ ev_timer *w)
  1164. {
  1165. if (expect_false (ev_is_active (w)))
  1166. return;
  1167. ((WT)w)->at += mn_now;
  1168. assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
  1169. ev_start (EV_A_ (W)w, ++timercnt);
  1170. array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
  1171. timers [timercnt - 1] = w;
  1172. upheap ((WT *)timers, timercnt - 1);
  1173. assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
  1174. }
  1175. void
  1176. ev_timer_stop (EV_P_ ev_timer *w)
  1177. {
  1178. ev_clear_pending (EV_A_ (W)w);
  1179. if (expect_false (!ev_is_active (w)))
  1180. return;
  1181. assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
  1182. if (expect_true (((W)w)->active < timercnt--))
  1183. {
  1184. timers [((W)w)->active - 1] = timers [timercnt];
  1185. adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
  1186. }
  1187. ((WT)w)->at -= mn_now;
  1188. ev_stop (EV_A_ (W)w);
  1189. }
  1190. void
  1191. ev_timer_again (EV_P_ ev_timer *w)
  1192. {
  1193. if (ev_is_active (w))
  1194. {
  1195. if (w->repeat)
  1196. {
  1197. ((WT)w)->at = mn_now + w->repeat;
  1198. adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
  1199. }
  1200. else
  1201. ev_timer_stop (EV_A_ w);
  1202. }
  1203. else if (w->repeat)
  1204. {
  1205. w->at = w->repeat;
  1206. ev_timer_start (EV_A_ w);
  1207. }
  1208. }
  1209. #if EV_PERIODICS
  1210. void
  1211. ev_periodic_start (EV_P_ ev_periodic *w)
  1212. {
  1213. if (expect_false (ev_is_active (w)))
  1214. return;
  1215. if (w->reschedule_cb)
  1216. ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
  1217. else if (w->interval)
  1218. {
  1219. assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
  1220. /* this formula differs from the one in periodic_reify because we do not always round up */
  1221. ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
  1222. }
  1223. ev_start (EV_A_ (W)w, ++periodiccnt);
  1224. array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
  1225. periodics [periodiccnt - 1] = w;
  1226. upheap ((WT *)periodics, periodiccnt - 1);
  1227. assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
  1228. }
  1229. void
  1230. ev_periodic_stop (EV_P_ ev_periodic *w)
  1231. {
  1232. ev_clear_pending (EV_A_ (W)w);
  1233. if (expect_false (!ev_is_active (w)))
  1234. return;
  1235. assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
  1236. if (expect_true (((W)w)->active < periodiccnt--))
  1237. {
  1238. periodics [((W)w)->active - 1] = periodics [periodiccnt];
  1239. adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
  1240. }
  1241. ev_stop (EV_A_ (W)w);
  1242. }
  1243. void
  1244. ev_periodic_again (EV_P_ ev_periodic *w)
  1245. {
  1246. /* TODO: use adjustheap and recalculation */
  1247. ev_periodic_stop (EV_A_ w);
  1248. ev_periodic_start (EV_A_ w);
  1249. }
  1250. #endif
  1251. void
  1252. ev_idle_start (EV_P_ ev_idle *w)
  1253. {
  1254. if (expect_false (ev_is_active (w)))
  1255. return;
  1256. ev_start (EV_A_ (W)w, ++idlecnt);
  1257. array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
  1258. idles [idlecnt - 1] = w;
  1259. }
  1260. void
  1261. ev_idle_stop (EV_P_ ev_idle *w)
  1262. {
  1263. ev_clear_pending (EV_A_ (W)w);
  1264. if (expect_false (!ev_is_active (w)))
  1265. return;
  1266. idles [((W)w)->active - 1] = idles [--idlecnt];
  1267. ev_stop (EV_A_ (W)w);
  1268. }
  1269. void
  1270. ev_prepare_start (EV_P_ ev_prepare *w)
  1271. {
  1272. if (expect_false (ev_is_active (w)))
  1273. return;
  1274. ev_start (EV_A_ (W)w, ++preparecnt);
  1275. array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
  1276. prepares [preparecnt - 1] = w;
  1277. }
  1278. void
  1279. ev_prepare_stop (EV_P_ ev_prepare *w)
  1280. {
  1281. ev_clear_pending (EV_A_ (W)w);
  1282. if (expect_false (!ev_is_active (w)))
  1283. return;
  1284. prepares [((W)w)->active - 1] = prepares [--preparecnt];
  1285. ev_stop (EV_A_ (W)w);
  1286. }
  1287. void
  1288. ev_check_start (EV_P_ ev_check *w)
  1289. {
  1290. if (expect_false (ev_is_active (w)))
  1291. return;
  1292. ev_start (EV_A_ (W)w, ++checkcnt);
  1293. array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
  1294. checks [checkcnt - 1] = w;
  1295. }
  1296. void
  1297. ev_check_stop (EV_P_ ev_check *w)
  1298. {
  1299. ev_clear_pending (EV_A_ (W)w);
  1300. if (expect_false (!ev_is_active (w)))
  1301. return;
  1302. checks [((W)w)->active - 1] = checks [--checkcnt];
  1303. ev_stop (EV_A_ (W)w);
  1304. }
  1305. #ifndef SA_RESTART
  1306. # define SA_RESTART 0
  1307. #endif
  1308. void
  1309. ev_signal_start (EV_P_ ev_signal *w)
  1310. {
  1311. #if EV_MULTIPLICITY
  1312. assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
  1313. #endif
  1314. if (expect_false (ev_is_active (w)))
  1315. return;
  1316. assert (("ev_signal_start called with illegal signal number", w->signum > 0));
  1317. ev_start (EV_A_ (W)w, 1);
  1318. array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
  1319. wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
  1320. if (!((WL)w)->next)
  1321. {
  1322. #if _WIN32
  1323. signal (w->signum, sighandler);
  1324. #else
  1325. struct sigaction sa;
  1326. sa.sa_handler = sighandler;
  1327. sigfillset (&sa.sa_mask);
  1328. sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
  1329. sigaction (w->signum, &sa, 0);
  1330. #endif
  1331. }
  1332. }
  1333. void
  1334. ev_signal_stop (EV_P_ ev_signal *w)
  1335. {
  1336. ev_clear_pending (EV_A_ (W)w);
  1337. if (expect_false (!ev_is_active (w)))
  1338. return;
  1339. wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
  1340. ev_stop (EV_A_ (W)w);
  1341. if (!signals [w->signum - 1].head)
  1342. signal (w->signum, SIG_DFL);
  1343. }
  1344. void
  1345. ev_child_start (EV_P_ ev_child *w)
  1346. {
  1347. #if EV_MULTIPLICITY
  1348. assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
  1349. #endif
  1350. if (expect_false (ev_is_active (w)))
  1351. return;
  1352. ev_start (EV_A_ (W)w, 1);
  1353. wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
  1354. }
  1355. void
  1356. ev_child_stop (EV_P_ ev_child *w)
  1357. {
  1358. ev_clear_pending (EV_A_ (W)w);
  1359. if (expect_false (!ev_is_active (w)))
  1360. return;
  1361. wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
  1362. ev_stop (EV_A_ (W)w);
  1363. }
  1364. #if EV_MULTIPLICITY
  1365. void
  1366. ev_embed_sweep (EV_P_ ev_embed *w)
  1367. {
  1368. ev_loop (w->loop, EVLOOP_NONBLOCK);
  1369. }
  1370. static void
  1371. embed_cb (EV_P_ ev_io *io, int revents)
  1372. {
  1373. ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
  1374. if (ev_cb (w))
  1375. ev_feed_event (EV_A_ (W)w, EV_EMBED);
  1376. else
  1377. ev_embed_sweep (loop, w);
  1378. }
  1379. void
  1380. ev_embed_start (EV_P_ ev_embed *w)
  1381. {
  1382. if (expect_false (ev_is_active (w)))
  1383. return;
  1384. {
  1385. struct ev_loop *loop = w->loop;
  1386. assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
  1387. ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
  1388. }
  1389. ev_set_priority (&w->io, ev_priority (w));
  1390. ev_io_start (EV_A_ &w->io);
  1391. ev_start (EV_A_ (W)w, 1);
  1392. }
  1393. void
  1394. ev_embed_stop (EV_P_ ev_embed *w)
  1395. {
  1396. ev_clear_pending (EV_A_ (W)w);
  1397. if (expect_false (!ev_is_active (w)))
  1398. return;
  1399. ev_io_stop (EV_A_ &w->io);
  1400. ev_stop (EV_A_ (W)w);
  1401. }
  1402. #endif
  1403. /*****************************************************************************/
  1404. struct ev_once
  1405. {
  1406. ev_io io;
  1407. ev_timer to;
  1408. void (*cb)(int revents, void *arg);
  1409. void *arg;
  1410. };
  1411. static void
  1412. once_cb (EV_P_ struct ev_once *once, int revents)
  1413. {
  1414. void (*cb)(int revents, void *arg) = once->cb;
  1415. void *arg = once->arg;
  1416. ev_io_stop (EV_A_ &once->io);
  1417. ev_timer_stop (EV_A_ &once->to);
  1418. ev_free (once);
  1419. cb (revents, arg);
  1420. }
  1421. static void
  1422. once_cb_io (EV_P_ ev_io *w, int revents)
  1423. {
  1424. once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
  1425. }
  1426. static void
  1427. once_cb_to (EV_P_ ev_timer *w, int revents)
  1428. {
  1429. once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
  1430. }
  1431. void
  1432. ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
  1433. {
  1434. struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
  1435. if (expect_false (!once))
  1436. {
  1437. cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
  1438. return;
  1439. }
  1440. once->cb = cb;
  1441. once->arg = arg;
  1442. ev_init (&once->io, once_cb_io);
  1443. if (fd >= 0)
  1444. {
  1445. ev_io_set (&once->io, fd, events);
  1446. ev_io_start (EV_A_ &once->io);
  1447. }
  1448. ev_init (&once->to, once_cb_to);
  1449. if (timeout >= 0.)
  1450. {
  1451. ev_timer_set (&once->to, timeout, 0.);
  1452. ev_timer_start (EV_A_ &once->to);
  1453. }
  1454. }
  1455. #ifdef __cplusplus
  1456. }
  1457. #endif