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