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

Comparing libev/ev.c (file contents):
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC vs.
Revision 1.22 by root, Wed Oct 31 19:07:43 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
1#include <math.h> 30#include <math.h>
2#include <stdlib.h> 31#include <stdlib.h>
3#include <unistd.h> 32#include <unistd.h>
4#include <fcntl.h> 33#include <fcntl.h>
5#include <signal.h> 34#include <signal.h>
7 36
8#include <stdio.h> 37#include <stdio.h>
9 38
10#include <assert.h> 39#include <assert.h>
11#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
12#include <sys/time.h> 43#include <sys/time.h>
13#include <time.h> 44#include <time.h>
14
15#define HAVE_EPOLL 1
16 45
17#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
18# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
19# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
20# endif 49# endif
32# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
33#endif 62#endif
34 63
35#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
36#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
37 67
38#include "ev.h" 68#include "ev.h"
39 69
40typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
41typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
159 189
160 for (i = 0; i < eventcnt; ++i) 190 for (i = 0; i < eventcnt; ++i)
161 event (events [i], type); 191 event (events [i], type);
162} 192}
163 193
194/* called on EBADF to verify fds */
195static void
196fd_recheck ()
197{
198 int fd;
199
200 for (fd = 0; fd < anfdmax; ++fd)
201 if (anfds [fd].wev)
202 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
203 while (anfds [fd].head)
204 evio_stop (anfds [fd].head);
205}
206
164/*****************************************************************************/ 207/*****************************************************************************/
165 208
166static struct ev_timer **timers; 209static struct ev_timer **timers;
167static int timermax, timercnt; 210static int timermax, timercnt;
168 211
284/*****************************************************************************/ 327/*****************************************************************************/
285 328
286static struct ev_idle **idles; 329static struct ev_idle **idles;
287static int idlemax, idlecnt; 330static int idlemax, idlecnt;
288 331
332static struct ev_prepare **prepares;
333static int preparemax, preparecnt;
334
289static struct ev_check **checks; 335static struct ev_check **checks;
290static int checkmax, checkcnt; 336static int checkmax, checkcnt;
337
338/*****************************************************************************/
339
340static struct ev_child *childs [PID_HASHSIZE];
341static struct ev_signal childev;
342
343#ifndef WCONTINUED
344# define WCONTINUED 0
345#endif
346
347static void
348childcb (struct ev_signal *sw, int revents)
349{
350 struct ev_child *w;
351 int pid, status;
352
353 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
354 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
355 if (w->pid == pid || w->pid == -1)
356 {
357 w->status = status;
358 event ((W)w, EV_CHILD);
359 }
360}
291 361
292/*****************************************************************************/ 362/*****************************************************************************/
293 363
294#if HAVE_EPOLL 364#if HAVE_EPOLL
295# include "ev_epoll.c" 365# include "ev_epoll.c"
325 395
326 if (ev_method) 396 if (ev_method)
327 { 397 {
328 evw_init (&sigev, sigcb); 398 evw_init (&sigev, sigcb);
329 siginit (); 399 siginit ();
400
401 evsignal_init (&childev, childcb, SIGCHLD);
402 evsignal_start (&childev);
330 } 403 }
331 404
332 return ev_method; 405 return ev_method;
333} 406}
334 407
387} 460}
388 461
389static void 462static void
390call_pending () 463call_pending ()
391{ 464{
392 int i; 465 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 466 {
396 ANPENDING *p = pendings + i; 467 ANPENDING *p = pendings + --pendingcnt;
397 468
398 if (p->w) 469 if (p->w)
399 { 470 {
400 p->w->pending = 0; 471 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 472 p->w->cb (p->w, p->events);
402 } 473 }
403 } 474 }
404
405 pendingcnt = 0;
406} 475}
407 476
408static void 477static void
409timers_reify () 478timers_reify ()
410{ 479{
517void ev_loop (int flags) 586void ev_loop (int flags)
518{ 587{
519 double block; 588 double block;
520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 589 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
521 590
522 if (checkcnt)
523 {
524 queue_events ((W *)checks, checkcnt, EV_CHECK);
525 call_pending ();
526 }
527
528 do 591 do
529 { 592 {
593 /* queue check watchers (and execute them) */
594 if (preparecnt)
595 {
596 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
597 call_pending ();
598 }
599
530 /* update fd-related kernel structures */ 600 /* update fd-related kernel structures */
531 fd_reify (); 601 fd_reify ();
532 602
533 /* calculate blocking time */ 603 /* calculate blocking time */
534 604
535 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 605 /* we only need this for !monotonic clockor timers, but as we basically
606 always have timers, we just calculate it always */
536 ev_now = ev_time (); 607 ev_now = ev_time ();
537 608
538 if (flags & EVLOOP_NONBLOCK || idlecnt) 609 if (flags & EVLOOP_NONBLOCK || idlecnt)
539 block = 0.; 610 block = 0.;
540 else 611 else
560 631
561 /* update ev_now, do magic */ 632 /* update ev_now, do magic */
562 time_update (); 633 time_update ();
563 634
564 /* queue pending timers and reschedule them */ 635 /* queue pending timers and reschedule them */
636 timers_reify (); /* relative timers called last */
565 periodics_reify (); /* absolute timers first */ 637 periodics_reify (); /* absolute timers called first */
566 timers_reify (); /* relative timers second */
567 638
568 /* queue idle watchers unless io or timers are pending */ 639 /* queue idle watchers unless io or timers are pending */
569 if (!pendingcnt) 640 if (!pendingcnt)
570 queue_events ((W *)idles, idlecnt, EV_IDLE); 641 queue_events ((W *)idles, idlecnt, EV_IDLE);
571 642
572 /* queue check and possibly idle watchers */ 643 /* queue check watchers, to be executed first */
644 if (checkcnt)
573 queue_events ((W *)checks, checkcnt, EV_CHECK); 645 queue_events ((W *)checks, checkcnt, EV_CHECK);
574 646
575 call_pending (); 647 call_pending ();
576 } 648 }
577 while (!ev_loop_done); 649 while (!ev_loop_done);
578 650
797 869
798 idles [w->active - 1] = idles [--idlecnt]; 870 idles [w->active - 1] = idles [--idlecnt];
799 ev_stop ((W)w); 871 ev_stop ((W)w);
800} 872}
801 873
874void evprepare_start (struct ev_prepare *w)
875{
876 if (ev_is_active (w))
877 return;
878
879 ev_start ((W)w, ++preparecnt);
880 array_needsize (prepares, preparemax, preparecnt, );
881 prepares [preparecnt - 1] = w;
882}
883
884void evprepare_stop (struct ev_prepare *w)
885{
886 ev_clear ((W)w);
887 if (ev_is_active (w))
888 return;
889
890 prepares [w->active - 1] = prepares [--preparecnt];
891 ev_stop ((W)w);
892}
893
802void evcheck_start (struct ev_check *w) 894void evcheck_start (struct ev_check *w)
803{ 895{
804 if (ev_is_active (w)) 896 if (ev_is_active (w))
805 return; 897 return;
806 898
814 ev_clear ((W)w); 906 ev_clear ((W)w);
815 if (ev_is_active (w)) 907 if (ev_is_active (w))
816 return; 908 return;
817 909
818 checks [w->active - 1] = checks [--checkcnt]; 910 checks [w->active - 1] = checks [--checkcnt];
911 ev_stop ((W)w);
912}
913
914void evchild_start (struct ev_child *w)
915{
916 if (ev_is_active (w))
917 return;
918
919 ev_start ((W)w, 1);
920 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
921}
922
923void evchild_stop (struct ev_child *w)
924{
925 ev_clear ((W)w);
926 if (ev_is_active (w))
927 return;
928
929 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
819 ev_stop ((W)w); 930 ev_stop ((W)w);
820} 931}
821 932
822/*****************************************************************************/ 933/*****************************************************************************/
823 934

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines