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

Comparing libev/ev.c (file contents):
Revision 1.66 by root, Sun Nov 4 23:30:53 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; \
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*/\
235 } 297 }
236 298
237#define array_free(stem, idx) \ 299#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239 301
240/*****************************************************************************/ 302/*****************************************************************************/
241 303
242static void 304static void
243anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
319} 381}
320 382
321static void 383static void
322fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
323{ 385{
324 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
325 return; 387 return;
326 388
327 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
328 390
329 ++fdchangecnt; 391 ++fdchangecnt;
362 int fd; 424 int fd;
363 425
364 for (fd = anfdmax; fd--; ) 426 for (fd = anfdmax; fd--; )
365 if (anfds [fd].events) 427 if (anfds [fd].events)
366 { 428 {
367 close (fd);
368 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
369 return; 430 return;
370 } 431 }
371} 432}
372 433
373/* 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 */
374static void 435static void
375fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
376{ 437{
377 int fd; 438 int fd;
378 439
430 491
431/*****************************************************************************/ 492/*****************************************************************************/
432 493
433typedef struct 494typedef struct
434{ 495{
435 struct ev_watcher_list *head; 496 WL head;
436 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
437} ANSIG; 498} ANSIG;
438 499
439static ANSIG *signals; 500static ANSIG *signals;
440static int signalmax; 501static int signalmax;
456} 517}
457 518
458static void 519static void
459sighandler (int signum) 520sighandler (int signum)
460{ 521{
522#if WIN32
523 signal (signum, sighandler);
524#endif
525
461 signals [signum - 1].gotsig = 1; 526 signals [signum - 1].gotsig = 1;
462 527
463 if (!gotsig) 528 if (!gotsig)
464 { 529 {
465 int old_errno = errno; 530 int old_errno = errno;
470} 535}
471 536
472static void 537static void
473sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
474{ 539{
475 struct ev_watcher_list *w; 540 WL w;
476 int signum; 541 int signum;
477 542
478 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
479 gotsig = 0; 544 gotsig = 0;
480 545
631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
632#endif 697#endif
633#if EV_USE_SELECT 698#if EV_USE_SELECT
634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
635#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
636 } 704 }
637} 705}
638 706
639void 707void
640loop_destroy (EV_P) 708loop_destroy (EV_P)
666 array_free (idle, ); 734 array_free (idle, );
667 array_free (prepare, ); 735 array_free (prepare, );
668 array_free (check, ); 736 array_free (check, );
669 737
670 method = 0; 738 method = 0;
671 /*TODO*/
672} 739}
673 740
674void 741static void
675loop_fork (EV_P) 742loop_fork (EV_P)
676{ 743{
677 /*TODO*/
678#if EV_USE_EPOLL 744#if EV_USE_EPOLL
679 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
680#endif 746#endif
681#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
682 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
683#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;
684} 767}
685 768
686#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
687struct ev_loop * 770struct ev_loop *
688ev_loop_new (int methods) 771ev_loop_new (int methods)
689{ 772{
690 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));
691 776
692 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
693 778
694 if (ev_method (EV_A)) 779 if (ev_method (EV_A))
695 return loop; 780 return loop;
699 784
700void 785void
701ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
702{ 787{
703 loop_destroy (EV_A); 788 loop_destroy (EV_A);
704 free (loop); 789 ev_free (loop);
705} 790}
706 791
707void 792void
708ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
709{ 794{
710 loop_fork (EV_A); 795 postfork = 1;
711} 796}
712 797
713#endif 798#endif
714 799
715#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
738 823
739 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
740 825
741 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
742 { 827 {
743 ev_watcher_init (&sigev, sigcb);
744 ev_set_priority (&sigev, EV_MAXPRI);
745 siginit (EV_A); 828 siginit (EV_A);
746 829
747#ifndef WIN32 830#ifndef WIN32
748 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
749 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
782{ 865{
783#if EV_MULTIPLICITY 866#if EV_MULTIPLICITY
784 struct ev_loop *loop = default_loop; 867 struct ev_loop *loop = default_loop;
785#endif 868#endif
786 869
787 loop_fork (EV_A); 870 if (method)
788 871 postfork = 1;
789 ev_io_stop (EV_A_ &sigev);
790 close (sigpipe [0]);
791 close (sigpipe [1]);
792 pipe (sigpipe);
793
794 ev_ref (EV_A); /* signal watcher */
795 siginit (EV_A);
796} 872}
797 873
798/*****************************************************************************/ 874/*****************************************************************************/
799 875
800static void 876static void
978 { 1054 {
979 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
980 call_pending (EV_A); 1056 call_pending (EV_A);
981 } 1057 }
982 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
983 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
984 fd_reify (EV_A); 1064 fd_reify (EV_A);
985 1065
986 /* calculate blocking time */ 1066 /* calculate blocking time */
987 1067
1310 array_needsize (signals, signalmax, w->signum, signals_init); 1390 array_needsize (signals, signalmax, w->signum, signals_init);
1311 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1391 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1312 1392
1313 if (!((WL)w)->next) 1393 if (!((WL)w)->next)
1314 { 1394 {
1395#if WIN32
1396 signal (w->signum, sighandler);
1397#else
1315 struct sigaction sa; 1398 struct sigaction sa;
1316 sa.sa_handler = sighandler; 1399 sa.sa_handler = sighandler;
1317 sigfillset (&sa.sa_mask); 1400 sigfillset (&sa.sa_mask);
1318 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 */
1319 sigaction (w->signum, &sa, 0); 1402 sigaction (w->signum, &sa, 0);
1403#endif
1320 } 1404 }
1321} 1405}
1322 1406
1323void 1407void
1324ev_signal_stop (EV_P_ struct ev_signal *w) 1408ev_signal_stop (EV_P_ struct ev_signal *w)
1374 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1375 void *arg = once->arg; 1459 void *arg = once->arg;
1376 1460
1377 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1378 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1379 free (once); 1463 ev_free (once);
1380 1464
1381 cb (revents, arg); 1465 cb (revents, arg);
1382} 1466}
1383 1467
1384static void 1468static void
1394} 1478}
1395 1479
1396void 1480void
1397ev_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)
1398{ 1482{
1399 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1400 1484
1401 if (!once) 1485 if (!once)
1402 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1403 else 1487 else
1404 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines