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