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