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