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