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 |
… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
30 | */ |
|
|
31 | #if EV_USE_CONFIG_H |
|
|
32 | # include "config.h" |
|
|
33 | #endif |
29 | |
34 | |
30 | #include <math.h> |
35 | #include <math.h> |
31 | #include <stdlib.h> |
36 | #include <stdlib.h> |
32 | #include <unistd.h> |
37 | #include <unistd.h> |
33 | #include <fcntl.h> |
38 | #include <fcntl.h> |
… | |
… | |
41 | #include <sys/types.h> |
46 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
48 | #include <sys/time.h> |
44 | #include <time.h> |
49 | #include <time.h> |
45 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
46 | #ifndef HAVE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
|
|
54 | # define EV_USE_MONOTONIC 1 |
|
|
55 | #endif |
|
|
56 | |
|
|
57 | #ifndef EV_USE_SELECT |
|
|
58 | # define EV_USE_SELECT 1 |
|
|
59 | #endif |
|
|
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 | |
|
|
65 | #ifndef EV_USE_EPOLL |
|
|
66 | # define EV_USE_EPOLL 0 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | #ifndef EV_USE_REALTIME |
|
|
70 | # define EV_USE_REALTIME 1 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | /**/ |
|
|
74 | |
47 | # ifdef CLOCK_MONOTONIC |
75 | #ifndef CLOCK_MONOTONIC |
|
|
76 | # undef EV_USE_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
77 | # define EV_USE_MONOTONIC 0 |
49 | # endif |
78 | #endif |
50 | #endif |
|
|
51 | |
79 | |
52 | #ifndef HAVE_SELECT |
|
|
53 | # define HAVE_SELECT 1 |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef HAVE_EPOLL |
|
|
57 | # define HAVE_EPOLL 0 |
|
|
58 | #endif |
|
|
59 | |
|
|
60 | #ifndef HAVE_REALTIME |
80 | #ifndef CLOCK_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
81 | # undef EV_USE_REALTIME |
|
|
82 | # define EV_USE_REALTIME 0 |
62 | #endif |
83 | #endif |
|
|
84 | |
|
|
85 | /**/ |
63 | |
86 | |
64 | #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) */ |
65 | #define MAX_BLOCKTIME 60. |
88 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
66 | #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 */ |
67 | |
91 | |
68 | #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) |
69 | |
107 | |
70 | typedef struct ev_watcher *W; |
108 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
109 | typedef struct ev_watcher_list *WL; |
72 | typedef struct ev_watcher_time *WT; |
110 | typedef struct ev_watcher_time *WT; |
73 | |
111 | |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
112 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
75 | ev_tstamp ev_now; |
113 | ev_tstamp ev_now; |
76 | int ev_method; |
114 | int ev_method; |
77 | |
115 | |
78 | static int have_monotonic; /* runtime */ |
116 | static int have_monotonic; /* runtime */ |
79 | |
117 | |
… | |
… | |
84 | /*****************************************************************************/ |
122 | /*****************************************************************************/ |
85 | |
123 | |
86 | ev_tstamp |
124 | ev_tstamp |
87 | ev_time (void) |
125 | ev_time (void) |
88 | { |
126 | { |
89 | #if HAVE_REALTIME |
127 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
128 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
129 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
130 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
131 | #else |
94 | struct timeval tv; |
132 | struct timeval tv; |
… | |
… | |
98 | } |
136 | } |
99 | |
137 | |
100 | static ev_tstamp |
138 | static ev_tstamp |
101 | get_clock (void) |
139 | get_clock (void) |
102 | { |
140 | { |
103 | #if HAVE_MONOTONIC |
141 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
142 | if (expect_true (have_monotonic)) |
105 | { |
143 | { |
106 | struct timespec ts; |
144 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
145 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
146 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
147 | } |
110 | #endif |
148 | #endif |
111 | |
149 | |
112 | return ev_time (); |
150 | return ev_time (); |
113 | } |
151 | } |
114 | |
152 | |
|
|
153 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
154 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
155 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
156 | if (expect_false ((cnt) > cur)) \ |
117 | { \ |
157 | { \ |
118 | int newcnt = cur; \ |
158 | int newcnt = cur; \ |
119 | do \ |
159 | do \ |
120 | { \ |
160 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
161 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
162 | } \ |
123 | while ((cnt) > newcnt); \ |
163 | while ((cnt) > newcnt); \ |
124 | \ |
164 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
165 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
166 | init (base + cur, newcnt - cur); \ |
… | |
… | |
130 | /*****************************************************************************/ |
170 | /*****************************************************************************/ |
131 | |
171 | |
132 | typedef struct |
172 | typedef struct |
133 | { |
173 | { |
134 | struct ev_io *head; |
174 | struct ev_io *head; |
135 | int events; |
175 | unsigned char events; |
|
|
176 | unsigned char reify; |
136 | } ANFD; |
177 | } ANFD; |
137 | |
178 | |
138 | static ANFD *anfds; |
179 | static ANFD *anfds; |
139 | static int anfdmax; |
180 | static int anfdmax; |
140 | |
181 | |
… | |
… | |
143 | { |
184 | { |
144 | while (count--) |
185 | while (count--) |
145 | { |
186 | { |
146 | base->head = 0; |
187 | base->head = 0; |
147 | base->events = EV_NONE; |
188 | base->events = EV_NONE; |
|
|
189 | base->reify = 0; |
|
|
190 | |
148 | ++base; |
191 | ++base; |
149 | } |
192 | } |
150 | } |
193 | } |
151 | |
194 | |
152 | typedef struct |
195 | typedef struct |
153 | { |
196 | { |
154 | W w; |
197 | W w; |
155 | int events; |
198 | int events; |
156 | } ANPENDING; |
199 | } ANPENDING; |
157 | |
200 | |
158 | static ANPENDING *pendings; |
201 | static ANPENDING *pendings [NUMPRI]; |
159 | static int pendingmax, pendingcnt; |
202 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
160 | |
203 | |
161 | static void |
204 | static void |
162 | event (W w, int events) |
205 | event (W w, int events) |
163 | { |
206 | { |
164 | if (w->active) |
207 | if (w->pending) |
165 | { |
208 | { |
166 | w->pending = ++pendingcnt; |
|
|
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
|
|
168 | pendings [pendingcnt - 1].w = w; |
|
|
169 | pendings [pendingcnt - 1].events = events; |
209 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
210 | return; |
170 | } |
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; |
171 | } |
217 | } |
172 | |
218 | |
173 | static void |
219 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
220 | queue_events (W *events, int eventcnt, int type) |
175 | { |
221 | { |
… | |
… | |
213 | int events = 0; |
259 | int events = 0; |
214 | |
260 | |
215 | for (w = anfd->head; w; w = w->next) |
261 | for (w = anfd->head; w; w = w->next) |
216 | events |= w->events; |
262 | events |= w->events; |
217 | |
263 | |
218 | anfd->events &= ~EV_REIFY; |
264 | anfd->reify = 0; |
219 | |
265 | |
220 | if (anfd->events != events) |
266 | if (anfd->events != events) |
221 | { |
267 | { |
222 | method_modify (fd, anfd->events, events); |
268 | method_modify (fd, anfd->events, events); |
223 | anfd->events = events; |
269 | anfd->events = events; |
… | |
… | |
228 | } |
274 | } |
229 | |
275 | |
230 | static void |
276 | static void |
231 | fd_change (int fd) |
277 | fd_change (int fd) |
232 | { |
278 | { |
233 | if (anfds [fd].events & EV_REIFY) |
279 | if (anfds [fd].reify || fdchangecnt < 0) |
234 | return; |
280 | return; |
235 | |
281 | |
236 | anfds [fd].events |= EV_REIFY; |
282 | anfds [fd].reify = 1; |
237 | |
283 | |
238 | ++fdchangecnt; |
284 | ++fdchangecnt; |
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
285 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
240 | fdchanges [fdchangecnt - 1] = fd; |
286 | fdchanges [fdchangecnt - 1] = fd; |
241 | } |
287 | } |
242 | |
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 | |
243 | /* called on EBADF to verify fds */ |
302 | /* called on EBADF to verify fds */ |
244 | static void |
303 | static void |
245 | fd_recheck (void) |
304 | fd_ebadf (void) |
246 | { |
305 | { |
247 | int fd; |
306 | int fd; |
248 | |
307 | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
308 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
309 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
310 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | 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) |
253 | { |
322 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
323 | close (fd); |
255 | evio_stop (anfds [fd].head); |
324 | fd_kill (fd); |
|
|
325 | return; |
256 | } |
326 | } |
257 | } |
327 | } |
258 | |
328 | |
259 | /*****************************************************************************/ |
329 | /*****************************************************************************/ |
260 | |
330 | |
261 | static struct ev_timer **timers; |
331 | static struct ev_timer **timers; |
… | |
… | |
308 | /*****************************************************************************/ |
378 | /*****************************************************************************/ |
309 | |
379 | |
310 | typedef struct |
380 | typedef struct |
311 | { |
381 | { |
312 | struct ev_signal *head; |
382 | struct ev_signal *head; |
313 | sig_atomic_t gotsig; |
383 | sig_atomic_t volatile gotsig; |
314 | } ANSIG; |
384 | } ANSIG; |
315 | |
385 | |
316 | static ANSIG *signals; |
386 | static ANSIG *signals; |
317 | static int signalmax; |
387 | static int signalmax; |
318 | |
388 | |
319 | static int sigpipe [2]; |
389 | static int sigpipe [2]; |
320 | static sig_atomic_t gotsig; |
390 | static sig_atomic_t volatile gotsig; |
321 | static struct ev_io sigev; |
391 | static struct ev_io sigev; |
322 | |
392 | |
323 | static void |
393 | static void |
324 | signals_init (ANSIG *base, int count) |
394 | signals_init (ANSIG *base, int count) |
325 | { |
395 | { |
326 | while (count--) |
396 | while (count--) |
327 | { |
397 | { |
328 | base->head = 0; |
398 | base->head = 0; |
329 | base->gotsig = 0; |
399 | base->gotsig = 0; |
|
|
400 | |
330 | ++base; |
401 | ++base; |
331 | } |
402 | } |
332 | } |
403 | } |
333 | |
404 | |
334 | static void |
405 | static void |
… | |
… | |
337 | signals [signum - 1].gotsig = 1; |
408 | signals [signum - 1].gotsig = 1; |
338 | |
409 | |
339 | if (!gotsig) |
410 | if (!gotsig) |
340 | { |
411 | { |
341 | gotsig = 1; |
412 | gotsig = 1; |
342 | write (sigpipe [1], &gotsig, 1); |
413 | write (sigpipe [1], &signum, 1); |
343 | } |
414 | } |
344 | } |
415 | } |
345 | |
416 | |
346 | static void |
417 | static void |
347 | sigcb (struct ev_io *iow, int revents) |
418 | sigcb (struct ev_io *iow, int revents) |
348 | { |
419 | { |
349 | struct ev_signal *w; |
420 | struct ev_signal *w; |
350 | int sig; |
421 | int signum; |
351 | |
422 | |
|
|
423 | read (sigpipe [0], &revents, 1); |
352 | gotsig = 0; |
424 | gotsig = 0; |
353 | read (sigpipe [0], &revents, 1); |
|
|
354 | |
425 | |
355 | for (sig = signalmax; sig--; ) |
426 | for (signum = signalmax; signum--; ) |
356 | if (signals [sig].gotsig) |
427 | if (signals [signum].gotsig) |
357 | { |
428 | { |
358 | signals [sig].gotsig = 0; |
429 | signals [signum].gotsig = 0; |
359 | |
430 | |
360 | for (w = signals [sig].head; w; w = w->next) |
431 | for (w = signals [signum].head; w; w = w->next) |
361 | event ((W)w, EV_SIGNAL); |
432 | event ((W)w, EV_SIGNAL); |
362 | } |
433 | } |
363 | } |
434 | } |
364 | |
435 | |
365 | static void |
436 | static void |
… | |
… | |
370 | |
441 | |
371 | /* rather than sort out wether we really need nb, set it */ |
442 | /* rather than sort out wether we really need nb, set it */ |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
443 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
444 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
374 | |
445 | |
375 | evio_set (&sigev, sigpipe [0], EV_READ); |
446 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
376 | evio_start (&sigev); |
447 | ev_io_start (&sigev); |
377 | } |
448 | } |
378 | |
449 | |
379 | /*****************************************************************************/ |
450 | /*****************************************************************************/ |
380 | |
451 | |
381 | static struct ev_idle **idles; |
452 | static struct ev_idle **idles; |
… | |
… | |
402 | struct ev_child *w; |
473 | struct ev_child *w; |
403 | int pid, status; |
474 | int pid, status; |
404 | |
475 | |
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
476 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
477 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
407 | if (w->pid == pid || w->pid == -1) |
478 | if (w->pid == pid || !w->pid) |
408 | { |
479 | { |
409 | w->status = status; |
480 | w->status = status; |
410 | event ((W)w, EV_CHILD); |
481 | event ((W)w, EV_CHILD); |
411 | } |
482 | } |
412 | } |
483 | } |
413 | |
484 | |
414 | /*****************************************************************************/ |
485 | /*****************************************************************************/ |
415 | |
486 | |
416 | #if HAVE_EPOLL |
487 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
488 | # include "ev_epoll.c" |
418 | #endif |
489 | #endif |
|
|
490 | #if EV_USE_POLL |
|
|
491 | # include "ev_poll.c" |
|
|
492 | #endif |
419 | #if HAVE_SELECT |
493 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
494 | # include "ev_select.c" |
421 | #endif |
495 | #endif |
422 | |
496 | |
423 | int |
497 | int |
424 | ev_version_major (void) |
498 | ev_version_major (void) |
… | |
… | |
430 | ev_version_minor (void) |
504 | ev_version_minor (void) |
431 | { |
505 | { |
432 | return EV_VERSION_MINOR; |
506 | return EV_VERSION_MINOR; |
433 | } |
507 | } |
434 | |
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 | |
435 | int ev_init (int flags) |
517 | int ev_init (int methods) |
436 | { |
518 | { |
437 | if (!ev_method) |
519 | if (!ev_method) |
438 | { |
520 | { |
439 | #if HAVE_MONOTONIC |
521 | #if EV_USE_MONOTONIC |
440 | { |
522 | { |
441 | struct timespec ts; |
523 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
524 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
525 | have_monotonic = 1; |
444 | } |
526 | } |
445 | #endif |
527 | #endif |
446 | |
528 | |
447 | ev_now = ev_time (); |
529 | ev_now = ev_time (); |
448 | now = get_clock (); |
530 | now = get_clock (); |
|
|
531 | now_floor = now; |
449 | diff = ev_now - now; |
532 | diff = ev_now - now; |
450 | |
533 | |
451 | if (pipe (sigpipe)) |
534 | if (pipe (sigpipe)) |
452 | return 0; |
535 | return 0; |
453 | |
536 | |
|
|
537 | if (methods == EVMETHOD_AUTO) |
|
|
538 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
539 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
540 | else |
454 | ev_method = EVMETHOD_NONE; |
541 | methods = EVMETHOD_ANY; |
|
|
542 | |
|
|
543 | ev_method = 0; |
455 | #if HAVE_EPOLL |
544 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
545 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
457 | #endif |
546 | #endif |
|
|
547 | #if EV_USE_POLL |
|
|
548 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
|
|
549 | #endif |
458 | #if HAVE_SELECT |
550 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
551 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
460 | #endif |
552 | #endif |
461 | |
553 | |
462 | if (ev_method) |
554 | if (ev_method) |
463 | { |
555 | { |
464 | evw_init (&sigev, sigcb); |
556 | ev_watcher_init (&sigev, sigcb); |
465 | siginit (); |
557 | siginit (); |
466 | |
558 | |
467 | evsignal_init (&childev, childcb, SIGCHLD); |
559 | ev_signal_init (&childev, childcb, SIGCHLD); |
468 | evsignal_start (&childev); |
560 | ev_signal_start (&childev); |
469 | } |
561 | } |
470 | } |
562 | } |
471 | |
563 | |
472 | return ev_method; |
564 | return ev_method; |
473 | } |
565 | } |
474 | |
566 | |
475 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
476 | |
568 | |
477 | void |
569 | void |
478 | ev_prefork (void) |
570 | ev_fork_prepare (void) |
479 | { |
571 | { |
480 | /* nop */ |
572 | /* nop */ |
481 | } |
573 | } |
482 | |
574 | |
483 | void |
575 | void |
484 | ev_postfork_parent (void) |
576 | ev_fork_parent (void) |
485 | { |
577 | { |
486 | /* nop */ |
578 | /* nop */ |
487 | } |
579 | } |
488 | |
580 | |
489 | void |
581 | void |
490 | ev_postfork_child (void) |
582 | ev_fork_child (void) |
491 | { |
583 | { |
492 | #if HAVE_EPOLL |
584 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
585 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
586 | epoll_postfork_child (); |
495 | #endif |
587 | #endif |
496 | |
588 | |
497 | evio_stop (&sigev); |
589 | ev_io_stop (&sigev); |
498 | close (sigpipe [0]); |
590 | close (sigpipe [0]); |
499 | close (sigpipe [1]); |
591 | close (sigpipe [1]); |
500 | pipe (sigpipe); |
592 | pipe (sigpipe); |
501 | siginit (); |
593 | siginit (); |
502 | } |
594 | } |
… | |
… | |
504 | /*****************************************************************************/ |
596 | /*****************************************************************************/ |
505 | |
597 | |
506 | static void |
598 | static void |
507 | call_pending (void) |
599 | call_pending (void) |
508 | { |
600 | { |
|
|
601 | int pri; |
|
|
602 | |
|
|
603 | for (pri = NUMPRI; pri--; ) |
509 | while (pendingcnt) |
604 | while (pendingcnt [pri]) |
510 | { |
605 | { |
511 | ANPENDING *p = pendings + --pendingcnt; |
606 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
512 | |
607 | |
513 | if (p->w) |
608 | if (p->w) |
514 | { |
609 | { |
515 | p->w->pending = 0; |
610 | p->w->pending = 0; |
516 | p->w->cb (p->w, p->events); |
611 | p->w->cb (p->w, p->events); |
517 | } |
612 | } |
518 | } |
613 | } |
519 | } |
614 | } |
520 | |
615 | |
521 | static void |
616 | static void |
522 | timers_reify (void) |
617 | timers_reify (void) |
523 | { |
618 | { |
524 | while (timercnt && timers [0]->at <= now) |
619 | while (timercnt && timers [0]->at <= now) |
525 | { |
620 | { |
526 | struct ev_timer *w = timers [0]; |
621 | struct ev_timer *w = timers [0]; |
527 | |
|
|
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
622 | |
530 | /* first reschedule or stop timer */ |
623 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
624 | if (w->repeat) |
532 | { |
625 | { |
|
|
626 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
627 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
628 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
629 | } |
537 | else |
630 | else |
538 | evtimer_stop (w); /* nonrepeating: stop timer */ |
631 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
632 | |
|
|
633 | event ((W)w, EV_TIMEOUT); |
539 | } |
634 | } |
540 | } |
635 | } |
541 | |
636 | |
542 | static void |
637 | static void |
543 | periodics_reify (void) |
638 | periodics_reify (void) |
… | |
… | |
548 | |
643 | |
549 | /* first reschedule or stop timer */ |
644 | /* first reschedule or stop timer */ |
550 | if (w->interval) |
645 | if (w->interval) |
551 | { |
646 | { |
552 | 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; |
553 | 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)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
649 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
650 | } |
556 | else |
651 | else |
557 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
652 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
653 | |
559 | event ((W)w, EV_TIMEOUT); |
654 | event ((W)w, EV_PERIODIC); |
560 | } |
655 | } |
561 | } |
656 | } |
562 | |
657 | |
563 | static void |
658 | static void |
564 | periodics_reschedule (ev_tstamp diff) |
659 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
574 | { |
669 | { |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
670 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
576 | |
671 | |
577 | if (fabs (diff) >= 1e-4) |
672 | if (fabs (diff) >= 1e-4) |
578 | { |
673 | { |
579 | evperiodic_stop (w); |
674 | ev_periodic_stop (w); |
580 | evperiodic_start (w); |
675 | ev_periodic_start (w); |
581 | |
676 | |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
677 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
583 | } |
678 | } |
584 | } |
679 | } |
585 | } |
680 | } |
586 | } |
681 | } |
587 | |
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 | |
588 | static void |
701 | static void |
589 | time_update (void) |
702 | time_update (void) |
590 | { |
703 | { |
591 | int i; |
704 | int i; |
592 | |
705 | |
593 | ev_now = ev_time (); |
706 | #if EV_USE_MONOTONIC |
594 | |
|
|
595 | if (have_monotonic) |
707 | if (expect_true (have_monotonic)) |
596 | { |
708 | { |
|
|
709 | if (time_update_monotonic ()) |
|
|
710 | { |
597 | ev_tstamp odiff = diff; |
711 | ev_tstamp odiff = diff; |
598 | |
712 | |
599 | 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 */ |
600 | { |
714 | { |
601 | now = get_clock (); |
|
|
602 | diff = ev_now - now; |
715 | diff = ev_now - now; |
603 | |
716 | |
604 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
717 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
605 | return; /* all is well */ |
718 | return; /* all is well */ |
606 | |
719 | |
607 | ev_now = ev_time (); |
720 | ev_now = ev_time (); |
|
|
721 | now = get_clock (); |
|
|
722 | now_floor = now; |
608 | } |
723 | } |
609 | |
724 | |
610 | periodics_reschedule (diff - odiff); |
725 | periodics_reschedule (diff - odiff); |
611 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
726 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
727 | } |
612 | } |
728 | } |
613 | else |
729 | else |
|
|
730 | #endif |
614 | { |
731 | { |
|
|
732 | ev_now = ev_time (); |
|
|
733 | |
615 | 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)) |
616 | { |
735 | { |
617 | periodics_reschedule (ev_now - now); |
736 | periodics_reschedule (ev_now - now); |
618 | |
737 | |
619 | /* 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 */ |
620 | for (i = 0; i < timercnt; ++i) |
739 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
752 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
634 | |
753 | |
635 | do |
754 | do |
636 | { |
755 | { |
637 | /* queue check watchers (and execute them) */ |
756 | /* queue check watchers (and execute them) */ |
638 | if (preparecnt) |
757 | if (expect_false (preparecnt)) |
639 | { |
758 | { |
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
759 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
641 | call_pending (); |
760 | call_pending (); |
642 | } |
761 | } |
643 | |
762 | |
… | |
… | |
646 | |
765 | |
647 | /* calculate blocking time */ |
766 | /* calculate blocking time */ |
648 | |
767 | |
649 | /* 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 |
650 | 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 | { |
651 | ev_now = ev_time (); |
776 | ev_now = ev_time (); |
|
|
777 | now = ev_now; |
|
|
778 | } |
652 | |
779 | |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
780 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
654 | block = 0.; |
781 | block = 0.; |
655 | else |
782 | else |
656 | { |
783 | { |
657 | block = MAX_BLOCKTIME; |
784 | block = MAX_BLOCKTIME; |
658 | |
785 | |
659 | if (timercnt) |
786 | if (timercnt) |
660 | { |
787 | { |
661 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
788 | ev_tstamp to = timers [0]->at - now + method_fudge; |
662 | if (block > to) block = to; |
789 | if (block > to) block = to; |
663 | } |
790 | } |
664 | |
791 | |
665 | if (periodiccnt) |
792 | if (periodiccnt) |
666 | { |
793 | { |
… | |
… | |
719 | head = &(*head)->next; |
846 | head = &(*head)->next; |
720 | } |
847 | } |
721 | } |
848 | } |
722 | |
849 | |
723 | static void |
850 | static void |
724 | ev_clear (W w) |
851 | ev_clear_pending (W w) |
725 | { |
852 | { |
726 | if (w->pending) |
853 | if (w->pending) |
727 | { |
854 | { |
728 | pendings [w->pending - 1].w = 0; |
855 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
729 | w->pending = 0; |
856 | w->pending = 0; |
730 | } |
857 | } |
731 | } |
858 | } |
732 | |
859 | |
733 | static void |
860 | static void |
… | |
… | |
743 | } |
870 | } |
744 | |
871 | |
745 | /*****************************************************************************/ |
872 | /*****************************************************************************/ |
746 | |
873 | |
747 | void |
874 | void |
748 | evio_start (struct ev_io *w) |
875 | ev_io_start (struct ev_io *w) |
749 | { |
876 | { |
|
|
877 | int fd = w->fd; |
|
|
878 | |
750 | if (ev_is_active (w)) |
879 | if (ev_is_active (w)) |
751 | return; |
880 | return; |
752 | |
881 | |
753 | int fd = w->fd; |
882 | assert (("ev_io_start called with negative fd", fd >= 0)); |
754 | |
883 | |
755 | ev_start ((W)w, 1); |
884 | ev_start ((W)w, 1); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
885 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
886 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
758 | |
887 | |
759 | fd_change (fd); |
888 | fd_change (fd); |
760 | } |
889 | } |
761 | |
890 | |
762 | void |
891 | void |
763 | evio_stop (struct ev_io *w) |
892 | ev_io_stop (struct ev_io *w) |
764 | { |
893 | { |
765 | ev_clear ((W)w); |
894 | ev_clear_pending ((W)w); |
766 | if (!ev_is_active (w)) |
895 | if (!ev_is_active (w)) |
767 | return; |
896 | return; |
768 | |
897 | |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
898 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
770 | ev_stop ((W)w); |
899 | ev_stop ((W)w); |
771 | |
900 | |
772 | fd_change (w->fd); |
901 | fd_change (w->fd); |
773 | } |
902 | } |
774 | |
903 | |
775 | void |
904 | void |
776 | evtimer_start (struct ev_timer *w) |
905 | ev_timer_start (struct ev_timer *w) |
777 | { |
906 | { |
778 | if (ev_is_active (w)) |
907 | if (ev_is_active (w)) |
779 | return; |
908 | return; |
780 | |
909 | |
781 | w->at += now; |
910 | w->at += now; |
782 | |
911 | |
783 | 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.)); |
784 | |
913 | |
785 | ev_start ((W)w, ++timercnt); |
914 | ev_start ((W)w, ++timercnt); |
786 | array_needsize (timers, timermax, timercnt, ); |
915 | array_needsize (timers, timermax, timercnt, ); |
787 | timers [timercnt - 1] = w; |
916 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
917 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
918 | } |
790 | |
919 | |
791 | void |
920 | void |
792 | evtimer_stop (struct ev_timer *w) |
921 | ev_timer_stop (struct ev_timer *w) |
793 | { |
922 | { |
794 | ev_clear ((W)w); |
923 | ev_clear_pending ((W)w); |
795 | if (!ev_is_active (w)) |
924 | if (!ev_is_active (w)) |
796 | return; |
925 | return; |
797 | |
926 | |
798 | if (w->active < timercnt--) |
927 | if (w->active < timercnt--) |
799 | { |
928 | { |
… | |
… | |
805 | |
934 | |
806 | ev_stop ((W)w); |
935 | ev_stop ((W)w); |
807 | } |
936 | } |
808 | |
937 | |
809 | void |
938 | void |
810 | evtimer_again (struct ev_timer *w) |
939 | ev_timer_again (struct ev_timer *w) |
811 | { |
940 | { |
812 | if (ev_is_active (w)) |
941 | if (ev_is_active (w)) |
813 | { |
942 | { |
814 | if (w->repeat) |
943 | if (w->repeat) |
815 | { |
944 | { |
816 | w->at = now + w->repeat; |
945 | w->at = now + w->repeat; |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
946 | downheap ((WT *)timers, timercnt, w->active - 1); |
818 | } |
947 | } |
819 | else |
948 | else |
820 | evtimer_stop (w); |
949 | ev_timer_stop (w); |
821 | } |
950 | } |
822 | else if (w->repeat) |
951 | else if (w->repeat) |
823 | evtimer_start (w); |
952 | ev_timer_start (w); |
824 | } |
953 | } |
825 | |
954 | |
826 | void |
955 | void |
827 | evperiodic_start (struct ev_periodic *w) |
956 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
957 | { |
829 | if (ev_is_active (w)) |
958 | if (ev_is_active (w)) |
830 | return; |
959 | return; |
831 | |
960 | |
832 | 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.)); |
833 | |
962 | |
834 | /* 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 */ |
835 | if (w->interval) |
964 | if (w->interval) |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
965 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
837 | |
966 | |
… | |
… | |
840 | periodics [periodiccnt - 1] = w; |
969 | periodics [periodiccnt - 1] = w; |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
970 | upheap ((WT *)periodics, periodiccnt - 1); |
842 | } |
971 | } |
843 | |
972 | |
844 | void |
973 | void |
845 | evperiodic_stop (struct ev_periodic *w) |
974 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
975 | { |
847 | ev_clear ((W)w); |
976 | ev_clear_pending ((W)w); |
848 | if (!ev_is_active (w)) |
977 | if (!ev_is_active (w)) |
849 | return; |
978 | return; |
850 | |
979 | |
851 | if (w->active < periodiccnt--) |
980 | if (w->active < periodiccnt--) |
852 | { |
981 | { |
… | |
… | |
856 | |
985 | |
857 | ev_stop ((W)w); |
986 | ev_stop ((W)w); |
858 | } |
987 | } |
859 | |
988 | |
860 | void |
989 | void |
861 | evsignal_start (struct ev_signal *w) |
990 | ev_signal_start (struct ev_signal *w) |
862 | { |
991 | { |
863 | if (ev_is_active (w)) |
992 | if (ev_is_active (w)) |
864 | return; |
993 | return; |
|
|
994 | |
|
|
995 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
865 | |
996 | |
866 | ev_start ((W)w, 1); |
997 | ev_start ((W)w, 1); |
867 | array_needsize (signals, signalmax, w->signum, signals_init); |
998 | array_needsize (signals, signalmax, w->signum, signals_init); |
868 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
999 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
869 | |
1000 | |
… | |
… | |
876 | sigaction (w->signum, &sa, 0); |
1007 | sigaction (w->signum, &sa, 0); |
877 | } |
1008 | } |
878 | } |
1009 | } |
879 | |
1010 | |
880 | void |
1011 | void |
881 | evsignal_stop (struct ev_signal *w) |
1012 | ev_signal_stop (struct ev_signal *w) |
882 | { |
1013 | { |
883 | ev_clear ((W)w); |
1014 | ev_clear_pending ((W)w); |
884 | if (!ev_is_active (w)) |
1015 | if (!ev_is_active (w)) |
885 | return; |
1016 | return; |
886 | |
1017 | |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1018 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | ev_stop ((W)w); |
1019 | ev_stop ((W)w); |
889 | |
1020 | |
890 | if (!signals [w->signum - 1].head) |
1021 | if (!signals [w->signum - 1].head) |
891 | signal (w->signum, SIG_DFL); |
1022 | signal (w->signum, SIG_DFL); |
892 | } |
1023 | } |
893 | |
1024 | |
|
|
1025 | void |
894 | void evidle_start (struct ev_idle *w) |
1026 | ev_idle_start (struct ev_idle *w) |
895 | { |
1027 | { |
896 | if (ev_is_active (w)) |
1028 | if (ev_is_active (w)) |
897 | return; |
1029 | return; |
898 | |
1030 | |
899 | ev_start ((W)w, ++idlecnt); |
1031 | ev_start ((W)w, ++idlecnt); |
900 | array_needsize (idles, idlemax, idlecnt, ); |
1032 | array_needsize (idles, idlemax, idlecnt, ); |
901 | idles [idlecnt - 1] = w; |
1033 | idles [idlecnt - 1] = w; |
902 | } |
1034 | } |
903 | |
1035 | |
|
|
1036 | void |
904 | void evidle_stop (struct ev_idle *w) |
1037 | ev_idle_stop (struct ev_idle *w) |
905 | { |
1038 | { |
906 | ev_clear ((W)w); |
1039 | ev_clear_pending ((W)w); |
907 | if (ev_is_active (w)) |
1040 | if (ev_is_active (w)) |
908 | return; |
1041 | return; |
909 | |
1042 | |
910 | idles [w->active - 1] = idles [--idlecnt]; |
1043 | idles [w->active - 1] = idles [--idlecnt]; |
911 | ev_stop ((W)w); |
1044 | ev_stop ((W)w); |
912 | } |
1045 | } |
913 | |
1046 | |
|
|
1047 | void |
914 | void evprepare_start (struct ev_prepare *w) |
1048 | ev_prepare_start (struct ev_prepare *w) |
915 | { |
1049 | { |
916 | if (ev_is_active (w)) |
1050 | if (ev_is_active (w)) |
917 | return; |
1051 | return; |
918 | |
1052 | |
919 | ev_start ((W)w, ++preparecnt); |
1053 | ev_start ((W)w, ++preparecnt); |
920 | array_needsize (prepares, preparemax, preparecnt, ); |
1054 | array_needsize (prepares, preparemax, preparecnt, ); |
921 | prepares [preparecnt - 1] = w; |
1055 | prepares [preparecnt - 1] = w; |
922 | } |
1056 | } |
923 | |
1057 | |
|
|
1058 | void |
924 | void evprepare_stop (struct ev_prepare *w) |
1059 | ev_prepare_stop (struct ev_prepare *w) |
925 | { |
1060 | { |
926 | ev_clear ((W)w); |
1061 | ev_clear_pending ((W)w); |
927 | if (ev_is_active (w)) |
1062 | if (ev_is_active (w)) |
928 | return; |
1063 | return; |
929 | |
1064 | |
930 | prepares [w->active - 1] = prepares [--preparecnt]; |
1065 | prepares [w->active - 1] = prepares [--preparecnt]; |
931 | ev_stop ((W)w); |
1066 | ev_stop ((W)w); |
932 | } |
1067 | } |
933 | |
1068 | |
|
|
1069 | void |
934 | void evcheck_start (struct ev_check *w) |
1070 | ev_check_start (struct ev_check *w) |
935 | { |
1071 | { |
936 | if (ev_is_active (w)) |
1072 | if (ev_is_active (w)) |
937 | return; |
1073 | return; |
938 | |
1074 | |
939 | ev_start ((W)w, ++checkcnt); |
1075 | ev_start ((W)w, ++checkcnt); |
940 | array_needsize (checks, checkmax, checkcnt, ); |
1076 | array_needsize (checks, checkmax, checkcnt, ); |
941 | checks [checkcnt - 1] = w; |
1077 | checks [checkcnt - 1] = w; |
942 | } |
1078 | } |
943 | |
1079 | |
|
|
1080 | void |
944 | void evcheck_stop (struct ev_check *w) |
1081 | ev_check_stop (struct ev_check *w) |
945 | { |
1082 | { |
946 | ev_clear ((W)w); |
1083 | ev_clear_pending ((W)w); |
947 | if (ev_is_active (w)) |
1084 | if (ev_is_active (w)) |
948 | return; |
1085 | return; |
949 | |
1086 | |
950 | checks [w->active - 1] = checks [--checkcnt]; |
1087 | checks [w->active - 1] = checks [--checkcnt]; |
951 | ev_stop ((W)w); |
1088 | ev_stop ((W)w); |
952 | } |
1089 | } |
953 | |
1090 | |
|
|
1091 | void |
954 | void evchild_start (struct ev_child *w) |
1092 | ev_child_start (struct ev_child *w) |
955 | { |
1093 | { |
956 | if (ev_is_active (w)) |
1094 | if (ev_is_active (w)) |
957 | return; |
1095 | return; |
958 | |
1096 | |
959 | ev_start ((W)w, 1); |
1097 | ev_start ((W)w, 1); |
960 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1098 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
961 | } |
1099 | } |
962 | |
1100 | |
|
|
1101 | void |
963 | void evchild_stop (struct ev_child *w) |
1102 | ev_child_stop (struct ev_child *w) |
964 | { |
1103 | { |
965 | ev_clear ((W)w); |
1104 | ev_clear_pending ((W)w); |
966 | if (ev_is_active (w)) |
1105 | if (ev_is_active (w)) |
967 | return; |
1106 | return; |
968 | |
1107 | |
969 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1108 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
970 | ev_stop ((W)w); |
1109 | ev_stop ((W)w); |
… | |
… | |
984 | once_cb (struct ev_once *once, int revents) |
1123 | once_cb (struct ev_once *once, int revents) |
985 | { |
1124 | { |
986 | void (*cb)(int revents, void *arg) = once->cb; |
1125 | void (*cb)(int revents, void *arg) = once->cb; |
987 | void *arg = once->arg; |
1126 | void *arg = once->arg; |
988 | |
1127 | |
989 | evio_stop (&once->io); |
1128 | ev_io_stop (&once->io); |
990 | evtimer_stop (&once->to); |
1129 | ev_timer_stop (&once->to); |
991 | free (once); |
1130 | free (once); |
992 | |
1131 | |
993 | cb (revents, arg); |
1132 | cb (revents, arg); |
994 | } |
1133 | } |
995 | |
1134 | |
… | |
… | |
1009 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1148 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1010 | { |
1149 | { |
1011 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1150 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1012 | |
1151 | |
1013 | if (!once) |
1152 | if (!once) |
1014 | cb (EV_ERROR, arg); |
1153 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1015 | else |
1154 | else |
1016 | { |
1155 | { |
1017 | once->cb = cb; |
1156 | once->cb = cb; |
1018 | once->arg = arg; |
1157 | once->arg = arg; |
1019 | |
1158 | |
1020 | evw_init (&once->io, once_cb_io); |
1159 | ev_watcher_init (&once->io, once_cb_io); |
1021 | |
|
|
1022 | if (fd >= 0) |
1160 | if (fd >= 0) |
1023 | { |
1161 | { |
1024 | evio_set (&once->io, fd, events); |
1162 | ev_io_set (&once->io, fd, events); |
1025 | evio_start (&once->io); |
1163 | ev_io_start (&once->io); |
1026 | } |
1164 | } |
1027 | |
1165 | |
1028 | evw_init (&once->to, once_cb_to); |
1166 | ev_watcher_init (&once->to, once_cb_to); |
1029 | |
|
|
1030 | if (timeout >= 0.) |
1167 | if (timeout >= 0.) |
1031 | { |
1168 | { |
1032 | evtimer_set (&once->to, timeout, 0.); |
1169 | ev_timer_set (&once->to, timeout, 0.); |
1033 | evtimer_start (&once->to); |
1170 | ev_timer_start (&once->to); |
1034 | } |
1171 | } |
1035 | } |
1172 | } |
1036 | } |
1173 | } |
1037 | |
1174 | |
1038 | /*****************************************************************************/ |
1175 | /*****************************************************************************/ |
… | |
… | |
1049 | |
1186 | |
1050 | static void |
1187 | static void |
1051 | ocb (struct ev_timer *w, int revents) |
1188 | ocb (struct ev_timer *w, int revents) |
1052 | { |
1189 | { |
1053 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1190 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1054 | evtimer_stop (w); |
1191 | ev_timer_stop (w); |
1055 | evtimer_start (w); |
1192 | ev_timer_start (w); |
1056 | } |
1193 | } |
1057 | |
1194 | |
1058 | static void |
1195 | static void |
1059 | scb (struct ev_signal *w, int revents) |
1196 | scb (struct ev_signal *w, int revents) |
1060 | { |
1197 | { |
1061 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1198 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1062 | evio_stop (&wio); |
1199 | ev_io_stop (&wio); |
1063 | evio_start (&wio); |
1200 | ev_io_start (&wio); |
1064 | } |
1201 | } |
1065 | |
1202 | |
1066 | static void |
1203 | static void |
1067 | gcb (struct ev_signal *w, int revents) |
1204 | gcb (struct ev_signal *w, int revents) |
1068 | { |
1205 | { |
… | |
… | |
1072 | |
1209 | |
1073 | int main (void) |
1210 | int main (void) |
1074 | { |
1211 | { |
1075 | ev_init (0); |
1212 | ev_init (0); |
1076 | |
1213 | |
1077 | evio_init (&wio, sin_cb, 0, EV_READ); |
1214 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1078 | evio_start (&wio); |
1215 | ev_io_start (&wio); |
1079 | |
1216 | |
1080 | struct ev_timer t[10000]; |
1217 | struct ev_timer t[10000]; |
1081 | |
1218 | |
1082 | #if 0 |
1219 | #if 0 |
1083 | int i; |
1220 | int i; |
1084 | for (i = 0; i < 10000; ++i) |
1221 | for (i = 0; i < 10000; ++i) |
1085 | { |
1222 | { |
1086 | struct ev_timer *w = t + i; |
1223 | struct ev_timer *w = t + i; |
1087 | evw_init (w, ocb, i); |
1224 | ev_watcher_init (w, ocb, i); |
1088 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1225 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1089 | evtimer_start (w); |
1226 | ev_timer_start (w); |
1090 | if (drand48 () < 0.5) |
1227 | if (drand48 () < 0.5) |
1091 | evtimer_stop (w); |
1228 | ev_timer_stop (w); |
1092 | } |
1229 | } |
1093 | #endif |
1230 | #endif |
1094 | |
1231 | |
1095 | struct ev_timer t1; |
1232 | struct ev_timer t1; |
1096 | evtimer_init (&t1, ocb, 5, 10); |
1233 | ev_timer_init (&t1, ocb, 5, 10); |
1097 | evtimer_start (&t1); |
1234 | ev_timer_start (&t1); |
1098 | |
1235 | |
1099 | struct ev_signal sig; |
1236 | struct ev_signal sig; |
1100 | evsignal_init (&sig, scb, SIGQUIT); |
1237 | ev_signal_init (&sig, scb, SIGQUIT); |
1101 | evsignal_start (&sig); |
1238 | ev_signal_start (&sig); |
1102 | |
1239 | |
1103 | struct ev_check cw; |
1240 | struct ev_check cw; |
1104 | evcheck_init (&cw, gcb); |
1241 | ev_check_init (&cw, gcb); |
1105 | evcheck_start (&cw); |
1242 | ev_check_start (&cw); |
1106 | |
1243 | |
1107 | struct ev_idle iw; |
1244 | struct ev_idle iw; |
1108 | evidle_init (&iw, gcb); |
1245 | ev_idle_init (&iw, gcb); |
1109 | evidle_start (&iw); |
1246 | ev_idle_start (&iw); |
1110 | |
1247 | |
1111 | ev_loop (0); |
1248 | ev_loop (0); |
1112 | |
1249 | |
1113 | return 0; |
1250 | return 0; |
1114 | } |
1251 | } |