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