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