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