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