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