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