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.29 by root, Thu Nov 1 08:10:03 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_nextsize(n) (((n) << 1) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
114 120
115#define array_needsize(base,cur,cnt,init) \ 121#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 122 if ((cnt) > cur) \
117 { \ 123 { \
118 int newcnt = cur; \ 124 int newcnt = cur; \
119 do \ 125 do \
120 { \ 126 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 127 newcnt = array_nextsize (newcnt); \
122 } \ 128 } \
123 while ((cnt) > newcnt); \ 129 while ((cnt) > newcnt); \
124 \ 130 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 131 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 132 init (base + cur, newcnt - cur); \
228} 234}
229 235
230static void 236static void
231fd_change (int fd) 237fd_change (int fd)
232{ 238{
233 if (anfds [fd].events & EV_REIFY) 239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
234 return; 240 return;
235 241
236 anfds [fd].events |= EV_REIFY; 242 anfds [fd].events |= EV_REIFY;
237 243
238 ++fdchangecnt; 244 ++fdchangecnt;
249 for (fd = 0; fd < anfdmax; ++fd) 255 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 256 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 258 while (anfds [fd].head)
253 { 259 {
254 event ((W)anfds [fd].head, EV_ERROR); 260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
255 ev_io_stop (anfds [fd].head); 261 ev_io_stop (anfds [fd].head);
256 } 262 }
257} 263}
258 264
259/*****************************************************************************/ 265/*****************************************************************************/
411 } 417 }
412} 418}
413 419
414/*****************************************************************************/ 420/*****************************************************************************/
415 421
416#if HAVE_EPOLL 422#if EV_USE_EPOLL
417# include "ev_epoll.c" 423# include "ev_epoll.c"
418#endif 424#endif
419#if HAVE_SELECT 425#if EV_USE_SELECT
420# include "ev_select.c" 426# include "ev_select.c"
421#endif 427#endif
422 428
423int 429int
424ev_version_major (void) 430ev_version_major (void)
434 440
435int ev_init (int flags) 441int ev_init (int flags)
436{ 442{
437 if (!ev_method) 443 if (!ev_method)
438 { 444 {
439#if HAVE_MONOTONIC 445#if EV_USE_MONOTONIC
440 { 446 {
441 struct timespec ts; 447 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 448 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 449 have_monotonic = 1;
444 } 450 }
450 456
451 if (pipe (sigpipe)) 457 if (pipe (sigpipe))
452 return 0; 458 return 0;
453 459
454 ev_method = EVMETHOD_NONE; 460 ev_method = EVMETHOD_NONE;
455#if HAVE_EPOLL 461#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 462 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457#endif 463#endif
458#if HAVE_SELECT 464#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 465 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 466#endif
461 467
462 if (ev_method) 468 if (ev_method)
463 { 469 {
487} 493}
488 494
489void 495void
490ev_postfork_child (void) 496ev_postfork_child (void)
491{ 497{
492#if HAVE_EPOLL 498#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 499 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 500 epoll_postfork_child ();
495#endif 501#endif
496 502
497 ev_io_stop (&sigev); 503 ev_io_stop (&sigev);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1023ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1024{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1025 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1026
1021 if (!once) 1027 if (!once)
1022 cb (EV_ERROR, arg); 1028 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1029 else
1024 { 1030 {
1025 once->cb = cb; 1031 once->cb = cb;
1026 once->arg = arg; 1032 once->arg = arg;
1027 1033
1028 ev_watcher_init (&once->io, once_cb_io); 1034 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1035 if (fd >= 0)
1031 { 1036 {
1032 ev_io_set (&once->io, fd, events); 1037 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1038 ev_io_start (&once->io);
1034 } 1039 }
1035 1040
1036 ev_watcher_init (&once->to, once_cb_to); 1041 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1042 if (timeout >= 0.)
1039 { 1043 {
1040 ev_timer_set (&once->to, timeout, 0.); 1044 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1045 ev_timer_start (&once->to);
1042 } 1046 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines