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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines