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

Comparing libev/ev.c (file contents):
Revision 1.351 by root, Sat Oct 16 06:46:55 2010 UTC vs.
Revision 1.357 by root, Sat Oct 23 22:25:44 2010 UTC

35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* this big block deduces configuration from config.h */ 40/* this big block deduces configuration from config.h */
45#ifndef EV_STANDALONE 41#ifndef EV_STANDALONE
46# ifdef EV_CONFIG_H 42# ifdef EV_CONFIG_H
47# include EV_CONFIG_H 43# include EV_CONFIG_H
48# else 44# else
182# include EV_H 178# include EV_H
183#else 179#else
184# include "ev.h" 180# include "ev.h"
185#endif 181#endif
186 182
183EV_CPP(extern "C" {)
184
187#ifndef _WIN32 185#ifndef _WIN32
188# include <sys/time.h> 186# include <sys/time.h>
189# include <sys/wait.h> 187# include <sys/wait.h>
190# include <unistd.h> 188# include <unistd.h>
191#else 189#else
384# include <sys/select.h> 382# include <sys/select.h>
385# endif 383# endif
386#endif 384#endif
387 385
388#if EV_USE_INOTIFY 386#if EV_USE_INOTIFY
389# include <sys/utsname.h>
390# include <sys/statfs.h> 387# include <sys/statfs.h>
391# include <sys/inotify.h> 388# include <sys/inotify.h>
392/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ 389/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
393# ifndef IN_DONT_FOLLOW 390# ifndef IN_DONT_FOLLOW
394# undef EV_USE_INOTIFY 391# undef EV_USE_INOTIFY
411# define EFD_CLOEXEC O_CLOEXEC 408# define EFD_CLOEXEC O_CLOEXEC
412# else 409# else
413# define EFD_CLOEXEC 02000000 410# define EFD_CLOEXEC 02000000
414# endif 411# endif
415# endif 412# endif
416# ifdef __cplusplus
417extern "C" {
418# endif
419int (eventfd) (unsigned int initval, int flags); 413EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
420# ifdef __cplusplus
421}
422# endif
423#endif 414#endif
424 415
425#if EV_USE_SIGNALFD 416#if EV_USE_SIGNALFD
426/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 417/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
427# include <stdint.h> 418# include <stdint.h>
433# define SFD_CLOEXEC O_CLOEXEC 424# define SFD_CLOEXEC O_CLOEXEC
434# else 425# else
435# define SFD_CLOEXEC 02000000 426# define SFD_CLOEXEC 02000000
436# endif 427# endif
437# endif 428# endif
438# ifdef __cplusplus
439extern "C" {
440# endif
441int signalfd (int fd, const sigset_t *mask, int flags); 429EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
442 430
443struct signalfd_siginfo 431struct signalfd_siginfo
444{ 432{
445 uint32_t ssi_signo; 433 uint32_t ssi_signo;
446 char pad[128 - sizeof (uint32_t)]; 434 char pad[128 - sizeof (uint32_t)];
447}; 435};
448# ifdef __cplusplus
449}
450# endif
451#endif 436#endif
452 437
453/**/ 438/**/
454 439
455#if EV_VERIFY >= 3 440#if EV_VERIFY >= 3
537# include "ev_win32.c" 522# include "ev_win32.c"
538#endif 523#endif
539 524
540/*****************************************************************************/ 525/*****************************************************************************/
541 526
527#ifdef __linux
528# include <sys/utsname.h>
529#endif
530
531static unsigned int noinline
532ev_linux_version (void)
533{
534#ifdef __linux
535 struct utsname buf;
536 unsigned int v;
537 int i;
538 char *p = buf.release;
539
540 if (uname (&buf))
541 return 0;
542
543 for (i = 3+1; --i; )
544 {
545 unsigned int c = 0;
546
547 for (;;)
548 {
549 if (*p >= '0' && *p <= '9')
550 c = c * 10 + *p++ - '0';
551 else
552 {
553 p += *p == '.';
554 break;
555 }
556 }
557
558 v = (v << 8) | c;
559 }
560
561 return v;
562#else
563 return 0;
564#endif
565}
566
567/*****************************************************************************/
568
542#if EV_AVOID_STDIO 569#if EV_AVOID_STDIO
543static void noinline 570static void noinline
544ev_printerr (const char *msg) 571ev_printerr (const char *msg)
545{ 572{
546 write (STDERR_FILENO, msg, strlen (msg)); 573 write (STDERR_FILENO, msg, strlen (msg));
641 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */ 668 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
642 unsigned char unused; 669 unsigned char unused;
643#if EV_USE_EPOLL 670#if EV_USE_EPOLL
644 unsigned int egen; /* generation counter to counter epoll bugs */ 671 unsigned int egen; /* generation counter to counter epoll bugs */
645#endif 672#endif
646#if EV_SELECT_IS_WINSOCKET 673#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
647 SOCKET handle; 674 SOCKET handle;
675#endif
676#if EV_USE_IOCP
677 OVERLAPPED or, ow;
648#endif 678#endif
649} ANFD; 679} ANFD;
650 680
651/* stores the pending event set for a given watcher */ 681/* stores the pending event set for a given watcher */
652typedef struct 682typedef struct
717# define EV_RELEASE_CB (void)0 747# define EV_RELEASE_CB (void)0
718# define EV_ACQUIRE_CB (void)0 748# define EV_ACQUIRE_CB (void)0
719# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 749# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
720#endif 750#endif
721 751
722#define EVUNLOOP_RECURSE 0x80 752#define EVBREAK_RECURSE 0x80
723 753
724/*****************************************************************************/ 754/*****************************************************************************/
725 755
726#ifndef EV_HAVE_EV_TIME 756#ifndef EV_HAVE_EV_TIME
727ev_tstamp 757ev_tstamp
948 unsigned char o_events = anfd->events; 978 unsigned char o_events = anfd->events;
949 unsigned char o_reify = anfd->reify; 979 unsigned char o_reify = anfd->reify;
950 980
951 anfd->reify = 0; 981 anfd->reify = 0;
952 982
953#if EV_SELECT_IS_WINSOCKET 983#if EV_SELECT_IS_WINSOCKET || EV_USE_IOCP
954 if (o_reify & EV__IOFDSET) 984 if (o_reify & EV__IOFDSET)
955 { 985 {
956 unsigned long arg; 986 unsigned long arg;
957 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 987 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
958 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0)); 988 assert (("libev: only socket fds supported in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
989 printf ("oi %d %x\n", fd, anfd->handle);//D
959 } 990 }
960#endif 991#endif
961 992
962 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */ 993 /*if (expect_true (o_reify & EV_ANFD_REIFY)) probably a deoptimisation */
963 { 994 {
1462 1493
1463#endif 1494#endif
1464 1495
1465/*****************************************************************************/ 1496/*****************************************************************************/
1466 1497
1498#if EV_USE_IOCP
1499# include "ev_iocp.c"
1500#endif
1467#if EV_USE_PORT 1501#if EV_USE_PORT
1468# include "ev_port.c" 1502# include "ev_port.c"
1469#endif 1503#endif
1470#if EV_USE_KQUEUE 1504#if EV_USE_KQUEUE
1471# include "ev_kqueue.c" 1505# include "ev_kqueue.c"
1544ev_embeddable_backends (void) 1578ev_embeddable_backends (void)
1545{ 1579{
1546 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 1580 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1547 1581
1548 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 1582 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1549 /* please fix it and tell me how to detect the fix */ 1583 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
1550 flags &= ~EVBACKEND_EPOLL; 1584 flags &= ~EVBACKEND_EPOLL;
1551 1585
1552 return flags; 1586 return flags;
1553} 1587}
1554 1588
1555unsigned int 1589unsigned int
1668#endif 1702#endif
1669 1703
1670 if (!(flags & 0x0000ffffU)) 1704 if (!(flags & 0x0000ffffU))
1671 flags |= ev_recommended_backends (); 1705 flags |= ev_recommended_backends ();
1672 1706
1707#if EV_USE_IOCP
1708 if (!backend && (flags & EVBACKEND_IOCP )) backend = iocp_init (EV_A_ flags);
1709#endif
1673#if EV_USE_PORT 1710#if EV_USE_PORT
1674 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); 1711 if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags);
1675#endif 1712#endif
1676#if EV_USE_KQUEUE 1713#if EV_USE_KQUEUE
1677 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); 1714 if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags);
1729#endif 1766#endif
1730 1767
1731 if (backend_fd >= 0) 1768 if (backend_fd >= 0)
1732 close (backend_fd); 1769 close (backend_fd);
1733 1770
1771#if EV_USE_IOCP
1772 if (backend == EVBACKEND_IOCP ) iocp_destroy (EV_A);
1773#endif
1734#if EV_USE_PORT 1774#if EV_USE_PORT
1735 if (backend == EVBACKEND_PORT ) port_destroy (EV_A); 1775 if (backend == EVBACKEND_PORT ) port_destroy (EV_A);
1736#endif 1776#endif
1737#if EV_USE_KQUEUE 1777#if EV_USE_KQUEUE
1738 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); 1778 if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
2292 mn_now = ev_rt_now; 2332 mn_now = ev_rt_now;
2293 } 2333 }
2294} 2334}
2295 2335
2296void 2336void
2297ev_loop (EV_P_ int flags) 2337ev_run (EV_P_ int flags)
2298{ 2338{
2299#if EV_FEATURE_API 2339#if EV_FEATURE_API
2300 ++loop_depth; 2340 ++loop_depth;
2301#endif 2341#endif
2302 2342
2303 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 2343 assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
2304 2344
2305 loop_done = EVUNLOOP_CANCEL; 2345 loop_done = EVBREAK_CANCEL;
2306 2346
2307 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 2347 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2308 2348
2309 do 2349 do
2310 { 2350 {
2353 /* calculate blocking time */ 2393 /* calculate blocking time */
2354 { 2394 {
2355 ev_tstamp waittime = 0.; 2395 ev_tstamp waittime = 0.;
2356 ev_tstamp sleeptime = 0.; 2396 ev_tstamp sleeptime = 0.;
2357 2397
2398 /* remember old timestamp for io_blocktime calculation */
2399 ev_tstamp prev_mn_now = mn_now;
2400
2401 /* update time to cancel out callback processing overhead */
2402 time_update (EV_A_ 1e100);
2403
2358 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2404 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
2359 { 2405 {
2360 /* remember old timestamp for io_blocktime calculation */
2361 ev_tstamp prev_mn_now = mn_now;
2362
2363 /* update time to cancel out callback processing overhead */
2364 time_update (EV_A_ 1e100);
2365
2366 waittime = MAX_BLOCKTIME; 2406 waittime = MAX_BLOCKTIME;
2367 2407
2368 if (timercnt) 2408 if (timercnt)
2369 { 2409 {
2370 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2410 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2400 } 2440 }
2401 2441
2402#if EV_FEATURE_API 2442#if EV_FEATURE_API
2403 ++loop_count; 2443 ++loop_count;
2404#endif 2444#endif
2405 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2445 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2406 backend_poll (EV_A_ waittime); 2446 backend_poll (EV_A_ waittime);
2407 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2447 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2408 2448
2409 /* update ev_rt_now, do magic */ 2449 /* update ev_rt_now, do magic */
2410 time_update (EV_A_ waittime + sleeptime); 2450 time_update (EV_A_ waittime + sleeptime);
2411 } 2451 }
2412 2452
2430 EV_INVOKE_PENDING; 2470 EV_INVOKE_PENDING;
2431 } 2471 }
2432 while (expect_true ( 2472 while (expect_true (
2433 activecnt 2473 activecnt
2434 && !loop_done 2474 && !loop_done
2435 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2475 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2436 )); 2476 ));
2437 2477
2438 if (loop_done == EVUNLOOP_ONE) 2478 if (loop_done == EVBREAK_ONE)
2439 loop_done = EVUNLOOP_CANCEL; 2479 loop_done = EVBREAK_CANCEL;
2440 2480
2441#if EV_FEATURE_API 2481#if EV_FEATURE_API
2442 --loop_depth; 2482 --loop_depth;
2443#endif 2483#endif
2444} 2484}
2445 2485
2446void 2486void
2447ev_unloop (EV_P_ int how) 2487ev_break (EV_P_ int how)
2448{ 2488{
2449 loop_done = how; 2489 loop_done = how;
2450} 2490}
2451 2491
2452void 2492void
3062 { 3102 {
3063 struct inotify_event *ev = (struct inotify_event *)(buf + ofs); 3103 struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
3064 infy_wd (EV_A_ ev->wd, ev->wd, ev); 3104 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3065 ofs += sizeof (struct inotify_event) + ev->len; 3105 ofs += sizeof (struct inotify_event) + ev->len;
3066 } 3106 }
3067}
3068
3069inline_size unsigned int
3070ev_linux_version (void)
3071{
3072 struct utsname buf;
3073 unsigned int v;
3074 int i;
3075 char *p = buf.release;
3076
3077 if (uname (&buf))
3078 return 0;
3079
3080 for (i = 3+1; --i; )
3081 {
3082 unsigned int c = 0;
3083
3084 for (;;)
3085 {
3086 if (*p >= '0' && *p <= '9')
3087 c = c * 10 + *p++ - '0';
3088 else
3089 {
3090 p += *p == '.';
3091 break;
3092 }
3093 }
3094
3095 v = (v << 8) | c;
3096 }
3097
3098 return v;
3099} 3107}
3100 3108
3101inline_size void 3109inline_size void
3102ev_check_2625 (EV_P) 3110ev_check_2625 (EV_P)
3103{ 3111{
3427 3435
3428#if EV_EMBED_ENABLE 3436#if EV_EMBED_ENABLE
3429void noinline 3437void noinline
3430ev_embed_sweep (EV_P_ ev_embed *w) 3438ev_embed_sweep (EV_P_ ev_embed *w)
3431{ 3439{
3432 ev_loop (w->other, EVLOOP_NONBLOCK); 3440 ev_run (w->other, EVRUN_NOWAIT);
3433} 3441}
3434 3442
3435static void 3443static void
3436embed_io_cb (EV_P_ ev_io *io, int revents) 3444embed_io_cb (EV_P_ ev_io *io, int revents)
3437{ 3445{
3438 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 3446 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3439 3447
3440 if (ev_cb (w)) 3448 if (ev_cb (w))
3441 ev_feed_event (EV_A_ (W)w, EV_EMBED); 3449 ev_feed_event (EV_A_ (W)w, EV_EMBED);
3442 else 3450 else
3443 ev_loop (w->other, EVLOOP_NONBLOCK); 3451 ev_run (w->other, EVRUN_NOWAIT);
3444} 3452}
3445 3453
3446static void 3454static void
3447embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3455embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3448{ 3456{
3452 EV_P = w->other; 3460 EV_P = w->other;
3453 3461
3454 while (fdchangecnt) 3462 while (fdchangecnt)
3455 { 3463 {
3456 fd_reify (EV_A); 3464 fd_reify (EV_A);
3457 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3465 ev_run (EV_A_ EVRUN_NOWAIT);
3458 } 3466 }
3459 } 3467 }
3460} 3468}
3461 3469
3462static void 3470static void
3468 3476
3469 { 3477 {
3470 EV_P = w->other; 3478 EV_P = w->other;
3471 3479
3472 ev_loop_fork (EV_A); 3480 ev_loop_fork (EV_A);
3473 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3481 ev_run (EV_A_ EVRUN_NOWAIT);
3474 } 3482 }
3475 3483
3476 ev_embed_start (EV_A_ w); 3484 ev_embed_start (EV_A_ w);
3477} 3485}
3478 3486
3576void 3584void
3577ev_async_start (EV_P_ ev_async *w) 3585ev_async_start (EV_P_ ev_async *w)
3578{ 3586{
3579 if (expect_false (ev_is_active (w))) 3587 if (expect_false (ev_is_active (w)))
3580 return; 3588 return;
3589
3590 w->sent = 0;
3581 3591
3582 evpipe_init (EV_A); 3592 evpipe_init (EV_A);
3583 3593
3584 EV_FREQUENT_CHECK; 3594 EV_FREQUENT_CHECK;
3585 3595
3805 3815
3806#if EV_MULTIPLICITY 3816#if EV_MULTIPLICITY
3807 #include "ev_wrap.h" 3817 #include "ev_wrap.h"
3808#endif 3818#endif
3809 3819
3810#ifdef __cplusplus 3820EV_CPP(})
3811}
3812#endif
3813 3821

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines