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) |
72 | |
104 | |
73 | typedef struct ev_watcher *W; |
105 | typedef struct ev_watcher *W; |
74 | typedef struct ev_watcher_list *WL; |
106 | typedef struct ev_watcher_list *WL; |
75 | typedef struct ev_watcher_time *WT; |
107 | typedef struct ev_watcher_time *WT; |
76 | |
108 | |
77 | static ev_tstamp now, diff; /* monotonic clock */ |
109 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
78 | ev_tstamp ev_now; |
110 | ev_tstamp ev_now; |
79 | int ev_method; |
111 | int ev_method; |
80 | |
112 | |
81 | static int have_monotonic; /* runtime */ |
113 | static int have_monotonic; /* runtime */ |
82 | |
114 | |
… | |
… | |
102 | |
134 | |
103 | static ev_tstamp |
135 | static ev_tstamp |
104 | get_clock (void) |
136 | get_clock (void) |
105 | { |
137 | { |
106 | #if EV_USE_MONOTONIC |
138 | #if EV_USE_MONOTONIC |
107 | if (have_monotonic) |
139 | if (expect_true (have_monotonic)) |
108 | { |
140 | { |
109 | struct timespec ts; |
141 | struct timespec ts; |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
142 | clock_gettime (CLOCK_MONOTONIC, &ts); |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
143 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
112 | } |
144 | } |
… | |
… | |
116 | } |
148 | } |
117 | |
149 | |
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
150 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
119 | |
151 | |
120 | #define array_needsize(base,cur,cnt,init) \ |
152 | #define array_needsize(base,cur,cnt,init) \ |
121 | if ((cnt) > cur) \ |
153 | if (expect_false ((cnt) > cur)) \ |
122 | { \ |
154 | { \ |
123 | int newcnt = cur; \ |
155 | int newcnt = cur; \ |
124 | do \ |
156 | do \ |
125 | { \ |
157 | { \ |
126 | newcnt = array_roundsize (base, newcnt << 1); \ |
158 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
135 | /*****************************************************************************/ |
167 | /*****************************************************************************/ |
136 | |
168 | |
137 | typedef struct |
169 | typedef struct |
138 | { |
170 | { |
139 | struct ev_io *head; |
171 | struct ev_io *head; |
140 | int events; |
172 | unsigned char events; |
|
|
173 | unsigned char reify; |
141 | } ANFD; |
174 | } ANFD; |
142 | |
175 | |
143 | static ANFD *anfds; |
176 | static ANFD *anfds; |
144 | static int anfdmax; |
177 | static int anfdmax; |
145 | |
178 | |
… | |
… | |
148 | { |
181 | { |
149 | while (count--) |
182 | while (count--) |
150 | { |
183 | { |
151 | base->head = 0; |
184 | base->head = 0; |
152 | base->events = EV_NONE; |
185 | base->events = EV_NONE; |
|
|
186 | base->reify = 0; |
|
|
187 | |
153 | ++base; |
188 | ++base; |
154 | } |
189 | } |
155 | } |
190 | } |
156 | |
191 | |
157 | typedef struct |
192 | typedef struct |
… | |
… | |
164 | static int pendingmax, pendingcnt; |
199 | static int pendingmax, pendingcnt; |
165 | |
200 | |
166 | static void |
201 | static void |
167 | event (W w, int events) |
202 | event (W w, int events) |
168 | { |
203 | { |
|
|
204 | if (w->pending) |
|
|
205 | { |
|
|
206 | pendings [w->pending - 1].events |= events; |
|
|
207 | return; |
|
|
208 | } |
|
|
209 | |
169 | w->pending = ++pendingcnt; |
210 | w->pending = ++pendingcnt; |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
211 | array_needsize (pendings, pendingmax, pendingcnt, ); |
171 | pendings [pendingcnt - 1].w = w; |
212 | pendings [pendingcnt - 1].w = w; |
172 | pendings [pendingcnt - 1].events = events; |
213 | pendings [pendingcnt - 1].events = events; |
173 | } |
214 | } |
… | |
… | |
215 | int events = 0; |
256 | int events = 0; |
216 | |
257 | |
217 | for (w = anfd->head; w; w = w->next) |
258 | for (w = anfd->head; w; w = w->next) |
218 | events |= w->events; |
259 | events |= w->events; |
219 | |
260 | |
220 | anfd->events &= ~EV_REIFY; |
261 | anfd->reify = 0; |
221 | |
262 | |
222 | if (anfd->events != events) |
263 | if (anfd->events != events) |
223 | { |
264 | { |
224 | method_modify (fd, anfd->events, events); |
265 | method_modify (fd, anfd->events, events); |
225 | anfd->events = events; |
266 | anfd->events = events; |
… | |
… | |
230 | } |
271 | } |
231 | |
272 | |
232 | static void |
273 | static void |
233 | fd_change (int fd) |
274 | fd_change (int fd) |
234 | { |
275 | { |
235 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
276 | if (anfds [fd].reify || fdchangecnt < 0) |
236 | return; |
277 | return; |
237 | |
278 | |
238 | anfds [fd].events |= EV_REIFY; |
279 | anfds [fd].reify = 1; |
239 | |
280 | |
240 | ++fdchangecnt; |
281 | ++fdchangecnt; |
241 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
282 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
242 | fdchanges [fdchangecnt - 1] = fd; |
283 | fdchanges [fdchangecnt - 1] = fd; |
243 | } |
284 | } |
244 | |
285 | |
|
|
286 | static void |
|
|
287 | fd_kill (int fd) |
|
|
288 | { |
|
|
289 | struct ev_io *w; |
|
|
290 | |
|
|
291 | printf ("killing fd %d\n", fd);//D |
|
|
292 | while ((w = anfds [fd].head)) |
|
|
293 | { |
|
|
294 | ev_io_stop (w); |
|
|
295 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
296 | } |
|
|
297 | } |
|
|
298 | |
245 | /* called on EBADF to verify fds */ |
299 | /* called on EBADF to verify fds */ |
246 | static void |
300 | static void |
247 | fd_recheck (void) |
301 | fd_ebadf (void) |
248 | { |
302 | { |
249 | int fd; |
303 | int fd; |
250 | |
304 | |
251 | for (fd = 0; fd < anfdmax; ++fd) |
305 | for (fd = 0; fd < anfdmax; ++fd) |
252 | if (anfds [fd].events) |
306 | if (anfds [fd].events) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
307 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
254 | while (anfds [fd].head) |
308 | fd_kill (fd); |
|
|
309 | } |
|
|
310 | |
|
|
311 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
312 | static void |
|
|
313 | fd_enomem (void) |
|
|
314 | { |
|
|
315 | int fd = anfdmax; |
|
|
316 | |
|
|
317 | while (fd--) |
|
|
318 | if (anfds [fd].events) |
255 | { |
319 | { |
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
320 | close (fd); |
257 | ev_io_stop (anfds [fd].head); |
321 | fd_kill (fd); |
|
|
322 | return; |
258 | } |
323 | } |
259 | } |
324 | } |
260 | |
325 | |
261 | /*****************************************************************************/ |
326 | /*****************************************************************************/ |
262 | |
327 | |
263 | static struct ev_timer **timers; |
328 | static struct ev_timer **timers; |
… | |
… | |
310 | /*****************************************************************************/ |
375 | /*****************************************************************************/ |
311 | |
376 | |
312 | typedef struct |
377 | typedef struct |
313 | { |
378 | { |
314 | struct ev_signal *head; |
379 | struct ev_signal *head; |
315 | sig_atomic_t gotsig; |
380 | sig_atomic_t volatile gotsig; |
316 | } ANSIG; |
381 | } ANSIG; |
317 | |
382 | |
318 | static ANSIG *signals; |
383 | static ANSIG *signals; |
319 | static int signalmax; |
384 | static int signalmax; |
320 | |
385 | |
321 | static int sigpipe [2]; |
386 | static int sigpipe [2]; |
322 | static sig_atomic_t gotsig; |
387 | static sig_atomic_t volatile gotsig; |
323 | static struct ev_io sigev; |
388 | static struct ev_io sigev; |
324 | |
389 | |
325 | static void |
390 | static void |
326 | signals_init (ANSIG *base, int count) |
391 | signals_init (ANSIG *base, int count) |
327 | { |
392 | { |
328 | while (count--) |
393 | while (count--) |
329 | { |
394 | { |
330 | base->head = 0; |
395 | base->head = 0; |
331 | base->gotsig = 0; |
396 | base->gotsig = 0; |
|
|
397 | |
332 | ++base; |
398 | ++base; |
333 | } |
399 | } |
334 | } |
400 | } |
335 | |
401 | |
336 | static void |
402 | static void |
… | |
… | |
339 | signals [signum - 1].gotsig = 1; |
405 | signals [signum - 1].gotsig = 1; |
340 | |
406 | |
341 | if (!gotsig) |
407 | if (!gotsig) |
342 | { |
408 | { |
343 | gotsig = 1; |
409 | gotsig = 1; |
344 | write (sigpipe [1], &gotsig, 1); |
410 | write (sigpipe [1], &signum, 1); |
345 | } |
411 | } |
346 | } |
412 | } |
347 | |
413 | |
348 | static void |
414 | static void |
349 | sigcb (struct ev_io *iow, int revents) |
415 | sigcb (struct ev_io *iow, int revents) |
350 | { |
416 | { |
351 | struct ev_signal *w; |
417 | struct ev_signal *w; |
352 | int sig; |
418 | int signum; |
353 | |
419 | |
|
|
420 | read (sigpipe [0], &revents, 1); |
354 | gotsig = 0; |
421 | gotsig = 0; |
355 | read (sigpipe [0], &revents, 1); |
|
|
356 | |
422 | |
357 | for (sig = signalmax; sig--; ) |
423 | for (signum = signalmax; signum--; ) |
358 | if (signals [sig].gotsig) |
424 | if (signals [signum].gotsig) |
359 | { |
425 | { |
360 | signals [sig].gotsig = 0; |
426 | signals [signum].gotsig = 0; |
361 | |
427 | |
362 | for (w = signals [sig].head; w; w = w->next) |
428 | for (w = signals [signum].head; w; w = w->next) |
363 | event ((W)w, EV_SIGNAL); |
429 | event ((W)w, EV_SIGNAL); |
364 | } |
430 | } |
365 | } |
431 | } |
366 | |
432 | |
367 | static void |
433 | static void |
… | |
… | |
404 | struct ev_child *w; |
470 | struct ev_child *w; |
405 | int pid, status; |
471 | int pid, status; |
406 | |
472 | |
407 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
408 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
409 | if (w->pid == pid || w->pid == -1) |
475 | if (w->pid == pid || !w->pid) |
410 | { |
476 | { |
411 | w->status = status; |
477 | w->status = status; |
412 | event ((W)w, EV_CHILD); |
478 | event ((W)w, EV_CHILD); |
413 | } |
479 | } |
414 | } |
480 | } |
… | |
… | |
416 | /*****************************************************************************/ |
482 | /*****************************************************************************/ |
417 | |
483 | |
418 | #if EV_USE_EPOLL |
484 | #if EV_USE_EPOLL |
419 | # include "ev_epoll.c" |
485 | # include "ev_epoll.c" |
420 | #endif |
486 | #endif |
|
|
487 | #if EV_USE_POLL |
|
|
488 | # include "ev_poll.c" |
|
|
489 | #endif |
421 | #if EV_USE_SELECT |
490 | #if EV_USE_SELECT |
422 | # include "ev_select.c" |
491 | # include "ev_select.c" |
423 | #endif |
492 | #endif |
424 | |
493 | |
425 | int |
494 | int |
… | |
… | |
432 | ev_version_minor (void) |
501 | ev_version_minor (void) |
433 | { |
502 | { |
434 | return EV_VERSION_MINOR; |
503 | return EV_VERSION_MINOR; |
435 | } |
504 | } |
436 | |
505 | |
|
|
506 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
507 | static int |
|
|
508 | enable_secure () |
|
|
509 | { |
|
|
510 | return getuid () != geteuid () |
|
|
511 | || getgid () != getegid (); |
|
|
512 | } |
|
|
513 | |
437 | int ev_init (int flags) |
514 | int ev_init (int methods) |
438 | { |
515 | { |
439 | if (!ev_method) |
516 | if (!ev_method) |
440 | { |
517 | { |
441 | #if EV_USE_MONOTONIC |
518 | #if EV_USE_MONOTONIC |
442 | { |
519 | { |
… | |
… | |
444 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
521 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
445 | have_monotonic = 1; |
522 | have_monotonic = 1; |
446 | } |
523 | } |
447 | #endif |
524 | #endif |
448 | |
525 | |
449 | ev_now = ev_time (); |
526 | ev_now = ev_time (); |
450 | now = get_clock (); |
527 | now = get_clock (); |
|
|
528 | now_floor = now; |
451 | diff = ev_now - now; |
529 | diff = ev_now - now; |
452 | |
530 | |
453 | if (pipe (sigpipe)) |
531 | if (pipe (sigpipe)) |
454 | return 0; |
532 | return 0; |
455 | |
533 | |
|
|
534 | if (methods == EVMETHOD_AUTO) |
|
|
535 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
537 | else |
456 | ev_method = EVMETHOD_NONE; |
538 | methods = EVMETHOD_ANY; |
|
|
539 | |
|
|
540 | ev_method = 0; |
457 | #if EV_USE_EPOLL |
541 | #if EV_USE_EPOLL |
458 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
543 | #endif |
|
|
544 | #if EV_USE_POLL |
|
|
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
459 | #endif |
546 | #endif |
460 | #if EV_USE_SELECT |
547 | #if EV_USE_SELECT |
461 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
548 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
462 | #endif |
549 | #endif |
463 | |
550 | |
464 | if (ev_method) |
551 | if (ev_method) |
465 | { |
552 | { |
466 | ev_watcher_init (&sigev, sigcb); |
553 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
475 | } |
562 | } |
476 | |
563 | |
477 | /*****************************************************************************/ |
564 | /*****************************************************************************/ |
478 | |
565 | |
479 | void |
566 | void |
480 | ev_prefork (void) |
567 | ev_fork_prepare (void) |
481 | { |
568 | { |
482 | /* nop */ |
569 | /* nop */ |
483 | } |
570 | } |
484 | |
571 | |
485 | void |
572 | void |
486 | ev_postfork_parent (void) |
573 | ev_fork_parent (void) |
487 | { |
574 | { |
488 | /* nop */ |
575 | /* nop */ |
489 | } |
576 | } |
490 | |
577 | |
491 | void |
578 | void |
492 | ev_postfork_child (void) |
579 | ev_fork_child (void) |
493 | { |
580 | { |
494 | #if EV_USE_EPOLL |
581 | #if EV_USE_EPOLL |
495 | if (ev_method == EVMETHOD_EPOLL) |
582 | if (ev_method == EVMETHOD_EPOLL) |
496 | epoll_postfork_child (); |
583 | epoll_postfork_child (); |
497 | #endif |
584 | #endif |
… | |
… | |
528 | struct ev_timer *w = timers [0]; |
615 | struct ev_timer *w = timers [0]; |
529 | |
616 | |
530 | /* first reschedule or stop timer */ |
617 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
618 | if (w->repeat) |
532 | { |
619 | { |
|
|
620 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
621 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
622 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
623 | } |
537 | else |
624 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
625 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
539 | |
626 | |
… | |
… | |
550 | |
637 | |
551 | /* first reschedule or stop timer */ |
638 | /* first reschedule or stop timer */ |
552 | if (w->interval) |
639 | if (w->interval) |
553 | { |
640 | { |
554 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
641 | 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)); |
642 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
643 | downheap ((WT *)periodics, periodiccnt, 0); |
557 | } |
644 | } |
558 | else |
645 | else |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
646 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
560 | |
647 | |
561 | event ((W)w, EV_TIMEOUT); |
648 | event ((W)w, EV_PERIODIC); |
562 | } |
649 | } |
563 | } |
650 | } |
564 | |
651 | |
565 | static void |
652 | static void |
566 | periodics_reschedule (ev_tstamp diff) |
653 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
585 | } |
672 | } |
586 | } |
673 | } |
587 | } |
674 | } |
588 | } |
675 | } |
589 | |
676 | |
|
|
677 | static int |
|
|
678 | time_update_monotonic (void) |
|
|
679 | { |
|
|
680 | now = get_clock (); |
|
|
681 | |
|
|
682 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
683 | { |
|
|
684 | ev_now = now + diff; |
|
|
685 | return 0; |
|
|
686 | } |
|
|
687 | else |
|
|
688 | { |
|
|
689 | now_floor = now; |
|
|
690 | ev_now = ev_time (); |
|
|
691 | return 1; |
|
|
692 | } |
|
|
693 | } |
|
|
694 | |
590 | static void |
695 | static void |
591 | time_update (void) |
696 | time_update (void) |
592 | { |
697 | { |
593 | int i; |
698 | int i; |
594 | |
699 | |
595 | ev_now = ev_time (); |
700 | #if EV_USE_MONOTONIC |
596 | |
|
|
597 | if (have_monotonic) |
701 | if (expect_true (have_monotonic)) |
598 | { |
702 | { |
|
|
703 | if (time_update_monotonic ()) |
|
|
704 | { |
599 | ev_tstamp odiff = diff; |
705 | ev_tstamp odiff = diff; |
600 | |
706 | |
601 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
707 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
602 | { |
708 | { |
603 | now = get_clock (); |
|
|
604 | diff = ev_now - now; |
709 | diff = ev_now - now; |
605 | |
710 | |
606 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
711 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
607 | return; /* all is well */ |
712 | return; /* all is well */ |
608 | |
713 | |
609 | ev_now = ev_time (); |
714 | ev_now = ev_time (); |
|
|
715 | now = get_clock (); |
|
|
716 | now_floor = now; |
610 | } |
717 | } |
611 | |
718 | |
612 | periodics_reschedule (diff - odiff); |
719 | periodics_reschedule (diff - odiff); |
613 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
720 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
721 | } |
614 | } |
722 | } |
615 | else |
723 | else |
|
|
724 | #endif |
616 | { |
725 | { |
|
|
726 | ev_now = ev_time (); |
|
|
727 | |
617 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
728 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
618 | { |
729 | { |
619 | periodics_reschedule (ev_now - now); |
730 | periodics_reschedule (ev_now - now); |
620 | |
731 | |
621 | /* adjust timers. this is easy, as the offset is the same for all */ |
732 | /* adjust timers. this is easy, as the offset is the same for all */ |
622 | for (i = 0; i < timercnt; ++i) |
733 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
635 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
746 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
636 | |
747 | |
637 | do |
748 | do |
638 | { |
749 | { |
639 | /* queue check watchers (and execute them) */ |
750 | /* queue check watchers (and execute them) */ |
640 | if (preparecnt) |
751 | if (expect_false (preparecnt)) |
641 | { |
752 | { |
642 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
753 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
643 | call_pending (); |
754 | call_pending (); |
644 | } |
755 | } |
645 | |
756 | |
… | |
… | |
648 | |
759 | |
649 | /* calculate blocking time */ |
760 | /* calculate blocking time */ |
650 | |
761 | |
651 | /* we only need this for !monotonic clockor timers, but as we basically |
762 | /* we only need this for !monotonic clockor timers, but as we basically |
652 | always have timers, we just calculate it always */ |
763 | always have timers, we just calculate it always */ |
|
|
764 | #if EV_USE_MONOTONIC |
|
|
765 | if (expect_true (have_monotonic)) |
|
|
766 | time_update_monotonic (); |
|
|
767 | else |
|
|
768 | #endif |
|
|
769 | { |
653 | ev_now = ev_time (); |
770 | ev_now = ev_time (); |
|
|
771 | now = ev_now; |
|
|
772 | } |
654 | |
773 | |
655 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
774 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
656 | block = 0.; |
775 | block = 0.; |
657 | else |
776 | else |
658 | { |
777 | { |
659 | block = MAX_BLOCKTIME; |
778 | block = MAX_BLOCKTIME; |
660 | |
779 | |
661 | if (timercnt) |
780 | if (timercnt) |
662 | { |
781 | { |
663 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
782 | ev_tstamp to = timers [0]->at - now + method_fudge; |
664 | if (block > to) block = to; |
783 | if (block > to) block = to; |
665 | } |
784 | } |
666 | |
785 | |
667 | if (periodiccnt) |
786 | if (periodiccnt) |
668 | { |
787 | { |
… | |
… | |
721 | head = &(*head)->next; |
840 | head = &(*head)->next; |
722 | } |
841 | } |
723 | } |
842 | } |
724 | |
843 | |
725 | static void |
844 | static void |
726 | ev_clear (W w) |
845 | ev_clear_pending (W w) |
727 | { |
846 | { |
728 | if (w->pending) |
847 | if (w->pending) |
729 | { |
848 | { |
730 | pendings [w->pending - 1].w = 0; |
849 | pendings [w->pending - 1].w = 0; |
731 | w->pending = 0; |
850 | w->pending = 0; |
… | |
… | |
747 | /*****************************************************************************/ |
866 | /*****************************************************************************/ |
748 | |
867 | |
749 | void |
868 | void |
750 | ev_io_start (struct ev_io *w) |
869 | ev_io_start (struct ev_io *w) |
751 | { |
870 | { |
|
|
871 | int fd = w->fd; |
|
|
872 | |
752 | if (ev_is_active (w)) |
873 | if (ev_is_active (w)) |
753 | return; |
874 | return; |
754 | |
875 | |
755 | int fd = w->fd; |
876 | assert (("ev_io_start called with negative fd", fd >= 0)); |
756 | |
877 | |
757 | ev_start ((W)w, 1); |
878 | ev_start ((W)w, 1); |
758 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
879 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
759 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
880 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
760 | |
881 | |
… | |
… | |
762 | } |
883 | } |
763 | |
884 | |
764 | void |
885 | void |
765 | ev_io_stop (struct ev_io *w) |
886 | ev_io_stop (struct ev_io *w) |
766 | { |
887 | { |
767 | ev_clear ((W)w); |
888 | ev_clear_pending ((W)w); |
768 | if (!ev_is_active (w)) |
889 | if (!ev_is_active (w)) |
769 | return; |
890 | return; |
770 | |
891 | |
771 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
892 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
772 | ev_stop ((W)w); |
893 | ev_stop ((W)w); |
… | |
… | |
780 | if (ev_is_active (w)) |
901 | if (ev_is_active (w)) |
781 | return; |
902 | return; |
782 | |
903 | |
783 | w->at += now; |
904 | w->at += now; |
784 | |
905 | |
785 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
906 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
786 | |
907 | |
787 | ev_start ((W)w, ++timercnt); |
908 | ev_start ((W)w, ++timercnt); |
788 | array_needsize (timers, timermax, timercnt, ); |
909 | array_needsize (timers, timermax, timercnt, ); |
789 | timers [timercnt - 1] = w; |
910 | timers [timercnt - 1] = w; |
790 | upheap ((WT *)timers, timercnt - 1); |
911 | upheap ((WT *)timers, timercnt - 1); |
791 | } |
912 | } |
792 | |
913 | |
793 | void |
914 | void |
794 | ev_timer_stop (struct ev_timer *w) |
915 | ev_timer_stop (struct ev_timer *w) |
795 | { |
916 | { |
796 | ev_clear ((W)w); |
917 | ev_clear_pending ((W)w); |
797 | if (!ev_is_active (w)) |
918 | if (!ev_is_active (w)) |
798 | return; |
919 | return; |
799 | |
920 | |
800 | if (w->active < timercnt--) |
921 | if (w->active < timercnt--) |
801 | { |
922 | { |
… | |
… | |
829 | ev_periodic_start (struct ev_periodic *w) |
950 | ev_periodic_start (struct ev_periodic *w) |
830 | { |
951 | { |
831 | if (ev_is_active (w)) |
952 | if (ev_is_active (w)) |
832 | return; |
953 | return; |
833 | |
954 | |
834 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
955 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
835 | |
956 | |
836 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
957 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
837 | if (w->interval) |
958 | if (w->interval) |
838 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
959 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
839 | |
960 | |
… | |
… | |
844 | } |
965 | } |
845 | |
966 | |
846 | void |
967 | void |
847 | ev_periodic_stop (struct ev_periodic *w) |
968 | ev_periodic_stop (struct ev_periodic *w) |
848 | { |
969 | { |
849 | ev_clear ((W)w); |
970 | ev_clear_pending ((W)w); |
850 | if (!ev_is_active (w)) |
971 | if (!ev_is_active (w)) |
851 | return; |
972 | return; |
852 | |
973 | |
853 | if (w->active < periodiccnt--) |
974 | if (w->active < periodiccnt--) |
854 | { |
975 | { |
… | |
… | |
862 | void |
983 | void |
863 | ev_signal_start (struct ev_signal *w) |
984 | ev_signal_start (struct ev_signal *w) |
864 | { |
985 | { |
865 | if (ev_is_active (w)) |
986 | if (ev_is_active (w)) |
866 | return; |
987 | return; |
|
|
988 | |
|
|
989 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
867 | |
990 | |
868 | ev_start ((W)w, 1); |
991 | ev_start ((W)w, 1); |
869 | array_needsize (signals, signalmax, w->signum, signals_init); |
992 | array_needsize (signals, signalmax, w->signum, signals_init); |
870 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
993 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
871 | |
994 | |
… | |
… | |
880 | } |
1003 | } |
881 | |
1004 | |
882 | void |
1005 | void |
883 | ev_signal_stop (struct ev_signal *w) |
1006 | ev_signal_stop (struct ev_signal *w) |
884 | { |
1007 | { |
885 | ev_clear ((W)w); |
1008 | ev_clear_pending ((W)w); |
886 | if (!ev_is_active (w)) |
1009 | if (!ev_is_active (w)) |
887 | return; |
1010 | return; |
888 | |
1011 | |
889 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1012 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
890 | ev_stop ((W)w); |
1013 | ev_stop ((W)w); |
… | |
… | |
905 | } |
1028 | } |
906 | |
1029 | |
907 | void |
1030 | void |
908 | ev_idle_stop (struct ev_idle *w) |
1031 | ev_idle_stop (struct ev_idle *w) |
909 | { |
1032 | { |
910 | ev_clear ((W)w); |
1033 | ev_clear_pending ((W)w); |
911 | if (ev_is_active (w)) |
1034 | if (ev_is_active (w)) |
912 | return; |
1035 | return; |
913 | |
1036 | |
914 | idles [w->active - 1] = idles [--idlecnt]; |
1037 | idles [w->active - 1] = idles [--idlecnt]; |
915 | ev_stop ((W)w); |
1038 | ev_stop ((W)w); |
… | |
… | |
927 | } |
1050 | } |
928 | |
1051 | |
929 | void |
1052 | void |
930 | ev_prepare_stop (struct ev_prepare *w) |
1053 | ev_prepare_stop (struct ev_prepare *w) |
931 | { |
1054 | { |
932 | ev_clear ((W)w); |
1055 | ev_clear_pending ((W)w); |
933 | if (ev_is_active (w)) |
1056 | if (ev_is_active (w)) |
934 | return; |
1057 | return; |
935 | |
1058 | |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
1059 | prepares [w->active - 1] = prepares [--preparecnt]; |
937 | ev_stop ((W)w); |
1060 | ev_stop ((W)w); |
… | |
… | |
949 | } |
1072 | } |
950 | |
1073 | |
951 | void |
1074 | void |
952 | ev_check_stop (struct ev_check *w) |
1075 | ev_check_stop (struct ev_check *w) |
953 | { |
1076 | { |
954 | ev_clear ((W)w); |
1077 | ev_clear_pending ((W)w); |
955 | if (ev_is_active (w)) |
1078 | if (ev_is_active (w)) |
956 | return; |
1079 | return; |
957 | |
1080 | |
958 | checks [w->active - 1] = checks [--checkcnt]; |
1081 | checks [w->active - 1] = checks [--checkcnt]; |
959 | ev_stop ((W)w); |
1082 | ev_stop ((W)w); |
… | |
… | |
970 | } |
1093 | } |
971 | |
1094 | |
972 | void |
1095 | void |
973 | ev_child_stop (struct ev_child *w) |
1096 | ev_child_stop (struct ev_child *w) |
974 | { |
1097 | { |
975 | ev_clear ((W)w); |
1098 | ev_clear_pending ((W)w); |
976 | if (ev_is_active (w)) |
1099 | if (ev_is_active (w)) |
977 | return; |
1100 | return; |
978 | |
1101 | |
979 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1102 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
980 | ev_stop ((W)w); |
1103 | ev_stop ((W)w); |