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

Comparing libev/ev.c (file contents):
Revision 1.28 by root, Thu Nov 1 06:48:49 2007 UTC vs.
Revision 1.30 by root, Thu Nov 1 08:28:33 2007 UTC

24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
29 32
30#include <math.h> 33#include <math.h>
31#include <stdlib.h> 34#include <stdlib.h>
32#include <unistd.h> 35#include <unistd.h>
33#include <fcntl.h> 36#include <fcntl.h>
41#include <sys/types.h> 44#include <sys/types.h>
42#include <sys/wait.h> 45#include <sys/wait.h>
43#include <sys/time.h> 46#include <sys/time.h>
44#include <time.h> 47#include <time.h>
45 48
46#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
49# endif 52# endif
50#endif 53#endif
51 54
52#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
54#endif 57#endif
55 58
56#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
58#endif 61#endif
59 62
60#ifndef HAVE_REALTIME 63#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 65#endif
63 66
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 67#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 68#define MAX_BLOCKTIME 59.731
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 69#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
67 70
68#include "ev.h" 71#include "ev.h"
69 72
70typedef struct ev_watcher *W; 73typedef struct ev_watcher *W;
84/*****************************************************************************/ 87/*****************************************************************************/
85 88
86ev_tstamp 89ev_tstamp
87ev_time (void) 90ev_time (void)
88{ 91{
89#if HAVE_REALTIME 92#if EV_USE_REALTIME
90 struct timespec ts; 93 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 94 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 95 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 96#else
94 struct timeval tv; 97 struct timeval tv;
98} 101}
99 102
100static ev_tstamp 103static ev_tstamp
101get_clock (void) 104get_clock (void)
102{ 105{
103#if HAVE_MONOTONIC 106#if EV_USE_MONOTONIC
104 if (have_monotonic) 107 if (have_monotonic)
105 { 108 {
106 struct timespec ts; 109 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 110 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 111 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 112 }
110#endif 113#endif
111 114
112 return ev_time (); 115 return ev_time ();
113} 116}
117
118#define array_roundsize(base,n) ((n) | 4 & ~3)
114 119
115#define array_needsize(base,cur,cnt,init) \ 120#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 121 if ((cnt) > cur) \
117 { \ 122 { \
118 int newcnt = cur; \ 123 int newcnt = cur; \
119 do \ 124 do \
120 { \ 125 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 126 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 127 } \
123 while ((cnt) > newcnt); \ 128 while ((cnt) > newcnt); \
124 \ 129 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 130 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 131 init (base + cur, newcnt - cur); \
159static int pendingmax, pendingcnt; 164static int pendingmax, pendingcnt;
160 165
161static void 166static void
162event (W w, int events) 167event (W w, int events)
163{ 168{
164 if (w->active)
165 {
166 w->pending = ++pendingcnt; 169 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, ); 170 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w; 171 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 172 pendings [pendingcnt - 1].events = events;
170 }
171} 173}
172 174
173static void 175static void
174queue_events (W *events, int eventcnt, int type) 176queue_events (W *events, int eventcnt, int type)
175{ 177{
228} 230}
229 231
230static void 232static void
231fd_change (int fd) 233fd_change (int fd)
232{ 234{
233 if (anfds [fd].events & EV_REIFY) 235 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
234 return; 236 return;
235 237
236 anfds [fd].events |= EV_REIFY; 238 anfds [fd].events |= EV_REIFY;
237 239
238 ++fdchangecnt; 240 ++fdchangecnt;
249 for (fd = 0; fd < anfdmax; ++fd) 251 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 252 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 253 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 254 while (anfds [fd].head)
253 { 255 {
254 event ((W)anfds [fd].head, EV_ERROR); 256 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
255 ev_io_stop (anfds [fd].head); 257 ev_io_stop (anfds [fd].head);
256 } 258 }
257} 259}
258 260
259/*****************************************************************************/ 261/*****************************************************************************/
411 } 413 }
412} 414}
413 415
414/*****************************************************************************/ 416/*****************************************************************************/
415 417
416#if HAVE_EPOLL 418#if EV_USE_EPOLL
417# include "ev_epoll.c" 419# include "ev_epoll.c"
418#endif 420#endif
419#if HAVE_SELECT 421#if EV_USE_SELECT
420# include "ev_select.c" 422# include "ev_select.c"
421#endif 423#endif
422 424
423int 425int
424ev_version_major (void) 426ev_version_major (void)
434 436
435int ev_init (int flags) 437int ev_init (int flags)
436{ 438{
437 if (!ev_method) 439 if (!ev_method)
438 { 440 {
439#if HAVE_MONOTONIC 441#if EV_USE_MONOTONIC
440 { 442 {
441 struct timespec ts; 443 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 444 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 445 have_monotonic = 1;
444 } 446 }
450 452
451 if (pipe (sigpipe)) 453 if (pipe (sigpipe))
452 return 0; 454 return 0;
453 455
454 ev_method = EVMETHOD_NONE; 456 ev_method = EVMETHOD_NONE;
455#if HAVE_EPOLL 457#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 458 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457#endif 459#endif
458#if HAVE_SELECT 460#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 461 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 462#endif
461 463
462 if (ev_method) 464 if (ev_method)
463 { 465 {
487} 489}
488 490
489void 491void
490ev_postfork_child (void) 492ev_postfork_child (void)
491{ 493{
492#if HAVE_EPOLL 494#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 495 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 496 epoll_postfork_child ();
495#endif 497#endif
496 498
497 ev_io_stop (&sigev); 499 ev_io_stop (&sigev);
523{ 525{
524 while (timercnt && timers [0]->at <= now) 526 while (timercnt && timers [0]->at <= now)
525 { 527 {
526 struct ev_timer *w = timers [0]; 528 struct ev_timer *w = timers [0];
527 529
528 event ((W)w, EV_TIMEOUT);
529
530 /* first reschedule or stop timer */ 530 /* first reschedule or stop timer */
531 if (w->repeat) 531 if (w->repeat)
532 { 532 {
533 w->at = now + w->repeat; 533 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now)); 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 535 downheap ((WT *)timers, timercnt, 0);
536 } 536 }
537 else 537 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 538 ev_timer_stop (w); /* nonrepeating: stop timer */
539
540 event ((W)w, EV_TIMEOUT);
539 } 541 }
540} 542}
541 543
542static void 544static void
543periodics_reify (void) 545periodics_reify (void)
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1019ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1020{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1021 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1022
1021 if (!once) 1023 if (!once)
1022 cb (EV_ERROR, arg); 1024 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1025 else
1024 { 1026 {
1025 once->cb = cb; 1027 once->cb = cb;
1026 once->arg = arg; 1028 once->arg = arg;
1027 1029
1028 ev_watcher_init (&once->io, once_cb_io); 1030 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1031 if (fd >= 0)
1031 { 1032 {
1032 ev_io_set (&once->io, fd, events); 1033 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1034 ev_io_start (&once->io);
1034 } 1035 }
1035 1036
1036 ev_watcher_init (&once->to, once_cb_to); 1037 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1038 if (timeout >= 0.)
1039 { 1039 {
1040 ev_timer_set (&once->to, timeout, 0.); 1040 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1041 ev_timer_start (&once->to);
1042 } 1042 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines