… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #if EV_USE_CONFIG_H |
31 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
|
|
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
|
|
38 | |
|
|
39 | # if HAVE_CLOCK_GETTIME |
|
|
40 | # ifndef EV_USE_MONOTONIC |
|
|
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
|
|
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
|
|
46 | # endif |
|
|
47 | |
|
|
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
|
|
49 | # define EV_USE_SELECT 1 |
|
|
50 | # endif |
|
|
51 | |
|
|
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
|
|
53 | # define EV_USE_POLL 1 |
|
|
54 | # endif |
|
|
55 | |
|
|
56 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
|
|
57 | # define EV_USE_EPOLL 1 |
|
|
58 | # endif |
|
|
59 | |
|
|
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
|
|
61 | # define EV_USE_KQUEUE 1 |
|
|
62 | # endif |
|
|
63 | |
33 | #endif |
64 | #endif |
34 | |
65 | |
35 | #include <math.h> |
66 | #include <math.h> |
36 | #include <stdlib.h> |
67 | #include <stdlib.h> |
37 | #include <unistd.h> |
|
|
38 | #include <fcntl.h> |
68 | #include <fcntl.h> |
39 | #include <signal.h> |
|
|
40 | #include <stddef.h> |
69 | #include <stddef.h> |
41 | |
70 | |
42 | #include <stdio.h> |
71 | #include <stdio.h> |
43 | |
72 | |
44 | #include <assert.h> |
73 | #include <assert.h> |
45 | #include <errno.h> |
74 | #include <errno.h> |
46 | #include <sys/types.h> |
75 | #include <sys/types.h> |
|
|
76 | #include <time.h> |
|
|
77 | |
|
|
78 | #include <signal.h> |
|
|
79 | |
47 | #ifndef WIN32 |
80 | #ifndef WIN32 |
|
|
81 | # include <unistd.h> |
|
|
82 | # include <sys/time.h> |
48 | # include <sys/wait.h> |
83 | # include <sys/wait.h> |
49 | #endif |
84 | #endif |
50 | #include <sys/time.h> |
|
|
51 | #include <time.h> |
|
|
52 | |
|
|
53 | /**/ |
85 | /**/ |
54 | |
86 | |
55 | #ifndef EV_USE_MONOTONIC |
87 | #ifndef EV_USE_MONOTONIC |
56 | # define EV_USE_MONOTONIC 1 |
88 | # define EV_USE_MONOTONIC 1 |
57 | #endif |
89 | #endif |
… | |
… | |
68 | # define EV_USE_EPOLL 0 |
100 | # define EV_USE_EPOLL 0 |
69 | #endif |
101 | #endif |
70 | |
102 | |
71 | #ifndef EV_USE_KQUEUE |
103 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
104 | # define EV_USE_KQUEUE 0 |
|
|
105 | #endif |
|
|
106 | |
|
|
107 | #ifndef EV_USE_WIN32 |
|
|
108 | # ifdef WIN32 |
|
|
109 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
110 | # undef EV_USE_SELECT |
|
|
111 | # define EV_USE_SELECT 1 |
|
|
112 | # else |
|
|
113 | # define EV_USE_WIN32 0 |
|
|
114 | # endif |
73 | #endif |
115 | #endif |
74 | |
116 | |
75 | #ifndef EV_USE_REALTIME |
117 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
118 | # define EV_USE_REALTIME 1 |
77 | #endif |
119 | #endif |
… | |
… | |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
135 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
136 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
137 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
138 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
97 | |
139 | |
|
|
140 | #ifdef EV_H |
|
|
141 | # include EV_H |
|
|
142 | #else |
98 | #include "ev.h" |
143 | # include "ev.h" |
|
|
144 | #endif |
99 | |
145 | |
100 | #if __GNUC__ >= 3 |
146 | #if __GNUC__ >= 3 |
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
147 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
102 | # define inline inline |
148 | # define inline inline |
103 | #else |
149 | #else |
… | |
… | |
113 | |
159 | |
114 | typedef struct ev_watcher *W; |
160 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
161 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
162 | typedef struct ev_watcher_time *WT; |
117 | |
163 | |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
164 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
|
|
165 | |
|
|
166 | #ifdef WIN32 |
|
|
167 | # include "ev_win32.c" |
|
|
168 | #endif |
|
|
169 | |
|
|
170 | /*****************************************************************************/ |
|
|
171 | |
|
|
172 | static void (*syserr_cb)(const char *msg); |
|
|
173 | |
|
|
174 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
175 | { |
|
|
176 | syserr_cb = cb; |
|
|
177 | } |
|
|
178 | |
|
|
179 | static void |
|
|
180 | syserr (const char *msg) |
|
|
181 | { |
|
|
182 | if (!msg) |
|
|
183 | msg = "(libev) system error"; |
|
|
184 | |
|
|
185 | if (syserr_cb) |
|
|
186 | syserr_cb (msg); |
|
|
187 | else |
|
|
188 | { |
|
|
189 | perror (msg); |
|
|
190 | abort (); |
|
|
191 | } |
|
|
192 | } |
|
|
193 | |
|
|
194 | static void *(*alloc)(void *ptr, long size); |
|
|
195 | |
|
|
196 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
197 | { |
|
|
198 | alloc = cb; |
|
|
199 | } |
|
|
200 | |
|
|
201 | static void * |
|
|
202 | ev_realloc (void *ptr, long size) |
|
|
203 | { |
|
|
204 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
205 | |
|
|
206 | if (!ptr && size) |
|
|
207 | { |
|
|
208 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
209 | abort (); |
|
|
210 | } |
|
|
211 | |
|
|
212 | return ptr; |
|
|
213 | } |
|
|
214 | |
|
|
215 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
216 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
217 | |
|
|
218 | /*****************************************************************************/ |
|
|
219 | |
|
|
220 | typedef struct |
|
|
221 | { |
|
|
222 | WL head; |
|
|
223 | unsigned char events; |
|
|
224 | unsigned char reify; |
|
|
225 | } ANFD; |
|
|
226 | |
|
|
227 | typedef struct |
|
|
228 | { |
|
|
229 | W w; |
|
|
230 | int events; |
|
|
231 | } ANPENDING; |
|
|
232 | |
|
|
233 | #if EV_MULTIPLICITY |
|
|
234 | |
|
|
235 | struct ev_loop |
|
|
236 | { |
|
|
237 | ev_tstamp ev_rt_now; |
|
|
238 | #define VAR(name,decl) decl; |
|
|
239 | #include "ev_vars.h" |
|
|
240 | #undef VAR |
|
|
241 | }; |
|
|
242 | #include "ev_wrap.h" |
|
|
243 | |
|
|
244 | struct ev_loop default_loop_struct; |
|
|
245 | static struct ev_loop *default_loop; |
|
|
246 | |
|
|
247 | #else |
|
|
248 | |
119 | ev_tstamp ev_now; |
249 | ev_tstamp ev_rt_now; |
120 | int ev_method; |
250 | #define VAR(name,decl) static decl; |
|
|
251 | #include "ev_vars.h" |
|
|
252 | #undef VAR |
121 | |
253 | |
122 | static int have_monotonic; /* runtime */ |
254 | static int default_loop; |
123 | |
255 | |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
256 | #endif |
125 | static void (*method_modify)(int fd, int oev, int nev); |
|
|
126 | static void (*method_poll)(ev_tstamp timeout); |
|
|
127 | |
257 | |
128 | /*****************************************************************************/ |
258 | /*****************************************************************************/ |
129 | |
259 | |
130 | ev_tstamp |
260 | ev_tstamp |
131 | ev_time (void) |
261 | ev_time (void) |
… | |
… | |
139 | gettimeofday (&tv, 0); |
269 | gettimeofday (&tv, 0); |
140 | return tv.tv_sec + tv.tv_usec * 1e-6; |
270 | return tv.tv_sec + tv.tv_usec * 1e-6; |
141 | #endif |
271 | #endif |
142 | } |
272 | } |
143 | |
273 | |
144 | static ev_tstamp |
274 | inline ev_tstamp |
145 | get_clock (void) |
275 | get_clock (void) |
146 | { |
276 | { |
147 | #if EV_USE_MONOTONIC |
277 | #if EV_USE_MONOTONIC |
148 | if (expect_true (have_monotonic)) |
278 | if (expect_true (have_monotonic)) |
149 | { |
279 | { |
… | |
… | |
154 | #endif |
284 | #endif |
155 | |
285 | |
156 | return ev_time (); |
286 | return ev_time (); |
157 | } |
287 | } |
158 | |
288 | |
|
|
289 | #if EV_MULTIPLICITY |
|
|
290 | ev_tstamp |
|
|
291 | ev_now (EV_P) |
|
|
292 | { |
|
|
293 | return ev_rt_now; |
|
|
294 | } |
|
|
295 | #endif |
|
|
296 | |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
297 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
160 | |
298 | |
161 | #define array_needsize(base,cur,cnt,init) \ |
299 | #define array_needsize(type,base,cur,cnt,init) \ |
162 | if (expect_false ((cnt) > cur)) \ |
300 | if (expect_false ((cnt) > cur)) \ |
163 | { \ |
301 | { \ |
164 | int newcnt = cur; \ |
302 | int newcnt = cur; \ |
165 | do \ |
303 | do \ |
166 | { \ |
304 | { \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
305 | newcnt = array_roundsize (type, newcnt << 1); \ |
168 | } \ |
306 | } \ |
169 | while ((cnt) > newcnt); \ |
307 | while ((cnt) > newcnt); \ |
170 | \ |
308 | \ |
171 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
309 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
172 | init (base + cur, newcnt - cur); \ |
310 | init (base + cur, newcnt - cur); \ |
173 | cur = newcnt; \ |
311 | cur = newcnt; \ |
174 | } |
312 | } |
|
|
313 | |
|
|
314 | #define array_slim(type,stem) \ |
|
|
315 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
316 | { \ |
|
|
317 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
318 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
|
|
319 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
320 | } |
|
|
321 | |
|
|
322 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
323 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
324 | #define array_free_microshit(stem) \ |
|
|
325 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
326 | |
|
|
327 | #define array_free(stem, idx) \ |
|
|
328 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
175 | |
329 | |
176 | /*****************************************************************************/ |
330 | /*****************************************************************************/ |
177 | |
|
|
178 | typedef struct |
|
|
179 | { |
|
|
180 | struct ev_io *head; |
|
|
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
|
|
183 | } ANFD; |
|
|
184 | |
|
|
185 | static ANFD *anfds; |
|
|
186 | static int anfdmax; |
|
|
187 | |
331 | |
188 | static void |
332 | static void |
189 | anfds_init (ANFD *base, int count) |
333 | anfds_init (ANFD *base, int count) |
190 | { |
334 | { |
191 | while (count--) |
335 | while (count--) |
… | |
… | |
196 | |
340 | |
197 | ++base; |
341 | ++base; |
198 | } |
342 | } |
199 | } |
343 | } |
200 | |
344 | |
201 | typedef struct |
345 | void |
|
|
346 | ev_feed_event (EV_P_ void *w, int revents) |
202 | { |
347 | { |
203 | W w; |
348 | W w_ = (W)w; |
204 | int events; |
|
|
205 | } ANPENDING; |
|
|
206 | |
349 | |
207 | static ANPENDING *pendings [NUMPRI]; |
|
|
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
209 | |
|
|
210 | static void |
|
|
211 | event (W w, int events) |
|
|
212 | { |
|
|
213 | if (w->pending) |
350 | if (w_->pending) |
214 | { |
351 | { |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
352 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
216 | return; |
353 | return; |
217 | } |
354 | } |
218 | |
355 | |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
356 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
357 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
358 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
359 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
223 | } |
360 | } |
224 | |
361 | |
225 | static void |
362 | static void |
226 | queue_events (W *events, int eventcnt, int type) |
363 | queue_events (EV_P_ W *events, int eventcnt, int type) |
227 | { |
364 | { |
228 | int i; |
365 | int i; |
229 | |
366 | |
230 | for (i = 0; i < eventcnt; ++i) |
367 | for (i = 0; i < eventcnt; ++i) |
231 | event (events [i], type); |
368 | ev_feed_event (EV_A_ events [i], type); |
232 | } |
369 | } |
233 | |
370 | |
234 | static void |
371 | inline void |
235 | fd_event (int fd, int events) |
372 | fd_event (EV_P_ int fd, int revents) |
236 | { |
373 | { |
237 | ANFD *anfd = anfds + fd; |
374 | ANFD *anfd = anfds + fd; |
238 | struct ev_io *w; |
375 | struct ev_io *w; |
239 | |
376 | |
240 | for (w = anfd->head; w; w = w->next) |
377 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
241 | { |
378 | { |
242 | int ev = w->events & events; |
379 | int ev = w->events & revents; |
243 | |
380 | |
244 | if (ev) |
381 | if (ev) |
245 | event ((W)w, ev); |
382 | ev_feed_event (EV_A_ (W)w, ev); |
246 | } |
383 | } |
|
|
384 | } |
|
|
385 | |
|
|
386 | void |
|
|
387 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
388 | { |
|
|
389 | fd_event (EV_A_ fd, revents); |
247 | } |
390 | } |
248 | |
391 | |
249 | /*****************************************************************************/ |
392 | /*****************************************************************************/ |
250 | |
393 | |
251 | static int *fdchanges; |
|
|
252 | static int fdchangemax, fdchangecnt; |
|
|
253 | |
|
|
254 | static void |
394 | static void |
255 | fd_reify (void) |
395 | fd_reify (EV_P) |
256 | { |
396 | { |
257 | int i; |
397 | int i; |
258 | |
398 | |
259 | for (i = 0; i < fdchangecnt; ++i) |
399 | for (i = 0; i < fdchangecnt; ++i) |
260 | { |
400 | { |
… | |
… | |
262 | ANFD *anfd = anfds + fd; |
402 | ANFD *anfd = anfds + fd; |
263 | struct ev_io *w; |
403 | struct ev_io *w; |
264 | |
404 | |
265 | int events = 0; |
405 | int events = 0; |
266 | |
406 | |
267 | for (w = anfd->head; w; w = w->next) |
407 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
268 | events |= w->events; |
408 | events |= w->events; |
269 | |
409 | |
270 | anfd->reify = 0; |
410 | anfd->reify = 0; |
271 | |
411 | |
272 | if (anfd->events != events) |
|
|
273 | { |
|
|
274 | method_modify (fd, anfd->events, events); |
412 | method_modify (EV_A_ fd, anfd->events, events); |
275 | anfd->events = events; |
413 | anfd->events = events; |
276 | } |
|
|
277 | } |
414 | } |
278 | |
415 | |
279 | fdchangecnt = 0; |
416 | fdchangecnt = 0; |
280 | } |
417 | } |
281 | |
418 | |
282 | static void |
419 | static void |
283 | fd_change (int fd) |
420 | fd_change (EV_P_ int fd) |
284 | { |
421 | { |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
422 | if (anfds [fd].reify) |
286 | return; |
423 | return; |
287 | |
424 | |
288 | anfds [fd].reify = 1; |
425 | anfds [fd].reify = 1; |
289 | |
426 | |
290 | ++fdchangecnt; |
427 | ++fdchangecnt; |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
428 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
292 | fdchanges [fdchangecnt - 1] = fd; |
429 | fdchanges [fdchangecnt - 1] = fd; |
293 | } |
430 | } |
294 | |
431 | |
295 | static void |
432 | static void |
296 | fd_kill (int fd) |
433 | fd_kill (EV_P_ int fd) |
297 | { |
434 | { |
298 | struct ev_io *w; |
435 | struct ev_io *w; |
299 | |
436 | |
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
437 | while ((w = (struct ev_io *)anfds [fd].head)) |
302 | { |
438 | { |
303 | ev_io_stop (w); |
439 | ev_io_stop (EV_A_ w); |
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
440 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
305 | } |
441 | } |
|
|
442 | } |
|
|
443 | |
|
|
444 | static int |
|
|
445 | fd_valid (int fd) |
|
|
446 | { |
|
|
447 | #ifdef WIN32 |
|
|
448 | return !!win32_get_osfhandle (fd); |
|
|
449 | #else |
|
|
450 | return fcntl (fd, F_GETFD) != -1; |
|
|
451 | #endif |
306 | } |
452 | } |
307 | |
453 | |
308 | /* called on EBADF to verify fds */ |
454 | /* called on EBADF to verify fds */ |
309 | static void |
455 | static void |
310 | fd_ebadf (void) |
456 | fd_ebadf (EV_P) |
311 | { |
457 | { |
312 | int fd; |
458 | int fd; |
313 | |
459 | |
314 | for (fd = 0; fd < anfdmax; ++fd) |
460 | for (fd = 0; fd < anfdmax; ++fd) |
315 | if (anfds [fd].events) |
461 | if (anfds [fd].events) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
462 | if (!fd_valid (fd) == -1 && errno == EBADF) |
317 | fd_kill (fd); |
463 | fd_kill (EV_A_ fd); |
318 | } |
464 | } |
319 | |
465 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
466 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
467 | static void |
322 | fd_enomem (void) |
468 | fd_enomem (EV_P) |
323 | { |
469 | { |
324 | int fd = anfdmax; |
470 | int fd; |
325 | |
471 | |
326 | while (fd--) |
472 | for (fd = anfdmax; fd--; ) |
327 | if (anfds [fd].events) |
473 | if (anfds [fd].events) |
328 | { |
474 | { |
329 | close (fd); |
|
|
330 | fd_kill (fd); |
475 | fd_kill (EV_A_ fd); |
331 | return; |
476 | return; |
332 | } |
477 | } |
333 | } |
478 | } |
334 | |
479 | |
|
|
480 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
|
|
481 | static void |
|
|
482 | fd_rearm_all (EV_P) |
|
|
483 | { |
|
|
484 | int fd; |
|
|
485 | |
|
|
486 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
487 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
488 | if (anfds [fd].events) |
|
|
489 | { |
|
|
490 | anfds [fd].events = 0; |
|
|
491 | fd_change (EV_A_ fd); |
|
|
492 | } |
|
|
493 | } |
|
|
494 | |
335 | /*****************************************************************************/ |
495 | /*****************************************************************************/ |
336 | |
496 | |
337 | static struct ev_timer **timers; |
|
|
338 | static int timermax, timercnt; |
|
|
339 | |
|
|
340 | static struct ev_periodic **periodics; |
|
|
341 | static int periodicmax, periodiccnt; |
|
|
342 | |
|
|
343 | static void |
497 | static void |
344 | upheap (WT *timers, int k) |
498 | upheap (WT *heap, int k) |
345 | { |
499 | { |
346 | WT w = timers [k]; |
500 | WT w = heap [k]; |
347 | |
501 | |
348 | while (k && timers [k >> 1]->at > w->at) |
502 | while (k && heap [k >> 1]->at > w->at) |
349 | { |
503 | { |
350 | timers [k] = timers [k >> 1]; |
504 | heap [k] = heap [k >> 1]; |
351 | timers [k]->active = k + 1; |
505 | ((W)heap [k])->active = k + 1; |
352 | k >>= 1; |
506 | k >>= 1; |
353 | } |
507 | } |
354 | |
508 | |
355 | timers [k] = w; |
509 | heap [k] = w; |
356 | timers [k]->active = k + 1; |
510 | ((W)heap [k])->active = k + 1; |
357 | |
511 | |
358 | } |
512 | } |
359 | |
513 | |
360 | static void |
514 | static void |
361 | downheap (WT *timers, int N, int k) |
515 | downheap (WT *heap, int N, int k) |
362 | { |
516 | { |
363 | WT w = timers [k]; |
517 | WT w = heap [k]; |
364 | |
518 | |
365 | while (k < (N >> 1)) |
519 | while (k < (N >> 1)) |
366 | { |
520 | { |
367 | int j = k << 1; |
521 | int j = k << 1; |
368 | |
522 | |
369 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
523 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
370 | ++j; |
524 | ++j; |
371 | |
525 | |
372 | if (w->at <= timers [j]->at) |
526 | if (w->at <= heap [j]->at) |
373 | break; |
527 | break; |
374 | |
528 | |
375 | timers [k] = timers [j]; |
529 | heap [k] = heap [j]; |
376 | timers [k]->active = k + 1; |
530 | ((W)heap [k])->active = k + 1; |
377 | k = j; |
531 | k = j; |
378 | } |
532 | } |
379 | |
533 | |
380 | timers [k] = w; |
534 | heap [k] = w; |
381 | timers [k]->active = k + 1; |
535 | ((W)heap [k])->active = k + 1; |
|
|
536 | } |
|
|
537 | |
|
|
538 | inline void |
|
|
539 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
540 | { |
|
|
541 | ev_tstamp old_at = heap [k]->at; |
|
|
542 | heap [k]->at = at; |
|
|
543 | |
|
|
544 | if (old_at < at) |
|
|
545 | downheap (heap, N, k); |
|
|
546 | else |
|
|
547 | upheap (heap, k); |
382 | } |
548 | } |
383 | |
549 | |
384 | /*****************************************************************************/ |
550 | /*****************************************************************************/ |
385 | |
551 | |
386 | typedef struct |
552 | typedef struct |
387 | { |
553 | { |
388 | struct ev_signal *head; |
554 | WL head; |
389 | sig_atomic_t volatile gotsig; |
555 | sig_atomic_t volatile gotsig; |
390 | } ANSIG; |
556 | } ANSIG; |
391 | |
557 | |
392 | static ANSIG *signals; |
558 | static ANSIG *signals; |
393 | static int signalmax; |
559 | static int signalmax; |
… | |
… | |
409 | } |
575 | } |
410 | |
576 | |
411 | static void |
577 | static void |
412 | sighandler (int signum) |
578 | sighandler (int signum) |
413 | { |
579 | { |
|
|
580 | #if WIN32 |
|
|
581 | signal (signum, sighandler); |
|
|
582 | #endif |
|
|
583 | |
414 | signals [signum - 1].gotsig = 1; |
584 | signals [signum - 1].gotsig = 1; |
415 | |
585 | |
416 | if (!gotsig) |
586 | if (!gotsig) |
417 | { |
587 | { |
418 | int old_errno = errno; |
588 | int old_errno = errno; |
419 | gotsig = 1; |
589 | gotsig = 1; |
|
|
590 | #ifdef WIN32 |
|
|
591 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
592 | #else |
420 | write (sigpipe [1], &signum, 1); |
593 | write (sigpipe [1], &signum, 1); |
|
|
594 | #endif |
421 | errno = old_errno; |
595 | errno = old_errno; |
422 | } |
596 | } |
423 | } |
597 | } |
424 | |
598 | |
|
|
599 | void |
|
|
600 | ev_feed_signal_event (EV_P_ int signum) |
|
|
601 | { |
|
|
602 | WL w; |
|
|
603 | |
|
|
604 | #if EV_MULTIPLICITY |
|
|
605 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
606 | #endif |
|
|
607 | |
|
|
608 | --signum; |
|
|
609 | |
|
|
610 | if (signum < 0 || signum >= signalmax) |
|
|
611 | return; |
|
|
612 | |
|
|
613 | signals [signum].gotsig = 0; |
|
|
614 | |
|
|
615 | for (w = signals [signum].head; w; w = w->next) |
|
|
616 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
617 | } |
|
|
618 | |
425 | static void |
619 | static void |
426 | sigcb (struct ev_io *iow, int revents) |
620 | sigcb (EV_P_ struct ev_io *iow, int revents) |
427 | { |
621 | { |
428 | struct ev_signal *w; |
|
|
429 | int signum; |
622 | int signum; |
430 | |
623 | |
|
|
624 | #ifdef WIN32 |
|
|
625 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
626 | #else |
431 | read (sigpipe [0], &revents, 1); |
627 | read (sigpipe [0], &revents, 1); |
|
|
628 | #endif |
432 | gotsig = 0; |
629 | gotsig = 0; |
433 | |
630 | |
434 | for (signum = signalmax; signum--; ) |
631 | for (signum = signalmax; signum--; ) |
435 | if (signals [signum].gotsig) |
632 | if (signals [signum].gotsig) |
436 | { |
633 | ev_feed_signal_event (EV_A_ signum + 1); |
437 | signals [signum].gotsig = 0; |
|
|
438 | |
|
|
439 | for (w = signals [signum].head; w; w = w->next) |
|
|
440 | event ((W)w, EV_SIGNAL); |
|
|
441 | } |
|
|
442 | } |
634 | } |
443 | |
635 | |
444 | static void |
636 | static void |
445 | siginit (void) |
637 | siginit (EV_P) |
446 | { |
638 | { |
447 | #ifndef WIN32 |
639 | #ifndef WIN32 |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
640 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
641 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
450 | |
642 | |
… | |
… | |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
644 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
645 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
454 | #endif |
646 | #endif |
455 | |
647 | |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
648 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
457 | ev_io_start (&sigev); |
649 | ev_io_start (EV_A_ &sigev); |
|
|
650 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
458 | } |
651 | } |
459 | |
652 | |
460 | /*****************************************************************************/ |
653 | /*****************************************************************************/ |
461 | |
654 | |
462 | static struct ev_idle **idles; |
|
|
463 | static int idlemax, idlecnt; |
|
|
464 | |
|
|
465 | static struct ev_prepare **prepares; |
|
|
466 | static int preparemax, preparecnt; |
|
|
467 | |
|
|
468 | static struct ev_check **checks; |
|
|
469 | static int checkmax, checkcnt; |
|
|
470 | |
|
|
471 | /*****************************************************************************/ |
|
|
472 | |
|
|
473 | static struct ev_child *childs [PID_HASHSIZE]; |
655 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
656 | |
|
|
657 | #ifndef WIN32 |
|
|
658 | |
474 | static struct ev_signal childev; |
659 | static struct ev_signal childev; |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
660 | |
478 | #ifndef WCONTINUED |
661 | #ifndef WCONTINUED |
479 | # define WCONTINUED 0 |
662 | # define WCONTINUED 0 |
480 | #endif |
663 | #endif |
481 | |
664 | |
482 | static void |
665 | static void |
483 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
666 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
484 | { |
667 | { |
485 | struct ev_child *w; |
668 | struct ev_child *w; |
486 | |
669 | |
487 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
670 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
488 | if (w->pid == pid || !w->pid) |
671 | if (w->pid == pid || !w->pid) |
489 | { |
672 | { |
490 | w->priority = sw->priority; /* need to do it *now* */ |
673 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
491 | w->rpid = pid; |
674 | w->rpid = pid; |
492 | w->rstatus = status; |
675 | w->rstatus = status; |
493 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
494 | event ((W)w, EV_CHILD); |
676 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
495 | } |
677 | } |
496 | } |
678 | } |
497 | |
679 | |
498 | static void |
680 | static void |
499 | childcb (struct ev_signal *sw, int revents) |
681 | childcb (EV_P_ struct ev_signal *sw, int revents) |
500 | { |
682 | { |
501 | int pid, status; |
683 | int pid, status; |
502 | |
684 | |
503 | printf ("chld %x\n", revents);//D |
|
|
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
685 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
505 | { |
686 | { |
506 | /* make sure we are called again until all childs have been reaped */ |
687 | /* make sure we are called again until all childs have been reaped */ |
507 | event ((W)sw, EV_SIGNAL); |
688 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
508 | |
689 | |
509 | child_reap (sw, pid, pid, status); |
690 | child_reap (EV_A_ sw, pid, pid, status); |
510 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
691 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
511 | } |
692 | } |
512 | } |
693 | } |
513 | |
694 | |
514 | #endif |
695 | #endif |
515 | |
696 | |
… | |
… | |
540 | return EV_VERSION_MINOR; |
721 | return EV_VERSION_MINOR; |
541 | } |
722 | } |
542 | |
723 | |
543 | /* return true if we are running with elevated privileges and should ignore env variables */ |
724 | /* return true if we are running with elevated privileges and should ignore env variables */ |
544 | static int |
725 | static int |
545 | enable_secure () |
726 | enable_secure (void) |
546 | { |
727 | { |
547 | #ifdef WIN32 |
728 | #ifdef WIN32 |
548 | return 0; |
729 | return 0; |
549 | #else |
730 | #else |
550 | return getuid () != geteuid () |
731 | return getuid () != geteuid () |
551 | || getgid () != getegid (); |
732 | || getgid () != getegid (); |
552 | #endif |
733 | #endif |
553 | } |
734 | } |
554 | |
735 | |
555 | int ev_init (int methods) |
736 | int |
|
|
737 | ev_method (EV_P) |
556 | { |
738 | { |
|
|
739 | return method; |
|
|
740 | } |
|
|
741 | |
|
|
742 | static void |
|
|
743 | loop_init (EV_P_ int methods) |
|
|
744 | { |
557 | if (!ev_method) |
745 | if (!method) |
558 | { |
746 | { |
559 | #if EV_USE_MONOTONIC |
747 | #if EV_USE_MONOTONIC |
560 | { |
748 | { |
561 | struct timespec ts; |
749 | struct timespec ts; |
562 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
750 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
563 | have_monotonic = 1; |
751 | have_monotonic = 1; |
564 | } |
752 | } |
565 | #endif |
753 | #endif |
566 | |
754 | |
567 | ev_now = ev_time (); |
755 | ev_rt_now = ev_time (); |
568 | now = get_clock (); |
756 | mn_now = get_clock (); |
569 | now_floor = now; |
757 | now_floor = mn_now; |
570 | diff = ev_now - now; |
758 | rtmn_diff = ev_rt_now - mn_now; |
571 | |
|
|
572 | if (pipe (sigpipe)) |
|
|
573 | return 0; |
|
|
574 | |
759 | |
575 | if (methods == EVMETHOD_AUTO) |
760 | if (methods == EVMETHOD_AUTO) |
576 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
761 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
577 | methods = atoi (getenv ("LIBEV_METHODS")); |
762 | methods = atoi (getenv ("LIBEV_METHODS")); |
578 | else |
763 | else |
579 | methods = EVMETHOD_ANY; |
764 | methods = EVMETHOD_ANY; |
580 | |
765 | |
581 | ev_method = 0; |
766 | method = 0; |
|
|
767 | #if EV_USE_WIN32 |
|
|
768 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
769 | #endif |
582 | #if EV_USE_KQUEUE |
770 | #if EV_USE_KQUEUE |
583 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
771 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
584 | #endif |
772 | #endif |
585 | #if EV_USE_EPOLL |
773 | #if EV_USE_EPOLL |
586 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
774 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
587 | #endif |
775 | #endif |
588 | #if EV_USE_POLL |
776 | #if EV_USE_POLL |
589 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
777 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
590 | #endif |
778 | #endif |
591 | #if EV_USE_SELECT |
779 | #if EV_USE_SELECT |
592 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
780 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
593 | #endif |
781 | #endif |
594 | |
782 | |
|
|
783 | ev_init (&sigev, sigcb); |
|
|
784 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
785 | } |
|
|
786 | } |
|
|
787 | |
|
|
788 | void |
|
|
789 | loop_destroy (EV_P) |
|
|
790 | { |
|
|
791 | int i; |
|
|
792 | |
|
|
793 | #if EV_USE_WIN32 |
|
|
794 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
795 | #endif |
|
|
796 | #if EV_USE_KQUEUE |
|
|
797 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
798 | #endif |
|
|
799 | #if EV_USE_EPOLL |
|
|
800 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
801 | #endif |
|
|
802 | #if EV_USE_POLL |
|
|
803 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
804 | #endif |
|
|
805 | #if EV_USE_SELECT |
|
|
806 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
807 | #endif |
|
|
808 | |
|
|
809 | for (i = NUMPRI; i--; ) |
|
|
810 | array_free (pending, [i]); |
|
|
811 | |
|
|
812 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
813 | array_free_microshit (fdchange); |
|
|
814 | array_free_microshit (timer); |
|
|
815 | #if EV_PERIODICS |
|
|
816 | array_free_microshit (periodic); |
|
|
817 | #endif |
|
|
818 | array_free_microshit (idle); |
|
|
819 | array_free_microshit (prepare); |
|
|
820 | array_free_microshit (check); |
|
|
821 | |
|
|
822 | method = 0; |
|
|
823 | } |
|
|
824 | |
|
|
825 | static void |
|
|
826 | loop_fork (EV_P) |
|
|
827 | { |
|
|
828 | #if EV_USE_EPOLL |
|
|
829 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
830 | #endif |
|
|
831 | #if EV_USE_KQUEUE |
|
|
832 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
833 | #endif |
|
|
834 | |
|
|
835 | if (ev_is_active (&sigev)) |
|
|
836 | { |
|
|
837 | /* default loop */ |
|
|
838 | |
|
|
839 | ev_ref (EV_A); |
|
|
840 | ev_io_stop (EV_A_ &sigev); |
|
|
841 | close (sigpipe [0]); |
|
|
842 | close (sigpipe [1]); |
|
|
843 | |
|
|
844 | while (pipe (sigpipe)) |
|
|
845 | syserr ("(libev) error creating pipe"); |
|
|
846 | |
|
|
847 | siginit (EV_A); |
|
|
848 | } |
|
|
849 | |
|
|
850 | postfork = 0; |
|
|
851 | } |
|
|
852 | |
|
|
853 | #if EV_MULTIPLICITY |
|
|
854 | struct ev_loop * |
|
|
855 | ev_loop_new (int methods) |
|
|
856 | { |
|
|
857 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
858 | |
|
|
859 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
860 | |
|
|
861 | loop_init (EV_A_ methods); |
|
|
862 | |
|
|
863 | if (ev_method (EV_A)) |
|
|
864 | return loop; |
|
|
865 | |
|
|
866 | return 0; |
|
|
867 | } |
|
|
868 | |
|
|
869 | void |
|
|
870 | ev_loop_destroy (EV_P) |
|
|
871 | { |
|
|
872 | loop_destroy (EV_A); |
|
|
873 | ev_free (loop); |
|
|
874 | } |
|
|
875 | |
|
|
876 | void |
|
|
877 | ev_loop_fork (EV_P) |
|
|
878 | { |
|
|
879 | postfork = 1; |
|
|
880 | } |
|
|
881 | |
|
|
882 | #endif |
|
|
883 | |
|
|
884 | #if EV_MULTIPLICITY |
|
|
885 | struct ev_loop * |
|
|
886 | #else |
|
|
887 | int |
|
|
888 | #endif |
|
|
889 | ev_default_loop (int methods) |
|
|
890 | { |
|
|
891 | if (sigpipe [0] == sigpipe [1]) |
|
|
892 | if (pipe (sigpipe)) |
|
|
893 | return 0; |
|
|
894 | |
|
|
895 | if (!default_loop) |
|
|
896 | { |
|
|
897 | #if EV_MULTIPLICITY |
|
|
898 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
899 | #else |
|
|
900 | default_loop = 1; |
|
|
901 | #endif |
|
|
902 | |
|
|
903 | loop_init (EV_A_ methods); |
|
|
904 | |
595 | if (ev_method) |
905 | if (ev_method (EV_A)) |
596 | { |
906 | { |
597 | ev_watcher_init (&sigev, sigcb); |
|
|
598 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
599 | siginit (); |
907 | siginit (EV_A); |
600 | |
908 | |
601 | #ifndef WIN32 |
909 | #ifndef WIN32 |
602 | ev_signal_init (&childev, childcb, SIGCHLD); |
910 | ev_signal_init (&childev, childcb, SIGCHLD); |
603 | ev_set_priority (&childev, EV_MAXPRI); |
911 | ev_set_priority (&childev, EV_MAXPRI); |
604 | ev_signal_start (&childev); |
912 | ev_signal_start (EV_A_ &childev); |
|
|
913 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
605 | #endif |
914 | #endif |
606 | } |
915 | } |
|
|
916 | else |
|
|
917 | default_loop = 0; |
607 | } |
918 | } |
608 | |
919 | |
609 | return ev_method; |
920 | return default_loop; |
|
|
921 | } |
|
|
922 | |
|
|
923 | void |
|
|
924 | ev_default_destroy (void) |
|
|
925 | { |
|
|
926 | #if EV_MULTIPLICITY |
|
|
927 | struct ev_loop *loop = default_loop; |
|
|
928 | #endif |
|
|
929 | |
|
|
930 | #ifndef WIN32 |
|
|
931 | ev_ref (EV_A); /* child watcher */ |
|
|
932 | ev_signal_stop (EV_A_ &childev); |
|
|
933 | #endif |
|
|
934 | |
|
|
935 | ev_ref (EV_A); /* signal watcher */ |
|
|
936 | ev_io_stop (EV_A_ &sigev); |
|
|
937 | |
|
|
938 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
939 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
940 | |
|
|
941 | loop_destroy (EV_A); |
|
|
942 | } |
|
|
943 | |
|
|
944 | void |
|
|
945 | ev_default_fork (void) |
|
|
946 | { |
|
|
947 | #if EV_MULTIPLICITY |
|
|
948 | struct ev_loop *loop = default_loop; |
|
|
949 | #endif |
|
|
950 | |
|
|
951 | if (method) |
|
|
952 | postfork = 1; |
610 | } |
953 | } |
611 | |
954 | |
612 | /*****************************************************************************/ |
955 | /*****************************************************************************/ |
613 | |
956 | |
614 | void |
|
|
615 | ev_fork_prepare (void) |
|
|
616 | { |
|
|
617 | /* nop */ |
|
|
618 | } |
|
|
619 | |
|
|
620 | void |
|
|
621 | ev_fork_parent (void) |
|
|
622 | { |
|
|
623 | /* nop */ |
|
|
624 | } |
|
|
625 | |
|
|
626 | void |
|
|
627 | ev_fork_child (void) |
|
|
628 | { |
|
|
629 | #if EV_USE_EPOLL |
|
|
630 | if (ev_method == EVMETHOD_EPOLL) |
|
|
631 | epoll_postfork_child (); |
|
|
632 | #endif |
|
|
633 | |
|
|
634 | ev_io_stop (&sigev); |
|
|
635 | close (sigpipe [0]); |
|
|
636 | close (sigpipe [1]); |
|
|
637 | pipe (sigpipe); |
|
|
638 | siginit (); |
|
|
639 | } |
|
|
640 | |
|
|
641 | /*****************************************************************************/ |
|
|
642 | |
|
|
643 | static void |
957 | static int |
|
|
958 | any_pending (EV_P) |
|
|
959 | { |
|
|
960 | int pri; |
|
|
961 | |
|
|
962 | for (pri = NUMPRI; pri--; ) |
|
|
963 | if (pendingcnt [pri]) |
|
|
964 | return 1; |
|
|
965 | |
|
|
966 | return 0; |
|
|
967 | } |
|
|
968 | |
|
|
969 | static void |
644 | call_pending (void) |
970 | call_pending (EV_P) |
645 | { |
971 | { |
646 | int pri; |
972 | int pri; |
647 | |
973 | |
648 | for (pri = NUMPRI; pri--; ) |
974 | for (pri = NUMPRI; pri--; ) |
649 | while (pendingcnt [pri]) |
975 | while (pendingcnt [pri]) |
… | |
… | |
651 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
977 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
652 | |
978 | |
653 | if (p->w) |
979 | if (p->w) |
654 | { |
980 | { |
655 | p->w->pending = 0; |
981 | p->w->pending = 0; |
656 | p->w->cb (p->w, p->events); |
982 | EV_CB_INVOKE (p->w, p->events); |
657 | } |
983 | } |
658 | } |
984 | } |
659 | } |
985 | } |
660 | |
986 | |
661 | static void |
987 | static void |
662 | timers_reify (void) |
988 | timers_reify (EV_P) |
663 | { |
989 | { |
664 | while (timercnt && timers [0]->at <= now) |
990 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
665 | { |
991 | { |
666 | struct ev_timer *w = timers [0]; |
992 | struct ev_timer *w = timers [0]; |
|
|
993 | |
|
|
994 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
667 | |
995 | |
668 | /* first reschedule or stop timer */ |
996 | /* first reschedule or stop timer */ |
669 | if (w->repeat) |
997 | if (w->repeat) |
670 | { |
998 | { |
671 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
999 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
1000 | |
672 | w->at = now + w->repeat; |
1001 | ((WT)w)->at += w->repeat; |
|
|
1002 | if (((WT)w)->at < mn_now) |
|
|
1003 | ((WT)w)->at = mn_now; |
|
|
1004 | |
673 | downheap ((WT *)timers, timercnt, 0); |
1005 | downheap ((WT *)timers, timercnt, 0); |
674 | } |
1006 | } |
675 | else |
1007 | else |
676 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
1008 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
677 | |
1009 | |
678 | event ((W)w, EV_TIMEOUT); |
1010 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
679 | } |
1011 | } |
680 | } |
1012 | } |
681 | |
1013 | |
|
|
1014 | #if EV_PERIODICS |
682 | static void |
1015 | static void |
683 | periodics_reify (void) |
1016 | periodics_reify (EV_P) |
684 | { |
1017 | { |
685 | while (periodiccnt && periodics [0]->at <= ev_now) |
1018 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
686 | { |
1019 | { |
687 | struct ev_periodic *w = periodics [0]; |
1020 | struct ev_periodic *w = periodics [0]; |
688 | |
1021 | |
|
|
1022 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
|
|
1023 | |
689 | /* first reschedule or stop timer */ |
1024 | /* first reschedule or stop timer */ |
690 | if (w->interval) |
1025 | if (w->reschedule_cb) |
691 | { |
1026 | { |
|
|
1027 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1028 | |
|
|
1029 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1031 | } |
|
|
1032 | else if (w->interval) |
|
|
1033 | { |
692 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
1034 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
693 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
1035 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
694 | downheap ((WT *)periodics, periodiccnt, 0); |
1036 | downheap ((WT *)periodics, periodiccnt, 0); |
695 | } |
1037 | } |
696 | else |
1038 | else |
697 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
1039 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
698 | |
1040 | |
699 | event ((W)w, EV_PERIODIC); |
1041 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
700 | } |
1042 | } |
701 | } |
1043 | } |
702 | |
1044 | |
703 | static void |
1045 | static void |
704 | periodics_reschedule (ev_tstamp diff) |
1046 | periodics_reschedule (EV_P) |
705 | { |
1047 | { |
706 | int i; |
1048 | int i; |
707 | |
1049 | |
708 | /* adjust periodics after time jump */ |
1050 | /* adjust periodics after time jump */ |
709 | for (i = 0; i < periodiccnt; ++i) |
1051 | for (i = 0; i < periodiccnt; ++i) |
710 | { |
1052 | { |
711 | struct ev_periodic *w = periodics [i]; |
1053 | struct ev_periodic *w = periodics [i]; |
712 | |
1054 | |
|
|
1055 | if (w->reschedule_cb) |
|
|
1056 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
713 | if (w->interval) |
1057 | else if (w->interval) |
714 | { |
|
|
715 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
1058 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
716 | |
|
|
717 | if (fabs (diff) >= 1e-4) |
|
|
718 | { |
|
|
719 | ev_periodic_stop (w); |
|
|
720 | ev_periodic_start (w); |
|
|
721 | |
|
|
722 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
723 | } |
|
|
724 | } |
|
|
725 | } |
1059 | } |
726 | } |
|
|
727 | |
1060 | |
728 | static int |
1061 | /* now rebuild the heap */ |
|
|
1062 | for (i = periodiccnt >> 1; i--; ) |
|
|
1063 | downheap ((WT *)periodics, periodiccnt, i); |
|
|
1064 | } |
|
|
1065 | #endif |
|
|
1066 | |
|
|
1067 | inline int |
729 | time_update_monotonic (void) |
1068 | time_update_monotonic (EV_P) |
730 | { |
1069 | { |
731 | now = get_clock (); |
1070 | mn_now = get_clock (); |
732 | |
1071 | |
733 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
1072 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
734 | { |
1073 | { |
735 | ev_now = now + diff; |
1074 | ev_rt_now = rtmn_diff + mn_now; |
736 | return 0; |
1075 | return 0; |
737 | } |
1076 | } |
738 | else |
1077 | else |
739 | { |
1078 | { |
740 | now_floor = now; |
1079 | now_floor = mn_now; |
741 | ev_now = ev_time (); |
1080 | ev_rt_now = ev_time (); |
742 | return 1; |
1081 | return 1; |
743 | } |
1082 | } |
744 | } |
1083 | } |
745 | |
1084 | |
746 | static void |
1085 | static void |
747 | time_update (void) |
1086 | time_update (EV_P) |
748 | { |
1087 | { |
749 | int i; |
1088 | int i; |
750 | |
1089 | |
751 | #if EV_USE_MONOTONIC |
1090 | #if EV_USE_MONOTONIC |
752 | if (expect_true (have_monotonic)) |
1091 | if (expect_true (have_monotonic)) |
753 | { |
1092 | { |
754 | if (time_update_monotonic ()) |
1093 | if (time_update_monotonic (EV_A)) |
755 | { |
1094 | { |
756 | ev_tstamp odiff = diff; |
1095 | ev_tstamp odiff = rtmn_diff; |
757 | |
1096 | |
758 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1097 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
759 | { |
1098 | { |
760 | diff = ev_now - now; |
1099 | rtmn_diff = ev_rt_now - mn_now; |
761 | |
1100 | |
762 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
1101 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
763 | return; /* all is well */ |
1102 | return; /* all is well */ |
764 | |
1103 | |
765 | ev_now = ev_time (); |
1104 | ev_rt_now = ev_time (); |
766 | now = get_clock (); |
1105 | mn_now = get_clock (); |
767 | now_floor = now; |
1106 | now_floor = mn_now; |
768 | } |
1107 | } |
769 | |
1108 | |
|
|
1109 | # if EV_PERIODICS |
770 | periodics_reschedule (diff - odiff); |
1110 | periodics_reschedule (EV_A); |
|
|
1111 | # endif |
771 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1112 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
1113 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
772 | } |
1114 | } |
773 | } |
1115 | } |
774 | else |
1116 | else |
775 | #endif |
1117 | #endif |
776 | { |
1118 | { |
777 | ev_now = ev_time (); |
1119 | ev_rt_now = ev_time (); |
778 | |
1120 | |
779 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1121 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
780 | { |
1122 | { |
|
|
1123 | #if EV_PERIODICS |
781 | periodics_reschedule (ev_now - now); |
1124 | periodics_reschedule (EV_A); |
|
|
1125 | #endif |
782 | |
1126 | |
783 | /* adjust timers. this is easy, as the offset is the same for all */ |
1127 | /* adjust timers. this is easy, as the offset is the same for all */ |
784 | for (i = 0; i < timercnt; ++i) |
1128 | for (i = 0; i < timercnt; ++i) |
785 | timers [i]->at += diff; |
1129 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
786 | } |
1130 | } |
787 | |
1131 | |
788 | now = ev_now; |
1132 | mn_now = ev_rt_now; |
789 | } |
1133 | } |
790 | } |
1134 | } |
791 | |
1135 | |
792 | int ev_loop_done; |
1136 | void |
|
|
1137 | ev_ref (EV_P) |
|
|
1138 | { |
|
|
1139 | ++activecnt; |
|
|
1140 | } |
793 | |
1141 | |
|
|
1142 | void |
|
|
1143 | ev_unref (EV_P) |
|
|
1144 | { |
|
|
1145 | --activecnt; |
|
|
1146 | } |
|
|
1147 | |
|
|
1148 | static int loop_done; |
|
|
1149 | |
|
|
1150 | void |
794 | void ev_loop (int flags) |
1151 | ev_loop (EV_P_ int flags) |
795 | { |
1152 | { |
796 | double block; |
1153 | double block; |
797 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1154 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
798 | |
1155 | |
799 | do |
1156 | do |
800 | { |
1157 | { |
801 | /* queue check watchers (and execute them) */ |
1158 | /* queue check watchers (and execute them) */ |
802 | if (expect_false (preparecnt)) |
1159 | if (expect_false (preparecnt)) |
803 | { |
1160 | { |
804 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
1161 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
805 | call_pending (); |
1162 | call_pending (EV_A); |
806 | } |
1163 | } |
807 | |
1164 | |
|
|
1165 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1166 | if (expect_false (postfork)) |
|
|
1167 | loop_fork (EV_A); |
|
|
1168 | |
808 | /* update fd-related kernel structures */ |
1169 | /* update fd-related kernel structures */ |
809 | fd_reify (); |
1170 | fd_reify (EV_A); |
810 | |
1171 | |
811 | /* calculate blocking time */ |
1172 | /* calculate blocking time */ |
812 | |
1173 | |
813 | /* we only need this for !monotonic clockor timers, but as we basically |
1174 | /* we only need this for !monotonic clock or timers, but as we basically |
814 | always have timers, we just calculate it always */ |
1175 | always have timers, we just calculate it always */ |
815 | #if EV_USE_MONOTONIC |
1176 | #if EV_USE_MONOTONIC |
816 | if (expect_true (have_monotonic)) |
1177 | if (expect_true (have_monotonic)) |
817 | time_update_monotonic (); |
1178 | time_update_monotonic (EV_A); |
818 | else |
1179 | else |
819 | #endif |
1180 | #endif |
820 | { |
1181 | { |
821 | ev_now = ev_time (); |
1182 | ev_rt_now = ev_time (); |
822 | now = ev_now; |
1183 | mn_now = ev_rt_now; |
823 | } |
1184 | } |
824 | |
1185 | |
825 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1186 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
826 | block = 0.; |
1187 | block = 0.; |
827 | else |
1188 | else |
828 | { |
1189 | { |
829 | block = MAX_BLOCKTIME; |
1190 | block = MAX_BLOCKTIME; |
830 | |
1191 | |
831 | if (timercnt) |
1192 | if (timercnt) |
832 | { |
1193 | { |
833 | ev_tstamp to = timers [0]->at - now + method_fudge; |
1194 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
834 | if (block > to) block = to; |
1195 | if (block > to) block = to; |
835 | } |
1196 | } |
836 | |
1197 | |
|
|
1198 | #if EV_PERIODICS |
837 | if (periodiccnt) |
1199 | if (periodiccnt) |
838 | { |
1200 | { |
839 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
1201 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
840 | if (block > to) block = to; |
1202 | if (block > to) block = to; |
841 | } |
1203 | } |
|
|
1204 | #endif |
842 | |
1205 | |
843 | if (block < 0.) block = 0.; |
1206 | if (block < 0.) block = 0.; |
844 | } |
1207 | } |
845 | |
1208 | |
846 | method_poll (block); |
1209 | method_poll (EV_A_ block); |
847 | |
1210 | |
848 | /* update ev_now, do magic */ |
1211 | /* update ev_rt_now, do magic */ |
849 | time_update (); |
1212 | time_update (EV_A); |
850 | |
1213 | |
851 | /* queue pending timers and reschedule them */ |
1214 | /* queue pending timers and reschedule them */ |
852 | timers_reify (); /* relative timers called last */ |
1215 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1216 | #if EV_PERIODICS |
853 | periodics_reify (); /* absolute timers called first */ |
1217 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1218 | #endif |
854 | |
1219 | |
855 | /* queue idle watchers unless io or timers are pending */ |
1220 | /* queue idle watchers unless io or timers are pending */ |
856 | if (!pendingcnt) |
1221 | if (idlecnt && !any_pending (EV_A)) |
857 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
1222 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
858 | |
1223 | |
859 | /* queue check watchers, to be executed first */ |
1224 | /* queue check watchers, to be executed first */ |
860 | if (checkcnt) |
1225 | if (checkcnt) |
861 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
1226 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
862 | |
1227 | |
863 | call_pending (); |
1228 | call_pending (EV_A); |
864 | } |
1229 | } |
865 | while (!ev_loop_done); |
1230 | while (activecnt && !loop_done); |
866 | |
1231 | |
867 | if (ev_loop_done != 2) |
1232 | if (loop_done != 2) |
868 | ev_loop_done = 0; |
1233 | loop_done = 0; |
|
|
1234 | } |
|
|
1235 | |
|
|
1236 | void |
|
|
1237 | ev_unloop (EV_P_ int how) |
|
|
1238 | { |
|
|
1239 | loop_done = how; |
869 | } |
1240 | } |
870 | |
1241 | |
871 | /*****************************************************************************/ |
1242 | /*****************************************************************************/ |
872 | |
1243 | |
873 | static void |
1244 | inline void |
874 | wlist_add (WL *head, WL elem) |
1245 | wlist_add (WL *head, WL elem) |
875 | { |
1246 | { |
876 | elem->next = *head; |
1247 | elem->next = *head; |
877 | *head = elem; |
1248 | *head = elem; |
878 | } |
1249 | } |
879 | |
1250 | |
880 | static void |
1251 | inline void |
881 | wlist_del (WL *head, WL elem) |
1252 | wlist_del (WL *head, WL elem) |
882 | { |
1253 | { |
883 | while (*head) |
1254 | while (*head) |
884 | { |
1255 | { |
885 | if (*head == elem) |
1256 | if (*head == elem) |
… | |
… | |
890 | |
1261 | |
891 | head = &(*head)->next; |
1262 | head = &(*head)->next; |
892 | } |
1263 | } |
893 | } |
1264 | } |
894 | |
1265 | |
895 | static void |
1266 | inline void |
896 | ev_clear_pending (W w) |
1267 | ev_clear_pending (EV_P_ W w) |
897 | { |
1268 | { |
898 | if (w->pending) |
1269 | if (w->pending) |
899 | { |
1270 | { |
900 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1271 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
901 | w->pending = 0; |
1272 | w->pending = 0; |
902 | } |
1273 | } |
903 | } |
1274 | } |
904 | |
1275 | |
905 | static void |
1276 | inline void |
906 | ev_start (W w, int active) |
1277 | ev_start (EV_P_ W w, int active) |
907 | { |
1278 | { |
908 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1279 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
909 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1280 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
910 | |
1281 | |
911 | w->active = active; |
1282 | w->active = active; |
|
|
1283 | ev_ref (EV_A); |
912 | } |
1284 | } |
913 | |
1285 | |
914 | static void |
1286 | inline void |
915 | ev_stop (W w) |
1287 | ev_stop (EV_P_ W w) |
916 | { |
1288 | { |
|
|
1289 | ev_unref (EV_A); |
917 | w->active = 0; |
1290 | w->active = 0; |
918 | } |
1291 | } |
919 | |
1292 | |
920 | /*****************************************************************************/ |
1293 | /*****************************************************************************/ |
921 | |
1294 | |
922 | void |
1295 | void |
923 | ev_io_start (struct ev_io *w) |
1296 | ev_io_start (EV_P_ struct ev_io *w) |
924 | { |
1297 | { |
925 | int fd = w->fd; |
1298 | int fd = w->fd; |
926 | |
1299 | |
927 | if (ev_is_active (w)) |
1300 | if (ev_is_active (w)) |
928 | return; |
1301 | return; |
929 | |
1302 | |
930 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1303 | assert (("ev_io_start called with negative fd", fd >= 0)); |
931 | |
1304 | |
932 | ev_start ((W)w, 1); |
1305 | ev_start (EV_A_ (W)w, 1); |
933 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1306 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
934 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1307 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
935 | |
1308 | |
936 | fd_change (fd); |
1309 | fd_change (EV_A_ fd); |
937 | } |
1310 | } |
938 | |
1311 | |
939 | void |
1312 | void |
940 | ev_io_stop (struct ev_io *w) |
1313 | ev_io_stop (EV_P_ struct ev_io *w) |
941 | { |
1314 | { |
942 | ev_clear_pending ((W)w); |
1315 | ev_clear_pending (EV_A_ (W)w); |
943 | if (!ev_is_active (w)) |
1316 | if (!ev_is_active (w)) |
944 | return; |
1317 | return; |
945 | |
1318 | |
|
|
1319 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1320 | |
946 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1321 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
947 | ev_stop ((W)w); |
1322 | ev_stop (EV_A_ (W)w); |
948 | |
1323 | |
949 | fd_change (w->fd); |
1324 | fd_change (EV_A_ w->fd); |
950 | } |
1325 | } |
951 | |
1326 | |
952 | void |
1327 | void |
953 | ev_timer_start (struct ev_timer *w) |
1328 | ev_timer_start (EV_P_ struct ev_timer *w) |
954 | { |
1329 | { |
955 | if (ev_is_active (w)) |
1330 | if (ev_is_active (w)) |
956 | return; |
1331 | return; |
957 | |
1332 | |
958 | w->at += now; |
1333 | ((WT)w)->at += mn_now; |
959 | |
1334 | |
960 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1335 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
961 | |
1336 | |
962 | ev_start ((W)w, ++timercnt); |
1337 | ev_start (EV_A_ (W)w, ++timercnt); |
963 | array_needsize (timers, timermax, timercnt, ); |
1338 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
964 | timers [timercnt - 1] = w; |
1339 | timers [timercnt - 1] = w; |
965 | upheap ((WT *)timers, timercnt - 1); |
1340 | upheap ((WT *)timers, timercnt - 1); |
966 | } |
|
|
967 | |
1341 | |
|
|
1342 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1343 | } |
|
|
1344 | |
968 | void |
1345 | void |
969 | ev_timer_stop (struct ev_timer *w) |
1346 | ev_timer_stop (EV_P_ struct ev_timer *w) |
970 | { |
1347 | { |
971 | ev_clear_pending ((W)w); |
1348 | ev_clear_pending (EV_A_ (W)w); |
972 | if (!ev_is_active (w)) |
1349 | if (!ev_is_active (w)) |
973 | return; |
1350 | return; |
974 | |
1351 | |
|
|
1352 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1353 | |
975 | if (w->active < timercnt--) |
1354 | if (((W)w)->active < timercnt--) |
976 | { |
1355 | { |
977 | timers [w->active - 1] = timers [timercnt]; |
1356 | timers [((W)w)->active - 1] = timers [timercnt]; |
978 | downheap ((WT *)timers, timercnt, w->active - 1); |
1357 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
979 | } |
1358 | } |
980 | |
1359 | |
981 | w->at = w->repeat; |
1360 | ((WT)w)->at -= mn_now; |
982 | |
1361 | |
983 | ev_stop ((W)w); |
1362 | ev_stop (EV_A_ (W)w); |
984 | } |
1363 | } |
985 | |
1364 | |
986 | void |
1365 | void |
987 | ev_timer_again (struct ev_timer *w) |
1366 | ev_timer_again (EV_P_ struct ev_timer *w) |
988 | { |
1367 | { |
989 | if (ev_is_active (w)) |
1368 | if (ev_is_active (w)) |
990 | { |
1369 | { |
991 | if (w->repeat) |
1370 | if (w->repeat) |
992 | { |
|
|
993 | w->at = now + w->repeat; |
|
|
994 | downheap ((WT *)timers, timercnt, w->active - 1); |
1371 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
995 | } |
|
|
996 | else |
1372 | else |
997 | ev_timer_stop (w); |
1373 | ev_timer_stop (EV_A_ w); |
998 | } |
1374 | } |
999 | else if (w->repeat) |
1375 | else if (w->repeat) |
1000 | ev_timer_start (w); |
1376 | ev_timer_start (EV_A_ w); |
1001 | } |
1377 | } |
1002 | |
1378 | |
|
|
1379 | #if EV_PERIODICS |
1003 | void |
1380 | void |
1004 | ev_periodic_start (struct ev_periodic *w) |
1381 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1005 | { |
1382 | { |
1006 | if (ev_is_active (w)) |
1383 | if (ev_is_active (w)) |
1007 | return; |
1384 | return; |
1008 | |
1385 | |
|
|
1386 | if (w->reschedule_cb) |
|
|
1387 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1388 | else if (w->interval) |
|
|
1389 | { |
1009 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1390 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1010 | |
|
|
1011 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1391 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1012 | if (w->interval) |
|
|
1013 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1392 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1393 | } |
1014 | |
1394 | |
1015 | ev_start ((W)w, ++periodiccnt); |
1395 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1016 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1396 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1017 | periodics [periodiccnt - 1] = w; |
1397 | periodics [periodiccnt - 1] = w; |
1018 | upheap ((WT *)periodics, periodiccnt - 1); |
1398 | upheap ((WT *)periodics, periodiccnt - 1); |
1019 | } |
|
|
1020 | |
1399 | |
|
|
1400 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1401 | } |
|
|
1402 | |
1021 | void |
1403 | void |
1022 | ev_periodic_stop (struct ev_periodic *w) |
1404 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1023 | { |
1405 | { |
1024 | ev_clear_pending ((W)w); |
1406 | ev_clear_pending (EV_A_ (W)w); |
1025 | if (!ev_is_active (w)) |
1407 | if (!ev_is_active (w)) |
1026 | return; |
1408 | return; |
1027 | |
1409 | |
|
|
1410 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1411 | |
1028 | if (w->active < periodiccnt--) |
1412 | if (((W)w)->active < periodiccnt--) |
1029 | { |
1413 | { |
1030 | periodics [w->active - 1] = periodics [periodiccnt]; |
1414 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1031 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1415 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1032 | } |
1416 | } |
1033 | |
1417 | |
1034 | ev_stop ((W)w); |
1418 | ev_stop (EV_A_ (W)w); |
|
|
1419 | } |
|
|
1420 | |
|
|
1421 | void |
|
|
1422 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1423 | { |
|
|
1424 | /* TODO: use adjustheap and recalculation */ |
|
|
1425 | ev_periodic_stop (EV_A_ w); |
|
|
1426 | ev_periodic_start (EV_A_ w); |
|
|
1427 | } |
|
|
1428 | #endif |
|
|
1429 | |
|
|
1430 | void |
|
|
1431 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1432 | { |
|
|
1433 | if (ev_is_active (w)) |
|
|
1434 | return; |
|
|
1435 | |
|
|
1436 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1437 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
|
|
1438 | idles [idlecnt - 1] = w; |
|
|
1439 | } |
|
|
1440 | |
|
|
1441 | void |
|
|
1442 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1443 | { |
|
|
1444 | ev_clear_pending (EV_A_ (W)w); |
|
|
1445 | if (ev_is_active (w)) |
|
|
1446 | return; |
|
|
1447 | |
|
|
1448 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1449 | ev_stop (EV_A_ (W)w); |
|
|
1450 | } |
|
|
1451 | |
|
|
1452 | void |
|
|
1453 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1454 | { |
|
|
1455 | if (ev_is_active (w)) |
|
|
1456 | return; |
|
|
1457 | |
|
|
1458 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1459 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
|
|
1460 | prepares [preparecnt - 1] = w; |
|
|
1461 | } |
|
|
1462 | |
|
|
1463 | void |
|
|
1464 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1465 | { |
|
|
1466 | ev_clear_pending (EV_A_ (W)w); |
|
|
1467 | if (ev_is_active (w)) |
|
|
1468 | return; |
|
|
1469 | |
|
|
1470 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1471 | ev_stop (EV_A_ (W)w); |
|
|
1472 | } |
|
|
1473 | |
|
|
1474 | void |
|
|
1475 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1476 | { |
|
|
1477 | if (ev_is_active (w)) |
|
|
1478 | return; |
|
|
1479 | |
|
|
1480 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1481 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
|
|
1482 | checks [checkcnt - 1] = w; |
|
|
1483 | } |
|
|
1484 | |
|
|
1485 | void |
|
|
1486 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1487 | { |
|
|
1488 | ev_clear_pending (EV_A_ (W)w); |
|
|
1489 | if (!ev_is_active (w)) |
|
|
1490 | return; |
|
|
1491 | |
|
|
1492 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
|
|
1493 | ev_stop (EV_A_ (W)w); |
1035 | } |
1494 | } |
1036 | |
1495 | |
1037 | #ifndef SA_RESTART |
1496 | #ifndef SA_RESTART |
1038 | # define SA_RESTART 0 |
1497 | # define SA_RESTART 0 |
1039 | #endif |
1498 | #endif |
1040 | |
1499 | |
1041 | void |
1500 | void |
1042 | ev_signal_start (struct ev_signal *w) |
1501 | ev_signal_start (EV_P_ struct ev_signal *w) |
1043 | { |
1502 | { |
|
|
1503 | #if EV_MULTIPLICITY |
|
|
1504 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1505 | #endif |
1044 | if (ev_is_active (w)) |
1506 | if (ev_is_active (w)) |
1045 | return; |
1507 | return; |
1046 | |
1508 | |
1047 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1509 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1048 | |
1510 | |
1049 | ev_start ((W)w, 1); |
1511 | ev_start (EV_A_ (W)w, 1); |
1050 | array_needsize (signals, signalmax, w->signum, signals_init); |
1512 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1051 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1513 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1052 | |
1514 | |
1053 | if (!w->next) |
1515 | if (!((WL)w)->next) |
1054 | { |
1516 | { |
|
|
1517 | #if WIN32 |
|
|
1518 | signal (w->signum, sighandler); |
|
|
1519 | #else |
1055 | struct sigaction sa; |
1520 | struct sigaction sa; |
1056 | sa.sa_handler = sighandler; |
1521 | sa.sa_handler = sighandler; |
1057 | sigfillset (&sa.sa_mask); |
1522 | sigfillset (&sa.sa_mask); |
1058 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1523 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1059 | sigaction (w->signum, &sa, 0); |
1524 | sigaction (w->signum, &sa, 0); |
|
|
1525 | #endif |
1060 | } |
1526 | } |
1061 | } |
1527 | } |
1062 | |
1528 | |
1063 | void |
1529 | void |
1064 | ev_signal_stop (struct ev_signal *w) |
1530 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1065 | { |
1531 | { |
1066 | ev_clear_pending ((W)w); |
1532 | ev_clear_pending (EV_A_ (W)w); |
1067 | if (!ev_is_active (w)) |
1533 | if (!ev_is_active (w)) |
1068 | return; |
1534 | return; |
1069 | |
1535 | |
1070 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1536 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1071 | ev_stop ((W)w); |
1537 | ev_stop (EV_A_ (W)w); |
1072 | |
1538 | |
1073 | if (!signals [w->signum - 1].head) |
1539 | if (!signals [w->signum - 1].head) |
1074 | signal (w->signum, SIG_DFL); |
1540 | signal (w->signum, SIG_DFL); |
1075 | } |
1541 | } |
1076 | |
1542 | |
1077 | void |
1543 | void |
1078 | ev_idle_start (struct ev_idle *w) |
1544 | ev_child_start (EV_P_ struct ev_child *w) |
1079 | { |
1545 | { |
|
|
1546 | #if EV_MULTIPLICITY |
|
|
1547 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1548 | #endif |
1080 | if (ev_is_active (w)) |
1549 | if (ev_is_active (w)) |
1081 | return; |
1550 | return; |
1082 | |
1551 | |
1083 | ev_start ((W)w, ++idlecnt); |
|
|
1084 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1085 | idles [idlecnt - 1] = w; |
|
|
1086 | } |
|
|
1087 | |
|
|
1088 | void |
|
|
1089 | ev_idle_stop (struct ev_idle *w) |
|
|
1090 | { |
|
|
1091 | ev_clear_pending ((W)w); |
|
|
1092 | if (ev_is_active (w)) |
|
|
1093 | return; |
|
|
1094 | |
|
|
1095 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1096 | ev_stop ((W)w); |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | void |
|
|
1100 | ev_prepare_start (struct ev_prepare *w) |
|
|
1101 | { |
|
|
1102 | if (ev_is_active (w)) |
|
|
1103 | return; |
|
|
1104 | |
|
|
1105 | ev_start ((W)w, ++preparecnt); |
|
|
1106 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1107 | prepares [preparecnt - 1] = w; |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | void |
|
|
1111 | ev_prepare_stop (struct ev_prepare *w) |
|
|
1112 | { |
|
|
1113 | ev_clear_pending ((W)w); |
|
|
1114 | if (ev_is_active (w)) |
|
|
1115 | return; |
|
|
1116 | |
|
|
1117 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1118 | ev_stop ((W)w); |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | void |
|
|
1122 | ev_check_start (struct ev_check *w) |
|
|
1123 | { |
|
|
1124 | if (ev_is_active (w)) |
|
|
1125 | return; |
|
|
1126 | |
|
|
1127 | ev_start ((W)w, ++checkcnt); |
|
|
1128 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1129 | checks [checkcnt - 1] = w; |
|
|
1130 | } |
|
|
1131 | |
|
|
1132 | void |
|
|
1133 | ev_check_stop (struct ev_check *w) |
|
|
1134 | { |
|
|
1135 | ev_clear_pending ((W)w); |
|
|
1136 | if (ev_is_active (w)) |
|
|
1137 | return; |
|
|
1138 | |
|
|
1139 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1140 | ev_stop ((W)w); |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | void |
|
|
1144 | ev_child_start (struct ev_child *w) |
|
|
1145 | { |
|
|
1146 | if (ev_is_active (w)) |
|
|
1147 | return; |
|
|
1148 | |
|
|
1149 | ev_start ((W)w, 1); |
1552 | ev_start (EV_A_ (W)w, 1); |
1150 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1553 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1151 | } |
1554 | } |
1152 | |
1555 | |
1153 | void |
1556 | void |
1154 | ev_child_stop (struct ev_child *w) |
1557 | ev_child_stop (EV_P_ struct ev_child *w) |
1155 | { |
1558 | { |
1156 | ev_clear_pending ((W)w); |
1559 | ev_clear_pending (EV_A_ (W)w); |
1157 | if (ev_is_active (w)) |
1560 | if (!ev_is_active (w)) |
1158 | return; |
1561 | return; |
1159 | |
1562 | |
1160 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1563 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1161 | ev_stop ((W)w); |
1564 | ev_stop (EV_A_ (W)w); |
1162 | } |
1565 | } |
1163 | |
1566 | |
1164 | /*****************************************************************************/ |
1567 | /*****************************************************************************/ |
1165 | |
1568 | |
1166 | struct ev_once |
1569 | struct ev_once |
… | |
… | |
1170 | void (*cb)(int revents, void *arg); |
1573 | void (*cb)(int revents, void *arg); |
1171 | void *arg; |
1574 | void *arg; |
1172 | }; |
1575 | }; |
1173 | |
1576 | |
1174 | static void |
1577 | static void |
1175 | once_cb (struct ev_once *once, int revents) |
1578 | once_cb (EV_P_ struct ev_once *once, int revents) |
1176 | { |
1579 | { |
1177 | void (*cb)(int revents, void *arg) = once->cb; |
1580 | void (*cb)(int revents, void *arg) = once->cb; |
1178 | void *arg = once->arg; |
1581 | void *arg = once->arg; |
1179 | |
1582 | |
1180 | ev_io_stop (&once->io); |
1583 | ev_io_stop (EV_A_ &once->io); |
1181 | ev_timer_stop (&once->to); |
1584 | ev_timer_stop (EV_A_ &once->to); |
1182 | free (once); |
1585 | ev_free (once); |
1183 | |
1586 | |
1184 | cb (revents, arg); |
1587 | cb (revents, arg); |
1185 | } |
1588 | } |
1186 | |
1589 | |
1187 | static void |
1590 | static void |
1188 | once_cb_io (struct ev_io *w, int revents) |
1591 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1189 | { |
1592 | { |
1190 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1593 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1191 | } |
1594 | } |
1192 | |
1595 | |
1193 | static void |
1596 | static void |
1194 | once_cb_to (struct ev_timer *w, int revents) |
1597 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1195 | { |
1598 | { |
1196 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1599 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1197 | } |
1600 | } |
1198 | |
1601 | |
1199 | void |
1602 | void |
1200 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1603 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1201 | { |
1604 | { |
1202 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1605 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1203 | |
1606 | |
1204 | if (!once) |
1607 | if (!once) |
1205 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1608 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1206 | else |
1609 | else |
1207 | { |
1610 | { |
1208 | once->cb = cb; |
1611 | once->cb = cb; |
1209 | once->arg = arg; |
1612 | once->arg = arg; |
1210 | |
1613 | |
1211 | ev_watcher_init (&once->io, once_cb_io); |
1614 | ev_init (&once->io, once_cb_io); |
1212 | if (fd >= 0) |
1615 | if (fd >= 0) |
1213 | { |
1616 | { |
1214 | ev_io_set (&once->io, fd, events); |
1617 | ev_io_set (&once->io, fd, events); |
1215 | ev_io_start (&once->io); |
1618 | ev_io_start (EV_A_ &once->io); |
1216 | } |
1619 | } |
1217 | |
1620 | |
1218 | ev_watcher_init (&once->to, once_cb_to); |
1621 | ev_init (&once->to, once_cb_to); |
1219 | if (timeout >= 0.) |
1622 | if (timeout >= 0.) |
1220 | { |
1623 | { |
1221 | ev_timer_set (&once->to, timeout, 0.); |
1624 | ev_timer_set (&once->to, timeout, 0.); |
1222 | ev_timer_start (&once->to); |
1625 | ev_timer_start (EV_A_ &once->to); |
1223 | } |
1626 | } |
1224 | } |
1627 | } |
1225 | } |
1628 | } |
1226 | |
1629 | |
1227 | /*****************************************************************************/ |
1630 | #ifdef __cplusplus |
1228 | |
|
|
1229 | #if 0 |
|
|
1230 | |
|
|
1231 | struct ev_io wio; |
|
|
1232 | |
|
|
1233 | static void |
|
|
1234 | sin_cb (struct ev_io *w, int revents) |
|
|
1235 | { |
|
|
1236 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1237 | } |
1631 | } |
1238 | |
|
|
1239 | static void |
|
|
1240 | ocb (struct ev_timer *w, int revents) |
|
|
1241 | { |
|
|
1242 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1243 | ev_timer_stop (w); |
|
|
1244 | ev_timer_start (w); |
|
|
1245 | } |
|
|
1246 | |
|
|
1247 | static void |
|
|
1248 | scb (struct ev_signal *w, int revents) |
|
|
1249 | { |
|
|
1250 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1251 | ev_io_stop (&wio); |
|
|
1252 | ev_io_start (&wio); |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | static void |
|
|
1256 | gcb (struct ev_signal *w, int revents) |
|
|
1257 | { |
|
|
1258 | fprintf (stderr, "generic %x\n", revents); |
|
|
1259 | |
|
|
1260 | } |
|
|
1261 | |
|
|
1262 | int main (void) |
|
|
1263 | { |
|
|
1264 | ev_init (0); |
|
|
1265 | |
|
|
1266 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1267 | ev_io_start (&wio); |
|
|
1268 | |
|
|
1269 | struct ev_timer t[10000]; |
|
|
1270 | |
|
|
1271 | #if 0 |
|
|
1272 | int i; |
|
|
1273 | for (i = 0; i < 10000; ++i) |
|
|
1274 | { |
|
|
1275 | struct ev_timer *w = t + i; |
|
|
1276 | ev_watcher_init (w, ocb, i); |
|
|
1277 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1278 | ev_timer_start (w); |
|
|
1279 | if (drand48 () < 0.5) |
|
|
1280 | ev_timer_stop (w); |
|
|
1281 | } |
|
|
1282 | #endif |
1632 | #endif |
1283 | |
1633 | |
1284 | struct ev_timer t1; |
|
|
1285 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1286 | ev_timer_start (&t1); |
|
|
1287 | |
|
|
1288 | struct ev_signal sig; |
|
|
1289 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1290 | ev_signal_start (&sig); |
|
|
1291 | |
|
|
1292 | struct ev_check cw; |
|
|
1293 | ev_check_init (&cw, gcb); |
|
|
1294 | ev_check_start (&cw); |
|
|
1295 | |
|
|
1296 | struct ev_idle iw; |
|
|
1297 | ev_idle_init (&iw, gcb); |
|
|
1298 | ev_idle_start (&iw); |
|
|
1299 | |
|
|
1300 | ev_loop (0); |
|
|
1301 | |
|
|
1302 | return 0; |
|
|
1303 | } |
|
|
1304 | |
|
|
1305 | #endif |
|
|
1306 | |
|
|
1307 | |
|
|
1308 | |
|
|
1309 | |
|
|