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.31 by root, Thu Nov 1 09:05:33 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
132 pendings [pendingcnt - 1].w = w; 175 pendings [pendingcnt - 1].w = w;
133 pendings [pendingcnt - 1].events = events; 176 pendings [pendingcnt - 1].events = events;
134} 177}
135 178
136static void 179static void
180queue_events (W *events, int eventcnt, int type)
181{
182 int i;
183
184 for (i = 0; i < eventcnt; ++i)
185 event (events [i], type);
186}
187
188static void
137fd_event (int fd, int events) 189fd_event (int fd, int events)
138{ 190{
139 ANFD *anfd = anfds + fd; 191 ANFD *anfd = anfds + fd;
140 struct ev_io *w; 192 struct ev_io *w;
141 193
146 if (ev) 198 if (ev)
147 event ((W)w, ev); 199 event ((W)w, ev);
148 } 200 }
149} 201}
150 202
203/*****************************************************************************/
204
205static int *fdchanges;
206static int fdchangemax, fdchangecnt;
207
151static void 208static void
152queue_events (W *events, int eventcnt, int type) 209fd_reify (void)
153{ 210{
154 int i; 211 int i;
155 212
156 for (i = 0; i < eventcnt; ++i) 213 for (i = 0; i < fdchangecnt; ++i)
157 event (events [i], type); 214 {
215 int fd = fdchanges [i];
216 ANFD *anfd = anfds + fd;
217 struct ev_io *w;
218
219 int events = 0;
220
221 for (w = anfd->head; w; w = w->next)
222 events |= w->events;
223
224 anfd->events &= ~EV_REIFY;
225
226 if (anfd->events != events)
227 {
228 method_modify (fd, anfd->events, events);
229 anfd->events = events;
230 }
231 }
232
233 fdchangecnt = 0;
234}
235
236static void
237fd_change (int fd)
238{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
240 return;
241
242 anfds [fd].events |= EV_REIFY;
243
244 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd;
247}
248
249/* called on EBADF to verify fds */
250static void
251fd_recheck (void)
252{
253 int fd;
254
255 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 while (anfds [fd].head)
259 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
261 ev_io_stop (anfds [fd].head);
262 }
158} 263}
159 264
160/*****************************************************************************/ 265/*****************************************************************************/
161 266
162static struct ev_timer **timers; 267static struct ev_timer **timers;
271 376
272 /* rather than sort out wether we really need nb, set it */ 377 /* rather than sort out wether we really need nb, set it */
273 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 378 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
274 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 379 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
275 380
276 evio_set (&sigev, sigpipe [0], EV_READ); 381 ev_io_set (&sigev, sigpipe [0], EV_READ);
277 evio_start (&sigev); 382 ev_io_start (&sigev);
278} 383}
279 384
280/*****************************************************************************/ 385/*****************************************************************************/
281 386
282static struct ev_idle **idles; 387static struct ev_idle **idles;
283static int idlemax, idlecnt; 388static int idlemax, idlecnt;
284 389
390static struct ev_prepare **prepares;
391static int preparemax, preparecnt;
392
285static struct ev_check **checks; 393static struct ev_check **checks;
286static int checkmax, checkcnt; 394static int checkmax, checkcnt;
287 395
288/*****************************************************************************/ 396/*****************************************************************************/
289 397
398static struct ev_child *childs [PID_HASHSIZE];
399static struct ev_signal childev;
400
401#ifndef WCONTINUED
402# define WCONTINUED 0
403#endif
404
405static void
406childcb (struct ev_signal *sw, int revents)
407{
408 struct ev_child *w;
409 int pid, status;
410
411 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
412 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
413 if (w->pid == pid || w->pid == -1)
414 {
415 w->status = status;
416 event ((W)w, EV_CHILD);
417 }
418}
419
420/*****************************************************************************/
421
290#if HAVE_EPOLL 422#if EV_USE_EPOLL
291# include "ev_epoll.c" 423# include "ev_epoll.c"
292#endif 424#endif
293#if HAVE_SELECT 425#if EV_USE_SELECT
294# include "ev_select.c" 426# include "ev_select.c"
295#endif 427#endif
296 428
429int
430ev_version_major (void)
431{
432 return EV_VERSION_MAJOR;
433}
434
435int
436ev_version_minor (void)
437{
438 return EV_VERSION_MINOR;
439}
440
297int ev_init (int flags) 441int ev_init (int flags)
298{ 442{
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) 443 if (!ev_method)
444 {
445#if EV_USE_MONOTONIC
323 { 446 {
447 struct timespec ts;
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1;
450 }
451#endif
452
453 ev_now = ev_time ();
454 now = get_clock ();
455 diff = ev_now - now;
456
457 if (pipe (sigpipe))
458 return 0;
459
460 ev_method = EVMETHOD_NONE;
461#if EV_USE_EPOLL
462 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
463#endif
464#if EV_USE_SELECT
465 if (ev_method == EVMETHOD_NONE) select_init (flags);
466#endif
467
468 if (ev_method)
469 {
324 evw_init (&sigev, sigcb); 470 ev_watcher_init (&sigev, sigcb);
325 siginit (); 471 siginit ();
472
473 ev_signal_init (&childev, childcb, SIGCHLD);
474 ev_signal_start (&childev);
475 }
326 } 476 }
327 477
328 return ev_method; 478 return ev_method;
329} 479}
330 480
331/*****************************************************************************/ 481/*****************************************************************************/
332 482
483void
333void ev_prefork (void) 484ev_prefork (void)
334{ 485{
335 /* nop */ 486 /* nop */
336} 487}
337 488
489void
338void ev_postfork_parent (void) 490ev_postfork_parent (void)
339{ 491{
340 /* nop */ 492 /* nop */
341} 493}
342 494
495void
343void ev_postfork_child (void) 496ev_postfork_child (void)
344{ 497{
345#if HAVE_EPOLL 498#if EV_USE_EPOLL
346 if (ev_method == EVMETHOD_EPOLL) 499 if (ev_method == EVMETHOD_EPOLL)
347 epoll_postfork_child (); 500 epoll_postfork_child ();
348#endif 501#endif
349 502
350 evio_stop (&sigev); 503 ev_io_stop (&sigev);
351 close (sigpipe [0]); 504 close (sigpipe [0]);
352 close (sigpipe [1]); 505 close (sigpipe [1]);
353 pipe (sigpipe); 506 pipe (sigpipe);
354 siginit (); 507 siginit ();
355} 508}
356 509
357/*****************************************************************************/ 510/*****************************************************************************/
358 511
359static void 512static 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 () 513call_pending (void)
387{ 514{
388 int i; 515 while (pendingcnt)
389
390 for (i = 0; i < pendingcnt; ++i)
391 { 516 {
392 ANPENDING *p = pendings + i; 517 ANPENDING *p = pendings + --pendingcnt;
393 518
394 if (p->w) 519 if (p->w)
395 { 520 {
396 p->w->pending = 0; 521 p->w->pending = 0;
397 p->w->cb (p->w, p->events); 522 p->w->cb (p->w, p->events);
398 } 523 }
399 } 524 }
400
401 pendingcnt = 0;
402} 525}
403 526
404static void 527static void
405timers_reify () 528timers_reify (void)
406{ 529{
407 while (timercnt && timers [0]->at <= now) 530 while (timercnt && timers [0]->at <= now)
408 { 531 {
409 struct ev_timer *w = timers [0]; 532 struct ev_timer *w = timers [0];
410 533
414 w->at = now + w->repeat; 537 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 538 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 539 downheap ((WT *)timers, timercnt, 0);
417 } 540 }
418 else 541 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 542 ev_timer_stop (w); /* nonrepeating: stop timer */
420 543
421 event ((W)w, EV_TIMEOUT); 544 event ((W)w, EV_TIMEOUT);
422 } 545 }
423} 546}
424 547
425static void 548static void
426periodics_reify () 549periodics_reify (void)
427{ 550{
428 while (periodiccnt && periodics [0]->at <= ev_now) 551 while (periodiccnt && periodics [0]->at <= ev_now)
429 { 552 {
430 struct ev_periodic *w = periodics [0]; 553 struct ev_periodic *w = periodics [0];
431 554
435 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 558 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)); 559 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
437 downheap ((WT *)periodics, periodiccnt, 0); 560 downheap ((WT *)periodics, periodiccnt, 0);
438 } 561 }
439 else 562 else
440 evperiodic_stop (w); /* nonrepeating: stop timer */ 563 ev_periodic_stop (w); /* nonrepeating: stop timer */
441 564
442 event ((W)w, EV_TIMEOUT); 565 event ((W)w, EV_TIMEOUT);
443 } 566 }
444} 567}
445 568
457 { 580 {
458 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 581 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
459 582
460 if (fabs (diff) >= 1e-4) 583 if (fabs (diff) >= 1e-4)
461 { 584 {
462 evperiodic_stop (w); 585 ev_periodic_stop (w);
463 evperiodic_start (w); 586 ev_periodic_start (w);
464 587
465 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 588 i = 0; /* restart loop, inefficient, but time jumps should be rare */
466 } 589 }
467 } 590 }
468 } 591 }
469} 592}
470 593
471static void 594static void
472time_update () 595time_update (void)
473{ 596{
474 int i; 597 int i;
475 598
476 ev_now = ev_time (); 599 ev_now = ev_time ();
477 600
511int ev_loop_done; 634int ev_loop_done;
512 635
513void ev_loop (int flags) 636void ev_loop (int flags)
514{ 637{
515 double block; 638 double block;
516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 639 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 640
524 do 641 do
525 { 642 {
643 /* queue check watchers (and execute them) */
644 if (preparecnt)
645 {
646 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
647 call_pending ();
648 }
649
526 /* update fd-related kernel structures */ 650 /* update fd-related kernel structures */
527 fd_reify (); 651 fd_reify ();
528 652
529 /* calculate blocking time */ 653 /* calculate blocking time */
530 654
531 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 655 /* we only need this for !monotonic clockor timers, but as we basically
656 always have timers, we just calculate it always */
532 ev_now = ev_time (); 657 ev_now = ev_time ();
533 658
534 if (flags & EVLOOP_NONBLOCK || idlecnt) 659 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 block = 0.; 660 block = 0.;
536 else 661 else
556 681
557 /* update ev_now, do magic */ 682 /* update ev_now, do magic */
558 time_update (); 683 time_update ();
559 684
560 /* queue pending timers and reschedule them */ 685 /* queue pending timers and reschedule them */
686 timers_reify (); /* relative timers called last */
561 periodics_reify (); /* absolute timers first */ 687 periodics_reify (); /* absolute timers called first */
562 timers_reify (); /* relative timers second */
563 688
564 /* queue idle watchers unless io or timers are pending */ 689 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt) 690 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE); 691 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 692
568 /* queue check and possibly idle watchers */ 693 /* queue check watchers, to be executed first */
694 if (checkcnt)
569 queue_events ((W *)checks, checkcnt, EV_CHECK); 695 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 696
571 call_pending (); 697 call_pending ();
572 } 698 }
573 while (!ev_loop_done); 699 while (!ev_loop_done);
574 700
599 head = &(*head)->next; 725 head = &(*head)->next;
600 } 726 }
601} 727}
602 728
603static void 729static void
730ev_clear (W w)
731{
732 if (w->pending)
733 {
734 pendings [w->pending - 1].w = 0;
735 w->pending = 0;
736 }
737}
738
739static void
604ev_start (W w, int active) 740ev_start (W w, int active)
605{ 741{
606 w->pending = 0;
607 w->active = active; 742 w->active = active;
608} 743}
609 744
610static void 745static void
611ev_stop (W w) 746ev_stop (W w)
612{ 747{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 748 w->active = 0;
617} 749}
618 750
619/*****************************************************************************/ 751/*****************************************************************************/
620 752
621void 753void
622evio_start (struct ev_io *w) 754ev_io_start (struct ev_io *w)
623{ 755{
624 if (ev_is_active (w)) 756 if (ev_is_active (w))
625 return; 757 return;
626 758
627 int fd = w->fd; 759 int fd = w->fd;
628 760
629 ev_start ((W)w, 1); 761 ev_start ((W)w, 1);
630 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 762 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
631 wlist_add ((WL *)&anfds[fd].head, (WL)w); 763 wlist_add ((WL *)&anfds[fd].head, (WL)w);
632 764
633 ++fdchangecnt; 765 fd_change (fd);
634 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
635 fdchanges [fdchangecnt - 1] = fd;
636} 766}
637 767
638void 768void
639evio_stop (struct ev_io *w) 769ev_io_stop (struct ev_io *w)
640{ 770{
771 ev_clear ((W)w);
641 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
642 return; 773 return;
643 774
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 776 ev_stop ((W)w);
646 777
647 ++fdchangecnt; 778 fd_change (w->fd);
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd;
650} 779}
651 780
652
653void 781void
654evtimer_start (struct ev_timer *w) 782ev_timer_start (struct ev_timer *w)
655{ 783{
656 if (ev_is_active (w)) 784 if (ev_is_active (w))
657 return; 785 return;
658 786
659 w->at += now; 787 w->at += now;
665 timers [timercnt - 1] = w; 793 timers [timercnt - 1] = w;
666 upheap ((WT *)timers, timercnt - 1); 794 upheap ((WT *)timers, timercnt - 1);
667} 795}
668 796
669void 797void
670evtimer_stop (struct ev_timer *w) 798ev_timer_stop (struct ev_timer *w)
671{ 799{
800 ev_clear ((W)w);
672 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
673 return; 802 return;
674 803
675 if (w->active < timercnt--) 804 if (w->active < timercnt--)
676 { 805 {
682 811
683 ev_stop ((W)w); 812 ev_stop ((W)w);
684} 813}
685 814
686void 815void
687evtimer_again (struct ev_timer *w) 816ev_timer_again (struct ev_timer *w)
688{ 817{
689 if (ev_is_active (w)) 818 if (ev_is_active (w))
690 { 819 {
691 if (w->repeat) 820 if (w->repeat)
692 { 821 {
693 w->at = now + w->repeat; 822 w->at = now + w->repeat;
694 downheap ((WT *)timers, timercnt, w->active - 1); 823 downheap ((WT *)timers, timercnt, w->active - 1);
695 } 824 }
696 else 825 else
697 evtimer_stop (w); 826 ev_timer_stop (w);
698 } 827 }
699 else if (w->repeat) 828 else if (w->repeat)
700 evtimer_start (w); 829 ev_timer_start (w);
701} 830}
702 831
703void 832void
704evperiodic_start (struct ev_periodic *w) 833ev_periodic_start (struct ev_periodic *w)
705{ 834{
706 if (ev_is_active (w)) 835 if (ev_is_active (w))
707 return; 836 return;
708 837
709 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 838 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
717 periodics [periodiccnt - 1] = w; 846 periodics [periodiccnt - 1] = w;
718 upheap ((WT *)periodics, periodiccnt - 1); 847 upheap ((WT *)periodics, periodiccnt - 1);
719} 848}
720 849
721void 850void
722evperiodic_stop (struct ev_periodic *w) 851ev_periodic_stop (struct ev_periodic *w)
723{ 852{
853 ev_clear ((W)w);
724 if (!ev_is_active (w)) 854 if (!ev_is_active (w))
725 return; 855 return;
726 856
727 if (w->active < periodiccnt--) 857 if (w->active < periodiccnt--)
728 { 858 {
732 862
733 ev_stop ((W)w); 863 ev_stop ((W)w);
734} 864}
735 865
736void 866void
737evsignal_start (struct ev_signal *w) 867ev_signal_start (struct ev_signal *w)
738{ 868{
739 if (ev_is_active (w)) 869 if (ev_is_active (w))
740 return; 870 return;
741 871
742 ev_start ((W)w, 1); 872 ev_start ((W)w, 1);
752 sigaction (w->signum, &sa, 0); 882 sigaction (w->signum, &sa, 0);
753 } 883 }
754} 884}
755 885
756void 886void
757evsignal_stop (struct ev_signal *w) 887ev_signal_stop (struct ev_signal *w)
758{ 888{
889 ev_clear ((W)w);
759 if (!ev_is_active (w)) 890 if (!ev_is_active (w))
760 return; 891 return;
761 892
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 894 ev_stop ((W)w);
764 895
765 if (!signals [w->signum - 1].head) 896 if (!signals [w->signum - 1].head)
766 signal (w->signum, SIG_DFL); 897 signal (w->signum, SIG_DFL);
767} 898}
768 899
900void
769void evidle_start (struct ev_idle *w) 901ev_idle_start (struct ev_idle *w)
770{ 902{
771 if (ev_is_active (w)) 903 if (ev_is_active (w))
772 return; 904 return;
773 905
774 ev_start ((W)w, ++idlecnt); 906 ev_start ((W)w, ++idlecnt);
775 array_needsize (idles, idlemax, idlecnt, ); 907 array_needsize (idles, idlemax, idlecnt, );
776 idles [idlecnt - 1] = w; 908 idles [idlecnt - 1] = w;
777} 909}
778 910
911void
779void evidle_stop (struct ev_idle *w) 912ev_idle_stop (struct ev_idle *w)
780{ 913{
914 ev_clear ((W)w);
915 if (ev_is_active (w))
916 return;
917
781 idles [w->active - 1] = idles [--idlecnt]; 918 idles [w->active - 1] = idles [--idlecnt];
782 ev_stop ((W)w); 919 ev_stop ((W)w);
783} 920}
784 921
922void
923ev_prepare_start (struct ev_prepare *w)
924{
925 if (ev_is_active (w))
926 return;
927
928 ev_start ((W)w, ++preparecnt);
929 array_needsize (prepares, preparemax, preparecnt, );
930 prepares [preparecnt - 1] = w;
931}
932
933void
934ev_prepare_stop (struct ev_prepare *w)
935{
936 ev_clear ((W)w);
937 if (ev_is_active (w))
938 return;
939
940 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w);
942}
943
944void
785void evcheck_start (struct ev_check *w) 945ev_check_start (struct ev_check *w)
786{ 946{
787 if (ev_is_active (w)) 947 if (ev_is_active (w))
788 return; 948 return;
789 949
790 ev_start ((W)w, ++checkcnt); 950 ev_start ((W)w, ++checkcnt);
791 array_needsize (checks, checkmax, checkcnt, ); 951 array_needsize (checks, checkmax, checkcnt, );
792 checks [checkcnt - 1] = w; 952 checks [checkcnt - 1] = w;
793} 953}
794 954
955void
795void evcheck_stop (struct ev_check *w) 956ev_check_stop (struct ev_check *w)
796{ 957{
958 ev_clear ((W)w);
959 if (ev_is_active (w))
960 return;
961
797 checks [w->active - 1] = checks [--checkcnt]; 962 checks [w->active - 1] = checks [--checkcnt];
798 ev_stop ((W)w); 963 ev_stop ((W)w);
799} 964}
800 965
966void
967ev_child_start (struct ev_child *w)
968{
969 if (ev_is_active (w))
970 return;
971
972 ev_start ((W)w, 1);
973 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
974}
975
976void
977ev_child_stop (struct ev_child *w)
978{
979 ev_clear ((W)w);
980 if (ev_is_active (w))
981 return;
982
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w);
985}
986
987/*****************************************************************************/
988
989struct ev_once
990{
991 struct ev_io io;
992 struct ev_timer to;
993 void (*cb)(int revents, void *arg);
994 void *arg;
995};
996
997static void
998once_cb (struct ev_once *once, int revents)
999{
1000 void (*cb)(int revents, void *arg) = once->cb;
1001 void *arg = once->arg;
1002
1003 ev_io_stop (&once->io);
1004 ev_timer_stop (&once->to);
1005 free (once);
1006
1007 cb (revents, arg);
1008}
1009
1010static void
1011once_cb_io (struct ev_io *w, int revents)
1012{
1013 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1014}
1015
1016static void
1017once_cb_to (struct ev_timer *w, int revents)
1018{
1019 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1020}
1021
1022void
1023ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1024{
1025 struct ev_once *once = malloc (sizeof (struct ev_once));
1026
1027 if (!once)
1028 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1029 else
1030 {
1031 once->cb = cb;
1032 once->arg = arg;
1033
1034 ev_watcher_init (&once->io, once_cb_io);
1035 if (fd >= 0)
1036 {
1037 ev_io_set (&once->io, fd, events);
1038 ev_io_start (&once->io);
1039 }
1040
1041 ev_watcher_init (&once->to, once_cb_to);
1042 if (timeout >= 0.)
1043 {
1044 ev_timer_set (&once->to, timeout, 0.);
1045 ev_timer_start (&once->to);
1046 }
1047 }
1048}
1049
801/*****************************************************************************/ 1050/*****************************************************************************/
802 1051
803#if 0 1052#if 0
804 1053
805struct ev_io wio; 1054struct ev_io wio;
812 1061
813static void 1062static void
814ocb (struct ev_timer *w, int revents) 1063ocb (struct ev_timer *w, int revents)
815{ 1064{
816 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1065 //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); 1066 ev_timer_stop (w);
818 evtimer_start (w); 1067 ev_timer_start (w);
819} 1068}
820 1069
821static void 1070static void
822scb (struct ev_signal *w, int revents) 1071scb (struct ev_signal *w, int revents)
823{ 1072{
824 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1073 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
825 evio_stop (&wio); 1074 ev_io_stop (&wio);
826 evio_start (&wio); 1075 ev_io_start (&wio);
827} 1076}
828 1077
829static void 1078static void
830gcb (struct ev_signal *w, int revents) 1079gcb (struct ev_signal *w, int revents)
831{ 1080{
835 1084
836int main (void) 1085int main (void)
837{ 1086{
838 ev_init (0); 1087 ev_init (0);
839 1088
840 evio_init (&wio, sin_cb, 0, EV_READ); 1089 ev_io_init (&wio, sin_cb, 0, EV_READ);
841 evio_start (&wio); 1090 ev_io_start (&wio);
842 1091
843 struct ev_timer t[10000]; 1092 struct ev_timer t[10000];
844 1093
845#if 0 1094#if 0
846 int i; 1095 int i;
847 for (i = 0; i < 10000; ++i) 1096 for (i = 0; i < 10000; ++i)
848 { 1097 {
849 struct ev_timer *w = t + i; 1098 struct ev_timer *w = t + i;
850 evw_init (w, ocb, i); 1099 ev_watcher_init (w, ocb, i);
851 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1100 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
852 evtimer_start (w); 1101 ev_timer_start (w);
853 if (drand48 () < 0.5) 1102 if (drand48 () < 0.5)
854 evtimer_stop (w); 1103 ev_timer_stop (w);
855 } 1104 }
856#endif 1105#endif
857 1106
858 struct ev_timer t1; 1107 struct ev_timer t1;
859 evtimer_init (&t1, ocb, 5, 10); 1108 ev_timer_init (&t1, ocb, 5, 10);
860 evtimer_start (&t1); 1109 ev_timer_start (&t1);
861 1110
862 struct ev_signal sig; 1111 struct ev_signal sig;
863 evsignal_init (&sig, scb, SIGQUIT); 1112 ev_signal_init (&sig, scb, SIGQUIT);
864 evsignal_start (&sig); 1113 ev_signal_start (&sig);
865 1114
866 struct ev_check cw; 1115 struct ev_check cw;
867 evcheck_init (&cw, gcb); 1116 ev_check_init (&cw, gcb);
868 evcheck_start (&cw); 1117 ev_check_start (&cw);
869 1118
870 struct ev_idle iw; 1119 struct ev_idle iw;
871 evidle_init (&iw, gcb); 1120 ev_idle_init (&iw, gcb);
872 evidle_start (&iw); 1121 ev_idle_start (&iw);
873 1122
874 ev_loop (0); 1123 ev_loop (0);
875 1124
876 return 0; 1125 return 0;
877} 1126}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines