ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.3 by root, Tue Oct 30 21:45:00 2007 UTC vs.
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines