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

Comparing libev/ev.c (file contents):
Revision 1.22 by root, Wed Oct 31 19:07:43 2007 UTC vs.
Revision 1.35 by root, Thu Nov 1 11:55:54 2007 UTC

24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
29 32
30#include <math.h> 33#include <math.h>
31#include <stdlib.h> 34#include <stdlib.h>
32#include <unistd.h> 35#include <unistd.h>
33#include <fcntl.h> 36#include <fcntl.h>
41#include <sys/types.h> 44#include <sys/types.h>
42#include <sys/wait.h> 45#include <sys/wait.h>
43#include <sys/time.h> 46#include <sys/time.h>
44#include <time.h> 47#include <time.h>
45 48
46#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
49# endif 52# endif
50#endif 53#endif
51 54
52#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
54#endif 57#endif
55 58
56#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
58#endif 61#endif
59 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
60#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 68#endif
63 69
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
67 74
68#include "ev.h" 75#include "ev.h"
69 76
70typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 91/*****************************************************************************/
85 92
86ev_tstamp 93ev_tstamp
87ev_time (void) 94ev_time (void)
88{ 95{
89#if HAVE_REALTIME 96#if EV_USE_REALTIME
90 struct timespec ts; 97 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 100#else
94 struct timeval tv; 101 struct timeval tv;
98} 105}
99 106
100static ev_tstamp 107static ev_tstamp
101get_clock (void) 108get_clock (void)
102{ 109{
103#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
104 if (have_monotonic) 111 if (have_monotonic)
105 { 112 {
106 struct timespec ts; 113 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
110#endif 117#endif
111 118
112 return ev_time (); 119 return ev_time ();
113} 120}
114 121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
123
115#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
117 { \ 126 { \
118 int newcnt = cur ? cur << 1 : 16; \ 127 int newcnt = cur; \
128 do \
129 { \
130 newcnt = array_roundsize (base, newcnt << 1); \
131 } \
132 while ((cnt) > newcnt); \
133 \
119 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
120 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
121 cur = newcnt; \ 136 cur = newcnt; \
122 } 137 }
123 138
124/*****************************************************************************/ 139/*****************************************************************************/
125 140
126typedef struct 141typedef struct
127{ 142{
128 struct ev_io *head; 143 struct ev_io *head;
129 unsigned char wev, rev; /* want, received event set */ 144 unsigned char events;
145 unsigned char reify;
130} ANFD; 146} ANFD;
131 147
132static ANFD *anfds; 148static ANFD *anfds;
133static int anfdmax; 149static int anfdmax;
134 150
135static int *fdchanges;
136static int fdchangemax, fdchangecnt;
137
138static void 151static void
139anfds_init (ANFD *base, int count) 152anfds_init (ANFD *base, int count)
140{ 153{
141 while (count--) 154 while (count--)
142 { 155 {
143 base->head = 0; 156 base->head = 0;
144 base->wev = base->rev = EV_NONE; 157 base->events = EV_NONE;
158 base->reify = 0;
159
145 ++base; 160 ++base;
146 } 161 }
147} 162}
148 163
149typedef struct 164typedef struct
156static int pendingmax, pendingcnt; 171static int pendingmax, pendingcnt;
157 172
158static void 173static void
159event (W w, int events) 174event (W w, int events)
160{ 175{
161 if (w->active) 176 if (w->pending)
177 {
178 pendings [w->pending - 1].events |= events;
179 return;
162 { 180 }
181
163 w->pending = ++pendingcnt; 182 w->pending = ++pendingcnt;
164 array_needsize (pendings, pendingmax, pendingcnt, ); 183 array_needsize (pendings, pendingmax, pendingcnt, );
165 pendings [pendingcnt - 1].w = w; 184 pendings [pendingcnt - 1].w = w;
166 pendings [pendingcnt - 1].events = events; 185 pendings [pendingcnt - 1].events = events;
167 } 186}
187
188static void
189queue_events (W *events, int eventcnt, int type)
190{
191 int i;
192
193 for (i = 0; i < eventcnt; ++i)
194 event (events [i], type);
168} 195}
169 196
170static void 197static void
171fd_event (int fd, int events) 198fd_event (int fd, int events)
172{ 199{
180 if (ev) 207 if (ev)
181 event ((W)w, ev); 208 event ((W)w, ev);
182 } 209 }
183} 210}
184 211
212/*****************************************************************************/
213
214static int *fdchanges;
215static int fdchangemax, fdchangecnt;
216
185static void 217static void
186queue_events (W *events, int eventcnt, int type) 218fd_reify (void)
187{ 219{
188 int i; 220 int i;
189 221
190 for (i = 0; i < eventcnt; ++i) 222 for (i = 0; i < fdchangecnt; ++i)
191 event (events [i], type); 223 {
224 int fd = fdchanges [i];
225 ANFD *anfd = anfds + fd;
226 struct ev_io *w;
227
228 int events = 0;
229
230 for (w = anfd->head; w; w = w->next)
231 events |= w->events;
232
233 anfd->reify = 0;
234
235 if (anfd->events != events)
236 {
237 method_modify (fd, anfd->events, events);
238 anfd->events = events;
239 }
240 }
241
242 fdchangecnt = 0;
243}
244
245static void
246fd_change (int fd)
247{
248 if (anfds [fd].reify || fdchangecnt < 0)
249 return;
250
251 anfds [fd].reify = 1;
252
253 ++fdchangecnt;
254 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
255 fdchanges [fdchangecnt - 1] = fd;
192} 256}
193 257
194/* called on EBADF to verify fds */ 258/* called on EBADF to verify fds */
195static void 259static void
196fd_recheck () 260fd_recheck (void)
197{ 261{
198 int fd; 262 int fd;
199 263
200 for (fd = 0; fd < anfdmax; ++fd) 264 for (fd = 0; fd < anfdmax; ++fd)
201 if (anfds [fd].wev) 265 if (anfds [fd].events)
202 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
203 while (anfds [fd].head) 267 while (anfds [fd].head)
268 {
204 evio_stop (anfds [fd].head); 269 ev_io_stop (anfds [fd].head);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
271 }
205} 272}
206 273
207/*****************************************************************************/ 274/*****************************************************************************/
208 275
209static struct ev_timer **timers; 276static struct ev_timer **timers;
256/*****************************************************************************/ 323/*****************************************************************************/
257 324
258typedef struct 325typedef struct
259{ 326{
260 struct ev_signal *head; 327 struct ev_signal *head;
261 sig_atomic_t gotsig; 328 sig_atomic_t volatile gotsig;
262} ANSIG; 329} ANSIG;
263 330
264static ANSIG *signals; 331static ANSIG *signals;
265static int signalmax; 332static int signalmax;
266 333
267static int sigpipe [2]; 334static int sigpipe [2];
268static sig_atomic_t gotsig; 335static sig_atomic_t volatile gotsig;
269static struct ev_io sigev; 336static struct ev_io sigev;
270 337
271static void 338static void
272signals_init (ANSIG *base, int count) 339signals_init (ANSIG *base, int count)
273{ 340{
274 while (count--) 341 while (count--)
275 { 342 {
276 base->head = 0; 343 base->head = 0;
277 base->gotsig = 0; 344 base->gotsig = 0;
345
278 ++base; 346 ++base;
279 } 347 }
280} 348}
281 349
282static void 350static void
285 signals [signum - 1].gotsig = 1; 353 signals [signum - 1].gotsig = 1;
286 354
287 if (!gotsig) 355 if (!gotsig)
288 { 356 {
289 gotsig = 1; 357 gotsig = 1;
290 write (sigpipe [1], &gotsig, 1); 358 write (sigpipe [1], &signum, 1);
291 } 359 }
292} 360}
293 361
294static void 362static void
295sigcb (struct ev_io *iow, int revents) 363sigcb (struct ev_io *iow, int revents)
296{ 364{
297 struct ev_signal *w; 365 struct ev_signal *w;
298 int sig; 366 int sig;
299 367
368 read (sigpipe [0], &revents, 1);
300 gotsig = 0; 369 gotsig = 0;
301 read (sigpipe [0], &revents, 1);
302 370
303 for (sig = signalmax; sig--; ) 371 for (sig = signalmax; sig--; )
304 if (signals [sig].gotsig) 372 if (signals [sig].gotsig)
305 { 373 {
306 signals [sig].gotsig = 0; 374 signals [sig].gotsig = 0;
318 386
319 /* rather than sort out wether we really need nb, set it */ 387 /* rather than sort out wether we really need nb, set it */
320 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
321 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
322 390
323 evio_set (&sigev, sigpipe [0], EV_READ); 391 ev_io_set (&sigev, sigpipe [0], EV_READ);
324 evio_start (&sigev); 392 ev_io_start (&sigev);
325} 393}
326 394
327/*****************************************************************************/ 395/*****************************************************************************/
328 396
329static struct ev_idle **idles; 397static struct ev_idle **idles;
359 } 427 }
360} 428}
361 429
362/*****************************************************************************/ 430/*****************************************************************************/
363 431
364#if HAVE_EPOLL 432#if EV_USE_EPOLL
365# include "ev_epoll.c" 433# include "ev_epoll.c"
366#endif 434#endif
367#if HAVE_SELECT 435#if EV_USE_SELECT
368# include "ev_select.c" 436# include "ev_select.c"
369#endif 437#endif
370 438
439int
440ev_version_major (void)
441{
442 return EV_VERSION_MAJOR;
443}
444
445int
446ev_version_minor (void)
447{
448 return EV_VERSION_MINOR;
449}
450
371int ev_init (int flags) 451int ev_init (int flags)
372{ 452{
373#if HAVE_MONOTONIC
374 {
375 struct timespec ts;
376 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
377 have_monotonic = 1;
378 }
379#endif
380
381 ev_now = ev_time ();
382 now = get_clock ();
383 diff = ev_now - now;
384
385 if (pipe (sigpipe))
386 return 0;
387
388 ev_method = EVMETHOD_NONE;
389#if HAVE_EPOLL
390 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
391#endif
392#if HAVE_SELECT
393 if (ev_method == EVMETHOD_NONE) select_init (flags);
394#endif
395
396 if (ev_method) 453 if (!ev_method)
454 {
455#if EV_USE_MONOTONIC
397 { 456 {
457 struct timespec ts;
458 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
459 have_monotonic = 1;
460 }
461#endif
462
463 ev_now = ev_time ();
464 now = get_clock ();
465 diff = ev_now - now;
466
467 if (pipe (sigpipe))
468 return 0;
469
470 ev_method = EVMETHOD_NONE;
471#if EV_USE_EPOLL
472 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
473#endif
474#if EV_USE_SELECT
475 if (ev_method == EVMETHOD_NONE) select_init (flags);
476#endif
477
478 if (ev_method)
479 {
398 evw_init (&sigev, sigcb); 480 ev_watcher_init (&sigev, sigcb);
399 siginit (); 481 siginit ();
400 482
401 evsignal_init (&childev, childcb, SIGCHLD); 483 ev_signal_init (&childev, childcb, SIGCHLD);
402 evsignal_start (&childev); 484 ev_signal_start (&childev);
485 }
403 } 486 }
404 487
405 return ev_method; 488 return ev_method;
406} 489}
407 490
408/*****************************************************************************/ 491/*****************************************************************************/
409 492
410void ev_prefork (void) 493void
494ev_fork_prepare (void)
411{ 495{
412 /* nop */ 496 /* nop */
413} 497}
414 498
499void
415void ev_postfork_parent (void) 500ev_fork_parent (void)
416{ 501{
417 /* nop */ 502 /* nop */
418} 503}
419 504
505void
420void ev_postfork_child (void) 506ev_fork_child (void)
421{ 507{
422#if HAVE_EPOLL 508#if EV_USE_EPOLL
423 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
424 epoll_postfork_child (); 510 epoll_postfork_child ();
425#endif 511#endif
426 512
427 evio_stop (&sigev); 513 ev_io_stop (&sigev);
428 close (sigpipe [0]); 514 close (sigpipe [0]);
429 close (sigpipe [1]); 515 close (sigpipe [1]);
430 pipe (sigpipe); 516 pipe (sigpipe);
431 siginit (); 517 siginit ();
432} 518}
433 519
434/*****************************************************************************/ 520/*****************************************************************************/
435 521
436static void 522static void
437fd_reify (void)
438{
439 int i;
440
441 for (i = 0; i < fdchangecnt; ++i)
442 {
443 int fd = fdchanges [i];
444 ANFD *anfd = anfds + fd;
445 struct ev_io *w;
446
447 int wev = 0;
448
449 for (w = anfd->head; w; w = w->next)
450 wev |= w->events;
451
452 if (anfd->wev != wev)
453 {
454 method_modify (fd, anfd->wev, wev);
455 anfd->wev = wev;
456 }
457 }
458
459 fdchangecnt = 0;
460}
461
462static void
463call_pending () 523call_pending (void)
464{ 524{
465 while (pendingcnt) 525 while (pendingcnt)
466 { 526 {
467 ANPENDING *p = pendings + --pendingcnt; 527 ANPENDING *p = pendings + --pendingcnt;
468 528
473 } 533 }
474 } 534 }
475} 535}
476 536
477static void 537static void
478timers_reify () 538timers_reify (void)
479{ 539{
480 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
481 { 541 {
482 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
483
484 event ((W)w, EV_TIMEOUT);
485 543
486 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
487 if (w->repeat) 545 if (w->repeat)
488 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
489 w->at = now + w->repeat; 548 w->at = now + w->repeat;
490 assert (("timer timeout in the past, negative repeat?", w->at > now));
491 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
492 } 550 }
493 else 551 else
494 evtimer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
495 }
496}
497 553
554 event ((W)w, EV_TIMEOUT);
555 }
556}
557
498static void 558static void
499periodics_reify () 559periodics_reify (void)
500{ 560{
501 while (periodiccnt && periodics [0]->at <= ev_now) 561 while (periodiccnt && periodics [0]->at <= ev_now)
502 { 562 {
503 struct ev_periodic *w = periodics [0]; 563 struct ev_periodic *w = periodics [0];
504 564
505 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
506 if (w->interval) 566 if (w->interval)
507 { 567 {
508 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 568 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
509 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 569 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
510 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
511 } 571 }
512 else 572 else
513 evperiodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
514 574
515 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
516 } 576 }
517} 577}
518 578
519static void 579static void
520periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
530 { 590 {
531 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 591 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
532 592
533 if (fabs (diff) >= 1e-4) 593 if (fabs (diff) >= 1e-4)
534 { 594 {
535 evperiodic_stop (w); 595 ev_periodic_stop (w);
536 evperiodic_start (w); 596 ev_periodic_start (w);
537 597
538 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 598 i = 0; /* restart loop, inefficient, but time jumps should be rare */
539 } 599 }
540 } 600 }
541 } 601 }
542} 602}
543 603
544static void 604static void
545time_update () 605time_update (void)
546{ 606{
547 int i; 607 int i;
548 608
549 ev_now = ev_time (); 609 ev_now = ev_time ();
550 610
584int ev_loop_done; 644int ev_loop_done;
585 645
586void ev_loop (int flags) 646void ev_loop (int flags)
587{ 647{
588 double block; 648 double block;
589 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
590 650
591 do 651 do
592 { 652 {
593 /* queue check watchers (and execute them) */ 653 /* queue check watchers (and execute them) */
594 if (preparecnt) 654 if (preparecnt)
675 head = &(*head)->next; 735 head = &(*head)->next;
676 } 736 }
677} 737}
678 738
679static void 739static void
680ev_clear (W w) 740ev_clear_pending (W w)
681{ 741{
682 if (w->pending) 742 if (w->pending)
683 { 743 {
684 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
685 w->pending = 0; 745 w->pending = 0;
699} 759}
700 760
701/*****************************************************************************/ 761/*****************************************************************************/
702 762
703void 763void
704evio_start (struct ev_io *w) 764ev_io_start (struct ev_io *w)
705{ 765{
706 if (ev_is_active (w)) 766 if (ev_is_active (w))
707 return; 767 return;
708 768
709 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
710 772
711 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
712 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
713 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
714 776
715 ++fdchangecnt; 777 fd_change (fd);
716 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
717 fdchanges [fdchangecnt - 1] = fd;
718} 778}
719 779
720void 780void
721evio_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
722{ 782{
723 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
724 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
725 return; 785 return;
726 786
727 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
728 ev_stop ((W)w); 788 ev_stop ((W)w);
729 789
730 ++fdchangecnt; 790 fd_change (w->fd);
731 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
732 fdchanges [fdchangecnt - 1] = w->fd;
733} 791}
734 792
735void 793void
736evtimer_start (struct ev_timer *w) 794ev_timer_start (struct ev_timer *w)
737{ 795{
738 if (ev_is_active (w)) 796 if (ev_is_active (w))
739 return; 797 return;
740 798
741 w->at += now; 799 w->at += now;
742 800
743 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 801 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
744 802
745 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
746 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
747 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
748 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
749} 807}
750 808
751void 809void
752evtimer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
753{ 811{
754 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
755 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
756 return; 814 return;
757 815
758 if (w->active < timercnt--) 816 if (w->active < timercnt--)
759 { 817 {
765 823
766 ev_stop ((W)w); 824 ev_stop ((W)w);
767} 825}
768 826
769void 827void
770evtimer_again (struct ev_timer *w) 828ev_timer_again (struct ev_timer *w)
771{ 829{
772 if (ev_is_active (w)) 830 if (ev_is_active (w))
773 { 831 {
774 if (w->repeat) 832 if (w->repeat)
775 { 833 {
776 w->at = now + w->repeat; 834 w->at = now + w->repeat;
777 downheap ((WT *)timers, timercnt, w->active - 1); 835 downheap ((WT *)timers, timercnt, w->active - 1);
778 } 836 }
779 else 837 else
780 evtimer_stop (w); 838 ev_timer_stop (w);
781 } 839 }
782 else if (w->repeat) 840 else if (w->repeat)
783 evtimer_start (w); 841 ev_timer_start (w);
784} 842}
785 843
786void 844void
787evperiodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
788{ 846{
789 if (ev_is_active (w)) 847 if (ev_is_active (w))
790 return; 848 return;
791 849
792 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 850 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
793 851
794 /* this formula differs from the one in periodic_reify because we do not always round up */ 852 /* this formula differs from the one in periodic_reify because we do not always round up */
795 if (w->interval) 853 if (w->interval)
796 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
797 855
800 periodics [periodiccnt - 1] = w; 858 periodics [periodiccnt - 1] = w;
801 upheap ((WT *)periodics, periodiccnt - 1); 859 upheap ((WT *)periodics, periodiccnt - 1);
802} 860}
803 861
804void 862void
805evperiodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
806{ 864{
807 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
808 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
809 return; 867 return;
810 868
811 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
812 { 870 {
816 874
817 ev_stop ((W)w); 875 ev_stop ((W)w);
818} 876}
819 877
820void 878void
821evsignal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
822{ 880{
823 if (ev_is_active (w)) 881 if (ev_is_active (w))
824 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
825 885
826 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
827 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
828 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
829 889
836 sigaction (w->signum, &sa, 0); 896 sigaction (w->signum, &sa, 0);
837 } 897 }
838} 898}
839 899
840void 900void
841evsignal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
842{ 902{
843 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
844 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
845 return; 905 return;
846 906
847 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
848 ev_stop ((W)w); 908 ev_stop ((W)w);
849 909
850 if (!signals [w->signum - 1].head) 910 if (!signals [w->signum - 1].head)
851 signal (w->signum, SIG_DFL); 911 signal (w->signum, SIG_DFL);
852} 912}
853 913
914void
854void evidle_start (struct ev_idle *w) 915ev_idle_start (struct ev_idle *w)
855{ 916{
856 if (ev_is_active (w)) 917 if (ev_is_active (w))
857 return; 918 return;
858 919
859 ev_start ((W)w, ++idlecnt); 920 ev_start ((W)w, ++idlecnt);
860 array_needsize (idles, idlemax, idlecnt, ); 921 array_needsize (idles, idlemax, idlecnt, );
861 idles [idlecnt - 1] = w; 922 idles [idlecnt - 1] = w;
862} 923}
863 924
925void
864void evidle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
865{ 927{
866 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
867 if (ev_is_active (w)) 929 if (ev_is_active (w))
868 return; 930 return;
869 931
870 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
871 ev_stop ((W)w); 933 ev_stop ((W)w);
872} 934}
873 935
936void
874void evprepare_start (struct ev_prepare *w) 937ev_prepare_start (struct ev_prepare *w)
875{ 938{
876 if (ev_is_active (w)) 939 if (ev_is_active (w))
877 return; 940 return;
878 941
879 ev_start ((W)w, ++preparecnt); 942 ev_start ((W)w, ++preparecnt);
880 array_needsize (prepares, preparemax, preparecnt, ); 943 array_needsize (prepares, preparemax, preparecnt, );
881 prepares [preparecnt - 1] = w; 944 prepares [preparecnt - 1] = w;
882} 945}
883 946
947void
884void evprepare_stop (struct ev_prepare *w) 948ev_prepare_stop (struct ev_prepare *w)
885{ 949{
886 ev_clear ((W)w); 950 ev_clear_pending ((W)w);
887 if (ev_is_active (w)) 951 if (ev_is_active (w))
888 return; 952 return;
889 953
890 prepares [w->active - 1] = prepares [--preparecnt]; 954 prepares [w->active - 1] = prepares [--preparecnt];
891 ev_stop ((W)w); 955 ev_stop ((W)w);
892} 956}
893 957
958void
894void evcheck_start (struct ev_check *w) 959ev_check_start (struct ev_check *w)
895{ 960{
896 if (ev_is_active (w)) 961 if (ev_is_active (w))
897 return; 962 return;
898 963
899 ev_start ((W)w, ++checkcnt); 964 ev_start ((W)w, ++checkcnt);
900 array_needsize (checks, checkmax, checkcnt, ); 965 array_needsize (checks, checkmax, checkcnt, );
901 checks [checkcnt - 1] = w; 966 checks [checkcnt - 1] = w;
902} 967}
903 968
969void
904void evcheck_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
905{ 971{
906 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
907 if (ev_is_active (w)) 973 if (ev_is_active (w))
908 return; 974 return;
909 975
910 checks [w->active - 1] = checks [--checkcnt]; 976 checks [w->active - 1] = checks [--checkcnt];
911 ev_stop ((W)w); 977 ev_stop ((W)w);
912} 978}
913 979
980void
914void evchild_start (struct ev_child *w) 981ev_child_start (struct ev_child *w)
915{ 982{
916 if (ev_is_active (w)) 983 if (ev_is_active (w))
917 return; 984 return;
918 985
919 ev_start ((W)w, 1); 986 ev_start ((W)w, 1);
920 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 987 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
921} 988}
922 989
990void
923void evchild_stop (struct ev_child *w) 991ev_child_stop (struct ev_child *w)
924{ 992{
925 ev_clear ((W)w); 993 ev_clear_pending ((W)w);
926 if (ev_is_active (w)) 994 if (ev_is_active (w))
927 return; 995 return;
928 996
929 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 997 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
930 ev_stop ((W)w); 998 ev_stop ((W)w);
944once_cb (struct ev_once *once, int revents) 1012once_cb (struct ev_once *once, int revents)
945{ 1013{
946 void (*cb)(int revents, void *arg) = once->cb; 1014 void (*cb)(int revents, void *arg) = once->cb;
947 void *arg = once->arg; 1015 void *arg = once->arg;
948 1016
949 evio_stop (&once->io); 1017 ev_io_stop (&once->io);
950 evtimer_stop (&once->to); 1018 ev_timer_stop (&once->to);
951 free (once); 1019 free (once);
952 1020
953 cb (revents, arg); 1021 cb (revents, arg);
954} 1022}
955 1023
969ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1037ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
970{ 1038{
971 struct ev_once *once = malloc (sizeof (struct ev_once)); 1039 struct ev_once *once = malloc (sizeof (struct ev_once));
972 1040
973 if (!once) 1041 if (!once)
974 cb (EV_ERROR, arg); 1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
975 else 1043 else
976 { 1044 {
977 once->cb = cb; 1045 once->cb = cb;
978 once->arg = arg; 1046 once->arg = arg;
979 1047
980 evw_init (&once->io, once_cb_io); 1048 ev_watcher_init (&once->io, once_cb_io);
981
982 if (fd >= 0) 1049 if (fd >= 0)
983 { 1050 {
984 evio_set (&once->io, fd, events); 1051 ev_io_set (&once->io, fd, events);
985 evio_start (&once->io); 1052 ev_io_start (&once->io);
986 } 1053 }
987 1054
988 evw_init (&once->to, once_cb_to); 1055 ev_watcher_init (&once->to, once_cb_to);
989
990 if (timeout >= 0.) 1056 if (timeout >= 0.)
991 { 1057 {
992 evtimer_set (&once->to, timeout, 0.); 1058 ev_timer_set (&once->to, timeout, 0.);
993 evtimer_start (&once->to); 1059 ev_timer_start (&once->to);
994 } 1060 }
995 } 1061 }
996} 1062}
997 1063
998/*****************************************************************************/ 1064/*****************************************************************************/
1009 1075
1010static void 1076static void
1011ocb (struct ev_timer *w, int revents) 1077ocb (struct ev_timer *w, int revents)
1012{ 1078{
1013 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1079 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1014 evtimer_stop (w); 1080 ev_timer_stop (w);
1015 evtimer_start (w); 1081 ev_timer_start (w);
1016} 1082}
1017 1083
1018static void 1084static void
1019scb (struct ev_signal *w, int revents) 1085scb (struct ev_signal *w, int revents)
1020{ 1086{
1021 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1022 evio_stop (&wio); 1088 ev_io_stop (&wio);
1023 evio_start (&wio); 1089 ev_io_start (&wio);
1024} 1090}
1025 1091
1026static void 1092static void
1027gcb (struct ev_signal *w, int revents) 1093gcb (struct ev_signal *w, int revents)
1028{ 1094{
1032 1098
1033int main (void) 1099int main (void)
1034{ 1100{
1035 ev_init (0); 1101 ev_init (0);
1036 1102
1037 evio_init (&wio, sin_cb, 0, EV_READ); 1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
1038 evio_start (&wio); 1104 ev_io_start (&wio);
1039 1105
1040 struct ev_timer t[10000]; 1106 struct ev_timer t[10000];
1041 1107
1042#if 0 1108#if 0
1043 int i; 1109 int i;
1044 for (i = 0; i < 10000; ++i) 1110 for (i = 0; i < 10000; ++i)
1045 { 1111 {
1046 struct ev_timer *w = t + i; 1112 struct ev_timer *w = t + i;
1047 evw_init (w, ocb, i); 1113 ev_watcher_init (w, ocb, i);
1048 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1049 evtimer_start (w); 1115 ev_timer_start (w);
1050 if (drand48 () < 0.5) 1116 if (drand48 () < 0.5)
1051 evtimer_stop (w); 1117 ev_timer_stop (w);
1052 } 1118 }
1053#endif 1119#endif
1054 1120
1055 struct ev_timer t1; 1121 struct ev_timer t1;
1056 evtimer_init (&t1, ocb, 5, 10); 1122 ev_timer_init (&t1, ocb, 5, 10);
1057 evtimer_start (&t1); 1123 ev_timer_start (&t1);
1058 1124
1059 struct ev_signal sig; 1125 struct ev_signal sig;
1060 evsignal_init (&sig, scb, SIGQUIT); 1126 ev_signal_init (&sig, scb, SIGQUIT);
1061 evsignal_start (&sig); 1127 ev_signal_start (&sig);
1062 1128
1063 struct ev_check cw; 1129 struct ev_check cw;
1064 evcheck_init (&cw, gcb); 1130 ev_check_init (&cw, gcb);
1065 evcheck_start (&cw); 1131 ev_check_start (&cw);
1066 1132
1067 struct ev_idle iw; 1133 struct ev_idle iw;
1068 evidle_init (&iw, gcb); 1134 ev_idle_init (&iw, gcb);
1069 evidle_start (&iw); 1135 ev_idle_start (&iw);
1070 1136
1071 ev_loop (0); 1137 ev_loop (0);
1072 1138
1073 return 0; 1139 return 0;
1074} 1140}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines