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