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

Comparing libev/ev.c (file contents):
Revision 1.67 by root, Mon Nov 5 16:42:15 2007 UTC vs.
Revision 1.70 by root, Tue Nov 6 00:52:32 2007 UTC

153volatile double SIGFPE_REQ = 0.0f; 153volatile double SIGFPE_REQ = 0.0f;
154#endif 154#endif
155 155
156/*****************************************************************************/ 156/*****************************************************************************/
157 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
158typedef struct 206typedef struct
159{ 207{
160 struct ev_watcher_list *head; 208 WL head;
161 unsigned char events; 209 unsigned char events;
162 unsigned char reify; 210 unsigned char reify;
163} ANFD; 211} ANFD;
164 212
165typedef struct 213typedef struct
223 return rt_now; 271 return rt_now;
224} 272}
225 273
226#define array_roundsize(base,n) ((n) | 4 & ~3) 274#define array_roundsize(base,n) ((n) | 4 & ~3)
227 275
228#define array_needsize(base,cur,cnt,init) \ 276#define array_needsize(base,cur,cnt,init) \
229 if (expect_false ((cnt) > cur)) \ 277 if (expect_false ((cnt) > cur)) \
230 { \ 278 { \
231 int newcnt = cur; \ 279 int newcnt = cur; \
232 do \ 280 do \
233 { \ 281 { \
234 newcnt = array_roundsize (base, newcnt << 1); \ 282 newcnt = array_roundsize (base, newcnt << 1); \
235 } \ 283 } \
236 while ((cnt) > newcnt); \ 284 while ((cnt) > newcnt); \
237 \ 285 \
238 base = realloc (base, sizeof (*base) * (newcnt)); \ 286 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
239 init (base + cur, newcnt - cur); \ 287 init (base + cur, newcnt - cur); \
240 cur = newcnt; \ 288 cur = newcnt; \
241 } 289 }
242 290
243#define array_slim(stem) \ 291#define array_slim(stem) \
244 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 292 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
245 { \ 293 { \
246 stem ## max = array_roundsize (stem ## cnt >> 1); \ 294 stem ## max = array_roundsize (stem ## cnt >> 1); \
247 base = realloc (base, sizeof (*base) * (stem ## max)); \ 295 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
248 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 296 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
249 } 297 }
250 298
251#define array_free(stem, idx) \ 299#define array_free(stem, idx) \
252 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 300 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
253 301
254/*****************************************************************************/ 302/*****************************************************************************/
255 303
256static void 304static void
257anfds_init (ANFD *base, int count) 305anfds_init (ANFD *base, int count)
333} 381}
334 382
335static void 383static void
336fd_change (EV_P_ int fd) 384fd_change (EV_P_ int fd)
337{ 385{
338 if (anfds [fd].reify || fdchangecnt < 0) 386 if (anfds [fd].reify)
339 return; 387 return;
340 388
341 anfds [fd].reify = 1; 389 anfds [fd].reify = 1;
342 390
343 ++fdchangecnt; 391 ++fdchangecnt;
376 int fd; 424 int fd;
377 425
378 for (fd = anfdmax; fd--; ) 426 for (fd = anfdmax; fd--; )
379 if (anfds [fd].events) 427 if (anfds [fd].events)
380 { 428 {
381 close (fd);
382 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
383 return; 430 return;
384 } 431 }
385} 432}
386 433
387/* 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 */
388static void 435static void
389fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
390{ 437{
391 int fd; 438 int fd;
392 439
444 491
445/*****************************************************************************/ 492/*****************************************************************************/
446 493
447typedef struct 494typedef struct
448{ 495{
449 struct ev_watcher_list *head; 496 WL head;
450 sig_atomic_t volatile gotsig; 497 sig_atomic_t volatile gotsig;
451} ANSIG; 498} ANSIG;
452 499
453static ANSIG *signals; 500static ANSIG *signals;
454static int signalmax; 501static int signalmax;
488} 535}
489 536
490static void 537static void
491sigcb (EV_P_ struct ev_io *iow, int revents) 538sigcb (EV_P_ struct ev_io *iow, int revents)
492{ 539{
493 struct ev_watcher_list *w; 540 WL w;
494 int signum; 541 int signum;
495 542
496 read (sigpipe [0], &revents, 1); 543 read (sigpipe [0], &revents, 1);
497 gotsig = 0; 544 gotsig = 0;
498 545
649 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
650#endif 697#endif
651#if EV_USE_SELECT 698#if EV_USE_SELECT
652 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
653#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
654 } 704 }
655} 705}
656 706
657void 707void
658loop_destroy (EV_P) 708loop_destroy (EV_P)
684 array_free (idle, ); 734 array_free (idle, );
685 array_free (prepare, ); 735 array_free (prepare, );
686 array_free (check, ); 736 array_free (check, );
687 737
688 method = 0; 738 method = 0;
689 /*TODO*/
690} 739}
691 740
692void 741static void
693loop_fork (EV_P) 742loop_fork (EV_P)
694{ 743{
695 /*TODO*/
696#if EV_USE_EPOLL 744#if EV_USE_EPOLL
697 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
698#endif 746#endif
699#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
700 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
701#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;
702} 767}
703 768
704#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
705struct ev_loop * 770struct ev_loop *
706ev_loop_new (int methods) 771ev_loop_new (int methods)
707{ 772{
708 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));
709 776
710 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
711 778
712 if (ev_method (EV_A)) 779 if (ev_method (EV_A))
713 return loop; 780 return loop;
717 784
718void 785void
719ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
720{ 787{
721 loop_destroy (EV_A); 788 loop_destroy (EV_A);
722 free (loop); 789 ev_free (loop);
723} 790}
724 791
725void 792void
726ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
727{ 794{
728 loop_fork (EV_A); 795 postfork = 1;
729} 796}
730 797
731#endif 798#endif
732 799
733#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
756 823
757 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
758 825
759 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
760 { 827 {
761 ev_watcher_init (&sigev, sigcb);
762 ev_set_priority (&sigev, EV_MAXPRI);
763 siginit (EV_A); 828 siginit (EV_A);
764 829
765#ifndef WIN32 830#ifndef WIN32
766 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
767 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
800{ 865{
801#if EV_MULTIPLICITY 866#if EV_MULTIPLICITY
802 struct ev_loop *loop = default_loop; 867 struct ev_loop *loop = default_loop;
803#endif 868#endif
804 869
805 loop_fork (EV_A); 870 if (method)
806 871 postfork = 1;
807 ev_io_stop (EV_A_ &sigev);
808 close (sigpipe [0]);
809 close (sigpipe [1]);
810 pipe (sigpipe);
811
812 ev_ref (EV_A); /* signal watcher */
813 siginit (EV_A);
814} 872}
815 873
816/*****************************************************************************/ 874/*****************************************************************************/
817 875
818static void 876static void
996 { 1054 {
997 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
998 call_pending (EV_A); 1056 call_pending (EV_A);
999 } 1057 }
1000 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
1001 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
1002 fd_reify (EV_A); 1064 fd_reify (EV_A);
1003 1065
1004 /* calculate blocking time */ 1066 /* calculate blocking time */
1005 1067
1396 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1397 void *arg = once->arg; 1459 void *arg = once->arg;
1398 1460
1399 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1400 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1401 free (once); 1463 ev_free (once);
1402 1464
1403 cb (revents, arg); 1465 cb (revents, arg);
1404} 1466}
1405 1467
1406static void 1468static void
1416} 1478}
1417 1479
1418void 1480void
1419ev_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)
1420{ 1482{
1421 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1422 1484
1423 if (!once) 1485 if (!once)
1424 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1425 else 1487 else
1426 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines