… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
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. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #include <math.h> |
35 | #include <math.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
… | |
… | |
58 | |
58 | |
59 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
61 | #endif |
61 | #endif |
62 | |
62 | |
63 | #ifndef EV_USE_POLL |
63 | #ifndef EV_USEV_POLL |
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
65 | #endif |
66 | |
66 | |
67 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
69 | #endif |
69 | #endif |
… | |
… | |
113 | |
113 | |
114 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
117 | |
117 | |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ |
119 | ev_tstamp ev_now; |
119 | static ev_tstamp rt_now; |
120 | int ev_method; |
120 | static int method; |
121 | |
121 | |
122 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
123 | |
123 | |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
125 | static void (*method_modify)(int fd, int oev, int nev); |
125 | static void (*method_modify)(EV_P_ int fd, int oev, int nev); |
126 | static void (*method_poll)(ev_tstamp timeout); |
126 | static void (*method_poll)(EV_P_ ev_tstamp timeout); |
|
|
127 | |
|
|
128 | static int activecnt; /* number of active events */ |
|
|
129 | |
|
|
130 | #if EV_USE_SELECT |
|
|
131 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
132 | static int vec_max; |
|
|
133 | #endif |
|
|
134 | |
|
|
135 | #if EV_USEV_POLL |
|
|
136 | static struct pollfd *polls; |
|
|
137 | static int pollmax, pollcnt; |
|
|
138 | static int *pollidxs; /* maps fds into structure indices */ |
|
|
139 | static int pollidxmax; |
|
|
140 | #endif |
|
|
141 | |
|
|
142 | #if EV_USE_EPOLL |
|
|
143 | static int epoll_fd = -1; |
|
|
144 | |
|
|
145 | static struct epoll_event *events; |
|
|
146 | static int eventmax; |
|
|
147 | #endif |
|
|
148 | |
|
|
149 | #if EV_USE_KQUEUE |
|
|
150 | static int kqueue_fd; |
|
|
151 | static struct kevent *kqueue_changes; |
|
|
152 | static int kqueue_changemax, kqueue_changecnt; |
|
|
153 | static struct kevent *kqueue_events; |
|
|
154 | static int kqueue_eventmax; |
|
|
155 | #endif |
127 | |
156 | |
128 | /*****************************************************************************/ |
157 | /*****************************************************************************/ |
129 | |
158 | |
130 | ev_tstamp |
159 | inline ev_tstamp |
131 | ev_time (void) |
160 | ev_time (void) |
132 | { |
161 | { |
133 | #if EV_USE_REALTIME |
162 | #if EV_USE_REALTIME |
134 | struct timespec ts; |
163 | struct timespec ts; |
135 | clock_gettime (CLOCK_REALTIME, &ts); |
164 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
139 | gettimeofday (&tv, 0); |
168 | gettimeofday (&tv, 0); |
140 | return tv.tv_sec + tv.tv_usec * 1e-6; |
169 | return tv.tv_sec + tv.tv_usec * 1e-6; |
141 | #endif |
170 | #endif |
142 | } |
171 | } |
143 | |
172 | |
144 | static ev_tstamp |
173 | inline ev_tstamp |
145 | get_clock (void) |
174 | get_clock (void) |
146 | { |
175 | { |
147 | #if EV_USE_MONOTONIC |
176 | #if EV_USE_MONOTONIC |
148 | if (expect_true (have_monotonic)) |
177 | if (expect_true (have_monotonic)) |
149 | { |
178 | { |
… | |
… | |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
181 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
153 | } |
182 | } |
154 | #endif |
183 | #endif |
155 | |
184 | |
156 | return ev_time (); |
185 | return ev_time (); |
|
|
186 | } |
|
|
187 | |
|
|
188 | ev_tstamp |
|
|
189 | ev_now (EV_P) |
|
|
190 | { |
|
|
191 | return rt_now; |
157 | } |
192 | } |
158 | |
193 | |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
194 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
160 | |
195 | |
161 | #define array_needsize(base,cur,cnt,init) \ |
196 | #define array_needsize(base,cur,cnt,init) \ |
… | |
… | |
175 | |
210 | |
176 | /*****************************************************************************/ |
211 | /*****************************************************************************/ |
177 | |
212 | |
178 | typedef struct |
213 | typedef struct |
179 | { |
214 | { |
180 | struct ev_io *head; |
215 | struct ev_watcher_list *head; |
181 | unsigned char events; |
216 | unsigned char events; |
182 | unsigned char reify; |
217 | unsigned char reify; |
183 | } ANFD; |
218 | } ANFD; |
184 | |
219 | |
185 | static ANFD *anfds; |
220 | static ANFD *anfds; |
… | |
… | |
206 | |
241 | |
207 | static ANPENDING *pendings [NUMPRI]; |
242 | static ANPENDING *pendings [NUMPRI]; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
243 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
209 | |
244 | |
210 | static void |
245 | static void |
211 | event (W w, int events) |
246 | event (EV_P_ W w, int events) |
212 | { |
247 | { |
213 | if (w->pending) |
248 | if (w->pending) |
214 | { |
249 | { |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
250 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
216 | return; |
251 | return; |
… | |
… | |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
256 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
257 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
223 | } |
258 | } |
224 | |
259 | |
225 | static void |
260 | static void |
226 | queue_events (W *events, int eventcnt, int type) |
261 | queue_events (EV_P_ W *events, int eventcnt, int type) |
227 | { |
262 | { |
228 | int i; |
263 | int i; |
229 | |
264 | |
230 | for (i = 0; i < eventcnt; ++i) |
265 | for (i = 0; i < eventcnt; ++i) |
231 | event (events [i], type); |
266 | event (EV_A_ events [i], type); |
232 | } |
267 | } |
233 | |
268 | |
234 | static void |
269 | static void |
235 | fd_event (int fd, int events) |
270 | fd_event (EV_P_ int fd, int events) |
236 | { |
271 | { |
237 | ANFD *anfd = anfds + fd; |
272 | ANFD *anfd = anfds + fd; |
238 | struct ev_io *w; |
273 | struct ev_io *w; |
239 | |
274 | |
240 | for (w = anfd->head; w; w = w->next) |
275 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
241 | { |
276 | { |
242 | int ev = w->events & events; |
277 | int ev = w->events & events; |
243 | |
278 | |
244 | if (ev) |
279 | if (ev) |
245 | event ((W)w, ev); |
280 | event (EV_A_ (W)w, ev); |
246 | } |
281 | } |
247 | } |
282 | } |
248 | |
283 | |
249 | /*****************************************************************************/ |
284 | /*****************************************************************************/ |
250 | |
285 | |
251 | static int *fdchanges; |
286 | static int *fdchanges; |
252 | static int fdchangemax, fdchangecnt; |
287 | static int fdchangemax, fdchangecnt; |
253 | |
288 | |
254 | static void |
289 | static void |
255 | fd_reify (void) |
290 | fd_reify (EV_P) |
256 | { |
291 | { |
257 | int i; |
292 | int i; |
258 | |
293 | |
259 | for (i = 0; i < fdchangecnt; ++i) |
294 | for (i = 0; i < fdchangecnt; ++i) |
260 | { |
295 | { |
… | |
… | |
262 | ANFD *anfd = anfds + fd; |
297 | ANFD *anfd = anfds + fd; |
263 | struct ev_io *w; |
298 | struct ev_io *w; |
264 | |
299 | |
265 | int events = 0; |
300 | int events = 0; |
266 | |
301 | |
267 | for (w = anfd->head; w; w = w->next) |
302 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
268 | events |= w->events; |
303 | events |= w->events; |
269 | |
304 | |
270 | anfd->reify = 0; |
305 | anfd->reify = 0; |
271 | |
306 | |
272 | if (anfd->events != events) |
307 | if (anfd->events != events) |
273 | { |
308 | { |
274 | method_modify (fd, anfd->events, events); |
309 | method_modify (EV_A_ fd, anfd->events, events); |
275 | anfd->events = events; |
310 | anfd->events = events; |
276 | } |
311 | } |
277 | } |
312 | } |
278 | |
313 | |
279 | fdchangecnt = 0; |
314 | fdchangecnt = 0; |
280 | } |
315 | } |
281 | |
316 | |
282 | static void |
317 | static void |
283 | fd_change (int fd) |
318 | fd_change (EV_P_ int fd) |
284 | { |
319 | { |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
320 | if (anfds [fd].reify || fdchangecnt < 0) |
286 | return; |
321 | return; |
287 | |
322 | |
288 | anfds [fd].reify = 1; |
323 | anfds [fd].reify = 1; |
… | |
… | |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
326 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
292 | fdchanges [fdchangecnt - 1] = fd; |
327 | fdchanges [fdchangecnt - 1] = fd; |
293 | } |
328 | } |
294 | |
329 | |
295 | static void |
330 | static void |
296 | fd_kill (int fd) |
331 | fd_kill (EV_P_ int fd) |
297 | { |
332 | { |
298 | struct ev_io *w; |
333 | struct ev_io *w; |
299 | |
334 | |
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
335 | while ((w = (struct ev_io *)anfds [fd].head)) |
302 | { |
336 | { |
303 | ev_io_stop (w); |
337 | ev_io_stop (EV_A_ w); |
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
338 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
305 | } |
339 | } |
306 | } |
340 | } |
307 | |
341 | |
308 | /* called on EBADF to verify fds */ |
342 | /* called on EBADF to verify fds */ |
309 | static void |
343 | static void |
310 | fd_ebadf (void) |
344 | fd_ebadf (EV_P) |
311 | { |
345 | { |
312 | int fd; |
346 | int fd; |
313 | |
347 | |
314 | for (fd = 0; fd < anfdmax; ++fd) |
348 | for (fd = 0; fd < anfdmax; ++fd) |
315 | if (anfds [fd].events) |
349 | if (anfds [fd].events) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
350 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
317 | fd_kill (fd); |
351 | fd_kill (EV_A_ fd); |
318 | } |
352 | } |
319 | |
353 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
354 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
355 | static void |
322 | fd_enomem (void) |
356 | fd_enomem (EV_P) |
323 | { |
357 | { |
324 | int fd = anfdmax; |
358 | int fd = anfdmax; |
325 | |
359 | |
326 | while (fd--) |
360 | while (fd--) |
327 | if (anfds [fd].events) |
361 | if (anfds [fd].events) |
328 | { |
362 | { |
329 | close (fd); |
363 | close (fd); |
330 | fd_kill (fd); |
364 | fd_kill (EV_A_ fd); |
331 | return; |
365 | return; |
332 | } |
366 | } |
333 | } |
367 | } |
334 | |
368 | |
335 | /*****************************************************************************/ |
369 | /*****************************************************************************/ |
… | |
… | |
383 | |
417 | |
384 | /*****************************************************************************/ |
418 | /*****************************************************************************/ |
385 | |
419 | |
386 | typedef struct |
420 | typedef struct |
387 | { |
421 | { |
388 | struct ev_signal *head; |
422 | struct ev_watcher_list *head; |
389 | sig_atomic_t volatile gotsig; |
423 | sig_atomic_t volatile gotsig; |
390 | } ANSIG; |
424 | } ANSIG; |
391 | |
425 | |
392 | static ANSIG *signals; |
426 | static ANSIG *signals; |
393 | static int signalmax; |
427 | static int signalmax; |
… | |
… | |
413 | { |
447 | { |
414 | signals [signum - 1].gotsig = 1; |
448 | signals [signum - 1].gotsig = 1; |
415 | |
449 | |
416 | if (!gotsig) |
450 | if (!gotsig) |
417 | { |
451 | { |
|
|
452 | int old_errno = errno; |
418 | gotsig = 1; |
453 | gotsig = 1; |
419 | write (sigpipe [1], &signum, 1); |
454 | write (sigpipe [1], &signum, 1); |
|
|
455 | errno = old_errno; |
420 | } |
456 | } |
421 | } |
457 | } |
422 | |
458 | |
423 | static void |
459 | static void |
424 | sigcb (struct ev_io *iow, int revents) |
460 | sigcb (EV_P_ struct ev_io *iow, int revents) |
425 | { |
461 | { |
426 | struct ev_signal *w; |
462 | struct ev_watcher_list *w; |
427 | int signum; |
463 | int signum; |
428 | |
464 | |
429 | read (sigpipe [0], &revents, 1); |
465 | read (sigpipe [0], &revents, 1); |
430 | gotsig = 0; |
466 | gotsig = 0; |
431 | |
467 | |
… | |
… | |
433 | if (signals [signum].gotsig) |
469 | if (signals [signum].gotsig) |
434 | { |
470 | { |
435 | signals [signum].gotsig = 0; |
471 | signals [signum].gotsig = 0; |
436 | |
472 | |
437 | for (w = signals [signum].head; w; w = w->next) |
473 | for (w = signals [signum].head; w; w = w->next) |
438 | event ((W)w, EV_SIGNAL); |
474 | event (EV_A_ (W)w, EV_SIGNAL); |
439 | } |
475 | } |
440 | } |
476 | } |
441 | |
477 | |
442 | static void |
478 | static void |
443 | siginit (void) |
479 | siginit (EV_P) |
444 | { |
480 | { |
445 | #ifndef WIN32 |
481 | #ifndef WIN32 |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
482 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
483 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
448 | |
484 | |
… | |
… | |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
452 | #endif |
488 | #endif |
453 | |
489 | |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
455 | ev_io_start (&sigev); |
491 | ev_io_start (&sigev); |
|
|
492 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
456 | } |
493 | } |
457 | |
494 | |
458 | /*****************************************************************************/ |
495 | /*****************************************************************************/ |
459 | |
496 | |
460 | static struct ev_idle **idles; |
497 | static struct ev_idle **idles; |
… | |
… | |
476 | #ifndef WCONTINUED |
513 | #ifndef WCONTINUED |
477 | # define WCONTINUED 0 |
514 | # define WCONTINUED 0 |
478 | #endif |
515 | #endif |
479 | |
516 | |
480 | static void |
517 | static void |
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
518 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
482 | { |
519 | { |
483 | struct ev_child *w; |
520 | struct ev_child *w; |
484 | |
521 | |
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
522 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
486 | if (w->pid == pid || !w->pid) |
523 | if (w->pid == pid || !w->pid) |
487 | { |
524 | { |
488 | w->priority = sw->priority; /* need to do it *now* */ |
525 | w->priority = sw->priority; /* need to do it *now* */ |
489 | w->rpid = pid; |
526 | w->rpid = pid; |
490 | w->rstatus = status; |
527 | w->rstatus = status; |
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
528 | event (EV_A_ (W)w, EV_CHILD); |
493 | } |
529 | } |
494 | } |
530 | } |
495 | |
531 | |
496 | static void |
532 | static void |
497 | childcb (struct ev_signal *sw, int revents) |
533 | childcb (EV_P_ struct ev_signal *sw, int revents) |
498 | { |
534 | { |
499 | int pid, status; |
535 | int pid, status; |
500 | |
536 | |
501 | printf ("chld %x\n", revents);//D |
|
|
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
537 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
503 | { |
538 | { |
504 | /* make sure we are called again until all childs have been reaped */ |
539 | /* make sure we are called again until all childs have been reaped */ |
505 | event ((W)sw, EV_SIGNAL); |
540 | event (EV_A_ (W)sw, EV_SIGNAL); |
506 | |
541 | |
507 | child_reap (sw, pid, pid, status); |
542 | child_reap (EV_A_ sw, pid, pid, status); |
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
543 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
509 | } |
544 | } |
510 | } |
545 | } |
511 | |
546 | |
512 | #endif |
547 | #endif |
513 | |
548 | |
… | |
… | |
517 | # include "ev_kqueue.c" |
552 | # include "ev_kqueue.c" |
518 | #endif |
553 | #endif |
519 | #if EV_USE_EPOLL |
554 | #if EV_USE_EPOLL |
520 | # include "ev_epoll.c" |
555 | # include "ev_epoll.c" |
521 | #endif |
556 | #endif |
522 | #if EV_USE_POLL |
557 | #if EV_USEV_POLL |
523 | # include "ev_poll.c" |
558 | # include "ev_poll.c" |
524 | #endif |
559 | #endif |
525 | #if EV_USE_SELECT |
560 | #if EV_USE_SELECT |
526 | # include "ev_select.c" |
561 | # include "ev_select.c" |
527 | #endif |
562 | #endif |
… | |
… | |
536 | ev_version_minor (void) |
571 | ev_version_minor (void) |
537 | { |
572 | { |
538 | return EV_VERSION_MINOR; |
573 | return EV_VERSION_MINOR; |
539 | } |
574 | } |
540 | |
575 | |
541 | /* return true if we are running with elevated privileges and ignore env variables */ |
576 | /* return true if we are running with elevated privileges and should ignore env variables */ |
542 | static int |
577 | static int |
543 | enable_secure () |
578 | enable_secure (void) |
544 | { |
579 | { |
|
|
580 | #ifdef WIN32 |
|
|
581 | return 0; |
|
|
582 | #else |
545 | return getuid () != geteuid () |
583 | return getuid () != geteuid () |
546 | || getgid () != getegid (); |
584 | || getgid () != getegid (); |
|
|
585 | #endif |
547 | } |
586 | } |
548 | |
587 | |
|
|
588 | int |
|
|
589 | ev_method (EV_P) |
|
|
590 | { |
|
|
591 | return method; |
|
|
592 | } |
|
|
593 | |
|
|
594 | int |
549 | int ev_init (int methods) |
595 | ev_init (EV_P_ int methods) |
550 | { |
596 | { |
551 | if (!ev_method) |
597 | if (!method) |
552 | { |
598 | { |
553 | #if EV_USE_MONOTONIC |
599 | #if EV_USE_MONOTONIC |
554 | { |
600 | { |
555 | struct timespec ts; |
601 | struct timespec ts; |
556 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
602 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
557 | have_monotonic = 1; |
603 | have_monotonic = 1; |
558 | } |
604 | } |
559 | #endif |
605 | #endif |
560 | |
606 | |
561 | ev_now = ev_time (); |
607 | rt_now = ev_time (); |
562 | now = get_clock (); |
608 | mn_now = get_clock (); |
563 | now_floor = now; |
609 | now_floor = mn_now; |
564 | diff = ev_now - now; |
610 | diff = rt_now - mn_now; |
565 | |
611 | |
566 | if (pipe (sigpipe)) |
612 | if (pipe (sigpipe)) |
567 | return 0; |
613 | return 0; |
568 | |
614 | |
569 | if (methods == EVMETHOD_AUTO) |
615 | if (methods == EVMETHOD_AUTO) |
570 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
616 | if (!enable_secure () && getenv ("LIBmethodS")) |
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
617 | methods = atoi (getenv ("LIBmethodS")); |
572 | else |
618 | else |
573 | methods = EVMETHOD_ANY; |
619 | methods = EVMETHOD_ANY; |
574 | |
620 | |
575 | ev_method = 0; |
621 | method = 0; |
576 | #if EV_USE_KQUEUE |
622 | #if EV_USE_KQUEUE |
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
623 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
578 | #endif |
624 | #endif |
579 | #if EV_USE_EPOLL |
625 | #if EV_USE_EPOLL |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
626 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
581 | #endif |
627 | #endif |
582 | #if EV_USE_POLL |
628 | #if EV_USEV_POLL |
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
629 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
584 | #endif |
630 | #endif |
585 | #if EV_USE_SELECT |
631 | #if EV_USE_SELECT |
586 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
632 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
587 | #endif |
633 | #endif |
588 | |
634 | |
589 | if (ev_method) |
635 | if (method) |
590 | { |
636 | { |
591 | ev_watcher_init (&sigev, sigcb); |
637 | ev_watcher_init (&sigev, sigcb); |
592 | ev_set_priority (&sigev, EV_MAXPRI); |
638 | ev_set_priority (&sigev, EV_MAXPRI); |
593 | siginit (); |
639 | siginit (EV_A); |
594 | |
640 | |
595 | #ifndef WIN32 |
641 | #ifndef WIN32 |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
642 | ev_signal_init (&childev, childcb, SIGCHLD); |
597 | ev_set_priority (&childev, EV_MAXPRI); |
643 | ev_set_priority (&childev, EV_MAXPRI); |
598 | ev_signal_start (&childev); |
644 | ev_signal_start (EV_A_ &childev); |
|
|
645 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
599 | #endif |
646 | #endif |
600 | } |
647 | } |
601 | } |
648 | } |
602 | |
649 | |
603 | return ev_method; |
650 | return method; |
604 | } |
651 | } |
605 | |
652 | |
606 | /*****************************************************************************/ |
653 | /*****************************************************************************/ |
607 | |
654 | |
608 | void |
655 | void |
… | |
… | |
619 | |
666 | |
620 | void |
667 | void |
621 | ev_fork_child (void) |
668 | ev_fork_child (void) |
622 | { |
669 | { |
623 | #if EV_USE_EPOLL |
670 | #if EV_USE_EPOLL |
624 | if (ev_method == EVMETHOD_EPOLL) |
671 | if (method == EVMETHOD_EPOLL) |
625 | epoll_postfork_child (); |
672 | epoll_postfork_child (); |
626 | #endif |
673 | #endif |
627 | |
674 | |
628 | ev_io_stop (&sigev); |
675 | ev_io_stop (&sigev); |
629 | close (sigpipe [0]); |
676 | close (sigpipe [0]); |
… | |
… | |
633 | } |
680 | } |
634 | |
681 | |
635 | /*****************************************************************************/ |
682 | /*****************************************************************************/ |
636 | |
683 | |
637 | static void |
684 | static void |
638 | call_pending (void) |
685 | call_pending (EV_P) |
639 | { |
686 | { |
640 | int pri; |
687 | int pri; |
641 | |
688 | |
642 | for (pri = NUMPRI; pri--; ) |
689 | for (pri = NUMPRI; pri--; ) |
643 | while (pendingcnt [pri]) |
690 | while (pendingcnt [pri]) |
… | |
… | |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
692 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
646 | |
693 | |
647 | if (p->w) |
694 | if (p->w) |
648 | { |
695 | { |
649 | p->w->pending = 0; |
696 | p->w->pending = 0; |
650 | p->w->cb (p->w, p->events); |
697 | p->w->cb (EV_A_ p->w, p->events); |
651 | } |
698 | } |
652 | } |
699 | } |
653 | } |
700 | } |
654 | |
701 | |
655 | static void |
702 | static void |
656 | timers_reify (void) |
703 | timers_reify (EV_P) |
657 | { |
704 | { |
658 | while (timercnt && timers [0]->at <= now) |
705 | while (timercnt && timers [0]->at <= mn_now) |
659 | { |
706 | { |
660 | struct ev_timer *w = timers [0]; |
707 | struct ev_timer *w = timers [0]; |
661 | |
708 | |
662 | /* first reschedule or stop timer */ |
709 | /* first reschedule or stop timer */ |
663 | if (w->repeat) |
710 | if (w->repeat) |
664 | { |
711 | { |
665 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
712 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
666 | w->at = now + w->repeat; |
713 | w->at = mn_now + w->repeat; |
667 | downheap ((WT *)timers, timercnt, 0); |
714 | downheap ((WT *)timers, timercnt, 0); |
668 | } |
715 | } |
669 | else |
716 | else |
670 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
717 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
671 | |
718 | |
672 | event ((W)w, EV_TIMEOUT); |
719 | event ((W)w, EV_TIMEOUT); |
673 | } |
720 | } |
674 | } |
721 | } |
675 | |
722 | |
676 | static void |
723 | static void |
677 | periodics_reify (void) |
724 | periodics_reify (EV_P) |
678 | { |
725 | { |
679 | while (periodiccnt && periodics [0]->at <= ev_now) |
726 | while (periodiccnt && periodics [0]->at <= rt_now) |
680 | { |
727 | { |
681 | struct ev_periodic *w = periodics [0]; |
728 | struct ev_periodic *w = periodics [0]; |
682 | |
729 | |
683 | /* first reschedule or stop timer */ |
730 | /* first reschedule or stop timer */ |
684 | if (w->interval) |
731 | if (w->interval) |
685 | { |
732 | { |
686 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
733 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
687 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
734 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
688 | downheap ((WT *)periodics, periodiccnt, 0); |
735 | downheap ((WT *)periodics, periodiccnt, 0); |
689 | } |
736 | } |
690 | else |
737 | else |
691 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
738 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
692 | |
739 | |
693 | event ((W)w, EV_PERIODIC); |
740 | event (EV_A_ (W)w, EV_PERIODIC); |
694 | } |
741 | } |
695 | } |
742 | } |
696 | |
743 | |
697 | static void |
744 | static void |
698 | periodics_reschedule (ev_tstamp diff) |
745 | periodics_reschedule (EV_P_ ev_tstamp diff) |
699 | { |
746 | { |
700 | int i; |
747 | int i; |
701 | |
748 | |
702 | /* adjust periodics after time jump */ |
749 | /* adjust periodics after time jump */ |
703 | for (i = 0; i < periodiccnt; ++i) |
750 | for (i = 0; i < periodiccnt; ++i) |
704 | { |
751 | { |
705 | struct ev_periodic *w = periodics [i]; |
752 | struct ev_periodic *w = periodics [i]; |
706 | |
753 | |
707 | if (w->interval) |
754 | if (w->interval) |
708 | { |
755 | { |
709 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
756 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
710 | |
757 | |
711 | if (fabs (diff) >= 1e-4) |
758 | if (fabs (diff) >= 1e-4) |
712 | { |
759 | { |
713 | ev_periodic_stop (w); |
760 | ev_periodic_stop (EV_A_ w); |
714 | ev_periodic_start (w); |
761 | ev_periodic_start (EV_A_ w); |
715 | |
762 | |
716 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
763 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
717 | } |
764 | } |
718 | } |
765 | } |
719 | } |
766 | } |
720 | } |
767 | } |
721 | |
768 | |
722 | static int |
769 | inline int |
723 | time_update_monotonic (void) |
770 | time_update_monotonic (EV_P) |
724 | { |
771 | { |
725 | now = get_clock (); |
772 | mn_now = get_clock (); |
726 | |
773 | |
727 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
774 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
728 | { |
775 | { |
729 | ev_now = now + diff; |
776 | rt_now = mn_now + diff; |
730 | return 0; |
777 | return 0; |
731 | } |
778 | } |
732 | else |
779 | else |
733 | { |
780 | { |
734 | now_floor = now; |
781 | now_floor = mn_now; |
735 | ev_now = ev_time (); |
782 | rt_now = ev_time (); |
736 | return 1; |
783 | return 1; |
737 | } |
784 | } |
738 | } |
785 | } |
739 | |
786 | |
740 | static void |
787 | static void |
741 | time_update (void) |
788 | time_update (EV_P) |
742 | { |
789 | { |
743 | int i; |
790 | int i; |
744 | |
791 | |
745 | #if EV_USE_MONOTONIC |
792 | #if EV_USE_MONOTONIC |
746 | if (expect_true (have_monotonic)) |
793 | if (expect_true (have_monotonic)) |
747 | { |
794 | { |
748 | if (time_update_monotonic ()) |
795 | if (time_update_monotonic (EV_A)) |
749 | { |
796 | { |
750 | ev_tstamp odiff = diff; |
797 | ev_tstamp odiff = diff; |
751 | |
798 | |
752 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
799 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
753 | { |
800 | { |
754 | diff = ev_now - now; |
801 | diff = rt_now - mn_now; |
755 | |
802 | |
756 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
803 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
757 | return; /* all is well */ |
804 | return; /* all is well */ |
758 | |
805 | |
759 | ev_now = ev_time (); |
806 | rt_now = ev_time (); |
760 | now = get_clock (); |
807 | mn_now = get_clock (); |
761 | now_floor = now; |
808 | now_floor = mn_now; |
762 | } |
809 | } |
763 | |
810 | |
764 | periodics_reschedule (diff - odiff); |
811 | periodics_reschedule (EV_A_ diff - odiff); |
765 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
812 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
766 | } |
813 | } |
767 | } |
814 | } |
768 | else |
815 | else |
769 | #endif |
816 | #endif |
770 | { |
817 | { |
771 | ev_now = ev_time (); |
818 | rt_now = ev_time (); |
772 | |
819 | |
773 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
820 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
774 | { |
821 | { |
775 | periodics_reschedule (ev_now - now); |
822 | periodics_reschedule (EV_A_ rt_now - mn_now); |
776 | |
823 | |
777 | /* adjust timers. this is easy, as the offset is the same for all */ |
824 | /* adjust timers. this is easy, as the offset is the same for all */ |
778 | for (i = 0; i < timercnt; ++i) |
825 | for (i = 0; i < timercnt; ++i) |
779 | timers [i]->at += diff; |
826 | timers [i]->at += diff; |
780 | } |
827 | } |
781 | |
828 | |
782 | now = ev_now; |
829 | mn_now = rt_now; |
783 | } |
830 | } |
784 | } |
831 | } |
785 | |
832 | |
786 | int ev_loop_done; |
833 | void |
|
|
834 | ev_ref (EV_P) |
|
|
835 | { |
|
|
836 | ++activecnt; |
|
|
837 | } |
787 | |
838 | |
|
|
839 | void |
|
|
840 | ev_unref (EV_P) |
|
|
841 | { |
|
|
842 | --activecnt; |
|
|
843 | } |
|
|
844 | |
|
|
845 | static int loop_done; |
|
|
846 | |
|
|
847 | void |
788 | void ev_loop (int flags) |
848 | ev_loop (EV_P_ int flags) |
789 | { |
849 | { |
790 | double block; |
850 | double block; |
791 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
851 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
792 | |
852 | |
793 | do |
853 | do |
794 | { |
854 | { |
795 | /* queue check watchers (and execute them) */ |
855 | /* queue check watchers (and execute them) */ |
796 | if (expect_false (preparecnt)) |
856 | if (expect_false (preparecnt)) |
797 | { |
857 | { |
798 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
858 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
799 | call_pending (); |
859 | call_pending (EV_A); |
800 | } |
860 | } |
801 | |
861 | |
802 | /* update fd-related kernel structures */ |
862 | /* update fd-related kernel structures */ |
803 | fd_reify (); |
863 | fd_reify (EV_A); |
804 | |
864 | |
805 | /* calculate blocking time */ |
865 | /* calculate blocking time */ |
806 | |
866 | |
807 | /* we only need this for !monotonic clockor timers, but as we basically |
867 | /* we only need this for !monotonic clockor timers, but as we basically |
808 | always have timers, we just calculate it always */ |
868 | always have timers, we just calculate it always */ |
809 | #if EV_USE_MONOTONIC |
869 | #if EV_USE_MONOTONIC |
810 | if (expect_true (have_monotonic)) |
870 | if (expect_true (have_monotonic)) |
811 | time_update_monotonic (); |
871 | time_update_monotonic (EV_A); |
812 | else |
872 | else |
813 | #endif |
873 | #endif |
814 | { |
874 | { |
815 | ev_now = ev_time (); |
875 | rt_now = ev_time (); |
816 | now = ev_now; |
876 | mn_now = rt_now; |
817 | } |
877 | } |
818 | |
878 | |
819 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
879 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
820 | block = 0.; |
880 | block = 0.; |
821 | else |
881 | else |
822 | { |
882 | { |
823 | block = MAX_BLOCKTIME; |
883 | block = MAX_BLOCKTIME; |
824 | |
884 | |
825 | if (timercnt) |
885 | if (timercnt) |
826 | { |
886 | { |
827 | ev_tstamp to = timers [0]->at - now + method_fudge; |
887 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
828 | if (block > to) block = to; |
888 | if (block > to) block = to; |
829 | } |
889 | } |
830 | |
890 | |
831 | if (periodiccnt) |
891 | if (periodiccnt) |
832 | { |
892 | { |
833 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
893 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
834 | if (block > to) block = to; |
894 | if (block > to) block = to; |
835 | } |
895 | } |
836 | |
896 | |
837 | if (block < 0.) block = 0.; |
897 | if (block < 0.) block = 0.; |
838 | } |
898 | } |
839 | |
899 | |
840 | method_poll (block); |
900 | method_poll (EV_A_ block); |
841 | |
901 | |
842 | /* update ev_now, do magic */ |
902 | /* update rt_now, do magic */ |
843 | time_update (); |
903 | time_update (EV_A); |
844 | |
904 | |
845 | /* queue pending timers and reschedule them */ |
905 | /* queue pending timers and reschedule them */ |
846 | timers_reify (); /* relative timers called last */ |
906 | timers_reify (EV_A); /* relative timers called last */ |
847 | periodics_reify (); /* absolute timers called first */ |
907 | periodics_reify (EV_A); /* absolute timers called first */ |
848 | |
908 | |
849 | /* queue idle watchers unless io or timers are pending */ |
909 | /* queue idle watchers unless io or timers are pending */ |
850 | if (!pendingcnt) |
910 | if (!pendingcnt) |
851 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
911 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
852 | |
912 | |
853 | /* queue check watchers, to be executed first */ |
913 | /* queue check watchers, to be executed first */ |
854 | if (checkcnt) |
914 | if (checkcnt) |
855 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
915 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
856 | |
916 | |
857 | call_pending (); |
917 | call_pending (EV_A); |
858 | } |
918 | } |
859 | while (!ev_loop_done); |
919 | while (activecnt && !loop_done); |
860 | |
920 | |
861 | if (ev_loop_done != 2) |
921 | if (loop_done != 2) |
862 | ev_loop_done = 0; |
922 | loop_done = 0; |
|
|
923 | } |
|
|
924 | |
|
|
925 | void |
|
|
926 | ev_unloop (EV_P_ int how) |
|
|
927 | { |
|
|
928 | loop_done = how; |
863 | } |
929 | } |
864 | |
930 | |
865 | /*****************************************************************************/ |
931 | /*****************************************************************************/ |
866 | |
932 | |
867 | static void |
933 | inline void |
868 | wlist_add (WL *head, WL elem) |
934 | wlist_add (WL *head, WL elem) |
869 | { |
935 | { |
870 | elem->next = *head; |
936 | elem->next = *head; |
871 | *head = elem; |
937 | *head = elem; |
872 | } |
938 | } |
873 | |
939 | |
874 | static void |
940 | inline void |
875 | wlist_del (WL *head, WL elem) |
941 | wlist_del (WL *head, WL elem) |
876 | { |
942 | { |
877 | while (*head) |
943 | while (*head) |
878 | { |
944 | { |
879 | if (*head == elem) |
945 | if (*head == elem) |
… | |
… | |
884 | |
950 | |
885 | head = &(*head)->next; |
951 | head = &(*head)->next; |
886 | } |
952 | } |
887 | } |
953 | } |
888 | |
954 | |
889 | static void |
955 | inline void |
890 | ev_clear_pending (W w) |
956 | ev_clear_pending (EV_P_ W w) |
891 | { |
957 | { |
892 | if (w->pending) |
958 | if (w->pending) |
893 | { |
959 | { |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
960 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
895 | w->pending = 0; |
961 | w->pending = 0; |
896 | } |
962 | } |
897 | } |
963 | } |
898 | |
964 | |
899 | static void |
965 | inline void |
900 | ev_start (W w, int active) |
966 | ev_start (EV_P_ W w, int active) |
901 | { |
967 | { |
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
968 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
969 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
904 | |
970 | |
905 | w->active = active; |
971 | w->active = active; |
|
|
972 | ev_ref (EV_A); |
906 | } |
973 | } |
907 | |
974 | |
908 | static void |
975 | inline void |
909 | ev_stop (W w) |
976 | ev_stop (EV_P_ W w) |
910 | { |
977 | { |
|
|
978 | ev_unref (EV_A); |
911 | w->active = 0; |
979 | w->active = 0; |
912 | } |
980 | } |
913 | |
981 | |
914 | /*****************************************************************************/ |
982 | /*****************************************************************************/ |
915 | |
983 | |
916 | void |
984 | void |
917 | ev_io_start (struct ev_io *w) |
985 | ev_io_start (EV_P_ struct ev_io *w) |
918 | { |
986 | { |
919 | int fd = w->fd; |
987 | int fd = w->fd; |
920 | |
988 | |
921 | if (ev_is_active (w)) |
989 | if (ev_is_active (w)) |
922 | return; |
990 | return; |
923 | |
991 | |
924 | assert (("ev_io_start called with negative fd", fd >= 0)); |
992 | assert (("ev_io_start called with negative fd", fd >= 0)); |
925 | |
993 | |
926 | ev_start ((W)w, 1); |
994 | ev_start (EV_A_ (W)w, 1); |
927 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
995 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
928 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
996 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
929 | |
997 | |
930 | fd_change (fd); |
998 | fd_change (EV_A_ fd); |
931 | } |
999 | } |
932 | |
1000 | |
933 | void |
1001 | void |
934 | ev_io_stop (struct ev_io *w) |
1002 | ev_io_stop (EV_P_ struct ev_io *w) |
935 | { |
1003 | { |
936 | ev_clear_pending ((W)w); |
1004 | ev_clear_pending (EV_A_ (W)w); |
937 | if (!ev_is_active (w)) |
1005 | if (!ev_is_active (w)) |
938 | return; |
1006 | return; |
939 | |
1007 | |
940 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1008 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
941 | ev_stop ((W)w); |
1009 | ev_stop (EV_A_ (W)w); |
942 | |
1010 | |
943 | fd_change (w->fd); |
1011 | fd_change (EV_A_ w->fd); |
944 | } |
1012 | } |
945 | |
1013 | |
946 | void |
1014 | void |
947 | ev_timer_start (struct ev_timer *w) |
1015 | ev_timer_start (EV_P_ struct ev_timer *w) |
948 | { |
1016 | { |
949 | if (ev_is_active (w)) |
1017 | if (ev_is_active (w)) |
950 | return; |
1018 | return; |
951 | |
1019 | |
952 | w->at += now; |
1020 | w->at += mn_now; |
953 | |
1021 | |
954 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1022 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
955 | |
1023 | |
956 | ev_start ((W)w, ++timercnt); |
1024 | ev_start (EV_A_ (W)w, ++timercnt); |
957 | array_needsize (timers, timermax, timercnt, ); |
1025 | array_needsize (timers, timermax, timercnt, ); |
958 | timers [timercnt - 1] = w; |
1026 | timers [timercnt - 1] = w; |
959 | upheap ((WT *)timers, timercnt - 1); |
1027 | upheap ((WT *)timers, timercnt - 1); |
960 | } |
1028 | } |
961 | |
1029 | |
962 | void |
1030 | void |
963 | ev_timer_stop (struct ev_timer *w) |
1031 | ev_timer_stop (EV_P_ struct ev_timer *w) |
964 | { |
1032 | { |
965 | ev_clear_pending ((W)w); |
1033 | ev_clear_pending (EV_A_ (W)w); |
966 | if (!ev_is_active (w)) |
1034 | if (!ev_is_active (w)) |
967 | return; |
1035 | return; |
968 | |
1036 | |
969 | if (w->active < timercnt--) |
1037 | if (w->active < timercnt--) |
970 | { |
1038 | { |
… | |
… | |
972 | downheap ((WT *)timers, timercnt, w->active - 1); |
1040 | downheap ((WT *)timers, timercnt, w->active - 1); |
973 | } |
1041 | } |
974 | |
1042 | |
975 | w->at = w->repeat; |
1043 | w->at = w->repeat; |
976 | |
1044 | |
977 | ev_stop ((W)w); |
1045 | ev_stop (EV_A_ (W)w); |
978 | } |
1046 | } |
979 | |
1047 | |
980 | void |
1048 | void |
981 | ev_timer_again (struct ev_timer *w) |
1049 | ev_timer_again (EV_P_ struct ev_timer *w) |
982 | { |
1050 | { |
983 | if (ev_is_active (w)) |
1051 | if (ev_is_active (w)) |
984 | { |
1052 | { |
985 | if (w->repeat) |
1053 | if (w->repeat) |
986 | { |
1054 | { |
987 | w->at = now + w->repeat; |
1055 | w->at = mn_now + w->repeat; |
988 | downheap ((WT *)timers, timercnt, w->active - 1); |
1056 | downheap ((WT *)timers, timercnt, w->active - 1); |
989 | } |
1057 | } |
990 | else |
1058 | else |
991 | ev_timer_stop (w); |
1059 | ev_timer_stop (EV_A_ w); |
992 | } |
1060 | } |
993 | else if (w->repeat) |
1061 | else if (w->repeat) |
994 | ev_timer_start (w); |
1062 | ev_timer_start (EV_A_ w); |
995 | } |
1063 | } |
996 | |
1064 | |
997 | void |
1065 | void |
998 | ev_periodic_start (struct ev_periodic *w) |
1066 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
999 | { |
1067 | { |
1000 | if (ev_is_active (w)) |
1068 | if (ev_is_active (w)) |
1001 | return; |
1069 | return; |
1002 | |
1070 | |
1003 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1071 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1004 | |
1072 | |
1005 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1073 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1006 | if (w->interval) |
1074 | if (w->interval) |
1007 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1075 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1008 | |
1076 | |
1009 | ev_start ((W)w, ++periodiccnt); |
1077 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1010 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1078 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1011 | periodics [periodiccnt - 1] = w; |
1079 | periodics [periodiccnt - 1] = w; |
1012 | upheap ((WT *)periodics, periodiccnt - 1); |
1080 | upheap ((WT *)periodics, periodiccnt - 1); |
1013 | } |
1081 | } |
1014 | |
1082 | |
1015 | void |
1083 | void |
1016 | ev_periodic_stop (struct ev_periodic *w) |
1084 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1017 | { |
1085 | { |
1018 | ev_clear_pending ((W)w); |
1086 | ev_clear_pending (EV_A_ (W)w); |
1019 | if (!ev_is_active (w)) |
1087 | if (!ev_is_active (w)) |
1020 | return; |
1088 | return; |
1021 | |
1089 | |
1022 | if (w->active < periodiccnt--) |
1090 | if (w->active < periodiccnt--) |
1023 | { |
1091 | { |
1024 | periodics [w->active - 1] = periodics [periodiccnt]; |
1092 | periodics [w->active - 1] = periodics [periodiccnt]; |
1025 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1093 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1026 | } |
1094 | } |
1027 | |
1095 | |
1028 | ev_stop ((W)w); |
1096 | ev_stop (EV_A_ (W)w); |
1029 | } |
1097 | } |
1030 | |
1098 | |
1031 | #ifndef SA_RESTART |
1099 | #ifndef SA_RESTART |
1032 | # define SA_RESTART 0 |
1100 | # define SA_RESTART 0 |
1033 | #endif |
1101 | #endif |
1034 | |
1102 | |
1035 | void |
1103 | void |
1036 | ev_signal_start (struct ev_signal *w) |
1104 | ev_signal_start (EV_P_ struct ev_signal *w) |
1037 | { |
1105 | { |
1038 | if (ev_is_active (w)) |
1106 | if (ev_is_active (w)) |
1039 | return; |
1107 | return; |
1040 | |
1108 | |
1041 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1109 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1042 | |
1110 | |
1043 | ev_start ((W)w, 1); |
1111 | ev_start (EV_A_ (W)w, 1); |
1044 | array_needsize (signals, signalmax, w->signum, signals_init); |
1112 | array_needsize (signals, signalmax, w->signum, signals_init); |
1045 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1113 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1046 | |
1114 | |
1047 | if (!w->next) |
1115 | if (!w->next) |
1048 | { |
1116 | { |
… | |
… | |
1053 | sigaction (w->signum, &sa, 0); |
1121 | sigaction (w->signum, &sa, 0); |
1054 | } |
1122 | } |
1055 | } |
1123 | } |
1056 | |
1124 | |
1057 | void |
1125 | void |
1058 | ev_signal_stop (struct ev_signal *w) |
1126 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1059 | { |
1127 | { |
1060 | ev_clear_pending ((W)w); |
1128 | ev_clear_pending (EV_A_ (W)w); |
1061 | if (!ev_is_active (w)) |
1129 | if (!ev_is_active (w)) |
1062 | return; |
1130 | return; |
1063 | |
1131 | |
1064 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1132 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1065 | ev_stop ((W)w); |
1133 | ev_stop (EV_A_ (W)w); |
1066 | |
1134 | |
1067 | if (!signals [w->signum - 1].head) |
1135 | if (!signals [w->signum - 1].head) |
1068 | signal (w->signum, SIG_DFL); |
1136 | signal (w->signum, SIG_DFL); |
1069 | } |
1137 | } |
1070 | |
1138 | |
1071 | void |
1139 | void |
1072 | ev_idle_start (struct ev_idle *w) |
1140 | ev_idle_start (EV_P_ struct ev_idle *w) |
1073 | { |
1141 | { |
1074 | if (ev_is_active (w)) |
1142 | if (ev_is_active (w)) |
1075 | return; |
1143 | return; |
1076 | |
1144 | |
1077 | ev_start ((W)w, ++idlecnt); |
1145 | ev_start (EV_A_ (W)w, ++idlecnt); |
1078 | array_needsize (idles, idlemax, idlecnt, ); |
1146 | array_needsize (idles, idlemax, idlecnt, ); |
1079 | idles [idlecnt - 1] = w; |
1147 | idles [idlecnt - 1] = w; |
1080 | } |
1148 | } |
1081 | |
1149 | |
1082 | void |
1150 | void |
1083 | ev_idle_stop (struct ev_idle *w) |
1151 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1084 | { |
1152 | { |
1085 | ev_clear_pending ((W)w); |
1153 | ev_clear_pending (EV_A_ (W)w); |
1086 | if (ev_is_active (w)) |
1154 | if (ev_is_active (w)) |
1087 | return; |
1155 | return; |
1088 | |
1156 | |
1089 | idles [w->active - 1] = idles [--idlecnt]; |
1157 | idles [w->active - 1] = idles [--idlecnt]; |
1090 | ev_stop ((W)w); |
1158 | ev_stop (EV_A_ (W)w); |
1091 | } |
1159 | } |
1092 | |
1160 | |
1093 | void |
1161 | void |
1094 | ev_prepare_start (struct ev_prepare *w) |
1162 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1095 | { |
1163 | { |
1096 | if (ev_is_active (w)) |
1164 | if (ev_is_active (w)) |
1097 | return; |
1165 | return; |
1098 | |
1166 | |
1099 | ev_start ((W)w, ++preparecnt); |
1167 | ev_start (EV_A_ (W)w, ++preparecnt); |
1100 | array_needsize (prepares, preparemax, preparecnt, ); |
1168 | array_needsize (prepares, preparemax, preparecnt, ); |
1101 | prepares [preparecnt - 1] = w; |
1169 | prepares [preparecnt - 1] = w; |
1102 | } |
1170 | } |
1103 | |
1171 | |
1104 | void |
1172 | void |
1105 | ev_prepare_stop (struct ev_prepare *w) |
1173 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1106 | { |
1174 | { |
1107 | ev_clear_pending ((W)w); |
1175 | ev_clear_pending (EV_A_ (W)w); |
1108 | if (ev_is_active (w)) |
1176 | if (ev_is_active (w)) |
1109 | return; |
1177 | return; |
1110 | |
1178 | |
1111 | prepares [w->active - 1] = prepares [--preparecnt]; |
1179 | prepares [w->active - 1] = prepares [--preparecnt]; |
1112 | ev_stop ((W)w); |
1180 | ev_stop (EV_A_ (W)w); |
1113 | } |
1181 | } |
1114 | |
1182 | |
1115 | void |
1183 | void |
1116 | ev_check_start (struct ev_check *w) |
1184 | ev_check_start (EV_P_ struct ev_check *w) |
1117 | { |
1185 | { |
1118 | if (ev_is_active (w)) |
1186 | if (ev_is_active (w)) |
1119 | return; |
1187 | return; |
1120 | |
1188 | |
1121 | ev_start ((W)w, ++checkcnt); |
1189 | ev_start (EV_A_ (W)w, ++checkcnt); |
1122 | array_needsize (checks, checkmax, checkcnt, ); |
1190 | array_needsize (checks, checkmax, checkcnt, ); |
1123 | checks [checkcnt - 1] = w; |
1191 | checks [checkcnt - 1] = w; |
1124 | } |
1192 | } |
1125 | |
1193 | |
1126 | void |
1194 | void |
1127 | ev_check_stop (struct ev_check *w) |
1195 | ev_check_stop (EV_P_ struct ev_check *w) |
1128 | { |
1196 | { |
1129 | ev_clear_pending ((W)w); |
1197 | ev_clear_pending (EV_A_ (W)w); |
1130 | if (ev_is_active (w)) |
1198 | if (ev_is_active (w)) |
1131 | return; |
1199 | return; |
1132 | |
1200 | |
1133 | checks [w->active - 1] = checks [--checkcnt]; |
1201 | checks [w->active - 1] = checks [--checkcnt]; |
1134 | ev_stop ((W)w); |
1202 | ev_stop (EV_A_ (W)w); |
1135 | } |
1203 | } |
1136 | |
1204 | |
1137 | void |
1205 | void |
1138 | ev_child_start (struct ev_child *w) |
1206 | ev_child_start (EV_P_ struct ev_child *w) |
1139 | { |
1207 | { |
1140 | if (ev_is_active (w)) |
1208 | if (ev_is_active (w)) |
1141 | return; |
1209 | return; |
1142 | |
1210 | |
1143 | ev_start ((W)w, 1); |
1211 | ev_start (EV_A_ (W)w, 1); |
1144 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1212 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1145 | } |
1213 | } |
1146 | |
1214 | |
1147 | void |
1215 | void |
1148 | ev_child_stop (struct ev_child *w) |
1216 | ev_child_stop (EV_P_ struct ev_child *w) |
1149 | { |
1217 | { |
1150 | ev_clear_pending ((W)w); |
1218 | ev_clear_pending (EV_A_ (W)w); |
1151 | if (ev_is_active (w)) |
1219 | if (ev_is_active (w)) |
1152 | return; |
1220 | return; |
1153 | |
1221 | |
1154 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1222 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1155 | ev_stop ((W)w); |
1223 | ev_stop (EV_A_ (W)w); |
1156 | } |
1224 | } |
1157 | |
1225 | |
1158 | /*****************************************************************************/ |
1226 | /*****************************************************************************/ |
1159 | |
1227 | |
1160 | struct ev_once |
1228 | struct ev_once |
… | |
… | |
1164 | void (*cb)(int revents, void *arg); |
1232 | void (*cb)(int revents, void *arg); |
1165 | void *arg; |
1233 | void *arg; |
1166 | }; |
1234 | }; |
1167 | |
1235 | |
1168 | static void |
1236 | static void |
1169 | once_cb (struct ev_once *once, int revents) |
1237 | once_cb (EV_P_ struct ev_once *once, int revents) |
1170 | { |
1238 | { |
1171 | void (*cb)(int revents, void *arg) = once->cb; |
1239 | void (*cb)(int revents, void *arg) = once->cb; |
1172 | void *arg = once->arg; |
1240 | void *arg = once->arg; |
1173 | |
1241 | |
1174 | ev_io_stop (&once->io); |
1242 | ev_io_stop (EV_A_ &once->io); |
1175 | ev_timer_stop (&once->to); |
1243 | ev_timer_stop (EV_A_ &once->to); |
1176 | free (once); |
1244 | free (once); |
1177 | |
1245 | |
1178 | cb (revents, arg); |
1246 | cb (revents, arg); |
1179 | } |
1247 | } |
1180 | |
1248 | |
1181 | static void |
1249 | static void |
1182 | once_cb_io (struct ev_io *w, int revents) |
1250 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1183 | { |
1251 | { |
1184 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1252 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1185 | } |
1253 | } |
1186 | |
1254 | |
1187 | static void |
1255 | static void |
1188 | once_cb_to (struct ev_timer *w, int revents) |
1256 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1189 | { |
1257 | { |
1190 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1258 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1191 | } |
1259 | } |
1192 | |
1260 | |
1193 | void |
1261 | void |
1194 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1262 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1195 | { |
1263 | { |
1196 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1264 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1197 | |
1265 | |
1198 | if (!once) |
1266 | if (!once) |
1199 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1267 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1204 | |
1272 | |
1205 | ev_watcher_init (&once->io, once_cb_io); |
1273 | ev_watcher_init (&once->io, once_cb_io); |
1206 | if (fd >= 0) |
1274 | if (fd >= 0) |
1207 | { |
1275 | { |
1208 | ev_io_set (&once->io, fd, events); |
1276 | ev_io_set (&once->io, fd, events); |
1209 | ev_io_start (&once->io); |
1277 | ev_io_start (EV_A_ &once->io); |
1210 | } |
1278 | } |
1211 | |
1279 | |
1212 | ev_watcher_init (&once->to, once_cb_to); |
1280 | ev_watcher_init (&once->to, once_cb_to); |
1213 | if (timeout >= 0.) |
1281 | if (timeout >= 0.) |
1214 | { |
1282 | { |
1215 | ev_timer_set (&once->to, timeout, 0.); |
1283 | ev_timer_set (&once->to, timeout, 0.); |
1216 | ev_timer_start (&once->to); |
1284 | ev_timer_start (EV_A_ &once->to); |
1217 | } |
1285 | } |
1218 | } |
1286 | } |
1219 | } |
1287 | } |
1220 | |
1288 | |
1221 | /*****************************************************************************/ |
1289 | /*****************************************************************************/ |