1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
45 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
46 | #include <sys/time.h> |
48 | #include <sys/time.h> |
47 | #include <time.h> |
49 | #include <time.h> |
48 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
49 | #ifndef EV_USE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
54 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
55 | #endif |
54 | |
56 | |
55 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
57 | #endif |
59 | #endif |
58 | |
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
59 | #ifndef EV_USE_EPOLL |
65 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
66 | # define EV_USE_EPOLL 0 |
61 | #endif |
67 | #endif |
62 | |
68 | |
63 | #ifndef EV_USE_REALTIME |
69 | #ifndef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
70 | # define EV_USE_REALTIME 1 |
65 | #endif |
71 | #endif |
|
|
72 | |
|
|
73 | /**/ |
|
|
74 | |
|
|
75 | #ifndef CLOCK_MONOTONIC |
|
|
76 | # undef EV_USE_MONOTONIC |
|
|
77 | # define EV_USE_MONOTONIC 0 |
|
|
78 | #endif |
|
|
79 | |
|
|
80 | #ifndef CLOCK_REALTIME |
|
|
81 | # undef EV_USE_REALTIME |
|
|
82 | # define EV_USE_REALTIME 0 |
|
|
83 | #endif |
|
|
84 | |
|
|
85 | /**/ |
66 | |
86 | |
67 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
87 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
68 | #define MAX_BLOCKTIME 59.731 |
88 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
69 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
89 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
90 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
70 | |
91 | |
71 | #include "ev.h" |
92 | #include "ev.h" |
|
|
93 | |
|
|
94 | #if __GNUC__ >= 3 |
|
|
95 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
96 | # define inline inline |
|
|
97 | #else |
|
|
98 | # define expect(expr,value) (expr) |
|
|
99 | # define inline static |
|
|
100 | #endif |
|
|
101 | |
|
|
102 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
103 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
104 | |
|
|
105 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
106 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
72 | |
107 | |
73 | typedef struct ev_watcher *W; |
108 | typedef struct ev_watcher *W; |
74 | typedef struct ev_watcher_list *WL; |
109 | typedef struct ev_watcher_list *WL; |
75 | typedef struct ev_watcher_time *WT; |
110 | typedef struct ev_watcher_time *WT; |
76 | |
111 | |
77 | static ev_tstamp now, diff; /* monotonic clock */ |
112 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
78 | ev_tstamp ev_now; |
113 | ev_tstamp ev_now; |
79 | int ev_method; |
114 | int ev_method; |
80 | |
115 | |
81 | static int have_monotonic; /* runtime */ |
116 | static int have_monotonic; /* runtime */ |
82 | |
117 | |
… | |
… | |
102 | |
137 | |
103 | static ev_tstamp |
138 | static ev_tstamp |
104 | get_clock (void) |
139 | get_clock (void) |
105 | { |
140 | { |
106 | #if EV_USE_MONOTONIC |
141 | #if EV_USE_MONOTONIC |
107 | if (have_monotonic) |
142 | if (expect_true (have_monotonic)) |
108 | { |
143 | { |
109 | struct timespec ts; |
144 | struct timespec ts; |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
145 | clock_gettime (CLOCK_MONOTONIC, &ts); |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
146 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
112 | } |
147 | } |
… | |
… | |
116 | } |
151 | } |
117 | |
152 | |
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
153 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
119 | |
154 | |
120 | #define array_needsize(base,cur,cnt,init) \ |
155 | #define array_needsize(base,cur,cnt,init) \ |
121 | if ((cnt) > cur) \ |
156 | if (expect_false ((cnt) > cur)) \ |
122 | { \ |
157 | { \ |
123 | int newcnt = cur; \ |
158 | int newcnt = cur; \ |
124 | do \ |
159 | do \ |
125 | { \ |
160 | { \ |
126 | newcnt = array_roundsize (base, newcnt << 1); \ |
161 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
135 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
136 | |
171 | |
137 | typedef struct |
172 | typedef struct |
138 | { |
173 | { |
139 | struct ev_io *head; |
174 | struct ev_io *head; |
140 | int events; |
175 | unsigned char events; |
|
|
176 | unsigned char reify; |
141 | } ANFD; |
177 | } ANFD; |
142 | |
178 | |
143 | static ANFD *anfds; |
179 | static ANFD *anfds; |
144 | static int anfdmax; |
180 | static int anfdmax; |
145 | |
181 | |
… | |
… | |
148 | { |
184 | { |
149 | while (count--) |
185 | while (count--) |
150 | { |
186 | { |
151 | base->head = 0; |
187 | base->head = 0; |
152 | base->events = EV_NONE; |
188 | base->events = EV_NONE; |
|
|
189 | base->reify = 0; |
|
|
190 | |
153 | ++base; |
191 | ++base; |
154 | } |
192 | } |
155 | } |
193 | } |
156 | |
194 | |
157 | typedef struct |
195 | typedef struct |
158 | { |
196 | { |
159 | W w; |
197 | W w; |
160 | int events; |
198 | int events; |
161 | } ANPENDING; |
199 | } ANPENDING; |
162 | |
200 | |
163 | static ANPENDING *pendings; |
201 | static ANPENDING *pendings [NUMPRI]; |
164 | static int pendingmax, pendingcnt; |
202 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
165 | |
203 | |
166 | static void |
204 | static void |
167 | event (W w, int events) |
205 | event (W w, int events) |
168 | { |
206 | { |
|
|
207 | if (w->pending) |
|
|
208 | { |
|
|
209 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
210 | return; |
|
|
211 | } |
|
|
212 | |
169 | w->pending = ++pendingcnt; |
213 | w->pending = ++pendingcnt [ABSPRI (w)]; |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
214 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
171 | pendings [pendingcnt - 1].w = w; |
215 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
172 | pendings [pendingcnt - 1].events = events; |
216 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
173 | } |
217 | } |
174 | |
218 | |
175 | static void |
219 | static void |
176 | queue_events (W *events, int eventcnt, int type) |
220 | queue_events (W *events, int eventcnt, int type) |
177 | { |
221 | { |
… | |
… | |
215 | int events = 0; |
259 | int events = 0; |
216 | |
260 | |
217 | for (w = anfd->head; w; w = w->next) |
261 | for (w = anfd->head; w; w = w->next) |
218 | events |= w->events; |
262 | events |= w->events; |
219 | |
263 | |
220 | anfd->events &= ~EV_REIFY; |
264 | anfd->reify = 0; |
221 | |
265 | |
222 | if (anfd->events != events) |
266 | if (anfd->events != events) |
223 | { |
267 | { |
224 | method_modify (fd, anfd->events, events); |
268 | method_modify (fd, anfd->events, events); |
225 | anfd->events = events; |
269 | anfd->events = events; |
… | |
… | |
230 | } |
274 | } |
231 | |
275 | |
232 | static void |
276 | static void |
233 | fd_change (int fd) |
277 | fd_change (int fd) |
234 | { |
278 | { |
235 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
279 | if (anfds [fd].reify || fdchangecnt < 0) |
236 | return; |
280 | return; |
237 | |
281 | |
238 | anfds [fd].events |= EV_REIFY; |
282 | anfds [fd].reify = 1; |
239 | |
283 | |
240 | ++fdchangecnt; |
284 | ++fdchangecnt; |
241 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
285 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
242 | fdchanges [fdchangecnt - 1] = fd; |
286 | fdchanges [fdchangecnt - 1] = fd; |
243 | } |
287 | } |
244 | |
288 | |
|
|
289 | static void |
|
|
290 | fd_kill (int fd) |
|
|
291 | { |
|
|
292 | struct ev_io *w; |
|
|
293 | |
|
|
294 | printf ("killing fd %d\n", fd);//D |
|
|
295 | while ((w = anfds [fd].head)) |
|
|
296 | { |
|
|
297 | ev_io_stop (w); |
|
|
298 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
299 | } |
|
|
300 | } |
|
|
301 | |
245 | /* called on EBADF to verify fds */ |
302 | /* called on EBADF to verify fds */ |
246 | static void |
303 | static void |
247 | fd_recheck (void) |
304 | fd_ebadf (void) |
248 | { |
305 | { |
249 | int fd; |
306 | int fd; |
250 | |
307 | |
251 | for (fd = 0; fd < anfdmax; ++fd) |
308 | for (fd = 0; fd < anfdmax; ++fd) |
252 | if (anfds [fd].events) |
309 | if (anfds [fd].events) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
310 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
254 | while (anfds [fd].head) |
311 | fd_kill (fd); |
|
|
312 | } |
|
|
313 | |
|
|
314 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
315 | static void |
|
|
316 | fd_enomem (void) |
|
|
317 | { |
|
|
318 | int fd = anfdmax; |
|
|
319 | |
|
|
320 | while (fd--) |
|
|
321 | if (anfds [fd].events) |
255 | { |
322 | { |
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
323 | close (fd); |
257 | ev_io_stop (anfds [fd].head); |
324 | fd_kill (fd); |
|
|
325 | return; |
258 | } |
326 | } |
259 | } |
327 | } |
260 | |
328 | |
261 | /*****************************************************************************/ |
329 | /*****************************************************************************/ |
262 | |
330 | |
263 | static struct ev_timer **timers; |
331 | static struct ev_timer **timers; |
… | |
… | |
310 | /*****************************************************************************/ |
378 | /*****************************************************************************/ |
311 | |
379 | |
312 | typedef struct |
380 | typedef struct |
313 | { |
381 | { |
314 | struct ev_signal *head; |
382 | struct ev_signal *head; |
315 | sig_atomic_t gotsig; |
383 | sig_atomic_t volatile gotsig; |
316 | } ANSIG; |
384 | } ANSIG; |
317 | |
385 | |
318 | static ANSIG *signals; |
386 | static ANSIG *signals; |
319 | static int signalmax; |
387 | static int signalmax; |
320 | |
388 | |
321 | static int sigpipe [2]; |
389 | static int sigpipe [2]; |
322 | static sig_atomic_t gotsig; |
390 | static sig_atomic_t volatile gotsig; |
323 | static struct ev_io sigev; |
391 | static struct ev_io sigev; |
324 | |
392 | |
325 | static void |
393 | static void |
326 | signals_init (ANSIG *base, int count) |
394 | signals_init (ANSIG *base, int count) |
327 | { |
395 | { |
328 | while (count--) |
396 | while (count--) |
329 | { |
397 | { |
330 | base->head = 0; |
398 | base->head = 0; |
331 | base->gotsig = 0; |
399 | base->gotsig = 0; |
|
|
400 | |
332 | ++base; |
401 | ++base; |
333 | } |
402 | } |
334 | } |
403 | } |
335 | |
404 | |
336 | static void |
405 | static void |
… | |
… | |
339 | signals [signum - 1].gotsig = 1; |
408 | signals [signum - 1].gotsig = 1; |
340 | |
409 | |
341 | if (!gotsig) |
410 | if (!gotsig) |
342 | { |
411 | { |
343 | gotsig = 1; |
412 | gotsig = 1; |
344 | write (sigpipe [1], &gotsig, 1); |
413 | write (sigpipe [1], &signum, 1); |
345 | } |
414 | } |
346 | } |
415 | } |
347 | |
416 | |
348 | static void |
417 | static void |
349 | sigcb (struct ev_io *iow, int revents) |
418 | sigcb (struct ev_io *iow, int revents) |
350 | { |
419 | { |
351 | struct ev_signal *w; |
420 | struct ev_signal *w; |
352 | int sig; |
421 | int signum; |
353 | |
422 | |
|
|
423 | read (sigpipe [0], &revents, 1); |
354 | gotsig = 0; |
424 | gotsig = 0; |
355 | read (sigpipe [0], &revents, 1); |
|
|
356 | |
425 | |
357 | for (sig = signalmax; sig--; ) |
426 | for (signum = signalmax; signum--; ) |
358 | if (signals [sig].gotsig) |
427 | if (signals [signum].gotsig) |
359 | { |
428 | { |
360 | signals [sig].gotsig = 0; |
429 | signals [signum].gotsig = 0; |
361 | |
430 | |
362 | for (w = signals [sig].head; w; w = w->next) |
431 | for (w = signals [signum].head; w; w = w->next) |
363 | event ((W)w, EV_SIGNAL); |
432 | event ((W)w, EV_SIGNAL); |
364 | } |
433 | } |
365 | } |
434 | } |
366 | |
435 | |
367 | static void |
436 | static void |
… | |
… | |
404 | struct ev_child *w; |
473 | struct ev_child *w; |
405 | int pid, status; |
474 | int pid, status; |
406 | |
475 | |
407 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
476 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
408 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
477 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
409 | if (w->pid == pid || w->pid == -1) |
478 | if (w->pid == pid || !w->pid) |
410 | { |
479 | { |
411 | w->status = status; |
480 | w->status = status; |
412 | event ((W)w, EV_CHILD); |
481 | event ((W)w, EV_CHILD); |
413 | } |
482 | } |
414 | } |
483 | } |
… | |
… | |
416 | /*****************************************************************************/ |
485 | /*****************************************************************************/ |
417 | |
486 | |
418 | #if EV_USE_EPOLL |
487 | #if EV_USE_EPOLL |
419 | # include "ev_epoll.c" |
488 | # include "ev_epoll.c" |
420 | #endif |
489 | #endif |
|
|
490 | #if EV_USE_POLL |
|
|
491 | # include "ev_poll.c" |
|
|
492 | #endif |
421 | #if EV_USE_SELECT |
493 | #if EV_USE_SELECT |
422 | # include "ev_select.c" |
494 | # include "ev_select.c" |
423 | #endif |
495 | #endif |
424 | |
496 | |
425 | int |
497 | int |
… | |
… | |
432 | ev_version_minor (void) |
504 | ev_version_minor (void) |
433 | { |
505 | { |
434 | return EV_VERSION_MINOR; |
506 | return EV_VERSION_MINOR; |
435 | } |
507 | } |
436 | |
508 | |
|
|
509 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
510 | static int |
|
|
511 | enable_secure () |
|
|
512 | { |
|
|
513 | return getuid () != geteuid () |
|
|
514 | || getgid () != getegid (); |
|
|
515 | } |
|
|
516 | |
437 | int ev_init (int flags) |
517 | int ev_init (int methods) |
438 | { |
518 | { |
439 | if (!ev_method) |
519 | if (!ev_method) |
440 | { |
520 | { |
441 | #if EV_USE_MONOTONIC |
521 | #if EV_USE_MONOTONIC |
442 | { |
522 | { |
… | |
… | |
444 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
524 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
445 | have_monotonic = 1; |
525 | have_monotonic = 1; |
446 | } |
526 | } |
447 | #endif |
527 | #endif |
448 | |
528 | |
449 | ev_now = ev_time (); |
529 | ev_now = ev_time (); |
450 | now = get_clock (); |
530 | now = get_clock (); |
|
|
531 | now_floor = now; |
451 | diff = ev_now - now; |
532 | diff = ev_now - now; |
452 | |
533 | |
453 | if (pipe (sigpipe)) |
534 | if (pipe (sigpipe)) |
454 | return 0; |
535 | return 0; |
455 | |
536 | |
|
|
537 | if (methods == EVMETHOD_AUTO) |
|
|
538 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
539 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
540 | else |
456 | ev_method = EVMETHOD_NONE; |
541 | methods = EVMETHOD_ANY; |
|
|
542 | |
|
|
543 | ev_method = 0; |
457 | #if EV_USE_EPOLL |
544 | #if EV_USE_EPOLL |
458 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
545 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
546 | #endif |
|
|
547 | #if EV_USE_POLL |
|
|
548 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
459 | #endif |
549 | #endif |
460 | #if EV_USE_SELECT |
550 | #if EV_USE_SELECT |
461 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
551 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
462 | #endif |
552 | #endif |
463 | |
553 | |
464 | if (ev_method) |
554 | if (ev_method) |
465 | { |
555 | { |
466 | ev_watcher_init (&sigev, sigcb); |
556 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
475 | } |
565 | } |
476 | |
566 | |
477 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
478 | |
568 | |
479 | void |
569 | void |
480 | ev_prefork (void) |
570 | ev_fork_prepare (void) |
481 | { |
571 | { |
482 | /* nop */ |
572 | /* nop */ |
483 | } |
573 | } |
484 | |
574 | |
485 | void |
575 | void |
486 | ev_postfork_parent (void) |
576 | ev_fork_parent (void) |
487 | { |
577 | { |
488 | /* nop */ |
578 | /* nop */ |
489 | } |
579 | } |
490 | |
580 | |
491 | void |
581 | void |
492 | ev_postfork_child (void) |
582 | ev_fork_child (void) |
493 | { |
583 | { |
494 | #if EV_USE_EPOLL |
584 | #if EV_USE_EPOLL |
495 | if (ev_method == EVMETHOD_EPOLL) |
585 | if (ev_method == EVMETHOD_EPOLL) |
496 | epoll_postfork_child (); |
586 | epoll_postfork_child (); |
497 | #endif |
587 | #endif |
… | |
… | |
506 | /*****************************************************************************/ |
596 | /*****************************************************************************/ |
507 | |
597 | |
508 | static void |
598 | static void |
509 | call_pending (void) |
599 | call_pending (void) |
510 | { |
600 | { |
|
|
601 | int pri; |
|
|
602 | |
|
|
603 | for (pri = NUMPRI; pri--; ) |
511 | while (pendingcnt) |
604 | while (pendingcnt [pri]) |
512 | { |
605 | { |
513 | ANPENDING *p = pendings + --pendingcnt; |
606 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
514 | |
607 | |
515 | if (p->w) |
608 | if (p->w) |
516 | { |
609 | { |
517 | p->w->pending = 0; |
610 | p->w->pending = 0; |
518 | p->w->cb (p->w, p->events); |
611 | p->w->cb (p->w, p->events); |
519 | } |
612 | } |
520 | } |
613 | } |
521 | } |
614 | } |
522 | |
615 | |
523 | static void |
616 | static void |
524 | timers_reify (void) |
617 | timers_reify (void) |
525 | { |
618 | { |
… | |
… | |
528 | struct ev_timer *w = timers [0]; |
621 | struct ev_timer *w = timers [0]; |
529 | |
622 | |
530 | /* first reschedule or stop timer */ |
623 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
624 | if (w->repeat) |
532 | { |
625 | { |
|
|
626 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
627 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
628 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
629 | } |
537 | else |
630 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
631 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
539 | |
632 | |
… | |
… | |
550 | |
643 | |
551 | /* first reschedule or stop timer */ |
644 | /* first reschedule or stop timer */ |
552 | if (w->interval) |
645 | if (w->interval) |
553 | { |
646 | { |
554 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
647 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
555 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
648 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
649 | downheap ((WT *)periodics, periodiccnt, 0); |
557 | } |
650 | } |
558 | else |
651 | else |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
652 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
560 | |
653 | |
561 | event ((W)w, EV_TIMEOUT); |
654 | event ((W)w, EV_PERIODIC); |
562 | } |
655 | } |
563 | } |
656 | } |
564 | |
657 | |
565 | static void |
658 | static void |
566 | periodics_reschedule (ev_tstamp diff) |
659 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
585 | } |
678 | } |
586 | } |
679 | } |
587 | } |
680 | } |
588 | } |
681 | } |
589 | |
682 | |
|
|
683 | static int |
|
|
684 | time_update_monotonic (void) |
|
|
685 | { |
|
|
686 | now = get_clock (); |
|
|
687 | |
|
|
688 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
689 | { |
|
|
690 | ev_now = now + diff; |
|
|
691 | return 0; |
|
|
692 | } |
|
|
693 | else |
|
|
694 | { |
|
|
695 | now_floor = now; |
|
|
696 | ev_now = ev_time (); |
|
|
697 | return 1; |
|
|
698 | } |
|
|
699 | } |
|
|
700 | |
590 | static void |
701 | static void |
591 | time_update (void) |
702 | time_update (void) |
592 | { |
703 | { |
593 | int i; |
704 | int i; |
594 | |
705 | |
595 | ev_now = ev_time (); |
706 | #if EV_USE_MONOTONIC |
596 | |
|
|
597 | if (have_monotonic) |
707 | if (expect_true (have_monotonic)) |
598 | { |
708 | { |
|
|
709 | if (time_update_monotonic ()) |
|
|
710 | { |
599 | ev_tstamp odiff = diff; |
711 | ev_tstamp odiff = diff; |
600 | |
712 | |
601 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
713 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
602 | { |
714 | { |
603 | now = get_clock (); |
|
|
604 | diff = ev_now - now; |
715 | diff = ev_now - now; |
605 | |
716 | |
606 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
717 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
607 | return; /* all is well */ |
718 | return; /* all is well */ |
608 | |
719 | |
609 | ev_now = ev_time (); |
720 | ev_now = ev_time (); |
|
|
721 | now = get_clock (); |
|
|
722 | now_floor = now; |
610 | } |
723 | } |
611 | |
724 | |
612 | periodics_reschedule (diff - odiff); |
725 | periodics_reschedule (diff - odiff); |
613 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
726 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
727 | } |
614 | } |
728 | } |
615 | else |
729 | else |
|
|
730 | #endif |
616 | { |
731 | { |
|
|
732 | ev_now = ev_time (); |
|
|
733 | |
617 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
734 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
618 | { |
735 | { |
619 | periodics_reschedule (ev_now - now); |
736 | periodics_reschedule (ev_now - now); |
620 | |
737 | |
621 | /* adjust timers. this is easy, as the offset is the same for all */ |
738 | /* adjust timers. this is easy, as the offset is the same for all */ |
622 | for (i = 0; i < timercnt; ++i) |
739 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
635 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
752 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
636 | |
753 | |
637 | do |
754 | do |
638 | { |
755 | { |
639 | /* queue check watchers (and execute them) */ |
756 | /* queue check watchers (and execute them) */ |
640 | if (preparecnt) |
757 | if (expect_false (preparecnt)) |
641 | { |
758 | { |
642 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
759 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
643 | call_pending (); |
760 | call_pending (); |
644 | } |
761 | } |
645 | |
762 | |
… | |
… | |
648 | |
765 | |
649 | /* calculate blocking time */ |
766 | /* calculate blocking time */ |
650 | |
767 | |
651 | /* we only need this for !monotonic clockor timers, but as we basically |
768 | /* we only need this for !monotonic clockor timers, but as we basically |
652 | always have timers, we just calculate it always */ |
769 | always have timers, we just calculate it always */ |
|
|
770 | #if EV_USE_MONOTONIC |
|
|
771 | if (expect_true (have_monotonic)) |
|
|
772 | time_update_monotonic (); |
|
|
773 | else |
|
|
774 | #endif |
|
|
775 | { |
653 | ev_now = ev_time (); |
776 | ev_now = ev_time (); |
|
|
777 | now = ev_now; |
|
|
778 | } |
654 | |
779 | |
655 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
780 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
656 | block = 0.; |
781 | block = 0.; |
657 | else |
782 | else |
658 | { |
783 | { |
659 | block = MAX_BLOCKTIME; |
784 | block = MAX_BLOCKTIME; |
660 | |
785 | |
661 | if (timercnt) |
786 | if (timercnt) |
662 | { |
787 | { |
663 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
788 | ev_tstamp to = timers [0]->at - now + method_fudge; |
664 | if (block > to) block = to; |
789 | if (block > to) block = to; |
665 | } |
790 | } |
666 | |
791 | |
667 | if (periodiccnt) |
792 | if (periodiccnt) |
668 | { |
793 | { |
… | |
… | |
721 | head = &(*head)->next; |
846 | head = &(*head)->next; |
722 | } |
847 | } |
723 | } |
848 | } |
724 | |
849 | |
725 | static void |
850 | static void |
726 | ev_clear (W w) |
851 | ev_clear_pending (W w) |
727 | { |
852 | { |
728 | if (w->pending) |
853 | if (w->pending) |
729 | { |
854 | { |
730 | pendings [w->pending - 1].w = 0; |
855 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
731 | w->pending = 0; |
856 | w->pending = 0; |
732 | } |
857 | } |
733 | } |
858 | } |
734 | |
859 | |
735 | static void |
860 | static void |
… | |
… | |
747 | /*****************************************************************************/ |
872 | /*****************************************************************************/ |
748 | |
873 | |
749 | void |
874 | void |
750 | ev_io_start (struct ev_io *w) |
875 | ev_io_start (struct ev_io *w) |
751 | { |
876 | { |
|
|
877 | int fd = w->fd; |
|
|
878 | |
752 | if (ev_is_active (w)) |
879 | if (ev_is_active (w)) |
753 | return; |
880 | return; |
754 | |
881 | |
755 | int fd = w->fd; |
882 | assert (("ev_io_start called with negative fd", fd >= 0)); |
756 | |
883 | |
757 | ev_start ((W)w, 1); |
884 | ev_start ((W)w, 1); |
758 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
885 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
759 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
886 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
760 | |
887 | |
… | |
… | |
762 | } |
889 | } |
763 | |
890 | |
764 | void |
891 | void |
765 | ev_io_stop (struct ev_io *w) |
892 | ev_io_stop (struct ev_io *w) |
766 | { |
893 | { |
767 | ev_clear ((W)w); |
894 | ev_clear_pending ((W)w); |
768 | if (!ev_is_active (w)) |
895 | if (!ev_is_active (w)) |
769 | return; |
896 | return; |
770 | |
897 | |
771 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
898 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
772 | ev_stop ((W)w); |
899 | ev_stop ((W)w); |
… | |
… | |
780 | if (ev_is_active (w)) |
907 | if (ev_is_active (w)) |
781 | return; |
908 | return; |
782 | |
909 | |
783 | w->at += now; |
910 | w->at += now; |
784 | |
911 | |
785 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
912 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
786 | |
913 | |
787 | ev_start ((W)w, ++timercnt); |
914 | ev_start ((W)w, ++timercnt); |
788 | array_needsize (timers, timermax, timercnt, ); |
915 | array_needsize (timers, timermax, timercnt, ); |
789 | timers [timercnt - 1] = w; |
916 | timers [timercnt - 1] = w; |
790 | upheap ((WT *)timers, timercnt - 1); |
917 | upheap ((WT *)timers, timercnt - 1); |
791 | } |
918 | } |
792 | |
919 | |
793 | void |
920 | void |
794 | ev_timer_stop (struct ev_timer *w) |
921 | ev_timer_stop (struct ev_timer *w) |
795 | { |
922 | { |
796 | ev_clear ((W)w); |
923 | ev_clear_pending ((W)w); |
797 | if (!ev_is_active (w)) |
924 | if (!ev_is_active (w)) |
798 | return; |
925 | return; |
799 | |
926 | |
800 | if (w->active < timercnt--) |
927 | if (w->active < timercnt--) |
801 | { |
928 | { |
… | |
… | |
829 | ev_periodic_start (struct ev_periodic *w) |
956 | ev_periodic_start (struct ev_periodic *w) |
830 | { |
957 | { |
831 | if (ev_is_active (w)) |
958 | if (ev_is_active (w)) |
832 | return; |
959 | return; |
833 | |
960 | |
834 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
961 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
835 | |
962 | |
836 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
963 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
837 | if (w->interval) |
964 | if (w->interval) |
838 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
965 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
839 | |
966 | |
… | |
… | |
844 | } |
971 | } |
845 | |
972 | |
846 | void |
973 | void |
847 | ev_periodic_stop (struct ev_periodic *w) |
974 | ev_periodic_stop (struct ev_periodic *w) |
848 | { |
975 | { |
849 | ev_clear ((W)w); |
976 | ev_clear_pending ((W)w); |
850 | if (!ev_is_active (w)) |
977 | if (!ev_is_active (w)) |
851 | return; |
978 | return; |
852 | |
979 | |
853 | if (w->active < periodiccnt--) |
980 | if (w->active < periodiccnt--) |
854 | { |
981 | { |
… | |
… | |
862 | void |
989 | void |
863 | ev_signal_start (struct ev_signal *w) |
990 | ev_signal_start (struct ev_signal *w) |
864 | { |
991 | { |
865 | if (ev_is_active (w)) |
992 | if (ev_is_active (w)) |
866 | return; |
993 | return; |
|
|
994 | |
|
|
995 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
867 | |
996 | |
868 | ev_start ((W)w, 1); |
997 | ev_start ((W)w, 1); |
869 | array_needsize (signals, signalmax, w->signum, signals_init); |
998 | array_needsize (signals, signalmax, w->signum, signals_init); |
870 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
999 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
871 | |
1000 | |
… | |
… | |
880 | } |
1009 | } |
881 | |
1010 | |
882 | void |
1011 | void |
883 | ev_signal_stop (struct ev_signal *w) |
1012 | ev_signal_stop (struct ev_signal *w) |
884 | { |
1013 | { |
885 | ev_clear ((W)w); |
1014 | ev_clear_pending ((W)w); |
886 | if (!ev_is_active (w)) |
1015 | if (!ev_is_active (w)) |
887 | return; |
1016 | return; |
888 | |
1017 | |
889 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1018 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
890 | ev_stop ((W)w); |
1019 | ev_stop ((W)w); |
… | |
… | |
905 | } |
1034 | } |
906 | |
1035 | |
907 | void |
1036 | void |
908 | ev_idle_stop (struct ev_idle *w) |
1037 | ev_idle_stop (struct ev_idle *w) |
909 | { |
1038 | { |
910 | ev_clear ((W)w); |
1039 | ev_clear_pending ((W)w); |
911 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
912 | return; |
1041 | return; |
913 | |
1042 | |
914 | idles [w->active - 1] = idles [--idlecnt]; |
1043 | idles [w->active - 1] = idles [--idlecnt]; |
915 | ev_stop ((W)w); |
1044 | ev_stop ((W)w); |
… | |
… | |
927 | } |
1056 | } |
928 | |
1057 | |
929 | void |
1058 | void |
930 | ev_prepare_stop (struct ev_prepare *w) |
1059 | ev_prepare_stop (struct ev_prepare *w) |
931 | { |
1060 | { |
932 | ev_clear ((W)w); |
1061 | ev_clear_pending ((W)w); |
933 | if (ev_is_active (w)) |
1062 | if (ev_is_active (w)) |
934 | return; |
1063 | return; |
935 | |
1064 | |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
1065 | prepares [w->active - 1] = prepares [--preparecnt]; |
937 | ev_stop ((W)w); |
1066 | ev_stop ((W)w); |
… | |
… | |
949 | } |
1078 | } |
950 | |
1079 | |
951 | void |
1080 | void |
952 | ev_check_stop (struct ev_check *w) |
1081 | ev_check_stop (struct ev_check *w) |
953 | { |
1082 | { |
954 | ev_clear ((W)w); |
1083 | ev_clear_pending ((W)w); |
955 | if (ev_is_active (w)) |
1084 | if (ev_is_active (w)) |
956 | return; |
1085 | return; |
957 | |
1086 | |
958 | checks [w->active - 1] = checks [--checkcnt]; |
1087 | checks [w->active - 1] = checks [--checkcnt]; |
959 | ev_stop ((W)w); |
1088 | ev_stop ((W)w); |
… | |
… | |
970 | } |
1099 | } |
971 | |
1100 | |
972 | void |
1101 | void |
973 | ev_child_stop (struct ev_child *w) |
1102 | ev_child_stop (struct ev_child *w) |
974 | { |
1103 | { |
975 | ev_clear ((W)w); |
1104 | ev_clear_pending ((W)w); |
976 | if (ev_is_active (w)) |
1105 | if (ev_is_active (w)) |
977 | return; |
1106 | return; |
978 | |
1107 | |
979 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1108 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
980 | ev_stop ((W)w); |
1109 | ev_stop ((W)w); |