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 | } |
113 | #endif |
148 | #endif |
114 | |
149 | |
115 | return ev_time (); |
150 | return ev_time (); |
116 | } |
151 | } |
117 | |
152 | |
118 | #define array_nextsize(n) (((n) << 1) | 4 & ~3) |
153 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
119 | #define array_prevsize(n) (((n) >> 1) | 4 & ~3) |
|
|
120 | |
154 | |
121 | #define array_needsize(base,cur,cnt,init) \ |
155 | #define array_needsize(base,cur,cnt,init) \ |
122 | if ((cnt) > cur) \ |
156 | if (expect_false ((cnt) > cur)) \ |
123 | { \ |
157 | { \ |
124 | int newcnt = cur; \ |
158 | int newcnt = cur; \ |
125 | do \ |
159 | do \ |
126 | { \ |
160 | { \ |
127 | newcnt = array_nextsize (newcnt); \ |
161 | newcnt = array_roundsize (base, newcnt << 1); \ |
128 | } \ |
162 | } \ |
129 | while ((cnt) > newcnt); \ |
163 | while ((cnt) > newcnt); \ |
130 | \ |
164 | \ |
131 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
165 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
132 | init (base + cur, newcnt - cur); \ |
166 | init (base + cur, newcnt - cur); \ |
… | |
… | |
136 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
137 | |
171 | |
138 | typedef struct |
172 | typedef struct |
139 | { |
173 | { |
140 | struct ev_io *head; |
174 | struct ev_io *head; |
141 | int events; |
175 | unsigned char events; |
|
|
176 | unsigned char reify; |
142 | } ANFD; |
177 | } ANFD; |
143 | |
178 | |
144 | static ANFD *anfds; |
179 | static ANFD *anfds; |
145 | static int anfdmax; |
180 | static int anfdmax; |
146 | |
181 | |
… | |
… | |
149 | { |
184 | { |
150 | while (count--) |
185 | while (count--) |
151 | { |
186 | { |
152 | base->head = 0; |
187 | base->head = 0; |
153 | base->events = EV_NONE; |
188 | base->events = EV_NONE; |
|
|
189 | base->reify = 0; |
|
|
190 | |
154 | ++base; |
191 | ++base; |
155 | } |
192 | } |
156 | } |
193 | } |
157 | |
194 | |
158 | typedef struct |
195 | typedef struct |
159 | { |
196 | { |
160 | W w; |
197 | W w; |
161 | int events; |
198 | int events; |
162 | } ANPENDING; |
199 | } ANPENDING; |
163 | |
200 | |
164 | static ANPENDING *pendings; |
201 | static ANPENDING *pendings [NUMPRI]; |
165 | static int pendingmax, pendingcnt; |
202 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
166 | |
203 | |
167 | static void |
204 | static void |
168 | event (W w, int events) |
205 | event (W w, int events) |
169 | { |
206 | { |
170 | if (w->active) |
207 | if (w->pending) |
171 | { |
208 | { |
172 | w->pending = ++pendingcnt; |
|
|
173 | array_needsize (pendings, pendingmax, pendingcnt, ); |
|
|
174 | pendings [pendingcnt - 1].w = w; |
|
|
175 | pendings [pendingcnt - 1].events = events; |
209 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
210 | return; |
176 | } |
211 | } |
|
|
212 | |
|
|
213 | w->pending = ++pendingcnt [ABSPRI (w)]; |
|
|
214 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
|
|
215 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
|
|
216 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
177 | } |
217 | } |
178 | |
218 | |
179 | static void |
219 | static void |
180 | queue_events (W *events, int eventcnt, int type) |
220 | queue_events (W *events, int eventcnt, int type) |
181 | { |
221 | { |
… | |
… | |
219 | int events = 0; |
259 | int events = 0; |
220 | |
260 | |
221 | for (w = anfd->head; w; w = w->next) |
261 | for (w = anfd->head; w; w = w->next) |
222 | events |= w->events; |
262 | events |= w->events; |
223 | |
263 | |
224 | anfd->events &= ~EV_REIFY; |
264 | anfd->reify = 0; |
225 | |
265 | |
226 | if (anfd->events != events) |
266 | if (anfd->events != events) |
227 | { |
267 | { |
228 | method_modify (fd, anfd->events, events); |
268 | method_modify (fd, anfd->events, events); |
229 | anfd->events = events; |
269 | anfd->events = events; |
… | |
… | |
234 | } |
274 | } |
235 | |
275 | |
236 | static void |
276 | static void |
237 | fd_change (int fd) |
277 | fd_change (int fd) |
238 | { |
278 | { |
239 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
279 | if (anfds [fd].reify || fdchangecnt < 0) |
240 | return; |
280 | return; |
241 | |
281 | |
242 | anfds [fd].events |= EV_REIFY; |
282 | anfds [fd].reify = 1; |
243 | |
283 | |
244 | ++fdchangecnt; |
284 | ++fdchangecnt; |
245 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
285 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
246 | fdchanges [fdchangecnt - 1] = fd; |
286 | fdchanges [fdchangecnt - 1] = fd; |
247 | } |
287 | } |
248 | |
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 | |
249 | /* called on EBADF to verify fds */ |
302 | /* called on EBADF to verify fds */ |
250 | static void |
303 | static void |
251 | fd_recheck (void) |
304 | fd_ebadf (void) |
252 | { |
305 | { |
253 | int fd; |
306 | int fd; |
254 | |
307 | |
255 | for (fd = 0; fd < anfdmax; ++fd) |
308 | for (fd = 0; fd < anfdmax; ++fd) |
256 | if (anfds [fd].events) |
309 | if (anfds [fd].events) |
257 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
310 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
258 | 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) |
259 | { |
322 | { |
260 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
323 | close (fd); |
261 | ev_io_stop (anfds [fd].head); |
324 | fd_kill (fd); |
|
|
325 | return; |
262 | } |
326 | } |
263 | } |
327 | } |
264 | |
328 | |
265 | /*****************************************************************************/ |
329 | /*****************************************************************************/ |
266 | |
330 | |
267 | static struct ev_timer **timers; |
331 | static struct ev_timer **timers; |
… | |
… | |
314 | /*****************************************************************************/ |
378 | /*****************************************************************************/ |
315 | |
379 | |
316 | typedef struct |
380 | typedef struct |
317 | { |
381 | { |
318 | struct ev_signal *head; |
382 | struct ev_signal *head; |
319 | sig_atomic_t gotsig; |
383 | sig_atomic_t volatile gotsig; |
320 | } ANSIG; |
384 | } ANSIG; |
321 | |
385 | |
322 | static ANSIG *signals; |
386 | static ANSIG *signals; |
323 | static int signalmax; |
387 | static int signalmax; |
324 | |
388 | |
325 | static int sigpipe [2]; |
389 | static int sigpipe [2]; |
326 | static sig_atomic_t gotsig; |
390 | static sig_atomic_t volatile gotsig; |
327 | static struct ev_io sigev; |
391 | static struct ev_io sigev; |
328 | |
392 | |
329 | static void |
393 | static void |
330 | signals_init (ANSIG *base, int count) |
394 | signals_init (ANSIG *base, int count) |
331 | { |
395 | { |
332 | while (count--) |
396 | while (count--) |
333 | { |
397 | { |
334 | base->head = 0; |
398 | base->head = 0; |
335 | base->gotsig = 0; |
399 | base->gotsig = 0; |
|
|
400 | |
336 | ++base; |
401 | ++base; |
337 | } |
402 | } |
338 | } |
403 | } |
339 | |
404 | |
340 | static void |
405 | static void |
… | |
… | |
343 | signals [signum - 1].gotsig = 1; |
408 | signals [signum - 1].gotsig = 1; |
344 | |
409 | |
345 | if (!gotsig) |
410 | if (!gotsig) |
346 | { |
411 | { |
347 | gotsig = 1; |
412 | gotsig = 1; |
348 | write (sigpipe [1], &gotsig, 1); |
413 | write (sigpipe [1], &signum, 1); |
349 | } |
414 | } |
350 | } |
415 | } |
351 | |
416 | |
352 | static void |
417 | static void |
353 | sigcb (struct ev_io *iow, int revents) |
418 | sigcb (struct ev_io *iow, int revents) |
354 | { |
419 | { |
355 | struct ev_signal *w; |
420 | struct ev_signal *w; |
356 | int sig; |
421 | int signum; |
357 | |
422 | |
|
|
423 | read (sigpipe [0], &revents, 1); |
358 | gotsig = 0; |
424 | gotsig = 0; |
359 | read (sigpipe [0], &revents, 1); |
|
|
360 | |
425 | |
361 | for (sig = signalmax; sig--; ) |
426 | for (signum = signalmax; signum--; ) |
362 | if (signals [sig].gotsig) |
427 | if (signals [signum].gotsig) |
363 | { |
428 | { |
364 | signals [sig].gotsig = 0; |
429 | signals [signum].gotsig = 0; |
365 | |
430 | |
366 | for (w = signals [sig].head; w; w = w->next) |
431 | for (w = signals [signum].head; w; w = w->next) |
367 | event ((W)w, EV_SIGNAL); |
432 | event ((W)w, EV_SIGNAL); |
368 | } |
433 | } |
369 | } |
434 | } |
370 | |
435 | |
371 | static void |
436 | static void |
… | |
… | |
408 | struct ev_child *w; |
473 | struct ev_child *w; |
409 | int pid, status; |
474 | int pid, status; |
410 | |
475 | |
411 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
476 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
412 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
477 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
413 | if (w->pid == pid || w->pid == -1) |
478 | if (w->pid == pid || !w->pid) |
414 | { |
479 | { |
415 | w->status = status; |
480 | w->status = status; |
416 | event ((W)w, EV_CHILD); |
481 | event ((W)w, EV_CHILD); |
417 | } |
482 | } |
418 | } |
483 | } |
… | |
… | |
420 | /*****************************************************************************/ |
485 | /*****************************************************************************/ |
421 | |
486 | |
422 | #if EV_USE_EPOLL |
487 | #if EV_USE_EPOLL |
423 | # include "ev_epoll.c" |
488 | # include "ev_epoll.c" |
424 | #endif |
489 | #endif |
|
|
490 | #if EV_USE_POLL |
|
|
491 | # include "ev_poll.c" |
|
|
492 | #endif |
425 | #if EV_USE_SELECT |
493 | #if EV_USE_SELECT |
426 | # include "ev_select.c" |
494 | # include "ev_select.c" |
427 | #endif |
495 | #endif |
428 | |
496 | |
429 | int |
497 | int |
… | |
… | |
436 | ev_version_minor (void) |
504 | ev_version_minor (void) |
437 | { |
505 | { |
438 | return EV_VERSION_MINOR; |
506 | return EV_VERSION_MINOR; |
439 | } |
507 | } |
440 | |
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 | |
441 | int ev_init (int flags) |
517 | int ev_init (int methods) |
442 | { |
518 | { |
443 | if (!ev_method) |
519 | if (!ev_method) |
444 | { |
520 | { |
445 | #if EV_USE_MONOTONIC |
521 | #if EV_USE_MONOTONIC |
446 | { |
522 | { |
… | |
… | |
448 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
524 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
449 | have_monotonic = 1; |
525 | have_monotonic = 1; |
450 | } |
526 | } |
451 | #endif |
527 | #endif |
452 | |
528 | |
453 | ev_now = ev_time (); |
529 | ev_now = ev_time (); |
454 | now = get_clock (); |
530 | now = get_clock (); |
|
|
531 | now_floor = now; |
455 | diff = ev_now - now; |
532 | diff = ev_now - now; |
456 | |
533 | |
457 | if (pipe (sigpipe)) |
534 | if (pipe (sigpipe)) |
458 | return 0; |
535 | return 0; |
459 | |
536 | |
|
|
537 | if (methods == EVMETHOD_AUTO) |
|
|
538 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
539 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
540 | else |
460 | ev_method = EVMETHOD_NONE; |
541 | methods = EVMETHOD_ANY; |
|
|
542 | |
|
|
543 | ev_method = 0; |
461 | #if EV_USE_EPOLL |
544 | #if EV_USE_EPOLL |
462 | 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); |
463 | #endif |
549 | #endif |
464 | #if EV_USE_SELECT |
550 | #if EV_USE_SELECT |
465 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
551 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
466 | #endif |
552 | #endif |
467 | |
553 | |
468 | if (ev_method) |
554 | if (ev_method) |
469 | { |
555 | { |
470 | ev_watcher_init (&sigev, sigcb); |
556 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
479 | } |
565 | } |
480 | |
566 | |
481 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
482 | |
568 | |
483 | void |
569 | void |
484 | ev_prefork (void) |
570 | ev_fork_prepare (void) |
485 | { |
571 | { |
486 | /* nop */ |
572 | /* nop */ |
487 | } |
573 | } |
488 | |
574 | |
489 | void |
575 | void |
490 | ev_postfork_parent (void) |
576 | ev_fork_parent (void) |
491 | { |
577 | { |
492 | /* nop */ |
578 | /* nop */ |
493 | } |
579 | } |
494 | |
580 | |
495 | void |
581 | void |
496 | ev_postfork_child (void) |
582 | ev_fork_child (void) |
497 | { |
583 | { |
498 | #if EV_USE_EPOLL |
584 | #if EV_USE_EPOLL |
499 | if (ev_method == EVMETHOD_EPOLL) |
585 | if (ev_method == EVMETHOD_EPOLL) |
500 | epoll_postfork_child (); |
586 | epoll_postfork_child (); |
501 | #endif |
587 | #endif |
… | |
… | |
510 | /*****************************************************************************/ |
596 | /*****************************************************************************/ |
511 | |
597 | |
512 | static void |
598 | static void |
513 | call_pending (void) |
599 | call_pending (void) |
514 | { |
600 | { |
|
|
601 | int pri; |
|
|
602 | |
|
|
603 | for (pri = NUMPRI; pri--; ) |
515 | while (pendingcnt) |
604 | while (pendingcnt [pri]) |
516 | { |
605 | { |
517 | ANPENDING *p = pendings + --pendingcnt; |
606 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
518 | |
607 | |
519 | if (p->w) |
608 | if (p->w) |
520 | { |
609 | { |
521 | p->w->pending = 0; |
610 | p->w->pending = 0; |
522 | p->w->cb (p->w, p->events); |
611 | p->w->cb (p->w, p->events); |
523 | } |
612 | } |
524 | } |
613 | } |
525 | } |
614 | } |
526 | |
615 | |
527 | static void |
616 | static void |
528 | timers_reify (void) |
617 | timers_reify (void) |
529 | { |
618 | { |
530 | while (timercnt && timers [0]->at <= now) |
619 | while (timercnt && timers [0]->at <= now) |
531 | { |
620 | { |
532 | struct ev_timer *w = timers [0]; |
621 | struct ev_timer *w = timers [0]; |
533 | |
|
|
534 | event ((W)w, EV_TIMEOUT); |
|
|
535 | |
622 | |
536 | /* first reschedule or stop timer */ |
623 | /* first reschedule or stop timer */ |
537 | if (w->repeat) |
624 | if (w->repeat) |
538 | { |
625 | { |
|
|
626 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
539 | w->at = now + w->repeat; |
627 | w->at = now + w->repeat; |
540 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
541 | downheap ((WT *)timers, timercnt, 0); |
628 | downheap ((WT *)timers, timercnt, 0); |
542 | } |
629 | } |
543 | else |
630 | else |
544 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
631 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
632 | |
|
|
633 | event ((W)w, EV_TIMEOUT); |
545 | } |
634 | } |
546 | } |
635 | } |
547 | |
636 | |
548 | static void |
637 | static void |
549 | periodics_reify (void) |
638 | periodics_reify (void) |
… | |
… | |
554 | |
643 | |
555 | /* first reschedule or stop timer */ |
644 | /* first reschedule or stop timer */ |
556 | if (w->interval) |
645 | if (w->interval) |
557 | { |
646 | { |
558 | 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; |
559 | 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)); |
560 | downheap ((WT *)periodics, periodiccnt, 0); |
649 | downheap ((WT *)periodics, periodiccnt, 0); |
561 | } |
650 | } |
562 | else |
651 | else |
563 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
652 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
564 | |
653 | |
565 | event ((W)w, EV_TIMEOUT); |
654 | event ((W)w, EV_PERIODIC); |
566 | } |
655 | } |
567 | } |
656 | } |
568 | |
657 | |
569 | static void |
658 | static void |
570 | periodics_reschedule (ev_tstamp diff) |
659 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
589 | } |
678 | } |
590 | } |
679 | } |
591 | } |
680 | } |
592 | } |
681 | } |
593 | |
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 | |
594 | static void |
701 | static void |
595 | time_update (void) |
702 | time_update (void) |
596 | { |
703 | { |
597 | int i; |
704 | int i; |
598 | |
705 | |
599 | ev_now = ev_time (); |
706 | #if EV_USE_MONOTONIC |
600 | |
|
|
601 | if (have_monotonic) |
707 | if (expect_true (have_monotonic)) |
602 | { |
708 | { |
|
|
709 | if (time_update_monotonic ()) |
|
|
710 | { |
603 | ev_tstamp odiff = diff; |
711 | ev_tstamp odiff = diff; |
604 | |
712 | |
605 | 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 */ |
606 | { |
714 | { |
607 | now = get_clock (); |
|
|
608 | diff = ev_now - now; |
715 | diff = ev_now - now; |
609 | |
716 | |
610 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
717 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
611 | return; /* all is well */ |
718 | return; /* all is well */ |
612 | |
719 | |
613 | ev_now = ev_time (); |
720 | ev_now = ev_time (); |
|
|
721 | now = get_clock (); |
|
|
722 | now_floor = now; |
614 | } |
723 | } |
615 | |
724 | |
616 | periodics_reschedule (diff - odiff); |
725 | periodics_reschedule (diff - odiff); |
617 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
726 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
727 | } |
618 | } |
728 | } |
619 | else |
729 | else |
|
|
730 | #endif |
620 | { |
731 | { |
|
|
732 | ev_now = ev_time (); |
|
|
733 | |
621 | 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)) |
622 | { |
735 | { |
623 | periodics_reschedule (ev_now - now); |
736 | periodics_reschedule (ev_now - now); |
624 | |
737 | |
625 | /* 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 */ |
626 | for (i = 0; i < timercnt; ++i) |
739 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
639 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
752 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
640 | |
753 | |
641 | do |
754 | do |
642 | { |
755 | { |
643 | /* queue check watchers (and execute them) */ |
756 | /* queue check watchers (and execute them) */ |
644 | if (preparecnt) |
757 | if (expect_false (preparecnt)) |
645 | { |
758 | { |
646 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
759 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
647 | call_pending (); |
760 | call_pending (); |
648 | } |
761 | } |
649 | |
762 | |
… | |
… | |
652 | |
765 | |
653 | /* calculate blocking time */ |
766 | /* calculate blocking time */ |
654 | |
767 | |
655 | /* 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 |
656 | 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 | { |
657 | ev_now = ev_time (); |
776 | ev_now = ev_time (); |
|
|
777 | now = ev_now; |
|
|
778 | } |
658 | |
779 | |
659 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
780 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
660 | block = 0.; |
781 | block = 0.; |
661 | else |
782 | else |
662 | { |
783 | { |
663 | block = MAX_BLOCKTIME; |
784 | block = MAX_BLOCKTIME; |
664 | |
785 | |
665 | if (timercnt) |
786 | if (timercnt) |
666 | { |
787 | { |
667 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
788 | ev_tstamp to = timers [0]->at - now + method_fudge; |
668 | if (block > to) block = to; |
789 | if (block > to) block = to; |
669 | } |
790 | } |
670 | |
791 | |
671 | if (periodiccnt) |
792 | if (periodiccnt) |
672 | { |
793 | { |
… | |
… | |
725 | head = &(*head)->next; |
846 | head = &(*head)->next; |
726 | } |
847 | } |
727 | } |
848 | } |
728 | |
849 | |
729 | static void |
850 | static void |
730 | ev_clear (W w) |
851 | ev_clear_pending (W w) |
731 | { |
852 | { |
732 | if (w->pending) |
853 | if (w->pending) |
733 | { |
854 | { |
734 | pendings [w->pending - 1].w = 0; |
855 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
735 | w->pending = 0; |
856 | w->pending = 0; |
736 | } |
857 | } |
737 | } |
858 | } |
738 | |
859 | |
739 | static void |
860 | static void |
… | |
… | |
751 | /*****************************************************************************/ |
872 | /*****************************************************************************/ |
752 | |
873 | |
753 | void |
874 | void |
754 | ev_io_start (struct ev_io *w) |
875 | ev_io_start (struct ev_io *w) |
755 | { |
876 | { |
|
|
877 | int fd = w->fd; |
|
|
878 | |
756 | if (ev_is_active (w)) |
879 | if (ev_is_active (w)) |
757 | return; |
880 | return; |
758 | |
881 | |
759 | int fd = w->fd; |
882 | assert (("ev_io_start called with negative fd", fd >= 0)); |
760 | |
883 | |
761 | ev_start ((W)w, 1); |
884 | ev_start ((W)w, 1); |
762 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
885 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
763 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
886 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
764 | |
887 | |
… | |
… | |
766 | } |
889 | } |
767 | |
890 | |
768 | void |
891 | void |
769 | ev_io_stop (struct ev_io *w) |
892 | ev_io_stop (struct ev_io *w) |
770 | { |
893 | { |
771 | ev_clear ((W)w); |
894 | ev_clear_pending ((W)w); |
772 | if (!ev_is_active (w)) |
895 | if (!ev_is_active (w)) |
773 | return; |
896 | return; |
774 | |
897 | |
775 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
898 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
776 | ev_stop ((W)w); |
899 | ev_stop ((W)w); |
… | |
… | |
784 | if (ev_is_active (w)) |
907 | if (ev_is_active (w)) |
785 | return; |
908 | return; |
786 | |
909 | |
787 | w->at += now; |
910 | w->at += now; |
788 | |
911 | |
789 | 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.)); |
790 | |
913 | |
791 | ev_start ((W)w, ++timercnt); |
914 | ev_start ((W)w, ++timercnt); |
792 | array_needsize (timers, timermax, timercnt, ); |
915 | array_needsize (timers, timermax, timercnt, ); |
793 | timers [timercnt - 1] = w; |
916 | timers [timercnt - 1] = w; |
794 | upheap ((WT *)timers, timercnt - 1); |
917 | upheap ((WT *)timers, timercnt - 1); |
795 | } |
918 | } |
796 | |
919 | |
797 | void |
920 | void |
798 | ev_timer_stop (struct ev_timer *w) |
921 | ev_timer_stop (struct ev_timer *w) |
799 | { |
922 | { |
800 | ev_clear ((W)w); |
923 | ev_clear_pending ((W)w); |
801 | if (!ev_is_active (w)) |
924 | if (!ev_is_active (w)) |
802 | return; |
925 | return; |
803 | |
926 | |
804 | if (w->active < timercnt--) |
927 | if (w->active < timercnt--) |
805 | { |
928 | { |
… | |
… | |
833 | ev_periodic_start (struct ev_periodic *w) |
956 | ev_periodic_start (struct ev_periodic *w) |
834 | { |
957 | { |
835 | if (ev_is_active (w)) |
958 | if (ev_is_active (w)) |
836 | return; |
959 | return; |
837 | |
960 | |
838 | 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.)); |
839 | |
962 | |
840 | /* 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 */ |
841 | if (w->interval) |
964 | if (w->interval) |
842 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
965 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
843 | |
966 | |
… | |
… | |
848 | } |
971 | } |
849 | |
972 | |
850 | void |
973 | void |
851 | ev_periodic_stop (struct ev_periodic *w) |
974 | ev_periodic_stop (struct ev_periodic *w) |
852 | { |
975 | { |
853 | ev_clear ((W)w); |
976 | ev_clear_pending ((W)w); |
854 | if (!ev_is_active (w)) |
977 | if (!ev_is_active (w)) |
855 | return; |
978 | return; |
856 | |
979 | |
857 | if (w->active < periodiccnt--) |
980 | if (w->active < periodiccnt--) |
858 | { |
981 | { |
… | |
… | |
866 | void |
989 | void |
867 | ev_signal_start (struct ev_signal *w) |
990 | ev_signal_start (struct ev_signal *w) |
868 | { |
991 | { |
869 | if (ev_is_active (w)) |
992 | if (ev_is_active (w)) |
870 | return; |
993 | return; |
|
|
994 | |
|
|
995 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
871 | |
996 | |
872 | ev_start ((W)w, 1); |
997 | ev_start ((W)w, 1); |
873 | array_needsize (signals, signalmax, w->signum, signals_init); |
998 | array_needsize (signals, signalmax, w->signum, signals_init); |
874 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
999 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
875 | |
1000 | |
… | |
… | |
884 | } |
1009 | } |
885 | |
1010 | |
886 | void |
1011 | void |
887 | ev_signal_stop (struct ev_signal *w) |
1012 | ev_signal_stop (struct ev_signal *w) |
888 | { |
1013 | { |
889 | ev_clear ((W)w); |
1014 | ev_clear_pending ((W)w); |
890 | if (!ev_is_active (w)) |
1015 | if (!ev_is_active (w)) |
891 | return; |
1016 | return; |
892 | |
1017 | |
893 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1018 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
894 | ev_stop ((W)w); |
1019 | ev_stop ((W)w); |
… | |
… | |
909 | } |
1034 | } |
910 | |
1035 | |
911 | void |
1036 | void |
912 | ev_idle_stop (struct ev_idle *w) |
1037 | ev_idle_stop (struct ev_idle *w) |
913 | { |
1038 | { |
914 | ev_clear ((W)w); |
1039 | ev_clear_pending ((W)w); |
915 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
916 | return; |
1041 | return; |
917 | |
1042 | |
918 | idles [w->active - 1] = idles [--idlecnt]; |
1043 | idles [w->active - 1] = idles [--idlecnt]; |
919 | ev_stop ((W)w); |
1044 | ev_stop ((W)w); |
… | |
… | |
931 | } |
1056 | } |
932 | |
1057 | |
933 | void |
1058 | void |
934 | ev_prepare_stop (struct ev_prepare *w) |
1059 | ev_prepare_stop (struct ev_prepare *w) |
935 | { |
1060 | { |
936 | ev_clear ((W)w); |
1061 | ev_clear_pending ((W)w); |
937 | if (ev_is_active (w)) |
1062 | if (ev_is_active (w)) |
938 | return; |
1063 | return; |
939 | |
1064 | |
940 | prepares [w->active - 1] = prepares [--preparecnt]; |
1065 | prepares [w->active - 1] = prepares [--preparecnt]; |
941 | ev_stop ((W)w); |
1066 | ev_stop ((W)w); |
… | |
… | |
953 | } |
1078 | } |
954 | |
1079 | |
955 | void |
1080 | void |
956 | ev_check_stop (struct ev_check *w) |
1081 | ev_check_stop (struct ev_check *w) |
957 | { |
1082 | { |
958 | ev_clear ((W)w); |
1083 | ev_clear_pending ((W)w); |
959 | if (ev_is_active (w)) |
1084 | if (ev_is_active (w)) |
960 | return; |
1085 | return; |
961 | |
1086 | |
962 | checks [w->active - 1] = checks [--checkcnt]; |
1087 | checks [w->active - 1] = checks [--checkcnt]; |
963 | ev_stop ((W)w); |
1088 | ev_stop ((W)w); |
… | |
… | |
974 | } |
1099 | } |
975 | |
1100 | |
976 | void |
1101 | void |
977 | ev_child_stop (struct ev_child *w) |
1102 | ev_child_stop (struct ev_child *w) |
978 | { |
1103 | { |
979 | ev_clear ((W)w); |
1104 | ev_clear_pending ((W)w); |
980 | if (ev_is_active (w)) |
1105 | if (ev_is_active (w)) |
981 | return; |
1106 | return; |
982 | |
1107 | |
983 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1108 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
984 | ev_stop ((W)w); |
1109 | ev_stop ((W)w); |