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

Comparing libev/ev.c (file contents):
Revision 1.15 by root, Wed Oct 31 11:56:34 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; \
128 do \
129 { \
130 newcnt = array_roundsize (base, newcnt << 1); \
131 } \
132 while ((cnt) > newcnt); \
133 \
88 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
89 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
90 cur = newcnt; \ 136 cur = newcnt; \
91 } 137 }
92 138
93/*****************************************************************************/ 139/*****************************************************************************/
94 140
95typedef struct 141typedef struct
96{ 142{
97 struct ev_io *head; 143 struct ev_io *head;
98 unsigned char wev, rev; /* want, received event set */ 144 int events;
99} ANFD; 145} ANFD;
100 146
101static ANFD *anfds; 147static ANFD *anfds;
102static int anfdmax; 148static int anfdmax;
103 149
104static int *fdchanges;
105static int fdchangemax, fdchangecnt;
106
107static void 150static void
108anfds_init (ANFD *base, int count) 151anfds_init (ANFD *base, int count)
109{ 152{
110 while (count--) 153 while (count--)
111 { 154 {
112 base->head = 0; 155 base->head = 0;
113 base->wev = base->rev = EV_NONE; 156 base->events = EV_NONE;
114 ++base; 157 ++base;
115 } 158 }
116} 159}
117 160
118typedef struct 161typedef struct
125static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
126 169
127static void 170static void
128event (W w, int events) 171event (W w, int events)
129{ 172{
173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
177 }
178
130 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
131 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
132 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
133 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
134} 183}
135 184
136static 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
137fd_event (int fd, int events) 195fd_event (int fd, int events)
138{ 196{
139 ANFD *anfd = anfds + fd; 197 ANFD *anfd = anfds + fd;
140 struct ev_io *w; 198 struct ev_io *w;
141 199
146 if (ev) 204 if (ev)
147 event ((W)w, ev); 205 event ((W)w, ev);
148 } 206 }
149} 207}
150 208
209/*****************************************************************************/
210
211static int *fdchanges;
212static int fdchangemax, fdchangecnt;
213
151static void 214static void
152queue_events (W *events, int eventcnt, int type) 215fd_reify (void)
153{ 216{
154 int i; 217 int i;
155 218
156 for (i = 0; i < eventcnt; ++i) 219 for (i = 0; i < fdchangecnt; ++i)
157 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 }
158} 269}
159 270
160/*****************************************************************************/ 271/*****************************************************************************/
161 272
162static struct ev_timer **timers; 273static struct ev_timer **timers;
271 382
272 /* rather than sort out wether we really need nb, set it */ 383 /* rather than sort out wether we really need nb, set it */
273 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
274 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
275 386
276 evio_set (&sigev, sigpipe [0], EV_READ); 387 ev_io_set (&sigev, sigpipe [0], EV_READ);
277 evio_start (&sigev); 388 ev_io_start (&sigev);
278} 389}
279 390
280/*****************************************************************************/ 391/*****************************************************************************/
281 392
282static struct ev_idle **idles; 393static struct ev_idle **idles;
283static int idlemax, idlecnt; 394static int idlemax, idlecnt;
284 395
396static struct ev_prepare **prepares;
397static int preparemax, preparecnt;
398
285static struct ev_check **checks; 399static struct ev_check **checks;
286static int checkmax, checkcnt; 400static int checkmax, checkcnt;
287 401
288/*****************************************************************************/ 402/*****************************************************************************/
289 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
290#if HAVE_EPOLL 428#if EV_USE_EPOLL
291# include "ev_epoll.c" 429# include "ev_epoll.c"
292#endif 430#endif
293#if HAVE_SELECT 431#if EV_USE_SELECT
294# include "ev_select.c" 432# include "ev_select.c"
295#endif 433#endif
296 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
297int ev_init (int flags) 447int ev_init (int flags)
298{ 448{
299#if HAVE_MONOTONIC
300 {
301 struct timespec ts;
302 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
303 have_monotonic = 1;
304 }
305#endif
306
307 ev_now = ev_time ();
308 now = get_clock ();
309 diff = ev_now - now;
310
311 if (pipe (sigpipe))
312 return 0;
313
314 ev_method = EVMETHOD_NONE;
315#if HAVE_EPOLL
316 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
317#endif
318#if HAVE_SELECT
319 if (ev_method == EVMETHOD_NONE) select_init (flags);
320#endif
321
322 if (ev_method) 449 if (!ev_method)
450 {
451#if EV_USE_MONOTONIC
323 { 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 {
324 evw_init (&sigev, sigcb); 476 ev_watcher_init (&sigev, sigcb);
325 siginit (); 477 siginit ();
478
479 ev_signal_init (&childev, childcb, SIGCHLD);
480 ev_signal_start (&childev);
481 }
326 } 482 }
327 483
328 return ev_method; 484 return ev_method;
329} 485}
330 486
331/*****************************************************************************/ 487/*****************************************************************************/
332 488
489void
333void ev_prefork (void) 490ev_prefork (void)
334{ 491{
335 /* nop */ 492 /* nop */
336} 493}
337 494
495void
338void ev_postfork_parent (void) 496ev_postfork_parent (void)
339{ 497{
340 /* nop */ 498 /* nop */
341} 499}
342 500
501void
343void ev_postfork_child (void) 502ev_postfork_child (void)
344{ 503{
345#if HAVE_EPOLL 504#if EV_USE_EPOLL
346 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
347 epoll_postfork_child (); 506 epoll_postfork_child ();
348#endif 507#endif
349 508
350 evio_stop (&sigev); 509 ev_io_stop (&sigev);
351 close (sigpipe [0]); 510 close (sigpipe [0]);
352 close (sigpipe [1]); 511 close (sigpipe [1]);
353 pipe (sigpipe); 512 pipe (sigpipe);
354 siginit (); 513 siginit ();
355} 514}
356 515
357/*****************************************************************************/ 516/*****************************************************************************/
358 517
359static void 518static 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;
383}
384
385static void
386call_pending () 519call_pending (void)
387{ 520{
388 int i; 521 while (pendingcnt)
389
390 for (i = 0; i < pendingcnt; ++i)
391 { 522 {
392 ANPENDING *p = pendings + i; 523 ANPENDING *p = pendings + --pendingcnt;
393 524
394 if (p->w) 525 if (p->w)
395 { 526 {
396 p->w->pending = 0; 527 p->w->pending = 0;
397 p->w->cb (p->w, p->events); 528 p->w->cb (p->w, p->events);
398 } 529 }
399 } 530 }
400
401 pendingcnt = 0;
402} 531}
403 532
404static void 533static void
405timers_reify () 534timers_reify (void)
406{ 535{
407 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
408 { 537 {
409 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
410 539
414 w->at = now + w->repeat; 543 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
417 } 546 }
418 else 547 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
420 549
421 event ((W)w, EV_TIMEOUT); 550 event ((W)w, EV_TIMEOUT);
422 } 551 }
423} 552}
424 553
425static void 554static void
426periodics_reify () 555periodics_reify (void)
427{ 556{
428 while (periodiccnt && periodics [0]->at <= ev_now) 557 while (periodiccnt && periodics [0]->at <= ev_now)
429 { 558 {
430 struct ev_periodic *w = periodics [0]; 559 struct ev_periodic *w = periodics [0];
431 560
435 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;
436 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));
437 downheap ((WT *)periodics, periodiccnt, 0); 566 downheap ((WT *)periodics, periodiccnt, 0);
438 } 567 }
439 else 568 else
440 evperiodic_stop (w); /* nonrepeating: stop timer */ 569 ev_periodic_stop (w); /* nonrepeating: stop timer */
441 570
442 event ((W)w, EV_TIMEOUT); 571 event ((W)w, EV_TIMEOUT);
443 } 572 }
444} 573}
445 574
457 { 586 {
458 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;
459 588
460 if (fabs (diff) >= 1e-4) 589 if (fabs (diff) >= 1e-4)
461 { 590 {
462 evperiodic_stop (w); 591 ev_periodic_stop (w);
463 evperiodic_start (w); 592 ev_periodic_start (w);
464 593
465 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 594 i = 0; /* restart loop, inefficient, but time jumps should be rare */
466 } 595 }
467 } 596 }
468 } 597 }
469} 598}
470 599
471static void 600static void
472time_update () 601time_update (void)
473{ 602{
474 int i; 603 int i;
475 604
476 ev_now = ev_time (); 605 ev_now = ev_time ();
477 606
511int ev_loop_done; 640int ev_loop_done;
512 641
513void ev_loop (int flags) 642void ev_loop (int flags)
514{ 643{
515 double block; 644 double block;
516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
517
518 if (checkcnt)
519 {
520 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 call_pending ();
522 }
523 646
524 do 647 do
525 { 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
526 /* update fd-related kernel structures */ 656 /* update fd-related kernel structures */
527 fd_reify (); 657 fd_reify ();
528 658
529 /* calculate blocking time */ 659 /* calculate blocking time */
530 660
531 /* 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 */
532 ev_now = ev_time (); 663 ev_now = ev_time ();
533 664
534 if (flags & EVLOOP_NONBLOCK || idlecnt) 665 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 block = 0.; 666 block = 0.;
536 else 667 else
556 687
557 /* update ev_now, do magic */ 688 /* update ev_now, do magic */
558 time_update (); 689 time_update ();
559 690
560 /* queue pending timers and reschedule them */ 691 /* queue pending timers and reschedule them */
692 timers_reify (); /* relative timers called last */
561 periodics_reify (); /* absolute timers first */ 693 periodics_reify (); /* absolute timers called first */
562 timers_reify (); /* relative timers second */
563 694
564 /* queue idle watchers unless io or timers are pending */ 695 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt) 696 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE); 697 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 698
568 /* queue check and possibly idle watchers */ 699 /* queue check watchers, to be executed first */
700 if (checkcnt)
569 queue_events ((W *)checks, checkcnt, EV_CHECK); 701 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 702
571 call_pending (); 703 call_pending ();
572 } 704 }
573 while (!ev_loop_done); 705 while (!ev_loop_done);
574 706
599 head = &(*head)->next; 731 head = &(*head)->next;
600 } 732 }
601} 733}
602 734
603static 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
604ev_start (W w, int active) 746ev_start (W w, int active)
605{ 747{
606 w->pending = 0;
607 w->active = active; 748 w->active = active;
608} 749}
609 750
610static void 751static void
611ev_stop (W w) 752ev_stop (W w)
612{ 753{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 754 w->active = 0;
617} 755}
618 756
619/*****************************************************************************/ 757/*****************************************************************************/
620 758
621void 759void
622evio_start (struct ev_io *w) 760ev_io_start (struct ev_io *w)
623{ 761{
624 if (ev_is_active (w)) 762 if (ev_is_active (w))
625 return; 763 return;
626 764
627 int fd = w->fd; 765 int fd = w->fd;
628 766
629 ev_start ((W)w, 1); 767 ev_start ((W)w, 1);
630 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 768 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
631 wlist_add ((WL *)&anfds[fd].head, (WL)w); 769 wlist_add ((WL *)&anfds[fd].head, (WL)w);
632 770
633 ++fdchangecnt; 771 fd_change (fd);
634 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
635 fdchanges [fdchangecnt - 1] = fd;
636} 772}
637 773
638void 774void
639evio_stop (struct ev_io *w) 775ev_io_stop (struct ev_io *w)
640{ 776{
777 ev_clear ((W)w);
641 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
642 return; 779 return;
643 780
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 782 ev_stop ((W)w);
646 783
647 ++fdchangecnt; 784 fd_change (w->fd);
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd;
650} 785}
651 786
652
653void 787void
654evtimer_start (struct ev_timer *w) 788ev_timer_start (struct ev_timer *w)
655{ 789{
656 if (ev_is_active (w)) 790 if (ev_is_active (w))
657 return; 791 return;
658 792
659 w->at += now; 793 w->at += now;
665 timers [timercnt - 1] = w; 799 timers [timercnt - 1] = w;
666 upheap ((WT *)timers, timercnt - 1); 800 upheap ((WT *)timers, timercnt - 1);
667} 801}
668 802
669void 803void
670evtimer_stop (struct ev_timer *w) 804ev_timer_stop (struct ev_timer *w)
671{ 805{
806 ev_clear ((W)w);
672 if (!ev_is_active (w)) 807 if (!ev_is_active (w))
673 return; 808 return;
674 809
675 if (w->active < timercnt--) 810 if (w->active < timercnt--)
676 { 811 {
682 817
683 ev_stop ((W)w); 818 ev_stop ((W)w);
684} 819}
685 820
686void 821void
687evtimer_again (struct ev_timer *w) 822ev_timer_again (struct ev_timer *w)
688{ 823{
689 if (ev_is_active (w)) 824 if (ev_is_active (w))
690 { 825 {
691 if (w->repeat) 826 if (w->repeat)
692 { 827 {
693 w->at = now + w->repeat; 828 w->at = now + w->repeat;
694 downheap ((WT *)timers, timercnt, w->active - 1); 829 downheap ((WT *)timers, timercnt, w->active - 1);
695 } 830 }
696 else 831 else
697 evtimer_stop (w); 832 ev_timer_stop (w);
698 } 833 }
699 else if (w->repeat) 834 else if (w->repeat)
700 evtimer_start (w); 835 ev_timer_start (w);
701} 836}
702 837
703void 838void
704evperiodic_start (struct ev_periodic *w) 839ev_periodic_start (struct ev_periodic *w)
705{ 840{
706 if (ev_is_active (w)) 841 if (ev_is_active (w))
707 return; 842 return;
708 843
709 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.));
717 periodics [periodiccnt - 1] = w; 852 periodics [periodiccnt - 1] = w;
718 upheap ((WT *)periodics, periodiccnt - 1); 853 upheap ((WT *)periodics, periodiccnt - 1);
719} 854}
720 855
721void 856void
722evperiodic_stop (struct ev_periodic *w) 857ev_periodic_stop (struct ev_periodic *w)
723{ 858{
859 ev_clear ((W)w);
724 if (!ev_is_active (w)) 860 if (!ev_is_active (w))
725 return; 861 return;
726 862
727 if (w->active < periodiccnt--) 863 if (w->active < periodiccnt--)
728 { 864 {
732 868
733 ev_stop ((W)w); 869 ev_stop ((W)w);
734} 870}
735 871
736void 872void
737evsignal_start (struct ev_signal *w) 873ev_signal_start (struct ev_signal *w)
738{ 874{
739 if (ev_is_active (w)) 875 if (ev_is_active (w))
740 return; 876 return;
741 877
742 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
752 sigaction (w->signum, &sa, 0); 888 sigaction (w->signum, &sa, 0);
753 } 889 }
754} 890}
755 891
756void 892void
757evsignal_stop (struct ev_signal *w) 893ev_signal_stop (struct ev_signal *w)
758{ 894{
895 ev_clear ((W)w);
759 if (!ev_is_active (w)) 896 if (!ev_is_active (w))
760 return; 897 return;
761 898
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 900 ev_stop ((W)w);
764 901
765 if (!signals [w->signum - 1].head) 902 if (!signals [w->signum - 1].head)
766 signal (w->signum, SIG_DFL); 903 signal (w->signum, SIG_DFL);
767} 904}
768 905
906void
769void evidle_start (struct ev_idle *w) 907ev_idle_start (struct ev_idle *w)
770{ 908{
771 if (ev_is_active (w)) 909 if (ev_is_active (w))
772 return; 910 return;
773 911
774 ev_start ((W)w, ++idlecnt); 912 ev_start ((W)w, ++idlecnt);
775 array_needsize (idles, idlemax, idlecnt, ); 913 array_needsize (idles, idlemax, idlecnt, );
776 idles [idlecnt - 1] = w; 914 idles [idlecnt - 1] = w;
777} 915}
778 916
917void
779void evidle_stop (struct ev_idle *w) 918ev_idle_stop (struct ev_idle *w)
780{ 919{
920 ev_clear ((W)w);
921 if (ev_is_active (w))
922 return;
923
781 idles [w->active - 1] = idles [--idlecnt]; 924 idles [w->active - 1] = idles [--idlecnt];
782 ev_stop ((W)w); 925 ev_stop ((W)w);
783} 926}
784 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
785void evcheck_start (struct ev_check *w) 951ev_check_start (struct ev_check *w)
786{ 952{
787 if (ev_is_active (w)) 953 if (ev_is_active (w))
788 return; 954 return;
789 955
790 ev_start ((W)w, ++checkcnt); 956 ev_start ((W)w, ++checkcnt);
791 array_needsize (checks, checkmax, checkcnt, ); 957 array_needsize (checks, checkmax, checkcnt, );
792 checks [checkcnt - 1] = w; 958 checks [checkcnt - 1] = w;
793} 959}
794 960
961void
795void evcheck_stop (struct ev_check *w) 962ev_check_stop (struct ev_check *w)
796{ 963{
964 ev_clear ((W)w);
965 if (ev_is_active (w))
966 return;
967
797 checks [w->active - 1] = checks [--checkcnt]; 968 checks [w->active - 1] = checks [--checkcnt];
798 ev_stop ((W)w); 969 ev_stop ((W)w);
799} 970}
800 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
801/*****************************************************************************/ 1056/*****************************************************************************/
802 1057
803#if 0 1058#if 0
804 1059
805struct ev_io wio; 1060struct ev_io wio;
812 1067
813static void 1068static void
814ocb (struct ev_timer *w, int revents) 1069ocb (struct ev_timer *w, int revents)
815{ 1070{
816 //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);
817 evtimer_stop (w); 1072 ev_timer_stop (w);
818 evtimer_start (w); 1073 ev_timer_start (w);
819} 1074}
820 1075
821static void 1076static void
822scb (struct ev_signal *w, int revents) 1077scb (struct ev_signal *w, int revents)
823{ 1078{
824 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1079 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
825 evio_stop (&wio); 1080 ev_io_stop (&wio);
826 evio_start (&wio); 1081 ev_io_start (&wio);
827} 1082}
828 1083
829static void 1084static void
830gcb (struct ev_signal *w, int revents) 1085gcb (struct ev_signal *w, int revents)
831{ 1086{
835 1090
836int main (void) 1091int main (void)
837{ 1092{
838 ev_init (0); 1093 ev_init (0);
839 1094
840 evio_init (&wio, sin_cb, 0, EV_READ); 1095 ev_io_init (&wio, sin_cb, 0, EV_READ);
841 evio_start (&wio); 1096 ev_io_start (&wio);
842 1097
843 struct ev_timer t[10000]; 1098 struct ev_timer t[10000];
844 1099
845#if 0 1100#if 0
846 int i; 1101 int i;
847 for (i = 0; i < 10000; ++i) 1102 for (i = 0; i < 10000; ++i)
848 { 1103 {
849 struct ev_timer *w = t + i; 1104 struct ev_timer *w = t + i;
850 evw_init (w, ocb, i); 1105 ev_watcher_init (w, ocb, i);
851 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1106 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
852 evtimer_start (w); 1107 ev_timer_start (w);
853 if (drand48 () < 0.5) 1108 if (drand48 () < 0.5)
854 evtimer_stop (w); 1109 ev_timer_stop (w);
855 } 1110 }
856#endif 1111#endif
857 1112
858 struct ev_timer t1; 1113 struct ev_timer t1;
859 evtimer_init (&t1, ocb, 5, 10); 1114 ev_timer_init (&t1, ocb, 5, 10);
860 evtimer_start (&t1); 1115 ev_timer_start (&t1);
861 1116
862 struct ev_signal sig; 1117 struct ev_signal sig;
863 evsignal_init (&sig, scb, SIGQUIT); 1118 ev_signal_init (&sig, scb, SIGQUIT);
864 evsignal_start (&sig); 1119 ev_signal_start (&sig);
865 1120
866 struct ev_check cw; 1121 struct ev_check cw;
867 evcheck_init (&cw, gcb); 1122 ev_check_init (&cw, gcb);
868 evcheck_start (&cw); 1123 ev_check_start (&cw);
869 1124
870 struct ev_idle iw; 1125 struct ev_idle iw;
871 evidle_init (&iw, gcb); 1126 ev_idle_init (&iw, gcb);
872 evidle_start (&iw); 1127 ev_idle_start (&iw);
873 1128
874 ev_loop (0); 1129 ev_loop (0);
875 1130
876 return 0; 1131 return 0;
877} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines