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