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> |
… | |
… | |
36 | |
41 | |
37 | #include <stdio.h> |
42 | #include <stdio.h> |
38 | |
43 | |
39 | #include <assert.h> |
44 | #include <assert.h> |
40 | #include <errno.h> |
45 | #include <errno.h> |
|
|
46 | #include <sys/types.h> |
|
|
47 | #include <sys/wait.h> |
41 | #include <sys/time.h> |
48 | #include <sys/time.h> |
42 | #include <time.h> |
49 | #include <time.h> |
43 | |
50 | |
44 | #define HAVE_EPOLL 1 |
51 | /**/ |
45 | |
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_KQUEUE |
|
|
70 | # define EV_USE_KQUEUE 0 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | #ifndef EV_USE_REALTIME |
|
|
74 | # define EV_USE_REALTIME 1 |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | /**/ |
|
|
78 | |
47 | # ifdef CLOCK_MONOTONIC |
79 | #ifndef CLOCK_MONOTONIC |
|
|
80 | # undef EV_USE_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
81 | # define EV_USE_MONOTONIC 0 |
49 | # endif |
82 | #endif |
50 | #endif |
|
|
51 | |
83 | |
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 |
84 | #ifndef CLOCK_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
85 | # undef EV_USE_REALTIME |
|
|
86 | # define EV_USE_REALTIME 0 |
62 | #endif |
87 | #endif |
|
|
88 | |
|
|
89 | /**/ |
63 | |
90 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
91 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
92 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
|
|
93 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
94 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
66 | |
95 | |
67 | #include "ev.h" |
96 | #include "ev.h" |
|
|
97 | |
|
|
98 | #if __GNUC__ >= 3 |
|
|
99 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
100 | # define inline inline |
|
|
101 | #else |
|
|
102 | # define expect(expr,value) (expr) |
|
|
103 | # define inline static |
|
|
104 | #endif |
|
|
105 | |
|
|
106 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
107 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
108 | |
|
|
109 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
110 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
68 | |
111 | |
69 | typedef struct ev_watcher *W; |
112 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher_list *WL; |
113 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_time *WT; |
114 | typedef struct ev_watcher_time *WT; |
72 | |
115 | |
73 | static ev_tstamp now, diff; /* monotonic clock */ |
116 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
74 | ev_tstamp ev_now; |
117 | ev_tstamp ev_now; |
75 | int ev_method; |
118 | int ev_method; |
76 | |
119 | |
77 | static int have_monotonic; /* runtime */ |
120 | static int have_monotonic; /* runtime */ |
78 | |
121 | |
… | |
… | |
83 | /*****************************************************************************/ |
126 | /*****************************************************************************/ |
84 | |
127 | |
85 | ev_tstamp |
128 | ev_tstamp |
86 | ev_time (void) |
129 | ev_time (void) |
87 | { |
130 | { |
88 | #if HAVE_REALTIME |
131 | #if EV_USE_REALTIME |
89 | struct timespec ts; |
132 | struct timespec ts; |
90 | clock_gettime (CLOCK_REALTIME, &ts); |
133 | clock_gettime (CLOCK_REALTIME, &ts); |
91 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
134 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
92 | #else |
135 | #else |
93 | struct timeval tv; |
136 | struct timeval tv; |
… | |
… | |
97 | } |
140 | } |
98 | |
141 | |
99 | static ev_tstamp |
142 | static ev_tstamp |
100 | get_clock (void) |
143 | get_clock (void) |
101 | { |
144 | { |
102 | #if HAVE_MONOTONIC |
145 | #if EV_USE_MONOTONIC |
103 | if (have_monotonic) |
146 | if (expect_true (have_monotonic)) |
104 | { |
147 | { |
105 | struct timespec ts; |
148 | struct timespec ts; |
106 | clock_gettime (CLOCK_MONOTONIC, &ts); |
149 | clock_gettime (CLOCK_MONOTONIC, &ts); |
107 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
150 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
108 | } |
151 | } |
109 | #endif |
152 | #endif |
110 | |
153 | |
111 | return ev_time (); |
154 | return ev_time (); |
112 | } |
155 | } |
113 | |
156 | |
|
|
157 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
158 | |
114 | #define array_needsize(base,cur,cnt,init) \ |
159 | #define array_needsize(base,cur,cnt,init) \ |
115 | if ((cnt) > cur) \ |
160 | if (expect_false ((cnt) > cur)) \ |
116 | { \ |
161 | { \ |
117 | int newcnt = cur ? cur << 1 : 16; \ |
162 | int newcnt = cur; \ |
|
|
163 | do \ |
|
|
164 | { \ |
|
|
165 | newcnt = array_roundsize (base, newcnt << 1); \ |
|
|
166 | } \ |
|
|
167 | while ((cnt) > newcnt); \ |
|
|
168 | \ |
118 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
169 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
119 | init (base + cur, newcnt - cur); \ |
170 | init (base + cur, newcnt - cur); \ |
120 | cur = newcnt; \ |
171 | cur = newcnt; \ |
121 | } |
172 | } |
122 | |
173 | |
123 | /*****************************************************************************/ |
174 | /*****************************************************************************/ |
124 | |
175 | |
125 | typedef struct |
176 | typedef struct |
126 | { |
177 | { |
127 | struct ev_io *head; |
178 | struct ev_io *head; |
128 | unsigned char wev, rev; /* want, received event set */ |
179 | unsigned char events; |
|
|
180 | unsigned char reify; |
129 | } ANFD; |
181 | } ANFD; |
130 | |
182 | |
131 | static ANFD *anfds; |
183 | static ANFD *anfds; |
132 | static int anfdmax; |
184 | static int anfdmax; |
133 | |
185 | |
134 | static int *fdchanges; |
|
|
135 | static int fdchangemax, fdchangecnt; |
|
|
136 | |
|
|
137 | static void |
186 | static void |
138 | anfds_init (ANFD *base, int count) |
187 | anfds_init (ANFD *base, int count) |
139 | { |
188 | { |
140 | while (count--) |
189 | while (count--) |
141 | { |
190 | { |
142 | base->head = 0; |
191 | base->head = 0; |
143 | base->wev = base->rev = EV_NONE; |
192 | base->events = EV_NONE; |
|
|
193 | base->reify = 0; |
|
|
194 | |
144 | ++base; |
195 | ++base; |
145 | } |
196 | } |
146 | } |
197 | } |
147 | |
198 | |
148 | typedef struct |
199 | typedef struct |
149 | { |
200 | { |
150 | W w; |
201 | W w; |
151 | int events; |
202 | int events; |
152 | } ANPENDING; |
203 | } ANPENDING; |
153 | |
204 | |
154 | static ANPENDING *pendings; |
205 | static ANPENDING *pendings [NUMPRI]; |
155 | static int pendingmax, pendingcnt; |
206 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
156 | |
207 | |
157 | static void |
208 | static void |
158 | event (W w, int events) |
209 | event (W w, int events) |
159 | { |
210 | { |
160 | if (w->active) |
211 | if (w->pending) |
161 | { |
212 | { |
162 | w->pending = ++pendingcnt; |
|
|
163 | array_needsize (pendings, pendingmax, pendingcnt, ); |
|
|
164 | pendings [pendingcnt - 1].w = w; |
|
|
165 | pendings [pendingcnt - 1].events = events; |
213 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
214 | return; |
166 | } |
215 | } |
|
|
216 | |
|
|
217 | w->pending = ++pendingcnt [ABSPRI (w)]; |
|
|
218 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
|
|
219 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
|
|
220 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
|
|
221 | } |
|
|
222 | |
|
|
223 | static void |
|
|
224 | queue_events (W *events, int eventcnt, int type) |
|
|
225 | { |
|
|
226 | int i; |
|
|
227 | |
|
|
228 | for (i = 0; i < eventcnt; ++i) |
|
|
229 | event (events [i], type); |
167 | } |
230 | } |
168 | |
231 | |
169 | static void |
232 | static void |
170 | fd_event (int fd, int events) |
233 | fd_event (int fd, int events) |
171 | { |
234 | { |
… | |
… | |
179 | if (ev) |
242 | if (ev) |
180 | event ((W)w, ev); |
243 | event ((W)w, ev); |
181 | } |
244 | } |
182 | } |
245 | } |
183 | |
246 | |
|
|
247 | /*****************************************************************************/ |
|
|
248 | |
|
|
249 | static int *fdchanges; |
|
|
250 | static int fdchangemax, fdchangecnt; |
|
|
251 | |
184 | static void |
252 | static void |
185 | queue_events (W *events, int eventcnt, int type) |
253 | fd_reify (void) |
186 | { |
254 | { |
187 | int i; |
255 | int i; |
188 | |
256 | |
189 | for (i = 0; i < eventcnt; ++i) |
257 | for (i = 0; i < fdchangecnt; ++i) |
190 | event (events [i], type); |
258 | { |
|
|
259 | int fd = fdchanges [i]; |
|
|
260 | ANFD *anfd = anfds + fd; |
|
|
261 | struct ev_io *w; |
|
|
262 | |
|
|
263 | int events = 0; |
|
|
264 | |
|
|
265 | for (w = anfd->head; w; w = w->next) |
|
|
266 | events |= w->events; |
|
|
267 | |
|
|
268 | anfd->reify = 0; |
|
|
269 | |
|
|
270 | if (anfd->events != events) |
|
|
271 | { |
|
|
272 | method_modify (fd, anfd->events, events); |
|
|
273 | anfd->events = events; |
|
|
274 | } |
|
|
275 | } |
|
|
276 | |
|
|
277 | fdchangecnt = 0; |
|
|
278 | } |
|
|
279 | |
|
|
280 | static void |
|
|
281 | fd_change (int fd) |
|
|
282 | { |
|
|
283 | if (anfds [fd].reify || fdchangecnt < 0) |
|
|
284 | return; |
|
|
285 | |
|
|
286 | anfds [fd].reify = 1; |
|
|
287 | |
|
|
288 | ++fdchangecnt; |
|
|
289 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
290 | fdchanges [fdchangecnt - 1] = fd; |
|
|
291 | } |
|
|
292 | |
|
|
293 | static void |
|
|
294 | fd_kill (int fd) |
|
|
295 | { |
|
|
296 | struct ev_io *w; |
|
|
297 | |
|
|
298 | printf ("killing fd %d\n", fd);//D |
|
|
299 | while ((w = anfds [fd].head)) |
|
|
300 | { |
|
|
301 | ev_io_stop (w); |
|
|
302 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
|
|
306 | /* called on EBADF to verify fds */ |
|
|
307 | static void |
|
|
308 | fd_ebadf (void) |
|
|
309 | { |
|
|
310 | int fd; |
|
|
311 | |
|
|
312 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
313 | if (anfds [fd].events) |
|
|
314 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
315 | fd_kill (fd); |
|
|
316 | } |
|
|
317 | |
|
|
318 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
319 | static void |
|
|
320 | fd_enomem (void) |
|
|
321 | { |
|
|
322 | int fd = anfdmax; |
|
|
323 | |
|
|
324 | while (fd--) |
|
|
325 | if (anfds [fd].events) |
|
|
326 | { |
|
|
327 | close (fd); |
|
|
328 | fd_kill (fd); |
|
|
329 | return; |
|
|
330 | } |
191 | } |
331 | } |
192 | |
332 | |
193 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
194 | |
334 | |
195 | static struct ev_timer **timers; |
335 | static struct ev_timer **timers; |
… | |
… | |
242 | /*****************************************************************************/ |
382 | /*****************************************************************************/ |
243 | |
383 | |
244 | typedef struct |
384 | typedef struct |
245 | { |
385 | { |
246 | struct ev_signal *head; |
386 | struct ev_signal *head; |
247 | sig_atomic_t gotsig; |
387 | sig_atomic_t volatile gotsig; |
248 | } ANSIG; |
388 | } ANSIG; |
249 | |
389 | |
250 | static ANSIG *signals; |
390 | static ANSIG *signals; |
251 | static int signalmax; |
391 | static int signalmax; |
252 | |
392 | |
253 | static int sigpipe [2]; |
393 | static int sigpipe [2]; |
254 | static sig_atomic_t gotsig; |
394 | static sig_atomic_t volatile gotsig; |
255 | static struct ev_io sigev; |
395 | static struct ev_io sigev; |
256 | |
396 | |
257 | static void |
397 | static void |
258 | signals_init (ANSIG *base, int count) |
398 | signals_init (ANSIG *base, int count) |
259 | { |
399 | { |
260 | while (count--) |
400 | while (count--) |
261 | { |
401 | { |
262 | base->head = 0; |
402 | base->head = 0; |
263 | base->gotsig = 0; |
403 | base->gotsig = 0; |
|
|
404 | |
264 | ++base; |
405 | ++base; |
265 | } |
406 | } |
266 | } |
407 | } |
267 | |
408 | |
268 | static void |
409 | static void |
… | |
… | |
271 | signals [signum - 1].gotsig = 1; |
412 | signals [signum - 1].gotsig = 1; |
272 | |
413 | |
273 | if (!gotsig) |
414 | if (!gotsig) |
274 | { |
415 | { |
275 | gotsig = 1; |
416 | gotsig = 1; |
276 | write (sigpipe [1], &gotsig, 1); |
417 | write (sigpipe [1], &signum, 1); |
277 | } |
418 | } |
278 | } |
419 | } |
279 | |
420 | |
280 | static void |
421 | static void |
281 | sigcb (struct ev_io *iow, int revents) |
422 | sigcb (struct ev_io *iow, int revents) |
282 | { |
423 | { |
283 | struct ev_signal *w; |
424 | struct ev_signal *w; |
284 | int sig; |
425 | int signum; |
285 | |
426 | |
|
|
427 | read (sigpipe [0], &revents, 1); |
286 | gotsig = 0; |
428 | gotsig = 0; |
287 | read (sigpipe [0], &revents, 1); |
|
|
288 | |
429 | |
289 | for (sig = signalmax; sig--; ) |
430 | for (signum = signalmax; signum--; ) |
290 | if (signals [sig].gotsig) |
431 | if (signals [signum].gotsig) |
291 | { |
432 | { |
292 | signals [sig].gotsig = 0; |
433 | signals [signum].gotsig = 0; |
293 | |
434 | |
294 | for (w = signals [sig].head; w; w = w->next) |
435 | for (w = signals [signum].head; w; w = w->next) |
295 | event ((W)w, EV_SIGNAL); |
436 | event ((W)w, EV_SIGNAL); |
296 | } |
437 | } |
297 | } |
438 | } |
298 | |
439 | |
299 | static void |
440 | static void |
… | |
… | |
304 | |
445 | |
305 | /* rather than sort out wether we really need nb, set it */ |
446 | /* rather than sort out wether we really need nb, set it */ |
306 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
447 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
307 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
448 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
308 | |
449 | |
309 | evio_set (&sigev, sigpipe [0], EV_READ); |
450 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
310 | evio_start (&sigev); |
451 | ev_io_start (&sigev); |
311 | } |
452 | } |
312 | |
453 | |
313 | /*****************************************************************************/ |
454 | /*****************************************************************************/ |
314 | |
455 | |
315 | static struct ev_idle **idles; |
456 | static struct ev_idle **idles; |
316 | static int idlemax, idlecnt; |
457 | static int idlemax, idlecnt; |
317 | |
458 | |
|
|
459 | static struct ev_prepare **prepares; |
|
|
460 | static int preparemax, preparecnt; |
|
|
461 | |
318 | static struct ev_check **checks; |
462 | static struct ev_check **checks; |
319 | static int checkmax, checkcnt; |
463 | static int checkmax, checkcnt; |
320 | |
464 | |
321 | /*****************************************************************************/ |
465 | /*****************************************************************************/ |
322 | |
466 | |
|
|
467 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
468 | static struct ev_signal childev; |
|
|
469 | |
|
|
470 | #ifndef WCONTINUED |
|
|
471 | # define WCONTINUED 0 |
|
|
472 | #endif |
|
|
473 | |
|
|
474 | static void |
|
|
475 | childcb (struct ev_signal *sw, int revents) |
|
|
476 | { |
|
|
477 | struct ev_child *w; |
|
|
478 | int pid, status; |
|
|
479 | |
|
|
480 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
481 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
482 | if (w->pid == pid || !w->pid) |
|
|
483 | { |
|
|
484 | w->status = status; |
|
|
485 | event ((W)w, EV_CHILD); |
|
|
486 | } |
|
|
487 | } |
|
|
488 | |
|
|
489 | /*****************************************************************************/ |
|
|
490 | |
|
|
491 | #if EV_USE_KQUEUE |
|
|
492 | # include "ev_kqueue.c" |
|
|
493 | #endif |
323 | #if HAVE_EPOLL |
494 | #if EV_USE_EPOLL |
324 | # include "ev_epoll.c" |
495 | # include "ev_epoll.c" |
325 | #endif |
496 | #endif |
|
|
497 | #if EV_USE_POLL |
|
|
498 | # include "ev_poll.c" |
|
|
499 | #endif |
326 | #if HAVE_SELECT |
500 | #if EV_USE_SELECT |
327 | # include "ev_select.c" |
501 | # include "ev_select.c" |
328 | #endif |
502 | #endif |
329 | |
503 | |
|
|
504 | int |
|
|
505 | ev_version_major (void) |
|
|
506 | { |
|
|
507 | return EV_VERSION_MAJOR; |
|
|
508 | } |
|
|
509 | |
|
|
510 | int |
|
|
511 | ev_version_minor (void) |
|
|
512 | { |
|
|
513 | return EV_VERSION_MINOR; |
|
|
514 | } |
|
|
515 | |
|
|
516 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
517 | static int |
|
|
518 | enable_secure () |
|
|
519 | { |
|
|
520 | return getuid () != geteuid () |
|
|
521 | || getgid () != getegid (); |
|
|
522 | } |
|
|
523 | |
330 | int ev_init (int flags) |
524 | int ev_init (int methods) |
331 | { |
525 | { |
332 | #if HAVE_MONOTONIC |
|
|
333 | { |
|
|
334 | struct timespec ts; |
|
|
335 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
336 | have_monotonic = 1; |
|
|
337 | } |
|
|
338 | #endif |
|
|
339 | |
|
|
340 | ev_now = ev_time (); |
|
|
341 | now = get_clock (); |
|
|
342 | diff = ev_now - now; |
|
|
343 | |
|
|
344 | if (pipe (sigpipe)) |
|
|
345 | return 0; |
|
|
346 | |
|
|
347 | ev_method = EVMETHOD_NONE; |
|
|
348 | #if HAVE_EPOLL |
|
|
349 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
350 | #endif |
|
|
351 | #if HAVE_SELECT |
|
|
352 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
353 | #endif |
|
|
354 | |
|
|
355 | if (ev_method) |
526 | if (!ev_method) |
|
|
527 | { |
|
|
528 | #if EV_USE_MONOTONIC |
356 | { |
529 | { |
|
|
530 | struct timespec ts; |
|
|
531 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
532 | have_monotonic = 1; |
|
|
533 | } |
|
|
534 | #endif |
|
|
535 | |
|
|
536 | ev_now = ev_time (); |
|
|
537 | now = get_clock (); |
|
|
538 | now_floor = now; |
|
|
539 | diff = ev_now - now; |
|
|
540 | |
|
|
541 | if (pipe (sigpipe)) |
|
|
542 | return 0; |
|
|
543 | |
|
|
544 | if (methods == EVMETHOD_AUTO) |
|
|
545 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
546 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
547 | else |
|
|
548 | methods = EVMETHOD_ANY; |
|
|
549 | |
|
|
550 | ev_method = 0; |
|
|
551 | #if EV_USE_KQUEUE |
|
|
552 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
553 | #endif |
|
|
554 | #if EV_USE_EPOLL |
|
|
555 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
556 | #endif |
|
|
557 | #if EV_USE_POLL |
|
|
558 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
|
|
559 | #endif |
|
|
560 | #if EV_USE_SELECT |
|
|
561 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
|
|
562 | #endif |
|
|
563 | |
|
|
564 | if (ev_method) |
|
|
565 | { |
357 | evw_init (&sigev, sigcb); |
566 | ev_watcher_init (&sigev, sigcb); |
358 | siginit (); |
567 | siginit (); |
|
|
568 | |
|
|
569 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
570 | ev_signal_start (&childev); |
|
|
571 | } |
359 | } |
572 | } |
360 | |
573 | |
361 | return ev_method; |
574 | return ev_method; |
362 | } |
575 | } |
363 | |
576 | |
364 | /*****************************************************************************/ |
577 | /*****************************************************************************/ |
365 | |
578 | |
366 | void ev_prefork (void) |
579 | void |
|
|
580 | ev_fork_prepare (void) |
367 | { |
581 | { |
368 | /* nop */ |
582 | /* nop */ |
369 | } |
583 | } |
370 | |
584 | |
|
|
585 | void |
371 | void ev_postfork_parent (void) |
586 | ev_fork_parent (void) |
372 | { |
587 | { |
373 | /* nop */ |
588 | /* nop */ |
374 | } |
589 | } |
375 | |
590 | |
|
|
591 | void |
376 | void ev_postfork_child (void) |
592 | ev_fork_child (void) |
377 | { |
593 | { |
378 | #if HAVE_EPOLL |
594 | #if EV_USE_EPOLL |
379 | if (ev_method == EVMETHOD_EPOLL) |
595 | if (ev_method == EVMETHOD_EPOLL) |
380 | epoll_postfork_child (); |
596 | epoll_postfork_child (); |
381 | #endif |
597 | #endif |
382 | |
598 | |
383 | evio_stop (&sigev); |
599 | ev_io_stop (&sigev); |
384 | close (sigpipe [0]); |
600 | close (sigpipe [0]); |
385 | close (sigpipe [1]); |
601 | close (sigpipe [1]); |
386 | pipe (sigpipe); |
602 | pipe (sigpipe); |
387 | siginit (); |
603 | siginit (); |
388 | } |
604 | } |
389 | |
605 | |
390 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
391 | |
607 | |
392 | static void |
608 | static void |
393 | fd_reify (void) |
609 | call_pending (void) |
394 | { |
610 | { |
395 | int i; |
611 | int pri; |
396 | |
612 | |
397 | for (i = 0; i < fdchangecnt; ++i) |
613 | for (pri = NUMPRI; pri--; ) |
398 | { |
614 | while (pendingcnt [pri]) |
399 | int fd = fdchanges [i]; |
|
|
400 | ANFD *anfd = anfds + fd; |
|
|
401 | struct ev_io *w; |
|
|
402 | |
|
|
403 | int wev = 0; |
|
|
404 | |
|
|
405 | for (w = anfd->head; w; w = w->next) |
|
|
406 | wev |= w->events; |
|
|
407 | |
|
|
408 | if (anfd->wev != wev) |
|
|
409 | { |
615 | { |
410 | method_modify (fd, anfd->wev, wev); |
|
|
411 | anfd->wev = wev; |
|
|
412 | } |
|
|
413 | } |
|
|
414 | |
|
|
415 | fdchangecnt = 0; |
|
|
416 | } |
|
|
417 | |
|
|
418 | static void |
|
|
419 | call_pending () |
|
|
420 | { |
|
|
421 | while (pendingcnt) |
|
|
422 | { |
|
|
423 | ANPENDING *p = pendings + --pendingcnt; |
616 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
424 | |
617 | |
425 | if (p->w) |
618 | if (p->w) |
426 | { |
619 | { |
427 | p->w->pending = 0; |
620 | p->w->pending = 0; |
428 | p->w->cb (p->w, p->events); |
621 | p->w->cb (p->w, p->events); |
429 | } |
622 | } |
430 | } |
623 | } |
431 | } |
624 | } |
432 | |
625 | |
433 | static void |
626 | static void |
434 | timers_reify () |
627 | timers_reify (void) |
435 | { |
628 | { |
436 | while (timercnt && timers [0]->at <= now) |
629 | while (timercnt && timers [0]->at <= now) |
437 | { |
630 | { |
438 | struct ev_timer *w = timers [0]; |
631 | struct ev_timer *w = timers [0]; |
439 | |
|
|
440 | event ((W)w, EV_TIMEOUT); |
|
|
441 | |
632 | |
442 | /* first reschedule or stop timer */ |
633 | /* first reschedule or stop timer */ |
443 | if (w->repeat) |
634 | if (w->repeat) |
444 | { |
635 | { |
|
|
636 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
445 | w->at = now + w->repeat; |
637 | w->at = now + w->repeat; |
446 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
447 | downheap ((WT *)timers, timercnt, 0); |
638 | downheap ((WT *)timers, timercnt, 0); |
448 | } |
639 | } |
449 | else |
640 | else |
450 | evtimer_stop (w); /* nonrepeating: stop timer */ |
641 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
451 | } |
|
|
452 | } |
|
|
453 | |
642 | |
|
|
643 | event ((W)w, EV_TIMEOUT); |
|
|
644 | } |
|
|
645 | } |
|
|
646 | |
454 | static void |
647 | static void |
455 | periodics_reify () |
648 | periodics_reify (void) |
456 | { |
649 | { |
457 | while (periodiccnt && periodics [0]->at <= ev_now) |
650 | while (periodiccnt && periodics [0]->at <= ev_now) |
458 | { |
651 | { |
459 | struct ev_periodic *w = periodics [0]; |
652 | struct ev_periodic *w = periodics [0]; |
460 | |
653 | |
461 | /* first reschedule or stop timer */ |
654 | /* first reschedule or stop timer */ |
462 | if (w->interval) |
655 | if (w->interval) |
463 | { |
656 | { |
464 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
657 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
465 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
658 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
466 | downheap ((WT *)periodics, periodiccnt, 0); |
659 | downheap ((WT *)periodics, periodiccnt, 0); |
467 | } |
660 | } |
468 | else |
661 | else |
469 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
662 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
470 | |
663 | |
471 | event ((W)w, EV_TIMEOUT); |
664 | event ((W)w, EV_PERIODIC); |
472 | } |
665 | } |
473 | } |
666 | } |
474 | |
667 | |
475 | static void |
668 | static void |
476 | periodics_reschedule (ev_tstamp diff) |
669 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
486 | { |
679 | { |
487 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
680 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
488 | |
681 | |
489 | if (fabs (diff) >= 1e-4) |
682 | if (fabs (diff) >= 1e-4) |
490 | { |
683 | { |
491 | evperiodic_stop (w); |
684 | ev_periodic_stop (w); |
492 | evperiodic_start (w); |
685 | ev_periodic_start (w); |
493 | |
686 | |
494 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
687 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
495 | } |
688 | } |
496 | } |
689 | } |
497 | } |
690 | } |
498 | } |
691 | } |
499 | |
692 | |
500 | static void |
693 | static int |
|
|
694 | time_update_monotonic (void) |
|
|
695 | { |
|
|
696 | now = get_clock (); |
|
|
697 | |
|
|
698 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
699 | { |
|
|
700 | ev_now = now + diff; |
|
|
701 | return 0; |
|
|
702 | } |
|
|
703 | else |
|
|
704 | { |
|
|
705 | now_floor = now; |
|
|
706 | ev_now = ev_time (); |
|
|
707 | return 1; |
|
|
708 | } |
|
|
709 | } |
|
|
710 | |
|
|
711 | static void |
501 | time_update () |
712 | time_update (void) |
502 | { |
713 | { |
503 | int i; |
714 | int i; |
504 | |
715 | |
505 | ev_now = ev_time (); |
716 | #if EV_USE_MONOTONIC |
506 | |
|
|
507 | if (have_monotonic) |
717 | if (expect_true (have_monotonic)) |
508 | { |
718 | { |
|
|
719 | if (time_update_monotonic ()) |
|
|
720 | { |
509 | ev_tstamp odiff = diff; |
721 | ev_tstamp odiff = diff; |
510 | |
722 | |
511 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
723 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
512 | { |
724 | { |
513 | now = get_clock (); |
|
|
514 | diff = ev_now - now; |
725 | diff = ev_now - now; |
515 | |
726 | |
516 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
727 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
517 | return; /* all is well */ |
728 | return; /* all is well */ |
518 | |
729 | |
519 | ev_now = ev_time (); |
730 | ev_now = ev_time (); |
|
|
731 | now = get_clock (); |
|
|
732 | now_floor = now; |
520 | } |
733 | } |
521 | |
734 | |
522 | periodics_reschedule (diff - odiff); |
735 | periodics_reschedule (diff - odiff); |
523 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
736 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
737 | } |
524 | } |
738 | } |
525 | else |
739 | else |
|
|
740 | #endif |
526 | { |
741 | { |
|
|
742 | ev_now = ev_time (); |
|
|
743 | |
527 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
744 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
528 | { |
745 | { |
529 | periodics_reschedule (ev_now - now); |
746 | periodics_reschedule (ev_now - now); |
530 | |
747 | |
531 | /* adjust timers. this is easy, as the offset is the same for all */ |
748 | /* adjust timers. this is easy, as the offset is the same for all */ |
532 | for (i = 0; i < timercnt; ++i) |
749 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
540 | int ev_loop_done; |
757 | int ev_loop_done; |
541 | |
758 | |
542 | void ev_loop (int flags) |
759 | void ev_loop (int flags) |
543 | { |
760 | { |
544 | double block; |
761 | double block; |
545 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
762 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
546 | |
|
|
547 | if (checkcnt) |
|
|
548 | { |
|
|
549 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
550 | call_pending (); |
|
|
551 | } |
|
|
552 | |
763 | |
553 | do |
764 | do |
554 | { |
765 | { |
|
|
766 | /* queue check watchers (and execute them) */ |
|
|
767 | if (expect_false (preparecnt)) |
|
|
768 | { |
|
|
769 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
770 | call_pending (); |
|
|
771 | } |
|
|
772 | |
555 | /* update fd-related kernel structures */ |
773 | /* update fd-related kernel structures */ |
556 | fd_reify (); |
774 | fd_reify (); |
557 | |
775 | |
558 | /* calculate blocking time */ |
776 | /* calculate blocking time */ |
559 | |
777 | |
560 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
778 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
779 | always have timers, we just calculate it always */ |
|
|
780 | #if EV_USE_MONOTONIC |
|
|
781 | if (expect_true (have_monotonic)) |
|
|
782 | time_update_monotonic (); |
|
|
783 | else |
|
|
784 | #endif |
|
|
785 | { |
561 | ev_now = ev_time (); |
786 | ev_now = ev_time (); |
|
|
787 | now = ev_now; |
|
|
788 | } |
562 | |
789 | |
563 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
790 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
564 | block = 0.; |
791 | block = 0.; |
565 | else |
792 | else |
566 | { |
793 | { |
567 | block = MAX_BLOCKTIME; |
794 | block = MAX_BLOCKTIME; |
568 | |
795 | |
569 | if (timercnt) |
796 | if (timercnt) |
570 | { |
797 | { |
571 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
798 | ev_tstamp to = timers [0]->at - now + method_fudge; |
572 | if (block > to) block = to; |
799 | if (block > to) block = to; |
573 | } |
800 | } |
574 | |
801 | |
575 | if (periodiccnt) |
802 | if (periodiccnt) |
576 | { |
803 | { |
… | |
… | |
585 | |
812 | |
586 | /* update ev_now, do magic */ |
813 | /* update ev_now, do magic */ |
587 | time_update (); |
814 | time_update (); |
588 | |
815 | |
589 | /* queue pending timers and reschedule them */ |
816 | /* queue pending timers and reschedule them */ |
|
|
817 | timers_reify (); /* relative timers called last */ |
590 | periodics_reify (); /* absolute timers first */ |
818 | periodics_reify (); /* absolute timers called first */ |
591 | timers_reify (); /* relative timers second */ |
|
|
592 | |
819 | |
593 | /* queue idle watchers unless io or timers are pending */ |
820 | /* queue idle watchers unless io or timers are pending */ |
594 | if (!pendingcnt) |
821 | if (!pendingcnt) |
595 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
822 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
596 | |
823 | |
597 | /* queue check and possibly idle watchers */ |
824 | /* queue check watchers, to be executed first */ |
|
|
825 | if (checkcnt) |
598 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
826 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
599 | |
827 | |
600 | call_pending (); |
828 | call_pending (); |
601 | } |
829 | } |
602 | while (!ev_loop_done); |
830 | while (!ev_loop_done); |
603 | |
831 | |
… | |
… | |
628 | head = &(*head)->next; |
856 | head = &(*head)->next; |
629 | } |
857 | } |
630 | } |
858 | } |
631 | |
859 | |
632 | static void |
860 | static void |
633 | ev_clear (W w) |
861 | ev_clear_pending (W w) |
634 | { |
862 | { |
635 | if (w->pending) |
863 | if (w->pending) |
636 | { |
864 | { |
637 | pendings [w->pending - 1].w = 0; |
865 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
638 | w->pending = 0; |
866 | w->pending = 0; |
639 | } |
867 | } |
640 | } |
868 | } |
641 | |
869 | |
642 | static void |
870 | static void |
643 | ev_start (W w, int active) |
871 | ev_start (W w, int active) |
644 | { |
872 | { |
|
|
873 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
874 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
875 | |
645 | w->active = active; |
876 | w->active = active; |
646 | } |
877 | } |
647 | |
878 | |
648 | static void |
879 | static void |
649 | ev_stop (W w) |
880 | ev_stop (W w) |
… | |
… | |
652 | } |
883 | } |
653 | |
884 | |
654 | /*****************************************************************************/ |
885 | /*****************************************************************************/ |
655 | |
886 | |
656 | void |
887 | void |
657 | evio_start (struct ev_io *w) |
888 | ev_io_start (struct ev_io *w) |
658 | { |
889 | { |
|
|
890 | int fd = w->fd; |
|
|
891 | |
659 | if (ev_is_active (w)) |
892 | if (ev_is_active (w)) |
660 | return; |
893 | return; |
661 | |
894 | |
662 | int fd = w->fd; |
895 | assert (("ev_io_start called with negative fd", fd >= 0)); |
663 | |
896 | |
664 | ev_start ((W)w, 1); |
897 | ev_start ((W)w, 1); |
665 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
898 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
666 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
899 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
667 | |
900 | |
668 | ++fdchangecnt; |
901 | fd_change (fd); |
669 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
670 | fdchanges [fdchangecnt - 1] = fd; |
|
|
671 | } |
902 | } |
672 | |
903 | |
673 | void |
904 | void |
674 | evio_stop (struct ev_io *w) |
905 | ev_io_stop (struct ev_io *w) |
675 | { |
906 | { |
676 | ev_clear ((W)w); |
907 | ev_clear_pending ((W)w); |
677 | if (!ev_is_active (w)) |
908 | if (!ev_is_active (w)) |
678 | return; |
909 | return; |
679 | |
910 | |
680 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
911 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
681 | ev_stop ((W)w); |
912 | ev_stop ((W)w); |
682 | |
913 | |
683 | ++fdchangecnt; |
914 | fd_change (w->fd); |
684 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
685 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
686 | } |
915 | } |
687 | |
916 | |
688 | void |
917 | void |
689 | evtimer_start (struct ev_timer *w) |
918 | ev_timer_start (struct ev_timer *w) |
690 | { |
919 | { |
691 | if (ev_is_active (w)) |
920 | if (ev_is_active (w)) |
692 | return; |
921 | return; |
693 | |
922 | |
694 | w->at += now; |
923 | w->at += now; |
695 | |
924 | |
696 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
925 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
697 | |
926 | |
698 | ev_start ((W)w, ++timercnt); |
927 | ev_start ((W)w, ++timercnt); |
699 | array_needsize (timers, timermax, timercnt, ); |
928 | array_needsize (timers, timermax, timercnt, ); |
700 | timers [timercnt - 1] = w; |
929 | timers [timercnt - 1] = w; |
701 | upheap ((WT *)timers, timercnt - 1); |
930 | upheap ((WT *)timers, timercnt - 1); |
702 | } |
931 | } |
703 | |
932 | |
704 | void |
933 | void |
705 | evtimer_stop (struct ev_timer *w) |
934 | ev_timer_stop (struct ev_timer *w) |
706 | { |
935 | { |
707 | ev_clear ((W)w); |
936 | ev_clear_pending ((W)w); |
708 | if (!ev_is_active (w)) |
937 | if (!ev_is_active (w)) |
709 | return; |
938 | return; |
710 | |
939 | |
711 | if (w->active < timercnt--) |
940 | if (w->active < timercnt--) |
712 | { |
941 | { |
… | |
… | |
718 | |
947 | |
719 | ev_stop ((W)w); |
948 | ev_stop ((W)w); |
720 | } |
949 | } |
721 | |
950 | |
722 | void |
951 | void |
723 | evtimer_again (struct ev_timer *w) |
952 | ev_timer_again (struct ev_timer *w) |
724 | { |
953 | { |
725 | if (ev_is_active (w)) |
954 | if (ev_is_active (w)) |
726 | { |
955 | { |
727 | if (w->repeat) |
956 | if (w->repeat) |
728 | { |
957 | { |
729 | w->at = now + w->repeat; |
958 | w->at = now + w->repeat; |
730 | downheap ((WT *)timers, timercnt, w->active - 1); |
959 | downheap ((WT *)timers, timercnt, w->active - 1); |
731 | } |
960 | } |
732 | else |
961 | else |
733 | evtimer_stop (w); |
962 | ev_timer_stop (w); |
734 | } |
963 | } |
735 | else if (w->repeat) |
964 | else if (w->repeat) |
736 | evtimer_start (w); |
965 | ev_timer_start (w); |
737 | } |
966 | } |
738 | |
967 | |
739 | void |
968 | void |
740 | evperiodic_start (struct ev_periodic *w) |
969 | ev_periodic_start (struct ev_periodic *w) |
741 | { |
970 | { |
742 | if (ev_is_active (w)) |
971 | if (ev_is_active (w)) |
743 | return; |
972 | return; |
744 | |
973 | |
745 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
974 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
746 | |
975 | |
747 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
976 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
748 | if (w->interval) |
977 | if (w->interval) |
749 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
978 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
750 | |
979 | |
… | |
… | |
753 | periodics [periodiccnt - 1] = w; |
982 | periodics [periodiccnt - 1] = w; |
754 | upheap ((WT *)periodics, periodiccnt - 1); |
983 | upheap ((WT *)periodics, periodiccnt - 1); |
755 | } |
984 | } |
756 | |
985 | |
757 | void |
986 | void |
758 | evperiodic_stop (struct ev_periodic *w) |
987 | ev_periodic_stop (struct ev_periodic *w) |
759 | { |
988 | { |
760 | ev_clear ((W)w); |
989 | ev_clear_pending ((W)w); |
761 | if (!ev_is_active (w)) |
990 | if (!ev_is_active (w)) |
762 | return; |
991 | return; |
763 | |
992 | |
764 | if (w->active < periodiccnt--) |
993 | if (w->active < periodiccnt--) |
765 | { |
994 | { |
… | |
… | |
769 | |
998 | |
770 | ev_stop ((W)w); |
999 | ev_stop ((W)w); |
771 | } |
1000 | } |
772 | |
1001 | |
773 | void |
1002 | void |
774 | evsignal_start (struct ev_signal *w) |
1003 | ev_signal_start (struct ev_signal *w) |
775 | { |
1004 | { |
776 | if (ev_is_active (w)) |
1005 | if (ev_is_active (w)) |
777 | return; |
1006 | return; |
|
|
1007 | |
|
|
1008 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
778 | |
1009 | |
779 | ev_start ((W)w, 1); |
1010 | ev_start ((W)w, 1); |
780 | array_needsize (signals, signalmax, w->signum, signals_init); |
1011 | array_needsize (signals, signalmax, w->signum, signals_init); |
781 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1012 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
782 | |
1013 | |
… | |
… | |
789 | sigaction (w->signum, &sa, 0); |
1020 | sigaction (w->signum, &sa, 0); |
790 | } |
1021 | } |
791 | } |
1022 | } |
792 | |
1023 | |
793 | void |
1024 | void |
794 | evsignal_stop (struct ev_signal *w) |
1025 | ev_signal_stop (struct ev_signal *w) |
795 | { |
1026 | { |
796 | ev_clear ((W)w); |
1027 | ev_clear_pending ((W)w); |
797 | if (!ev_is_active (w)) |
1028 | if (!ev_is_active (w)) |
798 | return; |
1029 | return; |
799 | |
1030 | |
800 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1031 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
801 | ev_stop ((W)w); |
1032 | ev_stop ((W)w); |
802 | |
1033 | |
803 | if (!signals [w->signum - 1].head) |
1034 | if (!signals [w->signum - 1].head) |
804 | signal (w->signum, SIG_DFL); |
1035 | signal (w->signum, SIG_DFL); |
805 | } |
1036 | } |
806 | |
1037 | |
|
|
1038 | void |
807 | void evidle_start (struct ev_idle *w) |
1039 | ev_idle_start (struct ev_idle *w) |
808 | { |
1040 | { |
809 | if (ev_is_active (w)) |
1041 | if (ev_is_active (w)) |
810 | return; |
1042 | return; |
811 | |
1043 | |
812 | ev_start ((W)w, ++idlecnt); |
1044 | ev_start ((W)w, ++idlecnt); |
813 | array_needsize (idles, idlemax, idlecnt, ); |
1045 | array_needsize (idles, idlemax, idlecnt, ); |
814 | idles [idlecnt - 1] = w; |
1046 | idles [idlecnt - 1] = w; |
815 | } |
1047 | } |
816 | |
1048 | |
|
|
1049 | void |
817 | void evidle_stop (struct ev_idle *w) |
1050 | ev_idle_stop (struct ev_idle *w) |
818 | { |
1051 | { |
819 | ev_clear ((W)w); |
1052 | ev_clear_pending ((W)w); |
820 | if (ev_is_active (w)) |
1053 | if (ev_is_active (w)) |
821 | return; |
1054 | return; |
822 | |
1055 | |
823 | idles [w->active - 1] = idles [--idlecnt]; |
1056 | idles [w->active - 1] = idles [--idlecnt]; |
824 | ev_stop ((W)w); |
1057 | ev_stop ((W)w); |
825 | } |
1058 | } |
826 | |
1059 | |
|
|
1060 | void |
|
|
1061 | ev_prepare_start (struct ev_prepare *w) |
|
|
1062 | { |
|
|
1063 | if (ev_is_active (w)) |
|
|
1064 | return; |
|
|
1065 | |
|
|
1066 | ev_start ((W)w, ++preparecnt); |
|
|
1067 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1068 | prepares [preparecnt - 1] = w; |
|
|
1069 | } |
|
|
1070 | |
|
|
1071 | void |
|
|
1072 | ev_prepare_stop (struct ev_prepare *w) |
|
|
1073 | { |
|
|
1074 | ev_clear_pending ((W)w); |
|
|
1075 | if (ev_is_active (w)) |
|
|
1076 | return; |
|
|
1077 | |
|
|
1078 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1079 | ev_stop ((W)w); |
|
|
1080 | } |
|
|
1081 | |
|
|
1082 | void |
827 | void evcheck_start (struct ev_check *w) |
1083 | ev_check_start (struct ev_check *w) |
828 | { |
1084 | { |
829 | if (ev_is_active (w)) |
1085 | if (ev_is_active (w)) |
830 | return; |
1086 | return; |
831 | |
1087 | |
832 | ev_start ((W)w, ++checkcnt); |
1088 | ev_start ((W)w, ++checkcnt); |
833 | array_needsize (checks, checkmax, checkcnt, ); |
1089 | array_needsize (checks, checkmax, checkcnt, ); |
834 | checks [checkcnt - 1] = w; |
1090 | checks [checkcnt - 1] = w; |
835 | } |
1091 | } |
836 | |
1092 | |
|
|
1093 | void |
837 | void evcheck_stop (struct ev_check *w) |
1094 | ev_check_stop (struct ev_check *w) |
838 | { |
1095 | { |
839 | ev_clear ((W)w); |
1096 | ev_clear_pending ((W)w); |
840 | if (ev_is_active (w)) |
1097 | if (ev_is_active (w)) |
841 | return; |
1098 | return; |
842 | |
1099 | |
843 | checks [w->active - 1] = checks [--checkcnt]; |
1100 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1101 | ev_stop ((W)w); |
|
|
1102 | } |
|
|
1103 | |
|
|
1104 | void |
|
|
1105 | ev_child_start (struct ev_child *w) |
|
|
1106 | { |
|
|
1107 | if (ev_is_active (w)) |
|
|
1108 | return; |
|
|
1109 | |
|
|
1110 | ev_start ((W)w, 1); |
|
|
1111 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
1112 | } |
|
|
1113 | |
|
|
1114 | void |
|
|
1115 | ev_child_stop (struct ev_child *w) |
|
|
1116 | { |
|
|
1117 | ev_clear_pending ((W)w); |
|
|
1118 | if (ev_is_active (w)) |
|
|
1119 | return; |
|
|
1120 | |
|
|
1121 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
844 | ev_stop ((W)w); |
1122 | ev_stop ((W)w); |
845 | } |
1123 | } |
846 | |
1124 | |
847 | /*****************************************************************************/ |
1125 | /*****************************************************************************/ |
848 | |
1126 | |
… | |
… | |
858 | once_cb (struct ev_once *once, int revents) |
1136 | once_cb (struct ev_once *once, int revents) |
859 | { |
1137 | { |
860 | void (*cb)(int revents, void *arg) = once->cb; |
1138 | void (*cb)(int revents, void *arg) = once->cb; |
861 | void *arg = once->arg; |
1139 | void *arg = once->arg; |
862 | |
1140 | |
863 | evio_stop (&once->io); |
1141 | ev_io_stop (&once->io); |
864 | evtimer_stop (&once->to); |
1142 | ev_timer_stop (&once->to); |
865 | free (once); |
1143 | free (once); |
866 | |
1144 | |
867 | cb (revents, arg); |
1145 | cb (revents, arg); |
868 | } |
1146 | } |
869 | |
1147 | |
… | |
… | |
883 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1161 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
884 | { |
1162 | { |
885 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1163 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
886 | |
1164 | |
887 | if (!once) |
1165 | if (!once) |
888 | cb (EV_ERROR, arg); |
1166 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
889 | else |
1167 | else |
890 | { |
1168 | { |
891 | once->cb = cb; |
1169 | once->cb = cb; |
892 | once->arg = arg; |
1170 | once->arg = arg; |
893 | |
1171 | |
894 | evw_init (&once->io, once_cb_io); |
1172 | ev_watcher_init (&once->io, once_cb_io); |
895 | |
|
|
896 | if (fd >= 0) |
1173 | if (fd >= 0) |
897 | { |
1174 | { |
898 | evio_set (&once->io, fd, events); |
1175 | ev_io_set (&once->io, fd, events); |
899 | evio_start (&once->io); |
1176 | ev_io_start (&once->io); |
900 | } |
1177 | } |
901 | |
1178 | |
902 | evw_init (&once->to, once_cb_to); |
1179 | ev_watcher_init (&once->to, once_cb_to); |
903 | |
|
|
904 | if (timeout >= 0.) |
1180 | if (timeout >= 0.) |
905 | { |
1181 | { |
906 | evtimer_set (&once->to, timeout, 0.); |
1182 | ev_timer_set (&once->to, timeout, 0.); |
907 | evtimer_start (&once->to); |
1183 | ev_timer_start (&once->to); |
908 | } |
1184 | } |
909 | } |
1185 | } |
910 | } |
1186 | } |
911 | |
1187 | |
912 | /*****************************************************************************/ |
1188 | /*****************************************************************************/ |
… | |
… | |
923 | |
1199 | |
924 | static void |
1200 | static void |
925 | ocb (struct ev_timer *w, int revents) |
1201 | ocb (struct ev_timer *w, int revents) |
926 | { |
1202 | { |
927 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1203 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
928 | evtimer_stop (w); |
1204 | ev_timer_stop (w); |
929 | evtimer_start (w); |
1205 | ev_timer_start (w); |
930 | } |
1206 | } |
931 | |
1207 | |
932 | static void |
1208 | static void |
933 | scb (struct ev_signal *w, int revents) |
1209 | scb (struct ev_signal *w, int revents) |
934 | { |
1210 | { |
935 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1211 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
936 | evio_stop (&wio); |
1212 | ev_io_stop (&wio); |
937 | evio_start (&wio); |
1213 | ev_io_start (&wio); |
938 | } |
1214 | } |
939 | |
1215 | |
940 | static void |
1216 | static void |
941 | gcb (struct ev_signal *w, int revents) |
1217 | gcb (struct ev_signal *w, int revents) |
942 | { |
1218 | { |
… | |
… | |
946 | |
1222 | |
947 | int main (void) |
1223 | int main (void) |
948 | { |
1224 | { |
949 | ev_init (0); |
1225 | ev_init (0); |
950 | |
1226 | |
951 | evio_init (&wio, sin_cb, 0, EV_READ); |
1227 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
952 | evio_start (&wio); |
1228 | ev_io_start (&wio); |
953 | |
1229 | |
954 | struct ev_timer t[10000]; |
1230 | struct ev_timer t[10000]; |
955 | |
1231 | |
956 | #if 0 |
1232 | #if 0 |
957 | int i; |
1233 | int i; |
958 | for (i = 0; i < 10000; ++i) |
1234 | for (i = 0; i < 10000; ++i) |
959 | { |
1235 | { |
960 | struct ev_timer *w = t + i; |
1236 | struct ev_timer *w = t + i; |
961 | evw_init (w, ocb, i); |
1237 | ev_watcher_init (w, ocb, i); |
962 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1238 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
963 | evtimer_start (w); |
1239 | ev_timer_start (w); |
964 | if (drand48 () < 0.5) |
1240 | if (drand48 () < 0.5) |
965 | evtimer_stop (w); |
1241 | ev_timer_stop (w); |
966 | } |
1242 | } |
967 | #endif |
1243 | #endif |
968 | |
1244 | |
969 | struct ev_timer t1; |
1245 | struct ev_timer t1; |
970 | evtimer_init (&t1, ocb, 5, 10); |
1246 | ev_timer_init (&t1, ocb, 5, 10); |
971 | evtimer_start (&t1); |
1247 | ev_timer_start (&t1); |
972 | |
1248 | |
973 | struct ev_signal sig; |
1249 | struct ev_signal sig; |
974 | evsignal_init (&sig, scb, SIGQUIT); |
1250 | ev_signal_init (&sig, scb, SIGQUIT); |
975 | evsignal_start (&sig); |
1251 | ev_signal_start (&sig); |
976 | |
1252 | |
977 | struct ev_check cw; |
1253 | struct ev_check cw; |
978 | evcheck_init (&cw, gcb); |
1254 | ev_check_init (&cw, gcb); |
979 | evcheck_start (&cw); |
1255 | ev_check_start (&cw); |
980 | |
1256 | |
981 | struct ev_idle iw; |
1257 | struct ev_idle iw; |
982 | evidle_init (&iw, gcb); |
1258 | ev_idle_init (&iw, gcb); |
983 | evidle_start (&iw); |
1259 | ev_idle_start (&iw); |
984 | |
1260 | |
985 | ev_loop (0); |
1261 | ev_loop (0); |
986 | |
1262 | |
987 | return 0; |
1263 | return 0; |
988 | } |
1264 | } |