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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines