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