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