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