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

Comparing libev/ev.c (file contents):
Revision 1.64 by root, Sun Nov 4 23:14:11 2007 UTC vs.
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC

145typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
147 147
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 149
150#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155
150/*****************************************************************************/ 156/*****************************************************************************/
151 157
158static void (*syserr_cb)(const char *msg);
159
160void ev_set_syserr_cb (void (*cb)(const char *msg))
161{
162 syserr_cb = cb;
163}
164
165static void
166syserr (const char *msg)
167{
168 if (!msg)
169 msg = "(libev) system error";
170
171 if (syserr_cb)
172 syserr_cb (msg);
173 else
174 {
175 perror (msg);
176 abort ();
177 }
178}
179
180static void *(*alloc)(void *ptr, long size);
181
182void ev_set_allocator (void *(*cb)(void *ptr, long size))
183{
184 alloc = cb;
185}
186
187static void *
188ev_realloc (void *ptr, long size)
189{
190 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
191
192 if (!ptr && size)
193 {
194 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
195 abort ();
196 }
197
198 return ptr;
199}
200
201#define ev_malloc(size) ev_realloc (0, (size))
202#define ev_free(ptr) ev_realloc ((ptr), 0)
203
204/*****************************************************************************/
205
152typedef struct 206typedef struct
153{ 207{
154 struct ev_watcher_list *head; 208 WL head;
155 unsigned char events; 209 unsigned char events;
156 unsigned char reify; 210 unsigned char reify;
157} ANFD; 211} ANFD;
158 212
159typedef struct 213typedef struct
217 return rt_now; 271 return rt_now;
218} 272}
219 273
220#define array_roundsize(base,n) ((n) | 4 & ~3) 274#define array_roundsize(base,n) ((n) | 4 & ~3)
221 275
222#define array_needsize(base,cur,cnt,init) \ 276#define array_needsize(base,cur,cnt,init) \
223 if (expect_false ((cnt) > cur)) \ 277 if (expect_false ((cnt) > cur)) \
224 { \ 278 { \
225 int newcnt = cur; \ 279 int newcnt = cur; \
226 do \ 280 do \
227 { \ 281 { \
228 newcnt = array_roundsize (base, newcnt << 1); \ 282 newcnt = array_roundsize (base, newcnt << 1); \
229 } \ 283 } \
230 while ((cnt) > newcnt); \ 284 while ((cnt) > newcnt); \
231 \ 285 \
232 base = realloc (base, sizeof (*base) * (newcnt)); \ 286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
233 init (base + cur, newcnt - cur); \ 287 init (base + cur, newcnt - cur); \
234 cur = newcnt; \ 288 cur = newcnt; \
235 } 289 }
290
291#define array_slim(stem) \
292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
293 { \
294 stem ## max = array_roundsize (stem ## cnt >> 1); \
295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
297 }
298
299#define array_free(stem, idx) \
300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
236 301
237/*****************************************************************************/ 302/*****************************************************************************/
238 303
239static void 304static void
240anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
316} 381}
317 382
318static void 383static void
319fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
320{ 385{
321 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
322 return; 387 return;
323 388
324 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
325 390
326 ++fdchangecnt; 391 ++fdchangecnt;
359 int fd; 424 int fd;
360 425
361 for (fd = anfdmax; fd--; ) 426 for (fd = anfdmax; fd--; )
362 if (anfds [fd].events) 427 if (anfds [fd].events)
363 { 428 {
364 close (fd);
365 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
366 return; 430 return;
367 } 431 }
368} 432}
369 433
370/* susually called after fork if method needs to re-arm all fds from scratch */ 434/* usually called after fork if method needs to re-arm all fds from scratch */
371static void 435static void
372fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
373{ 437{
374 int fd; 438 int fd;
375 439
427 491
428/*****************************************************************************/ 492/*****************************************************************************/
429 493
430typedef struct 494typedef struct
431{ 495{
432 struct ev_watcher_list *head; 496 WL head;
433 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
434} ANSIG; 498} ANSIG;
435 499
436static ANSIG *signals; 500static ANSIG *signals;
437static int signalmax; 501static int signalmax;
453} 517}
454 518
455static void 519static void
456sighandler (int signum) 520sighandler (int signum)
457{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
458 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
459 527
460 if (!gotsig) 528 if (!gotsig)
461 { 529 {
462 int old_errno = errno; 530 int old_errno = errno;
467} 535}
468 536
469static void 537static void
470sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
471{ 539{
472 struct ev_watcher_list *w; 540 WL w;
473 int signum; 541 int signum;
474 542
475 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
476 gotsig = 0; 544 gotsig = 0;
477 545
628 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
629#endif 697#endif
630#if EV_USE_SELECT 698#if EV_USE_SELECT
631 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
632#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
633 } 704 }
634} 705}
635 706
636void 707void
637loop_destroy (EV_P) 708loop_destroy (EV_P)
638{ 709{
710 int i;
711
639#if EV_USE_WIN32 712#if EV_USE_WIN32
640 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 713 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
641#endif 714#endif
642#if EV_USE_KQUEUE 715#if EV_USE_KQUEUE
643 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 716 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
650#endif 723#endif
651#if EV_USE_SELECT 724#if EV_USE_SELECT
652 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 725 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
653#endif 726#endif
654 727
728 for (i = NUMPRI; i--; )
729 array_free (pending, [i]);
730
731 array_free (fdchange, );
732 array_free (timer, );
733 array_free (periodic, );
734 array_free (idle, );
735 array_free (prepare, );
736 array_free (check, );
737
655 method = 0; 738 method = 0;
656 /*TODO*/
657} 739}
658 740
659void 741static void
660loop_fork (EV_P) 742loop_fork (EV_P)
661{ 743{
662 /*TODO*/
663#if EV_USE_EPOLL 744#if EV_USE_EPOLL
664 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
665#endif 746#endif
666#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
667 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
668#endif 749#endif
750
751 if (ev_is_active (&sigev))
752 {
753 /* default loop */
754
755 ev_ref (EV_A);
756 ev_io_stop (EV_A_ &sigev);
757 close (sigpipe [0]);
758 close (sigpipe [1]);
759
760 while (pipe (sigpipe))
761 syserr ("(libev) error creating pipe");
762
763 siginit (EV_A);
764 }
765
766 postfork = 0;
669} 767}
670 768
671#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
672struct ev_loop * 770struct ev_loop *
673ev_loop_new (int methods) 771ev_loop_new (int methods)
674{ 772{
675 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 773 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
774
775 memset (loop, 0, sizeof (struct ev_loop));
676 776
677 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
678 778
679 if (ev_method (EV_A)) 779 if (ev_method (EV_A))
680 return loop; 780 return loop;
684 784
685void 785void
686ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
687{ 787{
688 loop_destroy (EV_A); 788 loop_destroy (EV_A);
689 free (loop); 789 ev_free (loop);
690} 790}
691 791
692void 792void
693ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
694{ 794{
695 loop_fork (EV_A); 795 postfork = 1;
696} 796}
697 797
698#endif 798#endif
699 799
700#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
723 823
724 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
725 825
726 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
727 { 827 {
728 ev_watcher_init (&sigev, sigcb);
729 ev_set_priority (&sigev, EV_MAXPRI);
730 siginit (EV_A); 828 siginit (EV_A);
731 829
732#ifndef WIN32 830#ifndef WIN32
733 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
734 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
767{ 865{
768#if EV_MULTIPLICITY 866#if EV_MULTIPLICITY
769 struct ev_loop *loop = default_loop; 867 struct ev_loop *loop = default_loop;
770#endif 868#endif
771 869
772 loop_fork (EV_A); 870 if (method)
773 871 postfork = 1;
774 ev_io_stop (EV_A_ &sigev);
775 close (sigpipe [0]);
776 close (sigpipe [1]);
777 pipe (sigpipe);
778
779 ev_ref (EV_A); /* signal watcher */
780 siginit (EV_A);
781} 872}
782 873
783/*****************************************************************************/ 874/*****************************************************************************/
784 875
785static void 876static void
793 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 884 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
794 885
795 if (p->w) 886 if (p->w)
796 { 887 {
797 p->w->pending = 0; 888 p->w->pending = 0;
798
799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); 889 p->w->cb (EV_A_ p->w, p->events);
800 } 890 }
801 } 891 }
802} 892}
803 893
804static void 894static void
964 { 1054 {
965 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
966 call_pending (EV_A); 1056 call_pending (EV_A);
967 } 1057 }
968 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
969 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
970 fd_reify (EV_A); 1064 fd_reify (EV_A);
971 1065
972 /* calculate blocking time */ 1066 /* calculate blocking time */
973 1067
1296 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1298 1392
1299 if (!((WL)w)->next) 1393 if (!((WL)w)->next)
1300 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1301 struct sigaction sa; 1398 struct sigaction sa;
1302 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1303 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1304 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1401 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1305 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1306 } 1404 }
1307} 1405}
1308 1406
1309void 1407void
1310ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1360 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1361 void *arg = once->arg; 1459 void *arg = once->arg;
1362 1460
1363 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1364 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1365 free (once); 1463 ev_free (once);
1366 1464
1367 cb (revents, arg); 1465 cb (revents, arg);
1368} 1466}
1369 1467
1370static void 1468static void
1380} 1478}
1381 1479
1382void 1480void
1383ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1481ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1384{ 1482{
1385 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1386 1484
1387 if (!once) 1485 if (!once)
1388 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1389 else 1487 else
1390 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines