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