… | |
… | |
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) \ |
… | |
… | |
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 = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)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 | { |
… | |
… | |
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 | while ((w = (struct ev_io *)anfds [fd].head)) |
335 | while ((w = (struct ev_io *)anfds [fd].head)) |
301 | { |
336 | { |
302 | ev_io_stop (w); |
337 | ev_io_stop (EV_A_ w); |
303 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
338 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
304 | } |
339 | } |
305 | } |
340 | } |
306 | |
341 | |
307 | /* called on EBADF to verify fds */ |
342 | /* called on EBADF to verify fds */ |
308 | static void |
343 | static void |
309 | fd_ebadf (void) |
344 | fd_ebadf (EV_P) |
310 | { |
345 | { |
311 | int fd; |
346 | int fd; |
312 | |
347 | |
313 | for (fd = 0; fd < anfdmax; ++fd) |
348 | for (fd = 0; fd < anfdmax; ++fd) |
314 | if (anfds [fd].events) |
349 | if (anfds [fd].events) |
315 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
350 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | fd_kill (fd); |
351 | fd_kill (EV_A_ fd); |
317 | } |
352 | } |
318 | |
353 | |
319 | /* 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 */ |
320 | static void |
355 | static void |
321 | fd_enomem (void) |
356 | fd_enomem (EV_P) |
322 | { |
357 | { |
323 | int fd = anfdmax; |
358 | int fd = anfdmax; |
324 | |
359 | |
325 | while (fd--) |
360 | while (fd--) |
326 | if (anfds [fd].events) |
361 | if (anfds [fd].events) |
327 | { |
362 | { |
328 | close (fd); |
363 | close (fd); |
329 | fd_kill (fd); |
364 | fd_kill (EV_A_ fd); |
330 | return; |
365 | return; |
331 | } |
366 | } |
332 | } |
367 | } |
333 | |
368 | |
334 | /*****************************************************************************/ |
369 | /*****************************************************************************/ |
… | |
… | |
420 | errno = old_errno; |
455 | errno = old_errno; |
421 | } |
456 | } |
422 | } |
457 | } |
423 | |
458 | |
424 | static void |
459 | static void |
425 | sigcb (struct ev_io *iow, int revents) |
460 | sigcb (EV_P_ struct ev_io *iow, int revents) |
426 | { |
461 | { |
427 | struct ev_watcher_list *w; |
462 | struct ev_watcher_list *w; |
428 | int signum; |
463 | int signum; |
429 | |
464 | |
430 | read (sigpipe [0], &revents, 1); |
465 | read (sigpipe [0], &revents, 1); |
… | |
… | |
434 | if (signals [signum].gotsig) |
469 | if (signals [signum].gotsig) |
435 | { |
470 | { |
436 | signals [signum].gotsig = 0; |
471 | signals [signum].gotsig = 0; |
437 | |
472 | |
438 | for (w = signals [signum].head; w; w = w->next) |
473 | for (w = signals [signum].head; w; w = w->next) |
439 | event ((W)w, EV_SIGNAL); |
474 | event (EV_A_ (W)w, EV_SIGNAL); |
440 | } |
475 | } |
441 | } |
476 | } |
442 | |
477 | |
443 | static void |
478 | static void |
444 | siginit (void) |
479 | siginit (EV_P) |
445 | { |
480 | { |
446 | #ifndef WIN32 |
481 | #ifndef WIN32 |
447 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
482 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
483 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | |
484 | |
… | |
… | |
477 | #ifndef WCONTINUED |
512 | #ifndef WCONTINUED |
478 | # define WCONTINUED 0 |
513 | # define WCONTINUED 0 |
479 | #endif |
514 | #endif |
480 | |
515 | |
481 | static void |
516 | static void |
482 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
517 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
483 | { |
518 | { |
484 | struct ev_child *w; |
519 | struct ev_child *w; |
485 | |
520 | |
486 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
521 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
487 | if (w->pid == pid || !w->pid) |
522 | if (w->pid == pid || !w->pid) |
488 | { |
523 | { |
489 | w->priority = sw->priority; /* need to do it *now* */ |
524 | w->priority = sw->priority; /* need to do it *now* */ |
490 | w->rpid = pid; |
525 | w->rpid = pid; |
491 | w->rstatus = status; |
526 | w->rstatus = status; |
492 | event ((W)w, EV_CHILD); |
527 | event (EV_A_ (W)w, EV_CHILD); |
493 | } |
528 | } |
494 | } |
529 | } |
495 | |
530 | |
496 | static void |
531 | static void |
497 | childcb (struct ev_signal *sw, int revents) |
532 | childcb (EV_P_ struct ev_signal *sw, int revents) |
498 | { |
533 | { |
499 | int pid, status; |
534 | int pid, status; |
500 | |
535 | |
501 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
536 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
502 | { |
537 | { |
503 | /* make sure we are called again until all childs have been reaped */ |
538 | /* make sure we are called again until all childs have been reaped */ |
504 | event ((W)sw, EV_SIGNAL); |
539 | event (EV_A_ (W)sw, EV_SIGNAL); |
505 | |
540 | |
506 | child_reap (sw, pid, pid, status); |
541 | child_reap (EV_A_ sw, pid, pid, status); |
507 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
542 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
508 | } |
543 | } |
509 | } |
544 | } |
510 | |
545 | |
511 | #endif |
546 | #endif |
512 | |
547 | |
… | |
… | |
516 | # include "ev_kqueue.c" |
551 | # include "ev_kqueue.c" |
517 | #endif |
552 | #endif |
518 | #if EV_USE_EPOLL |
553 | #if EV_USE_EPOLL |
519 | # include "ev_epoll.c" |
554 | # include "ev_epoll.c" |
520 | #endif |
555 | #endif |
521 | #if EV_USE_POLL |
556 | #if EV_USEV_POLL |
522 | # include "ev_poll.c" |
557 | # include "ev_poll.c" |
523 | #endif |
558 | #endif |
524 | #if EV_USE_SELECT |
559 | #if EV_USE_SELECT |
525 | # include "ev_select.c" |
560 | # include "ev_select.c" |
526 | #endif |
561 | #endif |
… | |
… | |
537 | return EV_VERSION_MINOR; |
572 | return EV_VERSION_MINOR; |
538 | } |
573 | } |
539 | |
574 | |
540 | /* return true if we are running with elevated privileges and should ignore env variables */ |
575 | /* return true if we are running with elevated privileges and should ignore env variables */ |
541 | static int |
576 | static int |
542 | enable_secure () |
577 | enable_secure (void) |
543 | { |
578 | { |
544 | #ifdef WIN32 |
579 | #ifdef WIN32 |
545 | return 0; |
580 | return 0; |
546 | #else |
581 | #else |
547 | return getuid () != geteuid () |
582 | return getuid () != geteuid () |
548 | || getgid () != getegid (); |
583 | || getgid () != getegid (); |
549 | #endif |
584 | #endif |
550 | } |
585 | } |
551 | |
586 | |
|
|
587 | int |
|
|
588 | ev_method (EV_P) |
|
|
589 | { |
|
|
590 | return method; |
|
|
591 | } |
|
|
592 | |
|
|
593 | int |
552 | int ev_init (int methods) |
594 | ev_init (EV_P_ int methods) |
553 | { |
595 | { |
554 | if (!ev_method) |
596 | if (!method) |
555 | { |
597 | { |
556 | #if EV_USE_MONOTONIC |
598 | #if EV_USE_MONOTONIC |
557 | { |
599 | { |
558 | struct timespec ts; |
600 | struct timespec ts; |
559 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
601 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
560 | have_monotonic = 1; |
602 | have_monotonic = 1; |
561 | } |
603 | } |
562 | #endif |
604 | #endif |
563 | |
605 | |
564 | ev_now = ev_time (); |
606 | rt_now = ev_time (); |
565 | now = get_clock (); |
607 | mn_now = get_clock (); |
566 | now_floor = now; |
608 | now_floor = mn_now; |
567 | diff = ev_now - now; |
609 | diff = rt_now - mn_now; |
568 | |
610 | |
569 | if (pipe (sigpipe)) |
611 | if (pipe (sigpipe)) |
570 | return 0; |
612 | return 0; |
571 | |
613 | |
572 | if (methods == EVMETHOD_AUTO) |
614 | if (methods == EVMETHOD_AUTO) |
573 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
615 | if (!enable_secure () && getenv ("LIBmethodS")) |
574 | methods = atoi (getenv ("LIBEV_METHODS")); |
616 | methods = atoi (getenv ("LIBmethodS")); |
575 | else |
617 | else |
576 | methods = EVMETHOD_ANY; |
618 | methods = EVMETHOD_ANY; |
577 | |
619 | |
578 | ev_method = 0; |
620 | method = 0; |
579 | #if EV_USE_KQUEUE |
621 | #if EV_USE_KQUEUE |
580 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
622 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
581 | #endif |
623 | #endif |
582 | #if EV_USE_EPOLL |
624 | #if EV_USE_EPOLL |
583 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
625 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
584 | #endif |
626 | #endif |
585 | #if EV_USE_POLL |
627 | #if EV_USEV_POLL |
586 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
628 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
587 | #endif |
629 | #endif |
588 | #if EV_USE_SELECT |
630 | #if EV_USE_SELECT |
589 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
631 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
590 | #endif |
632 | #endif |
591 | |
633 | |
592 | if (ev_method) |
634 | if (method) |
593 | { |
635 | { |
594 | ev_watcher_init (&sigev, sigcb); |
636 | ev_watcher_init (&sigev, sigcb); |
595 | ev_set_priority (&sigev, EV_MAXPRI); |
637 | ev_set_priority (&sigev, EV_MAXPRI); |
596 | siginit (); |
638 | siginit (EV_A); |
597 | |
639 | |
598 | #ifndef WIN32 |
640 | #ifndef WIN32 |
599 | ev_signal_init (&childev, childcb, SIGCHLD); |
641 | ev_signal_init (&childev, childcb, SIGCHLD); |
600 | ev_set_priority (&childev, EV_MAXPRI); |
642 | ev_set_priority (&childev, EV_MAXPRI); |
601 | ev_signal_start (&childev); |
643 | ev_signal_start (EV_A_ &childev); |
602 | #endif |
644 | #endif |
603 | } |
645 | } |
604 | } |
646 | } |
605 | |
647 | |
606 | return ev_method; |
648 | return method; |
607 | } |
649 | } |
608 | |
650 | |
609 | /*****************************************************************************/ |
651 | /*****************************************************************************/ |
610 | |
652 | |
611 | void |
653 | void |
… | |
… | |
622 | |
664 | |
623 | void |
665 | void |
624 | ev_fork_child (void) |
666 | ev_fork_child (void) |
625 | { |
667 | { |
626 | #if EV_USE_EPOLL |
668 | #if EV_USE_EPOLL |
627 | if (ev_method == EVMETHOD_EPOLL) |
669 | if (method == EVMETHOD_EPOLL) |
628 | epoll_postfork_child (); |
670 | epoll_postfork_child (); |
629 | #endif |
671 | #endif |
630 | |
672 | |
631 | ev_io_stop (&sigev); |
673 | ev_io_stop (&sigev); |
632 | close (sigpipe [0]); |
674 | close (sigpipe [0]); |
… | |
… | |
636 | } |
678 | } |
637 | |
679 | |
638 | /*****************************************************************************/ |
680 | /*****************************************************************************/ |
639 | |
681 | |
640 | static void |
682 | static void |
641 | call_pending (void) |
683 | call_pending (EV_P) |
642 | { |
684 | { |
643 | int pri; |
685 | int pri; |
644 | |
686 | |
645 | for (pri = NUMPRI; pri--; ) |
687 | for (pri = NUMPRI; pri--; ) |
646 | while (pendingcnt [pri]) |
688 | while (pendingcnt [pri]) |
… | |
… | |
648 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
690 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
649 | |
691 | |
650 | if (p->w) |
692 | if (p->w) |
651 | { |
693 | { |
652 | p->w->pending = 0; |
694 | p->w->pending = 0; |
653 | p->w->cb (p->w, p->events); |
695 | p->w->cb (EV_A_ p->w, p->events); |
654 | } |
696 | } |
655 | } |
697 | } |
656 | } |
698 | } |
657 | |
699 | |
658 | static void |
700 | static void |
659 | timers_reify (void) |
701 | timers_reify (EV_P) |
660 | { |
702 | { |
661 | while (timercnt && timers [0]->at <= now) |
703 | while (timercnt && timers [0]->at <= mn_now) |
662 | { |
704 | { |
663 | struct ev_timer *w = timers [0]; |
705 | struct ev_timer *w = timers [0]; |
664 | |
706 | |
665 | /* first reschedule or stop timer */ |
707 | /* first reschedule or stop timer */ |
666 | if (w->repeat) |
708 | if (w->repeat) |
667 | { |
709 | { |
668 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
710 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
669 | w->at = now + w->repeat; |
711 | w->at = mn_now + w->repeat; |
670 | downheap ((WT *)timers, timercnt, 0); |
712 | downheap ((WT *)timers, timercnt, 0); |
671 | } |
713 | } |
672 | else |
714 | else |
673 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
715 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
674 | |
716 | |
675 | event ((W)w, EV_TIMEOUT); |
717 | event ((W)w, EV_TIMEOUT); |
676 | } |
718 | } |
677 | } |
719 | } |
678 | |
720 | |
679 | static void |
721 | static void |
680 | periodics_reify (void) |
722 | periodics_reify (EV_P) |
681 | { |
723 | { |
682 | while (periodiccnt && periodics [0]->at <= ev_now) |
724 | while (periodiccnt && periodics [0]->at <= rt_now) |
683 | { |
725 | { |
684 | struct ev_periodic *w = periodics [0]; |
726 | struct ev_periodic *w = periodics [0]; |
685 | |
727 | |
686 | /* first reschedule or stop timer */ |
728 | /* first reschedule or stop timer */ |
687 | if (w->interval) |
729 | if (w->interval) |
688 | { |
730 | { |
689 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
731 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
690 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
732 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
691 | downheap ((WT *)periodics, periodiccnt, 0); |
733 | downheap ((WT *)periodics, periodiccnt, 0); |
692 | } |
734 | } |
693 | else |
735 | else |
694 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
736 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
695 | |
737 | |
696 | event ((W)w, EV_PERIODIC); |
738 | event (EV_A_ (W)w, EV_PERIODIC); |
697 | } |
739 | } |
698 | } |
740 | } |
699 | |
741 | |
700 | static void |
742 | static void |
701 | periodics_reschedule (ev_tstamp diff) |
743 | periodics_reschedule (EV_P_ ev_tstamp diff) |
702 | { |
744 | { |
703 | int i; |
745 | int i; |
704 | |
746 | |
705 | /* adjust periodics after time jump */ |
747 | /* adjust periodics after time jump */ |
706 | for (i = 0; i < periodiccnt; ++i) |
748 | for (i = 0; i < periodiccnt; ++i) |
707 | { |
749 | { |
708 | struct ev_periodic *w = periodics [i]; |
750 | struct ev_periodic *w = periodics [i]; |
709 | |
751 | |
710 | if (w->interval) |
752 | if (w->interval) |
711 | { |
753 | { |
712 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
754 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
713 | |
755 | |
714 | if (fabs (diff) >= 1e-4) |
756 | if (fabs (diff) >= 1e-4) |
715 | { |
757 | { |
716 | ev_periodic_stop (w); |
758 | ev_periodic_stop (EV_A_ w); |
717 | ev_periodic_start (w); |
759 | ev_periodic_start (EV_A_ w); |
718 | |
760 | |
719 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
761 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
720 | } |
762 | } |
721 | } |
763 | } |
722 | } |
764 | } |
723 | } |
765 | } |
724 | |
766 | |
725 | static int |
767 | inline int |
726 | time_update_monotonic (void) |
768 | time_update_monotonic (EV_P) |
727 | { |
769 | { |
728 | now = get_clock (); |
770 | mn_now = get_clock (); |
729 | |
771 | |
730 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
772 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
731 | { |
773 | { |
732 | ev_now = now + diff; |
774 | rt_now = mn_now + diff; |
733 | return 0; |
775 | return 0; |
734 | } |
776 | } |
735 | else |
777 | else |
736 | { |
778 | { |
737 | now_floor = now; |
779 | now_floor = mn_now; |
738 | ev_now = ev_time (); |
780 | rt_now = ev_time (); |
739 | return 1; |
781 | return 1; |
740 | } |
782 | } |
741 | } |
783 | } |
742 | |
784 | |
743 | static void |
785 | static void |
744 | time_update (void) |
786 | time_update (EV_P) |
745 | { |
787 | { |
746 | int i; |
788 | int i; |
747 | |
789 | |
748 | #if EV_USE_MONOTONIC |
790 | #if EV_USE_MONOTONIC |
749 | if (expect_true (have_monotonic)) |
791 | if (expect_true (have_monotonic)) |
750 | { |
792 | { |
751 | if (time_update_monotonic ()) |
793 | if (time_update_monotonic (EV_A)) |
752 | { |
794 | { |
753 | ev_tstamp odiff = diff; |
795 | ev_tstamp odiff = diff; |
754 | |
796 | |
755 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
797 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
756 | { |
798 | { |
757 | diff = ev_now - now; |
799 | diff = rt_now - mn_now; |
758 | |
800 | |
759 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
801 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
760 | return; /* all is well */ |
802 | return; /* all is well */ |
761 | |
803 | |
762 | ev_now = ev_time (); |
804 | rt_now = ev_time (); |
763 | now = get_clock (); |
805 | mn_now = get_clock (); |
764 | now_floor = now; |
806 | now_floor = mn_now; |
765 | } |
807 | } |
766 | |
808 | |
767 | periodics_reschedule (diff - odiff); |
809 | periodics_reschedule (EV_A_ diff - odiff); |
768 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
810 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
769 | } |
811 | } |
770 | } |
812 | } |
771 | else |
813 | else |
772 | #endif |
814 | #endif |
773 | { |
815 | { |
774 | ev_now = ev_time (); |
816 | rt_now = ev_time (); |
775 | |
817 | |
776 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
818 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
777 | { |
819 | { |
778 | periodics_reschedule (ev_now - now); |
820 | periodics_reschedule (EV_A_ rt_now - mn_now); |
779 | |
821 | |
780 | /* adjust timers. this is easy, as the offset is the same for all */ |
822 | /* adjust timers. this is easy, as the offset is the same for all */ |
781 | for (i = 0; i < timercnt; ++i) |
823 | for (i = 0; i < timercnt; ++i) |
782 | timers [i]->at += diff; |
824 | timers [i]->at += diff; |
783 | } |
825 | } |
784 | |
826 | |
785 | now = ev_now; |
827 | mn_now = rt_now; |
786 | } |
828 | } |
787 | } |
829 | } |
788 | |
830 | |
789 | int ev_loop_done; |
831 | void |
|
|
832 | ev_ref (EV_P) |
|
|
833 | { |
|
|
834 | ++activecnt; |
|
|
835 | } |
790 | |
836 | |
|
|
837 | void |
|
|
838 | ev_unref (EV_P) |
|
|
839 | { |
|
|
840 | --activecnt; |
|
|
841 | } |
|
|
842 | |
|
|
843 | static int loop_done; |
|
|
844 | |
|
|
845 | void |
791 | void ev_loop (int flags) |
846 | ev_loop (EV_P_ int flags) |
792 | { |
847 | { |
793 | double block; |
848 | double block; |
794 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
849 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
795 | |
850 | |
796 | do |
851 | do |
797 | { |
852 | { |
798 | /* queue check watchers (and execute them) */ |
853 | /* queue check watchers (and execute them) */ |
799 | if (expect_false (preparecnt)) |
854 | if (expect_false (preparecnt)) |
800 | { |
855 | { |
801 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
856 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
802 | call_pending (); |
857 | call_pending (EV_A); |
803 | } |
858 | } |
804 | |
859 | |
805 | /* update fd-related kernel structures */ |
860 | /* update fd-related kernel structures */ |
806 | fd_reify (); |
861 | fd_reify (EV_A); |
807 | |
862 | |
808 | /* calculate blocking time */ |
863 | /* calculate blocking time */ |
809 | |
864 | |
810 | /* we only need this for !monotonic clockor timers, but as we basically |
865 | /* we only need this for !monotonic clockor timers, but as we basically |
811 | always have timers, we just calculate it always */ |
866 | always have timers, we just calculate it always */ |
812 | #if EV_USE_MONOTONIC |
867 | #if EV_USE_MONOTONIC |
813 | if (expect_true (have_monotonic)) |
868 | if (expect_true (have_monotonic)) |
814 | time_update_monotonic (); |
869 | time_update_monotonic (EV_A); |
815 | else |
870 | else |
816 | #endif |
871 | #endif |
817 | { |
872 | { |
818 | ev_now = ev_time (); |
873 | rt_now = ev_time (); |
819 | now = ev_now; |
874 | mn_now = rt_now; |
820 | } |
875 | } |
821 | |
876 | |
822 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
877 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
823 | block = 0.; |
878 | block = 0.; |
824 | else |
879 | else |
825 | { |
880 | { |
826 | block = MAX_BLOCKTIME; |
881 | block = MAX_BLOCKTIME; |
827 | |
882 | |
828 | if (timercnt) |
883 | if (timercnt) |
829 | { |
884 | { |
830 | ev_tstamp to = timers [0]->at - now + method_fudge; |
885 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
831 | if (block > to) block = to; |
886 | if (block > to) block = to; |
832 | } |
887 | } |
833 | |
888 | |
834 | if (periodiccnt) |
889 | if (periodiccnt) |
835 | { |
890 | { |
836 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
891 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
837 | if (block > to) block = to; |
892 | if (block > to) block = to; |
838 | } |
893 | } |
839 | |
894 | |
840 | if (block < 0.) block = 0.; |
895 | if (block < 0.) block = 0.; |
841 | } |
896 | } |
842 | |
897 | |
843 | method_poll (block); |
898 | method_poll (EV_A_ block); |
844 | |
899 | |
845 | /* update ev_now, do magic */ |
900 | /* update rt_now, do magic */ |
846 | time_update (); |
901 | time_update (EV_A); |
847 | |
902 | |
848 | /* queue pending timers and reschedule them */ |
903 | /* queue pending timers and reschedule them */ |
849 | timers_reify (); /* relative timers called last */ |
904 | timers_reify (EV_A); /* relative timers called last */ |
850 | periodics_reify (); /* absolute timers called first */ |
905 | periodics_reify (EV_A); /* absolute timers called first */ |
851 | |
906 | |
852 | /* queue idle watchers unless io or timers are pending */ |
907 | /* queue idle watchers unless io or timers are pending */ |
853 | if (!pendingcnt) |
908 | if (!pendingcnt) |
854 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
909 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
855 | |
910 | |
856 | /* queue check watchers, to be executed first */ |
911 | /* queue check watchers, to be executed first */ |
857 | if (checkcnt) |
912 | if (checkcnt) |
858 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
913 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
859 | |
914 | |
860 | call_pending (); |
915 | call_pending (EV_A); |
|
|
916 | printf ("activecnt %d\n", activecnt);//D |
861 | } |
917 | } |
862 | while (!ev_loop_done); |
918 | while (activecnt && !loop_done); |
863 | |
919 | |
864 | if (ev_loop_done != 2) |
920 | if (loop_done != 2) |
865 | ev_loop_done = 0; |
921 | loop_done = 0; |
|
|
922 | } |
|
|
923 | |
|
|
924 | void |
|
|
925 | ev_unloop (EV_P_ int how) |
|
|
926 | { |
|
|
927 | loop_done = how; |
866 | } |
928 | } |
867 | |
929 | |
868 | /*****************************************************************************/ |
930 | /*****************************************************************************/ |
869 | |
931 | |
870 | static void |
932 | inline void |
871 | wlist_add (WL *head, WL elem) |
933 | wlist_add (WL *head, WL elem) |
872 | { |
934 | { |
873 | elem->next = *head; |
935 | elem->next = *head; |
874 | *head = elem; |
936 | *head = elem; |
875 | } |
937 | } |
876 | |
938 | |
877 | static void |
939 | inline void |
878 | wlist_del (WL *head, WL elem) |
940 | wlist_del (WL *head, WL elem) |
879 | { |
941 | { |
880 | while (*head) |
942 | while (*head) |
881 | { |
943 | { |
882 | if (*head == elem) |
944 | if (*head == elem) |
… | |
… | |
887 | |
949 | |
888 | head = &(*head)->next; |
950 | head = &(*head)->next; |
889 | } |
951 | } |
890 | } |
952 | } |
891 | |
953 | |
892 | static void |
954 | inline void |
893 | ev_clear_pending (W w) |
955 | ev_clear_pending (EV_P_ W w) |
894 | { |
956 | { |
895 | if (w->pending) |
957 | if (w->pending) |
896 | { |
958 | { |
897 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
959 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
898 | w->pending = 0; |
960 | w->pending = 0; |
899 | } |
961 | } |
900 | } |
962 | } |
901 | |
963 | |
902 | static void |
964 | inline void |
903 | ev_start (W w, int active) |
965 | ev_start (EV_P_ W w, int active) |
904 | { |
966 | { |
905 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
967 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
906 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
968 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
907 | |
969 | |
908 | w->active = active; |
970 | w->active = active; |
|
|
971 | ev_ref (EV_A); |
909 | } |
972 | } |
910 | |
973 | |
911 | static void |
974 | inline void |
912 | ev_stop (W w) |
975 | ev_stop (EV_P_ W w) |
913 | { |
976 | { |
|
|
977 | ev_unref (EV_A); |
914 | w->active = 0; |
978 | w->active = 0; |
915 | } |
979 | } |
916 | |
980 | |
917 | /*****************************************************************************/ |
981 | /*****************************************************************************/ |
918 | |
982 | |
919 | void |
983 | void |
920 | ev_io_start (struct ev_io *w) |
984 | ev_io_start (EV_P_ struct ev_io *w) |
921 | { |
985 | { |
922 | int fd = w->fd; |
986 | int fd = w->fd; |
923 | |
987 | |
924 | if (ev_is_active (w)) |
988 | if (ev_is_active (w)) |
925 | return; |
989 | return; |
926 | |
990 | |
927 | assert (("ev_io_start called with negative fd", fd >= 0)); |
991 | assert (("ev_io_start called with negative fd", fd >= 0)); |
928 | |
992 | |
929 | ev_start ((W)w, 1); |
993 | ev_start (EV_A_ (W)w, 1); |
930 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
994 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
931 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
995 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
932 | |
996 | |
933 | fd_change (fd); |
997 | fd_change (EV_A_ fd); |
934 | } |
998 | } |
935 | |
999 | |
936 | void |
1000 | void |
937 | ev_io_stop (struct ev_io *w) |
1001 | ev_io_stop (EV_P_ struct ev_io *w) |
938 | { |
1002 | { |
939 | ev_clear_pending ((W)w); |
1003 | ev_clear_pending (EV_A_ (W)w); |
940 | if (!ev_is_active (w)) |
1004 | if (!ev_is_active (w)) |
941 | return; |
1005 | return; |
942 | |
1006 | |
943 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1007 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
944 | ev_stop ((W)w); |
1008 | ev_stop (EV_A_ (W)w); |
945 | |
1009 | |
946 | fd_change (w->fd); |
1010 | fd_change (EV_A_ w->fd); |
947 | } |
1011 | } |
948 | |
1012 | |
949 | void |
1013 | void |
950 | ev_timer_start (struct ev_timer *w) |
1014 | ev_timer_start (EV_P_ struct ev_timer *w) |
951 | { |
1015 | { |
952 | if (ev_is_active (w)) |
1016 | if (ev_is_active (w)) |
953 | return; |
1017 | return; |
954 | |
1018 | |
955 | w->at += now; |
1019 | w->at += mn_now; |
956 | |
1020 | |
957 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1021 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
958 | |
1022 | |
959 | ev_start ((W)w, ++timercnt); |
1023 | ev_start (EV_A_ (W)w, ++timercnt); |
960 | array_needsize (timers, timermax, timercnt, ); |
1024 | array_needsize (timers, timermax, timercnt, ); |
961 | timers [timercnt - 1] = w; |
1025 | timers [timercnt - 1] = w; |
962 | upheap ((WT *)timers, timercnt - 1); |
1026 | upheap ((WT *)timers, timercnt - 1); |
963 | } |
1027 | } |
964 | |
1028 | |
965 | void |
1029 | void |
966 | ev_timer_stop (struct ev_timer *w) |
1030 | ev_timer_stop (EV_P_ struct ev_timer *w) |
967 | { |
1031 | { |
968 | ev_clear_pending ((W)w); |
1032 | ev_clear_pending (EV_A_ (W)w); |
969 | if (!ev_is_active (w)) |
1033 | if (!ev_is_active (w)) |
970 | return; |
1034 | return; |
971 | |
1035 | |
972 | if (w->active < timercnt--) |
1036 | if (w->active < timercnt--) |
973 | { |
1037 | { |
… | |
… | |
975 | downheap ((WT *)timers, timercnt, w->active - 1); |
1039 | downheap ((WT *)timers, timercnt, w->active - 1); |
976 | } |
1040 | } |
977 | |
1041 | |
978 | w->at = w->repeat; |
1042 | w->at = w->repeat; |
979 | |
1043 | |
980 | ev_stop ((W)w); |
1044 | ev_stop (EV_A_ (W)w); |
981 | } |
1045 | } |
982 | |
1046 | |
983 | void |
1047 | void |
984 | ev_timer_again (struct ev_timer *w) |
1048 | ev_timer_again (EV_P_ struct ev_timer *w) |
985 | { |
1049 | { |
986 | if (ev_is_active (w)) |
1050 | if (ev_is_active (w)) |
987 | { |
1051 | { |
988 | if (w->repeat) |
1052 | if (w->repeat) |
989 | { |
1053 | { |
990 | w->at = now + w->repeat; |
1054 | w->at = mn_now + w->repeat; |
991 | downheap ((WT *)timers, timercnt, w->active - 1); |
1055 | downheap ((WT *)timers, timercnt, w->active - 1); |
992 | } |
1056 | } |
993 | else |
1057 | else |
994 | ev_timer_stop (w); |
1058 | ev_timer_stop (EV_A_ w); |
995 | } |
1059 | } |
996 | else if (w->repeat) |
1060 | else if (w->repeat) |
997 | ev_timer_start (w); |
1061 | ev_timer_start (EV_A_ w); |
998 | } |
1062 | } |
999 | |
1063 | |
1000 | void |
1064 | void |
1001 | ev_periodic_start (struct ev_periodic *w) |
1065 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1002 | { |
1066 | { |
1003 | if (ev_is_active (w)) |
1067 | if (ev_is_active (w)) |
1004 | return; |
1068 | return; |
1005 | |
1069 | |
1006 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1070 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1007 | |
1071 | |
1008 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1072 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1009 | if (w->interval) |
1073 | if (w->interval) |
1010 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1074 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1011 | |
1075 | |
1012 | ev_start ((W)w, ++periodiccnt); |
1076 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1013 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1077 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1014 | periodics [periodiccnt - 1] = w; |
1078 | periodics [periodiccnt - 1] = w; |
1015 | upheap ((WT *)periodics, periodiccnt - 1); |
1079 | upheap ((WT *)periodics, periodiccnt - 1); |
1016 | } |
1080 | } |
1017 | |
1081 | |
1018 | void |
1082 | void |
1019 | ev_periodic_stop (struct ev_periodic *w) |
1083 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1020 | { |
1084 | { |
1021 | ev_clear_pending ((W)w); |
1085 | ev_clear_pending (EV_A_ (W)w); |
1022 | if (!ev_is_active (w)) |
1086 | if (!ev_is_active (w)) |
1023 | return; |
1087 | return; |
1024 | |
1088 | |
1025 | if (w->active < periodiccnt--) |
1089 | if (w->active < periodiccnt--) |
1026 | { |
1090 | { |
1027 | periodics [w->active - 1] = periodics [periodiccnt]; |
1091 | periodics [w->active - 1] = periodics [periodiccnt]; |
1028 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1092 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1029 | } |
1093 | } |
1030 | |
1094 | |
1031 | ev_stop ((W)w); |
1095 | ev_stop (EV_A_ (W)w); |
1032 | } |
1096 | } |
1033 | |
1097 | |
1034 | #ifndef SA_RESTART |
1098 | #ifndef SA_RESTART |
1035 | # define SA_RESTART 0 |
1099 | # define SA_RESTART 0 |
1036 | #endif |
1100 | #endif |
1037 | |
1101 | |
1038 | void |
1102 | void |
1039 | ev_signal_start (struct ev_signal *w) |
1103 | ev_signal_start (EV_P_ struct ev_signal *w) |
1040 | { |
1104 | { |
1041 | if (ev_is_active (w)) |
1105 | if (ev_is_active (w)) |
1042 | return; |
1106 | return; |
1043 | |
1107 | |
1044 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1108 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1045 | |
1109 | |
1046 | ev_start ((W)w, 1); |
1110 | ev_start (EV_A_ (W)w, 1); |
1047 | array_needsize (signals, signalmax, w->signum, signals_init); |
1111 | array_needsize (signals, signalmax, w->signum, signals_init); |
1048 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1112 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1049 | |
1113 | |
1050 | if (!w->next) |
1114 | if (!w->next) |
1051 | { |
1115 | { |
… | |
… | |
1056 | sigaction (w->signum, &sa, 0); |
1120 | sigaction (w->signum, &sa, 0); |
1057 | } |
1121 | } |
1058 | } |
1122 | } |
1059 | |
1123 | |
1060 | void |
1124 | void |
1061 | ev_signal_stop (struct ev_signal *w) |
1125 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1062 | { |
1126 | { |
1063 | ev_clear_pending ((W)w); |
1127 | ev_clear_pending (EV_A_ (W)w); |
1064 | if (!ev_is_active (w)) |
1128 | if (!ev_is_active (w)) |
1065 | return; |
1129 | return; |
1066 | |
1130 | |
1067 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1131 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1068 | ev_stop ((W)w); |
1132 | ev_stop (EV_A_ (W)w); |
1069 | |
1133 | |
1070 | if (!signals [w->signum - 1].head) |
1134 | if (!signals [w->signum - 1].head) |
1071 | signal (w->signum, SIG_DFL); |
1135 | signal (w->signum, SIG_DFL); |
1072 | } |
1136 | } |
1073 | |
1137 | |
1074 | void |
1138 | void |
1075 | ev_idle_start (struct ev_idle *w) |
1139 | ev_idle_start (EV_P_ struct ev_idle *w) |
1076 | { |
1140 | { |
1077 | if (ev_is_active (w)) |
1141 | if (ev_is_active (w)) |
1078 | return; |
1142 | return; |
1079 | |
1143 | |
1080 | ev_start ((W)w, ++idlecnt); |
1144 | ev_start (EV_A_ (W)w, ++idlecnt); |
1081 | array_needsize (idles, idlemax, idlecnt, ); |
1145 | array_needsize (idles, idlemax, idlecnt, ); |
1082 | idles [idlecnt - 1] = w; |
1146 | idles [idlecnt - 1] = w; |
1083 | } |
1147 | } |
1084 | |
1148 | |
1085 | void |
1149 | void |
1086 | ev_idle_stop (struct ev_idle *w) |
1150 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1087 | { |
1151 | { |
1088 | ev_clear_pending ((W)w); |
1152 | ev_clear_pending (EV_A_ (W)w); |
1089 | if (ev_is_active (w)) |
1153 | if (ev_is_active (w)) |
1090 | return; |
1154 | return; |
1091 | |
1155 | |
1092 | idles [w->active - 1] = idles [--idlecnt]; |
1156 | idles [w->active - 1] = idles [--idlecnt]; |
1093 | ev_stop ((W)w); |
1157 | ev_stop (EV_A_ (W)w); |
1094 | } |
1158 | } |
1095 | |
1159 | |
1096 | void |
1160 | void |
1097 | ev_prepare_start (struct ev_prepare *w) |
1161 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1098 | { |
1162 | { |
1099 | if (ev_is_active (w)) |
1163 | if (ev_is_active (w)) |
1100 | return; |
1164 | return; |
1101 | |
1165 | |
1102 | ev_start ((W)w, ++preparecnt); |
1166 | ev_start (EV_A_ (W)w, ++preparecnt); |
1103 | array_needsize (prepares, preparemax, preparecnt, ); |
1167 | array_needsize (prepares, preparemax, preparecnt, ); |
1104 | prepares [preparecnt - 1] = w; |
1168 | prepares [preparecnt - 1] = w; |
1105 | } |
1169 | } |
1106 | |
1170 | |
1107 | void |
1171 | void |
1108 | ev_prepare_stop (struct ev_prepare *w) |
1172 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1109 | { |
1173 | { |
1110 | ev_clear_pending ((W)w); |
1174 | ev_clear_pending (EV_A_ (W)w); |
1111 | if (ev_is_active (w)) |
1175 | if (ev_is_active (w)) |
1112 | return; |
1176 | return; |
1113 | |
1177 | |
1114 | prepares [w->active - 1] = prepares [--preparecnt]; |
1178 | prepares [w->active - 1] = prepares [--preparecnt]; |
1115 | ev_stop ((W)w); |
1179 | ev_stop (EV_A_ (W)w); |
1116 | } |
1180 | } |
1117 | |
1181 | |
1118 | void |
1182 | void |
1119 | ev_check_start (struct ev_check *w) |
1183 | ev_check_start (EV_P_ struct ev_check *w) |
1120 | { |
1184 | { |
1121 | if (ev_is_active (w)) |
1185 | if (ev_is_active (w)) |
1122 | return; |
1186 | return; |
1123 | |
1187 | |
1124 | ev_start ((W)w, ++checkcnt); |
1188 | ev_start (EV_A_ (W)w, ++checkcnt); |
1125 | array_needsize (checks, checkmax, checkcnt, ); |
1189 | array_needsize (checks, checkmax, checkcnt, ); |
1126 | checks [checkcnt - 1] = w; |
1190 | checks [checkcnt - 1] = w; |
1127 | } |
1191 | } |
1128 | |
1192 | |
1129 | void |
1193 | void |
1130 | ev_check_stop (struct ev_check *w) |
1194 | ev_check_stop (EV_P_ struct ev_check *w) |
1131 | { |
1195 | { |
1132 | ev_clear_pending ((W)w); |
1196 | ev_clear_pending (EV_A_ (W)w); |
1133 | if (ev_is_active (w)) |
1197 | if (ev_is_active (w)) |
1134 | return; |
1198 | return; |
1135 | |
1199 | |
1136 | checks [w->active - 1] = checks [--checkcnt]; |
1200 | checks [w->active - 1] = checks [--checkcnt]; |
1137 | ev_stop ((W)w); |
1201 | ev_stop (EV_A_ (W)w); |
1138 | } |
1202 | } |
1139 | |
1203 | |
1140 | void |
1204 | void |
1141 | ev_child_start (struct ev_child *w) |
1205 | ev_child_start (EV_P_ struct ev_child *w) |
1142 | { |
1206 | { |
1143 | if (ev_is_active (w)) |
1207 | if (ev_is_active (w)) |
1144 | return; |
1208 | return; |
1145 | |
1209 | |
1146 | ev_start ((W)w, 1); |
1210 | ev_start (EV_A_ (W)w, 1); |
1147 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1211 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1148 | } |
1212 | } |
1149 | |
1213 | |
1150 | void |
1214 | void |
1151 | ev_child_stop (struct ev_child *w) |
1215 | ev_child_stop (EV_P_ struct ev_child *w) |
1152 | { |
1216 | { |
1153 | ev_clear_pending ((W)w); |
1217 | ev_clear_pending (EV_A_ (W)w); |
1154 | if (ev_is_active (w)) |
1218 | if (ev_is_active (w)) |
1155 | return; |
1219 | return; |
1156 | |
1220 | |
1157 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1221 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1158 | ev_stop ((W)w); |
1222 | ev_stop (EV_A_ (W)w); |
1159 | } |
1223 | } |
1160 | |
1224 | |
1161 | /*****************************************************************************/ |
1225 | /*****************************************************************************/ |
1162 | |
1226 | |
1163 | struct ev_once |
1227 | struct ev_once |
… | |
… | |
1167 | void (*cb)(int revents, void *arg); |
1231 | void (*cb)(int revents, void *arg); |
1168 | void *arg; |
1232 | void *arg; |
1169 | }; |
1233 | }; |
1170 | |
1234 | |
1171 | static void |
1235 | static void |
1172 | once_cb (struct ev_once *once, int revents) |
1236 | once_cb (EV_P_ struct ev_once *once, int revents) |
1173 | { |
1237 | { |
1174 | void (*cb)(int revents, void *arg) = once->cb; |
1238 | void (*cb)(int revents, void *arg) = once->cb; |
1175 | void *arg = once->arg; |
1239 | void *arg = once->arg; |
1176 | |
1240 | |
1177 | ev_io_stop (&once->io); |
1241 | ev_io_stop (EV_A_ &once->io); |
1178 | ev_timer_stop (&once->to); |
1242 | ev_timer_stop (EV_A_ &once->to); |
1179 | free (once); |
1243 | free (once); |
1180 | |
1244 | |
1181 | cb (revents, arg); |
1245 | cb (revents, arg); |
1182 | } |
1246 | } |
1183 | |
1247 | |
1184 | static void |
1248 | static void |
1185 | once_cb_io (struct ev_io *w, int revents) |
1249 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1186 | { |
1250 | { |
1187 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1251 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1188 | } |
1252 | } |
1189 | |
1253 | |
1190 | static void |
1254 | static void |
1191 | once_cb_to (struct ev_timer *w, int revents) |
1255 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1192 | { |
1256 | { |
1193 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1257 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1194 | } |
1258 | } |
1195 | |
1259 | |
1196 | void |
1260 | void |
1197 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1261 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1198 | { |
1262 | { |
1199 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1263 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1200 | |
1264 | |
1201 | if (!once) |
1265 | if (!once) |
1202 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1266 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
… | |
… | |
1207 | |
1271 | |
1208 | ev_watcher_init (&once->io, once_cb_io); |
1272 | ev_watcher_init (&once->io, once_cb_io); |
1209 | if (fd >= 0) |
1273 | if (fd >= 0) |
1210 | { |
1274 | { |
1211 | ev_io_set (&once->io, fd, events); |
1275 | ev_io_set (&once->io, fd, events); |
1212 | ev_io_start (&once->io); |
1276 | ev_io_start (EV_A_ &once->io); |
1213 | } |
1277 | } |
1214 | |
1278 | |
1215 | ev_watcher_init (&once->to, once_cb_to); |
1279 | ev_watcher_init (&once->to, once_cb_to); |
1216 | if (timeout >= 0.) |
1280 | if (timeout >= 0.) |
1217 | { |
1281 | { |
1218 | ev_timer_set (&once->to, timeout, 0.); |
1282 | ev_timer_set (&once->to, timeout, 0.); |
1219 | ev_timer_start (&once->to); |
1283 | ev_timer_start (EV_A_ &once->to); |
1220 | } |
1284 | } |
1221 | } |
1285 | } |
1222 | } |
1286 | } |
1223 | |
1287 | |
1224 | /*****************************************************************************/ |
1288 | /*****************************************************************************/ |