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

Comparing libev/ev.c (file contents):
Revision 1.68 by root, Mon Nov 5 20:19:00 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 WL head; 208 WL head;
161 unsigned char events; 209 unsigned char events;
162 unsigned char reify; 210 unsigned char reify;
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;
381 fd_kill (EV_A_ fd); 429 fd_kill (EV_A_ fd);
382 return; 430 return;
383 } 431 }
384} 432}
385 433
386/* 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 */
387static void 435static void
388fd_rearm_all (EV_P) 436fd_rearm_all (EV_P)
389{ 437{
390 int fd; 438 int fd;
391 439
648 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 696 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
649#endif 697#endif
650#if EV_USE_SELECT 698#if EV_USE_SELECT
651 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 699 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
652#endif 700#endif
701
702 ev_watcher_init (&sigev, sigcb);
703 ev_set_priority (&sigev, EV_MAXPRI);
653 } 704 }
654} 705}
655 706
656void 707void
657loop_destroy (EV_P) 708loop_destroy (EV_P)
683 array_free (idle, ); 734 array_free (idle, );
684 array_free (prepare, ); 735 array_free (prepare, );
685 array_free (check, ); 736 array_free (check, );
686 737
687 method = 0; 738 method = 0;
688 /*TODO*/
689} 739}
690 740
691void 741static void
692loop_fork (EV_P) 742loop_fork (EV_P)
693{ 743{
694 /*TODO*/
695#if EV_USE_EPOLL 744#if EV_USE_EPOLL
696 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); 745 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
697#endif 746#endif
698#if EV_USE_KQUEUE 747#if EV_USE_KQUEUE
699 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); 748 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
700#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;
701} 767}
702 768
703#if EV_MULTIPLICITY 769#if EV_MULTIPLICITY
704struct ev_loop * 770struct ev_loop *
705ev_loop_new (int methods) 771ev_loop_new (int methods)
706{ 772{
707 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));
708 776
709 loop_init (EV_A_ methods); 777 loop_init (EV_A_ methods);
710 778
711 if (ev_method (EV_A)) 779 if (ev_method (EV_A))
712 return loop; 780 return loop;
716 784
717void 785void
718ev_loop_destroy (EV_P) 786ev_loop_destroy (EV_P)
719{ 787{
720 loop_destroy (EV_A); 788 loop_destroy (EV_A);
721 free (loop); 789 ev_free (loop);
722} 790}
723 791
724void 792void
725ev_loop_fork (EV_P) 793ev_loop_fork (EV_P)
726{ 794{
727 loop_fork (EV_A); 795 postfork = 1;
728} 796}
729 797
730#endif 798#endif
731 799
732#if EV_MULTIPLICITY 800#if EV_MULTIPLICITY
755 823
756 loop_init (EV_A_ methods); 824 loop_init (EV_A_ methods);
757 825
758 if (ev_method (EV_A)) 826 if (ev_method (EV_A))
759 { 827 {
760 ev_watcher_init (&sigev, sigcb);
761 ev_set_priority (&sigev, EV_MAXPRI);
762 siginit (EV_A); 828 siginit (EV_A);
763 829
764#ifndef WIN32 830#ifndef WIN32
765 ev_signal_init (&childev, childcb, SIGCHLD); 831 ev_signal_init (&childev, childcb, SIGCHLD);
766 ev_set_priority (&childev, EV_MAXPRI); 832 ev_set_priority (&childev, EV_MAXPRI);
799{ 865{
800#if EV_MULTIPLICITY 866#if EV_MULTIPLICITY
801 struct ev_loop *loop = default_loop; 867 struct ev_loop *loop = default_loop;
802#endif 868#endif
803 869
804 loop_fork (EV_A); 870 if (method)
805 871 postfork = 1;
806 ev_io_stop (EV_A_ &sigev);
807 close (sigpipe [0]);
808 close (sigpipe [1]);
809 pipe (sigpipe);
810
811 ev_ref (EV_A); /* signal watcher */
812 siginit (EV_A);
813} 872}
814 873
815/*****************************************************************************/ 874/*****************************************************************************/
816 875
817static void 876static void
995 { 1054 {
996 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1055 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
997 call_pending (EV_A); 1056 call_pending (EV_A);
998 } 1057 }
999 1058
1059 /* we might have forked, so reify kernel state if necessary */
1060 if (expect_false (postfork))
1061 loop_fork (EV_A);
1062
1000 /* update fd-related kernel structures */ 1063 /* update fd-related kernel structures */
1001 fd_reify (EV_A); 1064 fd_reify (EV_A);
1002 1065
1003 /* calculate blocking time */ 1066 /* calculate blocking time */
1004 1067
1395 void (*cb)(int revents, void *arg) = once->cb; 1458 void (*cb)(int revents, void *arg) = once->cb;
1396 void *arg = once->arg; 1459 void *arg = once->arg;
1397 1460
1398 ev_io_stop (EV_A_ &once->io); 1461 ev_io_stop (EV_A_ &once->io);
1399 ev_timer_stop (EV_A_ &once->to); 1462 ev_timer_stop (EV_A_ &once->to);
1400 free (once); 1463 ev_free (once);
1401 1464
1402 cb (revents, arg); 1465 cb (revents, arg);
1403} 1466}
1404 1467
1405static void 1468static void
1415} 1478}
1416 1479
1417void 1480void
1418ev_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)
1419{ 1482{
1420 struct ev_once *once = malloc (sizeof (struct ev_once)); 1483 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1421 1484
1422 if (!once) 1485 if (!once)
1423 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1486 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1424 else 1487 else
1425 { 1488 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines