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