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

Comparing libev/ev.c (file contents):
Revision 1.13 by root, Wed Oct 31 10:50:05 2007 UTC vs.
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC

1/*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
32
1#include <math.h> 33#include <math.h>
2#include <stdlib.h> 34#include <stdlib.h>
3#include <unistd.h> 35#include <unistd.h>
4#include <fcntl.h> 36#include <fcntl.h>
5#include <signal.h> 37#include <signal.h>
38#include <stddef.h>
6 39
7#include <stdio.h> 40#include <stdio.h>
8 41
9#include <assert.h> 42#include <assert.h>
10#include <errno.h> 43#include <errno.h>
44#include <sys/types.h>
45#include <sys/wait.h>
11#include <sys/time.h> 46#include <sys/time.h>
12#include <time.h> 47#include <time.h>
13 48
14#define HAVE_EPOLL 1
15
16#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
17# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
18# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
19# endif 52# endif
20#endif 53#endif
21 54
22#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
23# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
24#endif 57#endif
25 58
26#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
27# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
28#endif 61#endif
29 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
30#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
31# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
32#endif 68#endif
33 69
34#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) */
35#define MAX_BLOCKTIME 60. 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
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 */
36 74
37#include "ev.h" 75#include "ev.h"
38 76
39typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
40typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
53/*****************************************************************************/ 91/*****************************************************************************/
54 92
55ev_tstamp 93ev_tstamp
56ev_time (void) 94ev_time (void)
57{ 95{
58#if HAVE_REALTIME 96#if EV_USE_REALTIME
59 struct timespec ts; 97 struct timespec ts;
60 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
61 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
62#else 100#else
63 struct timeval tv; 101 struct timeval tv;
67} 105}
68 106
69static ev_tstamp 107static ev_tstamp
70get_clock (void) 108get_clock (void)
71{ 109{
72#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
73 if (have_monotonic) 111 if (have_monotonic)
74 { 112 {
75 struct timespec ts; 113 struct timespec ts;
76 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
77 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
79#endif 117#endif
80 118
81 return ev_time (); 119 return ev_time ();
82} 120}
83 121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
123
84#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
85 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
86 { \ 126 { \
87 int newcnt = cur ? cur << 1 : 16; \ 127 int newcnt = cur; \
88 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ 128 do \
129 { \
130 newcnt = array_roundsize (base, newcnt << 1); \
131 } \
132 while ((cnt) > newcnt); \
133 \
89 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
91 cur = newcnt; \ 136 cur = newcnt; \
92 } 137 }
93 138
94/*****************************************************************************/ 139/*****************************************************************************/
95 140
96typedef struct 141typedef struct
97{ 142{
98 struct ev_io *head; 143 struct ev_io *head;
99 unsigned char wev, rev; /* want, received event set */ 144 int events;
100} ANFD; 145} ANFD;
101 146
102static ANFD *anfds; 147static ANFD *anfds;
103static int anfdmax; 148static int anfdmax;
104 149
105static int *fdchanges;
106static int fdchangemax, fdchangecnt;
107
108static void 150static void
109anfds_init (ANFD *base, int count) 151anfds_init (ANFD *base, int count)
110{ 152{
111 while (count--) 153 while (count--)
112 { 154 {
113 base->head = 0; 155 base->head = 0;
114 base->wev = base->rev = EV_NONE; 156 base->events = EV_NONE;
115 ++base; 157 ++base;
116 } 158 }
117} 159}
118 160
119typedef struct 161typedef struct
126static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
127 169
128static void 170static void
129event (W w, int events) 171event (W w, int events)
130{ 172{
173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
177 }
178
131 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
132 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
133 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
134 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
135} 183}
136 184
137static void 185static void
186queue_events (W *events, int eventcnt, int type)
187{
188 int i;
189
190 for (i = 0; i < eventcnt; ++i)
191 event (events [i], type);
192}
193
194static void
138fd_event (int fd, int events) 195fd_event (int fd, int events)
139{ 196{
140 ANFD *anfd = anfds + fd; 197 ANFD *anfd = anfds + fd;
141 struct ev_io *w; 198 struct ev_io *w;
142 199
147 if (ev) 204 if (ev)
148 event ((W)w, ev); 205 event ((W)w, ev);
149 } 206 }
150} 207}
151 208
209/*****************************************************************************/
210
211static int *fdchanges;
212static int fdchangemax, fdchangecnt;
213
152static void 214static void
153queue_events (W *events, int eventcnt, int type) 215fd_reify (void)
154{ 216{
155 int i; 217 int i;
156 218
157 for (i = 0; i < eventcnt; ++i) 219 for (i = 0; i < fdchangecnt; ++i)
158 event (events [i], type); 220 {
221 int fd = fdchanges [i];
222 ANFD *anfd = anfds + fd;
223 struct ev_io *w;
224
225 int events = 0;
226
227 for (w = anfd->head; w; w = w->next)
228 events |= w->events;
229
230 anfd->events &= ~EV_REIFY;
231
232 if (anfd->events != events)
233 {
234 method_modify (fd, anfd->events, events);
235 anfd->events = events;
236 }
237 }
238
239 fdchangecnt = 0;
240}
241
242static void
243fd_change (int fd)
244{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
246 return;
247
248 anfds [fd].events |= EV_REIFY;
249
250 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd;
253}
254
255/* called on EBADF to verify fds */
256static void
257fd_recheck (void)
258{
259 int fd;
260
261 for (fd = 0; fd < anfdmax; ++fd)
262 if (anfds [fd].events)
263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
264 while (anfds [fd].head)
265 {
266 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
268 }
159} 269}
160 270
161/*****************************************************************************/ 271/*****************************************************************************/
162 272
163static struct ev_timer **timers; 273static struct ev_timer **timers;
272 382
273 /* rather than sort out wether we really need nb, set it */ 383 /* rather than sort out wether we really need nb, set it */
274 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
275 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
276 386
277 evio_set (&sigev, sigpipe [0], EV_READ); 387 ev_io_set (&sigev, sigpipe [0], EV_READ);
278 evio_start (&sigev); 388 ev_io_start (&sigev);
279} 389}
280 390
281/*****************************************************************************/ 391/*****************************************************************************/
282 392
283static struct ev_idle **idles; 393static struct ev_idle **idles;
284static int idlemax, idlecnt; 394static int idlemax, idlecnt;
285 395
396static struct ev_prepare **prepares;
397static int preparemax, preparecnt;
398
286static struct ev_check **checks; 399static struct ev_check **checks;
287static int checkmax, checkcnt; 400static int checkmax, checkcnt;
288 401
289/*****************************************************************************/ 402/*****************************************************************************/
290 403
404static struct ev_child *childs [PID_HASHSIZE];
405static struct ev_signal childev;
406
407#ifndef WCONTINUED
408# define WCONTINUED 0
409#endif
410
411static void
412childcb (struct ev_signal *sw, int revents)
413{
414 struct ev_child *w;
415 int pid, status;
416
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
419 if (w->pid == pid || w->pid == -1)
420 {
421 w->status = status;
422 event ((W)w, EV_CHILD);
423 }
424}
425
426/*****************************************************************************/
427
291#if HAVE_EPOLL 428#if EV_USE_EPOLL
292# include "ev_epoll.c" 429# include "ev_epoll.c"
293#endif 430#endif
294#if HAVE_SELECT 431#if EV_USE_SELECT
295# include "ev_select.c" 432# include "ev_select.c"
296#endif 433#endif
297 434
435int
436ev_version_major (void)
437{
438 return EV_VERSION_MAJOR;
439}
440
441int
442ev_version_minor (void)
443{
444 return EV_VERSION_MINOR;
445}
446
298int ev_init (int flags) 447int ev_init (int flags)
299{ 448{
300#if HAVE_MONOTONIC
301 {
302 struct timespec ts;
303 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
304 have_monotonic = 1;
305 }
306#endif
307
308 ev_now = ev_time ();
309 now = get_clock ();
310 diff = ev_now - now;
311
312 if (pipe (sigpipe))
313 return 0;
314
315 ev_method = EVMETHOD_NONE;
316#if HAVE_EPOLL
317 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
318#endif
319#if HAVE_SELECT
320 if (ev_method == EVMETHOD_NONE) select_init (flags);
321#endif
322
323 if (ev_method) 449 if (!ev_method)
450 {
451#if EV_USE_MONOTONIC
324 { 452 {
453 struct timespec ts;
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1;
456 }
457#endif
458
459 ev_now = ev_time ();
460 now = get_clock ();
461 diff = ev_now - now;
462
463 if (pipe (sigpipe))
464 return 0;
465
466 ev_method = EVMETHOD_NONE;
467#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
469#endif
470#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags);
472#endif
473
474 if (ev_method)
475 {
325 evw_init (&sigev, sigcb); 476 ev_watcher_init (&sigev, sigcb);
326 siginit (); 477 siginit ();
478
479 ev_signal_init (&childev, childcb, SIGCHLD);
480 ev_signal_start (&childev);
481 }
327 } 482 }
328 483
329 return ev_method; 484 return ev_method;
330} 485}
331 486
332/*****************************************************************************/ 487/*****************************************************************************/
333 488
489void
334void ev_prefork (void) 490ev_prefork (void)
335{ 491{
336 /* nop */ 492 /* nop */
337} 493}
338 494
495void
339void ev_postfork_parent (void) 496ev_postfork_parent (void)
340{ 497{
341 /* nop */ 498 /* nop */
342} 499}
343 500
501void
344void ev_postfork_child (void) 502ev_postfork_child (void)
345{ 503{
346#if HAVE_EPOLL 504#if EV_USE_EPOLL
347 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
348 epoll_postfork_child (); 506 epoll_postfork_child ();
349#endif 507#endif
350 508
351 evio_stop (&sigev); 509 ev_io_stop (&sigev);
352 close (sigpipe [0]); 510 close (sigpipe [0]);
353 close (sigpipe [1]); 511 close (sigpipe [1]);
354 pipe (sigpipe); 512 pipe (sigpipe);
355 siginit (); 513 siginit ();
356} 514}
357 515
358/*****************************************************************************/ 516/*****************************************************************************/
359 517
360static void 518static void
361fd_reify (void)
362{
363 int i;
364
365 for (i = 0; i < fdchangecnt; ++i)
366 {
367 int fd = fdchanges [i];
368 ANFD *anfd = anfds + fd;
369 struct ev_io *w;
370
371 int wev = 0;
372
373 for (w = anfd->head; w; w = w->next)
374 wev |= w->events;
375
376 if (anfd->wev != wev)
377 {
378 method_modify (fd, anfd->wev, wev);
379 anfd->wev = wev;
380 }
381 }
382
383 fdchangecnt = 0;
384}
385
386static void
387call_pending () 519call_pending (void)
388{ 520{
389 int i; 521 while (pendingcnt)
390
391 for (i = 0; i < pendingcnt; ++i)
392 { 522 {
393 ANPENDING *p = pendings + i; 523 ANPENDING *p = pendings + --pendingcnt;
394 524
395 if (p->w) 525 if (p->w)
396 { 526 {
397 p->w->pending = 0; 527 p->w->pending = 0;
398 p->w->cb (p->w, p->events); 528 p->w->cb (p->w, p->events);
399 } 529 }
400 } 530 }
401
402 pendingcnt = 0;
403} 531}
404 532
405static void 533static void
406timers_reify () 534timers_reify (void)
407{ 535{
408 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
409 { 537 {
410 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
411 539
415 w->at = now + w->repeat; 543 w->at = now + w->repeat;
416 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
417 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
418 } 546 }
419 else 547 else
420 evtimer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
421 549
422 event ((W)w, EV_TIMEOUT); 550 event ((W)w, EV_TIMEOUT);
423 } 551 }
424} 552}
425 553
426static void 554static void
427periodics_reify () 555periodics_reify (void)
428{ 556{
429 while (periodiccnt && periodics [0]->at <= ev_now) 557 while (periodiccnt && periodics [0]->at <= ev_now)
430 { 558 {
431 struct ev_periodic *w = periodics [0]; 559 struct ev_periodic *w = periodics [0];
432 560
436 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
437 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 565 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
438 downheap ((WT *)periodics, periodiccnt, 0); 566 downheap ((WT *)periodics, periodiccnt, 0);
439 } 567 }
440 else 568 else
441 evperiodic_stop (w); /* nonrepeating: stop timer */ 569 ev_periodic_stop (w); /* nonrepeating: stop timer */
442 570
443 event ((W)w, EV_TIMEOUT); 571 event ((W)w, EV_TIMEOUT);
444 } 572 }
445} 573}
446 574
458 { 586 {
459 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 587 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
460 588
461 if (fabs (diff) >= 1e-4) 589 if (fabs (diff) >= 1e-4)
462 { 590 {
463 evperiodic_stop (w); 591 ev_periodic_stop (w);
464 evperiodic_start (w); 592 ev_periodic_start (w);
465 593
466 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 594 i = 0; /* restart loop, inefficient, but time jumps should be rare */
467 } 595 }
468 } 596 }
469 } 597 }
470} 598}
471 599
472static void 600static void
473time_update () 601time_update (void)
474{ 602{
475 int i; 603 int i;
476 604
477 ev_now = ev_time (); 605 ev_now = ev_time ();
478 606
512int ev_loop_done; 640int ev_loop_done;
513 641
514void ev_loop (int flags) 642void ev_loop (int flags)
515{ 643{
516 double block; 644 double block;
517 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
518
519 if (checkcnt)
520 {
521 queue_events ((W *)checks, checkcnt, EV_CHECK);
522 call_pending ();
523 }
524 646
525 do 647 do
526 { 648 {
649 /* queue check watchers (and execute them) */
650 if (preparecnt)
651 {
652 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
653 call_pending ();
654 }
655
527 /* update fd-related kernel structures */ 656 /* update fd-related kernel structures */
528 fd_reify (); 657 fd_reify ();
529 658
530 /* calculate blocking time */ 659 /* calculate blocking time */
531 660
532 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 661 /* we only need this for !monotonic clockor timers, but as we basically
662 always have timers, we just calculate it always */
533 ev_now = ev_time (); 663 ev_now = ev_time ();
534 664
535 if (flags & EVLOOP_NONBLOCK || idlecnt) 665 if (flags & EVLOOP_NONBLOCK || idlecnt)
536 block = 0.; 666 block = 0.;
537 else 667 else
538 { 668 {
539 block = MAX_BLOCKTIME; 669 block = MAX_BLOCKTIME;
540 670
541 if (timercnt) 671 if (timercnt)
542 { 672 {
543 ev_tstamp to = timers [0]->at - get_clock () + method_fudge; 673 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
544 if (block > to) block = to; 674 if (block > to) block = to;
545 } 675 }
546 676
547 if (periodiccnt) 677 if (periodiccnt)
548 { 678 {
557 687
558 /* update ev_now, do magic */ 688 /* update ev_now, do magic */
559 time_update (); 689 time_update ();
560 690
561 /* queue pending timers and reschedule them */ 691 /* queue pending timers and reschedule them */
692 timers_reify (); /* relative timers called last */
562 periodics_reify (); /* absolute timers first */ 693 periodics_reify (); /* absolute timers called first */
563 timers_reify (); /* relative timers second */
564 694
565 /* queue idle watchers unless io or timers are pending */ 695 /* queue idle watchers unless io or timers are pending */
566 if (!pendingcnt) 696 if (!pendingcnt)
567 queue_events ((W *)idles, idlecnt, EV_IDLE); 697 queue_events ((W *)idles, idlecnt, EV_IDLE);
568 698
569 /* queue check and possibly idle watchers */ 699 /* queue check watchers, to be executed first */
700 if (checkcnt)
570 queue_events ((W *)checks, checkcnt, EV_CHECK); 701 queue_events ((W *)checks, checkcnt, EV_CHECK);
571 702
572 call_pending (); 703 call_pending ();
573 } 704 }
574 while (!ev_loop_done); 705 while (!ev_loop_done);
575 706
600 head = &(*head)->next; 731 head = &(*head)->next;
601 } 732 }
602} 733}
603 734
604static void 735static void
736ev_clear (W w)
737{
738 if (w->pending)
739 {
740 pendings [w->pending - 1].w = 0;
741 w->pending = 0;
742 }
743}
744
745static void
605ev_start (W w, int active) 746ev_start (W w, int active)
606{ 747{
607 w->pending = 0;
608 w->active = active; 748 w->active = active;
609} 749}
610 750
611static void 751static void
612ev_stop (W w) 752ev_stop (W w)
613{ 753{
614 if (w->pending)
615 pendings [w->pending - 1].w = 0;
616
617 w->active = 0; 754 w->active = 0;
618} 755}
619 756
620/*****************************************************************************/ 757/*****************************************************************************/
621 758
622void 759void
623evio_start (struct ev_io *w) 760ev_io_start (struct ev_io *w)
624{ 761{
625 if (ev_is_active (w)) 762 if (ev_is_active (w))
626 return; 763 return;
627 764
628 int fd = w->fd; 765 int fd = w->fd;
629 766
630 ev_start ((W)w, 1); 767 ev_start ((W)w, 1);
631 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 768 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
632 wlist_add ((WL *)&anfds[fd].head, (WL)w); 769 wlist_add ((WL *)&anfds[fd].head, (WL)w);
633 770
634 ++fdchangecnt; 771 fd_change (fd);
635 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
636 fdchanges [fdchangecnt - 1] = fd;
637} 772}
638 773
639void 774void
640evio_stop (struct ev_io *w) 775ev_io_stop (struct ev_io *w)
641{ 776{
777 ev_clear ((W)w);
642 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
643 return; 779 return;
644 780
645 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
646 ev_stop ((W)w); 782 ev_stop ((W)w);
647 783
648 ++fdchangecnt; 784 fd_change (w->fd);
649 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
650 fdchanges [fdchangecnt - 1] = w->fd;
651} 785}
652 786
653
654void 787void
655evtimer_start (struct ev_timer *w) 788ev_timer_start (struct ev_timer *w)
656{ 789{
657 if (ev_is_active (w)) 790 if (ev_is_active (w))
658 return; 791 return;
659 792
660 w->at += now; 793 w->at += now;
666 timers [timercnt - 1] = w; 799 timers [timercnt - 1] = w;
667 upheap ((WT *)timers, timercnt - 1); 800 upheap ((WT *)timers, timercnt - 1);
668} 801}
669 802
670void 803void
671evtimer_stop (struct ev_timer *w) 804ev_timer_stop (struct ev_timer *w)
672{ 805{
806 ev_clear ((W)w);
673 if (!ev_is_active (w)) 807 if (!ev_is_active (w))
674 return; 808 return;
675 809
676 if (w->active < timercnt--) 810 if (w->active < timercnt--)
677 { 811 {
678 timers [w->active - 1] = timers [timercnt]; 812 timers [w->active - 1] = timers [timercnt];
679 downheap ((WT *)timers, timercnt, w->active - 1); 813 downheap ((WT *)timers, timercnt, w->active - 1);
680 } 814 }
681 815
816 w->at = w->repeat;
817
682 ev_stop ((W)w); 818 ev_stop ((W)w);
683} 819}
684 820
685void 821void
822ev_timer_again (struct ev_timer *w)
823{
824 if (ev_is_active (w))
825 {
826 if (w->repeat)
827 {
828 w->at = now + w->repeat;
829 downheap ((WT *)timers, timercnt, w->active - 1);
830 }
831 else
832 ev_timer_stop (w);
833 }
834 else if (w->repeat)
835 ev_timer_start (w);
836}
837
838void
686evperiodic_start (struct ev_periodic *w) 839ev_periodic_start (struct ev_periodic *w)
687{ 840{
688 if (ev_is_active (w)) 841 if (ev_is_active (w))
689 return; 842 return;
690 843
691 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
699 periodics [periodiccnt - 1] = w; 852 periodics [periodiccnt - 1] = w;
700 upheap ((WT *)periodics, periodiccnt - 1); 853 upheap ((WT *)periodics, periodiccnt - 1);
701} 854}
702 855
703void 856void
704evperiodic_stop (struct ev_periodic *w) 857ev_periodic_stop (struct ev_periodic *w)
705{ 858{
859 ev_clear ((W)w);
706 if (!ev_is_active (w)) 860 if (!ev_is_active (w))
707 return; 861 return;
708 862
709 if (w->active < periodiccnt--) 863 if (w->active < periodiccnt--)
710 { 864 {
714 868
715 ev_stop ((W)w); 869 ev_stop ((W)w);
716} 870}
717 871
718void 872void
719evsignal_start (struct ev_signal *w) 873ev_signal_start (struct ev_signal *w)
720{ 874{
721 if (ev_is_active (w)) 875 if (ev_is_active (w))
722 return; 876 return;
723 877
724 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
734 sigaction (w->signum, &sa, 0); 888 sigaction (w->signum, &sa, 0);
735 } 889 }
736} 890}
737 891
738void 892void
739evsignal_stop (struct ev_signal *w) 893ev_signal_stop (struct ev_signal *w)
740{ 894{
895 ev_clear ((W)w);
741 if (!ev_is_active (w)) 896 if (!ev_is_active (w))
742 return; 897 return;
743 898
744 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
745 ev_stop ((W)w); 900 ev_stop ((W)w);
746 901
747 if (!signals [w->signum - 1].head) 902 if (!signals [w->signum - 1].head)
748 signal (w->signum, SIG_DFL); 903 signal (w->signum, SIG_DFL);
749} 904}
750 905
906void
751void evidle_start (struct ev_idle *w) 907ev_idle_start (struct ev_idle *w)
752{ 908{
753 if (ev_is_active (w)) 909 if (ev_is_active (w))
754 return; 910 return;
755 911
756 ev_start ((W)w, ++idlecnt); 912 ev_start ((W)w, ++idlecnt);
757 array_needsize (idles, idlemax, idlecnt, ); 913 array_needsize (idles, idlemax, idlecnt, );
758 idles [idlecnt - 1] = w; 914 idles [idlecnt - 1] = w;
759} 915}
760 916
917void
761void evidle_stop (struct ev_idle *w) 918ev_idle_stop (struct ev_idle *w)
762{ 919{
920 ev_clear ((W)w);
921 if (ev_is_active (w))
922 return;
923
763 idles [w->active - 1] = idles [--idlecnt]; 924 idles [w->active - 1] = idles [--idlecnt];
764 ev_stop ((W)w); 925 ev_stop ((W)w);
765} 926}
766 927
928void
929ev_prepare_start (struct ev_prepare *w)
930{
931 if (ev_is_active (w))
932 return;
933
934 ev_start ((W)w, ++preparecnt);
935 array_needsize (prepares, preparemax, preparecnt, );
936 prepares [preparecnt - 1] = w;
937}
938
939void
940ev_prepare_stop (struct ev_prepare *w)
941{
942 ev_clear ((W)w);
943 if (ev_is_active (w))
944 return;
945
946 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w);
948}
949
950void
767void evcheck_start (struct ev_check *w) 951ev_check_start (struct ev_check *w)
768{ 952{
769 if (ev_is_active (w)) 953 if (ev_is_active (w))
770 return; 954 return;
771 955
772 ev_start ((W)w, ++checkcnt); 956 ev_start ((W)w, ++checkcnt);
773 array_needsize (checks, checkmax, checkcnt, ); 957 array_needsize (checks, checkmax, checkcnt, );
774 checks [checkcnt - 1] = w; 958 checks [checkcnt - 1] = w;
775} 959}
776 960
961void
777void evcheck_stop (struct ev_check *w) 962ev_check_stop (struct ev_check *w)
778{ 963{
964 ev_clear ((W)w);
965 if (ev_is_active (w))
966 return;
967
779 checks [w->active - 1] = checks [--checkcnt]; 968 checks [w->active - 1] = checks [--checkcnt];
780 ev_stop ((W)w); 969 ev_stop ((W)w);
781} 970}
782 971
972void
973ev_child_start (struct ev_child *w)
974{
975 if (ev_is_active (w))
976 return;
977
978 ev_start ((W)w, 1);
979 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
980}
981
982void
983ev_child_stop (struct ev_child *w)
984{
985 ev_clear ((W)w);
986 if (ev_is_active (w))
987 return;
988
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w);
991}
992
993/*****************************************************************************/
994
995struct ev_once
996{
997 struct ev_io io;
998 struct ev_timer to;
999 void (*cb)(int revents, void *arg);
1000 void *arg;
1001};
1002
1003static void
1004once_cb (struct ev_once *once, int revents)
1005{
1006 void (*cb)(int revents, void *arg) = once->cb;
1007 void *arg = once->arg;
1008
1009 ev_io_stop (&once->io);
1010 ev_timer_stop (&once->to);
1011 free (once);
1012
1013 cb (revents, arg);
1014}
1015
1016static void
1017once_cb_io (struct ev_io *w, int revents)
1018{
1019 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1020}
1021
1022static void
1023once_cb_to (struct ev_timer *w, int revents)
1024{
1025 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1026}
1027
1028void
1029ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1030{
1031 struct ev_once *once = malloc (sizeof (struct ev_once));
1032
1033 if (!once)
1034 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1035 else
1036 {
1037 once->cb = cb;
1038 once->arg = arg;
1039
1040 ev_watcher_init (&once->io, once_cb_io);
1041 if (fd >= 0)
1042 {
1043 ev_io_set (&once->io, fd, events);
1044 ev_io_start (&once->io);
1045 }
1046
1047 ev_watcher_init (&once->to, once_cb_to);
1048 if (timeout >= 0.)
1049 {
1050 ev_timer_set (&once->to, timeout, 0.);
1051 ev_timer_start (&once->to);
1052 }
1053 }
1054}
1055
783/*****************************************************************************/ 1056/*****************************************************************************/
784 1057
785#if 0 1058#if 0
786 1059
787struct ev_io wio; 1060struct ev_io wio;
794 1067
795static void 1068static void
796ocb (struct ev_timer *w, int revents) 1069ocb (struct ev_timer *w, int revents)
797{ 1070{
798 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1071 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
799 evtimer_stop (w); 1072 ev_timer_stop (w);
800 evtimer_start (w); 1073 ev_timer_start (w);
801} 1074}
802 1075
803static void 1076static void
804scb (struct ev_signal *w, int revents) 1077scb (struct ev_signal *w, int revents)
805{ 1078{
806 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1079 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
807 evio_stop (&wio); 1080 ev_io_stop (&wio);
808 evio_start (&wio); 1081 ev_io_start (&wio);
809} 1082}
810 1083
811static void 1084static void
812gcb (struct ev_signal *w, int revents) 1085gcb (struct ev_signal *w, int revents)
813{ 1086{
817 1090
818int main (void) 1091int main (void)
819{ 1092{
820 ev_init (0); 1093 ev_init (0);
821 1094
822 evio_init (&wio, sin_cb, 0, EV_READ); 1095 ev_io_init (&wio, sin_cb, 0, EV_READ);
823 evio_start (&wio); 1096 ev_io_start (&wio);
824 1097
825 struct ev_timer t[10000]; 1098 struct ev_timer t[10000];
826 1099
827#if 0 1100#if 0
828 int i; 1101 int i;
829 for (i = 0; i < 10000; ++i) 1102 for (i = 0; i < 10000; ++i)
830 { 1103 {
831 struct ev_timer *w = t + i; 1104 struct ev_timer *w = t + i;
832 evw_init (w, ocb, i); 1105 ev_watcher_init (w, ocb, i);
833 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1106 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
834 evtimer_start (w); 1107 ev_timer_start (w);
835 if (drand48 () < 0.5) 1108 if (drand48 () < 0.5)
836 evtimer_stop (w); 1109 ev_timer_stop (w);
837 } 1110 }
838#endif 1111#endif
839 1112
840 struct ev_timer t1; 1113 struct ev_timer t1;
841 evtimer_init (&t1, ocb, 5, 10); 1114 ev_timer_init (&t1, ocb, 5, 10);
842 evtimer_start (&t1); 1115 ev_timer_start (&t1);
843 1116
844 struct ev_signal sig; 1117 struct ev_signal sig;
845 evsignal_init (&sig, scb, SIGQUIT); 1118 ev_signal_init (&sig, scb, SIGQUIT);
846 evsignal_start (&sig); 1119 ev_signal_start (&sig);
847 1120
848 struct ev_check cw; 1121 struct ev_check cw;
849 evcheck_init (&cw, gcb); 1122 ev_check_init (&cw, gcb);
850 evcheck_start (&cw); 1123 ev_check_start (&cw);
851 1124
852 struct ev_idle iw; 1125 struct ev_idle iw;
853 evidle_init (&iw, gcb); 1126 ev_idle_init (&iw, gcb);
854 evidle_start (&iw); 1127 ev_idle_start (&iw);
855 1128
856 ev_loop (0); 1129 ev_loop (0);
857 1130
858 return 0; 1131 return 0;
859} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines