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