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