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

Comparing libev/ev.c (file contents):
Revision 1.55 by root, Sun Nov 4 00:39:24 2007 UTC vs.
Revision 1.58 by root, Sun Nov 4 16:52:52 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_STANDALONE 31#ifndef EV_EMBED
32# include "config.h" 32# include "config.h"
33#endif 33#endif
34 34
35#include <math.h> 35#include <math.h>
36#include <stdlib.h> 36#include <stdlib.h>
93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
97 97
98#ifndef EV_EMBED
98#include "ev.h" 99# include "ev.h"
100#endif
99 101
100#if __GNUC__ >= 3 102#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value)) 103# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline 104# define inline inline
103#else 105#else
338 fd_kill (EV_A_ fd); 340 fd_kill (EV_A_ fd);
339 return; 341 return;
340 } 342 }
341} 343}
342 344
345/* susually called after fork if method needs to re-arm all fds from scratch */
346static void
347fd_rearm_all (EV_P)
348{
349 int fd;
350
351 /* this should be highly optimised to not do anything but set a flag */
352 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events)
354 {
355 anfds [fd].events = 0;
356 fd_change (fd);
357 }
358}
359
343/*****************************************************************************/ 360/*****************************************************************************/
344 361
345static void 362static void
346upheap (WT *heap, int k) 363upheap (WT *heap, int k)
347{ 364{
542ev_method (EV_P) 559ev_method (EV_P)
543{ 560{
544 return method; 561 return method;
545} 562}
546 563
547inline int 564static void
548loop_init (EV_P_ int methods) 565loop_init (EV_P_ int methods)
549{ 566{
550 if (!method) 567 if (!method)
551 { 568 {
552#if EV_USE_MONOTONIC 569#if EV_USE_MONOTONIC
560 rt_now = ev_time (); 577 rt_now = ev_time ();
561 mn_now = get_clock (); 578 mn_now = get_clock ();
562 now_floor = mn_now; 579 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 580 rtmn_diff = rt_now - mn_now;
564 581
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 582 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 583 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 584 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 585 else
572 methods = EVMETHOD_ANY; 586 methods = EVMETHOD_ANY;
573 587
574 method = 0; 588 method = 0;
575#if EV_USE_KQUEUE 589#if EV_USE_KQUEUE
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 596 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 597#endif
584#if EV_USE_SELECT 598#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 599 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 600#endif
601 }
602}
587 603
604void
605loop_destroy (EV_P)
606{
607#if EV_USE_KQUEUE
608 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
609#endif
610#if EV_USE_EPOLL
611 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
612#endif
613#if EV_USEV_POLL
614 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
615#endif
616#if EV_USE_SELECT
617 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
618#endif
619
620 method = 0;
621 /*TODO*/
622}
623
624void
625loop_fork (EV_P)
626{
627 /*TODO*/
628#if EV_USE_EPOLL
629 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
630#endif
631#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
633#endif
634}
635
636#if EV_MULTIPLICITY
637struct ev_loop *
638ev_loop_new (int methods)
639{
640 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
641
642 loop_init (EV_A_ methods);
643
644 if (ev_methods (EV_A))
645 return loop;
646
647 return 0;
648}
649
650void
651ev_loop_destroy (EV_P)
652{
653 loop_destroy (EV_A);
654 free (loop);
655}
656
657void
658ev_loop_fork (EV_P)
659{
660 loop_fork (EV_A);
661}
662
663#endif
664
665#if EV_MULTIPLICITY
666struct ev_loop default_loop_struct;
667static struct ev_loop *default_loop;
668
669struct ev_loop *
670#else
671static int default_loop;
672
673int
674#endif
675ev_default_loop (int methods)
676{
677 if (sigpipe [0] == sigpipe [1])
678 if (pipe (sigpipe))
679 return 0;
680
681 if (!default_loop)
682 {
683#if EV_MULTIPLICITY
684 struct ev_loop *loop = default_loop = &default_loop_struct;
685#else
686 default_loop = 1;
687#endif
688
689 loop_init (EV_A_ methods);
690
588 if (method) 691 if (ev_method (EV_A))
589 { 692 {
590 ev_watcher_init (&sigev, sigcb); 693 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 694 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 695 siginit (EV_A);
593 696
596 ev_set_priority (&childev, EV_MAXPRI); 699 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 700 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 701 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 702#endif
600 } 703 }
704 else
705 default_loop = 0;
601 } 706 }
602 707
603 return method; 708 return default_loop;
604} 709}
605 710
711void
712ev_default_destroy (void)
713{
606#if EV_MULTIPLICITY 714#if EV_MULTIPLICITY
607 715 struct ev_loop *loop = default_loop;
608struct ev_loop *
609ev_loop_new (int methods)
610{
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
612
613 if (loop_init (EV_A_ methods))
614 return loop;
615
616 ev_loop_delete (loop);
617
618 return 0;
619}
620
621void
622ev_loop_delete (EV_P)
623{
624 /*TODO*/
625 free (loop);
626}
627
628#else
629
630int
631ev_init (int methods)
632{
633 return loop_init (methods);
634}
635
636#endif 716#endif
637 717
638/*****************************************************************************/ 718 ev_ref (EV_A); /* child watcher */
719 ev_signal_stop (EV_A_ &childev);
639 720
640void 721 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void) 722 ev_io_stop (EV_A_ &sigev);
642{
643 /* nop */
644}
645 723
646void 724 close (sigpipe [0]); sigpipe [0] = 0;
647ev_fork_parent (void) 725 close (sigpipe [1]); sigpipe [1] = 0;
648{
649 /* nop */
650}
651 726
727 loop_destroy (EV_A);
728}
729
652void 730void
653ev_fork_child (void) 731ev_default_fork (EV_P)
654{ 732{
655 /*TODO*/ 733 loop_fork (EV_A);
656#if !EV_MULTIPLICITY
657#if EV_USE_EPOLL
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif
661 734
662 ev_io_stop (EV_A_ &sigev); 735 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 736 close (sigpipe [0]);
664 close (sigpipe [1]); 737 close (sigpipe [1]);
665 pipe (sigpipe); 738 pipe (sigpipe);
739
740 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 741 siginit (EV_A);
667#endif
668} 742}
669 743
670/*****************************************************************************/ 744/*****************************************************************************/
671 745
672static void 746static void
1083 } 1157 }
1084 1158
1085 ev_stop (EV_A_ (W)w); 1159 ev_stop (EV_A_ (W)w);
1086} 1160}
1087 1161
1162void
1163ev_idle_start (EV_P_ struct ev_idle *w)
1164{
1165 if (ev_is_active (w))
1166 return;
1167
1168 ev_start (EV_A_ (W)w, ++idlecnt);
1169 array_needsize (idles, idlemax, idlecnt, );
1170 idles [idlecnt - 1] = w;
1171}
1172
1173void
1174ev_idle_stop (EV_P_ struct ev_idle *w)
1175{
1176 ev_clear_pending (EV_A_ (W)w);
1177 if (ev_is_active (w))
1178 return;
1179
1180 idles [w->active - 1] = idles [--idlecnt];
1181 ev_stop (EV_A_ (W)w);
1182}
1183
1184void
1185ev_prepare_start (EV_P_ struct ev_prepare *w)
1186{
1187 if (ev_is_active (w))
1188 return;
1189
1190 ev_start (EV_A_ (W)w, ++preparecnt);
1191 array_needsize (prepares, preparemax, preparecnt, );
1192 prepares [preparecnt - 1] = w;
1193}
1194
1195void
1196ev_prepare_stop (EV_P_ struct ev_prepare *w)
1197{
1198 ev_clear_pending (EV_A_ (W)w);
1199 if (ev_is_active (w))
1200 return;
1201
1202 prepares [w->active - 1] = prepares [--preparecnt];
1203 ev_stop (EV_A_ (W)w);
1204}
1205
1206void
1207ev_check_start (EV_P_ struct ev_check *w)
1208{
1209 if (ev_is_active (w))
1210 return;
1211
1212 ev_start (EV_A_ (W)w, ++checkcnt);
1213 array_needsize (checks, checkmax, checkcnt, );
1214 checks [checkcnt - 1] = w;
1215}
1216
1217void
1218ev_check_stop (EV_P_ struct ev_check *w)
1219{
1220 ev_clear_pending (EV_A_ (W)w);
1221 if (ev_is_active (w))
1222 return;
1223
1224 checks [w->active - 1] = checks [--checkcnt];
1225 ev_stop (EV_A_ (W)w);
1226}
1227
1088#ifndef SA_RESTART 1228#ifndef SA_RESTART
1089# define SA_RESTART 0 1229# define SA_RESTART 0
1090#endif 1230#endif
1091 1231
1092void 1232void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1233ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1234{
1235#if EV_MULTIPLICITY
1236 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1237#endif
1095 if (ev_is_active (w)) 1238 if (ev_is_active (w))
1096 return; 1239 return;
1097 1240
1098 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1241 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1099 1242
1124 if (!signals [w->signum - 1].head) 1267 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1268 signal (w->signum, SIG_DFL);
1126} 1269}
1127 1270
1128void 1271void
1129ev_idle_start (EV_P_ struct ev_idle *w)
1130{
1131 if (ev_is_active (w))
1132 return;
1133
1134 ev_start (EV_A_ (W)w, ++idlecnt);
1135 array_needsize (idles, idlemax, idlecnt, );
1136 idles [idlecnt - 1] = w;
1137}
1138
1139void
1140ev_idle_stop (EV_P_ struct ev_idle *w)
1141{
1142 ev_clear_pending (EV_A_ (W)w);
1143 if (ev_is_active (w))
1144 return;
1145
1146 idles [w->active - 1] = idles [--idlecnt];
1147 ev_stop (EV_A_ (W)w);
1148}
1149
1150void
1151ev_prepare_start (EV_P_ struct ev_prepare *w)
1152{
1153 if (ev_is_active (w))
1154 return;
1155
1156 ev_start (EV_A_ (W)w, ++preparecnt);
1157 array_needsize (prepares, preparemax, preparecnt, );
1158 prepares [preparecnt - 1] = w;
1159}
1160
1161void
1162ev_prepare_stop (EV_P_ struct ev_prepare *w)
1163{
1164 ev_clear_pending (EV_A_ (W)w);
1165 if (ev_is_active (w))
1166 return;
1167
1168 prepares [w->active - 1] = prepares [--preparecnt];
1169 ev_stop (EV_A_ (W)w);
1170}
1171
1172void
1173ev_check_start (EV_P_ struct ev_check *w)
1174{
1175 if (ev_is_active (w))
1176 return;
1177
1178 ev_start (EV_A_ (W)w, ++checkcnt);
1179 array_needsize (checks, checkmax, checkcnt, );
1180 checks [checkcnt - 1] = w;
1181}
1182
1183void
1184ev_check_stop (EV_P_ struct ev_check *w)
1185{
1186 ev_clear_pending (EV_A_ (W)w);
1187 if (ev_is_active (w))
1188 return;
1189
1190 checks [w->active - 1] = checks [--checkcnt];
1191 ev_stop (EV_A_ (W)w);
1192}
1193
1194void
1195ev_child_start (EV_P_ struct ev_child *w) 1272ev_child_start (EV_P_ struct ev_child *w)
1196{ 1273{
1274#if EV_MULTIPLICITY
1275 assert (("child watchers are only supported in the default loop", loop == default_loop));
1276#endif
1197 if (ev_is_active (w)) 1277 if (ev_is_active (w))
1198 return; 1278 return;
1199 1279
1200 ev_start (EV_A_ (W)w, 1); 1280 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1281 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1353 ev_timer_start (EV_A_ &once->to);
1274 } 1354 }
1275 } 1355 }
1276} 1356}
1277 1357
1278/*****************************************************************************/
1279
1280#if 0
1281
1282struct ev_io wio;
1283
1284static void
1285sin_cb (struct ev_io *w, int revents)
1286{
1287 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1288}
1289
1290static void
1291ocb (struct ev_timer *w, int revents)
1292{
1293 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1294 ev_timer_stop (w);
1295 ev_timer_start (w);
1296}
1297
1298static void
1299scb (struct ev_signal *w, int revents)
1300{
1301 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1302 ev_io_stop (&wio);
1303 ev_io_start (&wio);
1304}
1305
1306static void
1307gcb (struct ev_signal *w, int revents)
1308{
1309 fprintf (stderr, "generic %x\n", revents);
1310
1311}
1312
1313int main (void)
1314{
1315 ev_init (0);
1316
1317 ev_io_init (&wio, sin_cb, 0, EV_READ);
1318 ev_io_start (&wio);
1319
1320 struct ev_timer t[10000];
1321
1322#if 0
1323 int i;
1324 for (i = 0; i < 10000; ++i)
1325 {
1326 struct ev_timer *w = t + i;
1327 ev_watcher_init (w, ocb, i);
1328 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1329 ev_timer_start (w);
1330 if (drand48 () < 0.5)
1331 ev_timer_stop (w);
1332 }
1333#endif
1334
1335 struct ev_timer t1;
1336 ev_timer_init (&t1, ocb, 5, 10);
1337 ev_timer_start (&t1);
1338
1339 struct ev_signal sig;
1340 ev_signal_init (&sig, scb, SIGQUIT);
1341 ev_signal_start (&sig);
1342
1343 struct ev_check cw;
1344 ev_check_init (&cw, gcb);
1345 ev_check_start (&cw);
1346
1347 struct ev_idle iw;
1348 ev_idle_init (&iw, gcb);
1349 ev_idle_start (&iw);
1350
1351 ev_loop (0);
1352
1353 return 0;
1354}
1355
1356#endif
1357
1358
1359
1360

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines