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