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

Comparing libev/ev.c (file contents):
Revision 1.161 by root, Sat Dec 1 23:43:45 2007 UTC vs.
Revision 1.165 by root, Fri Dec 7 18:09:38 2007 UTC

241 241
242#define expect_false(expr) expect ((expr) != 0, 0) 242#define expect_false(expr) expect ((expr) != 0, 0)
243#define expect_true(expr) expect ((expr) != 0, 1) 243#define expect_true(expr) expect ((expr) != 0, 1)
244 244
245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 245#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
246#define ABSPRI(w) ((w)->priority - EV_MINPRI) 246#define ABSPRI(w) (((W)w)->priority - EV_MINPRI)
247 247
248#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 248#define EMPTY /* required for microsofts broken pseudo-c compiler */
249#define EMPTY2(a,b) /* used to suppress some warnings */ 249#define EMPTY2(a,b) /* used to suppress some warnings */
250 250
251typedef ev_watcher *W; 251typedef ev_watcher *W;
252typedef ev_watcher_list *WL; 252typedef ev_watcher_list *WL;
253typedef ev_watcher_time *WT; 253typedef ev_watcher_time *WT;
396{ 396{
397 return ev_rt_now; 397 return ev_rt_now;
398} 398}
399#endif 399#endif
400 400
401#define array_roundsize(type,n) (((n) | 4) & ~3) 401int inline_size
402array_nextsize (int elem, int cur, int cnt)
403{
404 int ncur = cur + 1;
405
406 do
407 ncur <<= 1;
408 while (cnt > ncur);
409
410 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
411 if (elem * ncur > 4096)
412 {
413 ncur *= elem;
414 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
415 ncur = ncur - sizeof (void *) * 4;
416 ncur /= elem;
417 }
418
419 return ncur;
420}
421
422inline_speed void *
423array_realloc (int elem, void *base, int *cur, int cnt)
424{
425 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur);
427}
402 428
403#define array_needsize(type,base,cur,cnt,init) \ 429#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 430 if (expect_false ((cnt) > (cur))) \
405 { \ 431 { \
406 int newcnt = cur; \ 432 int ocur_ = (cur); \
407 do \ 433 (base) = (type *)array_realloc \
408 { \ 434 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 435 init ((base) + (ocur_), (cur) - ocur_); \
410 } \
411 while ((cnt) > newcnt); \
412 \
413 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
414 init (base + cur, newcnt - cur); \
415 cur = newcnt; \
416 } 436 }
417 437
438#if 0
418#define array_slim(type,stem) \ 439#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 440 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 441 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 442 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 443 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 444 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 445 }
446#endif
425 447
426#define array_free(stem, idx) \ 448#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 449 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 450
429/*****************************************************************************/ 451/*****************************************************************************/
764 ev_child *w; 786 ev_child *w;
765 787
766 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 788 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
767 if (w->pid == pid || !w->pid) 789 if (w->pid == pid || !w->pid)
768 { 790 {
769 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 791 ev_set_priority (w, ev_priority (sw)); /* need to do it *now* */
770 w->rpid = pid; 792 w->rpid = pid;
771 w->rstatus = status; 793 w->rstatus = status;
772 ev_feed_event (EV_A_ (W)w, EV_CHILD); 794 ev_feed_event (EV_A_ (W)w, EV_CHILD);
773 } 795 }
774} 796}
775 797
776#ifndef WCONTINUED 798#ifndef WCONTINUED
886ev_backend (EV_P) 908ev_backend (EV_P)
887{ 909{
888 return backend; 910 return backend;
889} 911}
890 912
913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
917}
918
891static void noinline 919static void noinline
892loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
893{ 921{
894 if (!backend) 922 if (!backend)
895 { 923 {
975#if EV_USE_SELECT 1003#if EV_USE_SELECT
976 if (backend == EVBACKEND_SELECT) select_destroy (EV_A); 1004 if (backend == EVBACKEND_SELECT) select_destroy (EV_A);
977#endif 1005#endif
978 1006
979 for (i = NUMPRI; i--; ) 1007 for (i = NUMPRI; i--; )
1008 {
980 array_free (pending, [i]); 1009 array_free (pending, [i]);
1010#if EV_IDLE_ENABLE
1011 array_free (idle, [i]);
1012#endif
1013 }
981 1014
982 /* have to use the microsoft-never-gets-it-right macro */ 1015 /* have to use the microsoft-never-gets-it-right macro */
983 array_free (fdchange, EMPTY0); 1016 array_free (fdchange, EMPTY);
984 array_free (timer, EMPTY0); 1017 array_free (timer, EMPTY);
985#if EV_PERIODIC_ENABLE 1018#if EV_PERIODIC_ENABLE
986 array_free (periodic, EMPTY0); 1019 array_free (periodic, EMPTY);
987#endif 1020#endif
988 array_free (idle, EMPTY0);
989 array_free (prepare, EMPTY0); 1021 array_free (prepare, EMPTY);
990 array_free (check, EMPTY0); 1022 array_free (check, EMPTY);
991 1023
992 backend = 0; 1024 backend = 0;
993} 1025}
994 1026
995void inline_size infy_fork (EV_P); 1027void inline_size infy_fork (EV_P);
1130 if (backend) 1162 if (backend)
1131 postfork = 1; 1163 postfork = 1;
1132} 1164}
1133 1165
1134/*****************************************************************************/ 1166/*****************************************************************************/
1135
1136int inline_size
1137any_pending (EV_P)
1138{
1139 int pri;
1140
1141 for (pri = NUMPRI; pri--; )
1142 if (pendingcnt [pri])
1143 return 1;
1144
1145 return 0;
1146}
1147 1167
1148void inline_speed 1168void inline_speed
1149call_pending (EV_P) 1169call_pending (EV_P)
1150{ 1170{
1151 int pri; 1171 int pri;
1242 for (i = periodiccnt >> 1; i--; ) 1262 for (i = periodiccnt >> 1; i--; )
1243 downheap ((WT *)periodics, periodiccnt, i); 1263 downheap ((WT *)periodics, periodiccnt, i);
1244} 1264}
1245#endif 1265#endif
1246 1266
1267#if EV_IDLE_ENABLE
1268void inline_size
1269idle_reify (EV_P)
1270{
1271 if (expect_false (idleall))
1272 {
1273 int pri;
1274
1275 for (pri = NUMPRI; pri--; )
1276 {
1277 if (pendingcnt [pri])
1278 break;
1279
1280 if (idlecnt [pri])
1281 {
1282 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1283 break;
1284 }
1285 }
1286 }
1287}
1288#endif
1289
1247int inline_size 1290int inline_size
1248time_update_monotonic (EV_P) 1291time_update_monotonic (EV_P)
1249{ 1292{
1250 mn_now = get_clock (); 1293 mn_now = get_clock ();
1251 1294
1384 1427
1385 /* calculate blocking time */ 1428 /* calculate blocking time */
1386 { 1429 {
1387 ev_tstamp block; 1430 ev_tstamp block;
1388 1431
1389 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt)) 1432 if (expect_false (flags & EVLOOP_NONBLOCK || idleall || !activecnt))
1390 block = 0.; /* do not block at all */ 1433 block = 0.; /* do not block at all */
1391 else 1434 else
1392 { 1435 {
1393 /* update time to cancel out callback processing overhead */ 1436 /* update time to cancel out callback processing overhead */
1394#if EV_USE_MONOTONIC 1437#if EV_USE_MONOTONIC
1418#endif 1461#endif
1419 1462
1420 if (expect_false (block < 0.)) block = 0.; 1463 if (expect_false (block < 0.)) block = 0.;
1421 } 1464 }
1422 1465
1466 ++loop_count;
1423 backend_poll (EV_A_ block); 1467 backend_poll (EV_A_ block);
1424 } 1468 }
1425 1469
1426 /* update ev_rt_now, do magic */ 1470 /* update ev_rt_now, do magic */
1427 time_update (EV_A); 1471 time_update (EV_A);
1430 timers_reify (EV_A); /* relative timers called last */ 1474 timers_reify (EV_A); /* relative timers called last */
1431#if EV_PERIODIC_ENABLE 1475#if EV_PERIODIC_ENABLE
1432 periodics_reify (EV_A); /* absolute timers called first */ 1476 periodics_reify (EV_A); /* absolute timers called first */
1433#endif 1477#endif
1434 1478
1479#if EV_IDLE_ENABLE
1435 /* queue idle watchers unless other events are pending */ 1480 /* queue idle watchers unless other events are pending */
1436 if (idlecnt && !any_pending (EV_A)) 1481 idle_reify (EV_A);
1437 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1482#endif
1438 1483
1439 /* queue check watchers, to be executed first */ 1484 /* queue check watchers, to be executed first */
1440 if (expect_false (checkcnt)) 1485 if (expect_false (checkcnt))
1441 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1486 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1442 1487
1487 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1532 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1488 w->pending = 0; 1533 w->pending = 0;
1489 } 1534 }
1490} 1535}
1491 1536
1537void inline_size
1538pri_adjust (EV_P_ W w)
1539{
1540 int pri = w->priority;
1541 pri = pri < EV_MINPRI ? EV_MINPRI : pri;
1542 pri = pri > EV_MAXPRI ? EV_MAXPRI : pri;
1543 w->priority = pri;
1544}
1545
1492void inline_speed 1546void inline_speed
1493ev_start (EV_P_ W w, int active) 1547ev_start (EV_P_ W w, int active)
1494{ 1548{
1495 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1549 pri_adjust (EV_A_ w);
1496 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1497
1498 w->active = active; 1550 w->active = active;
1499 ev_ref (EV_A); 1551 ev_ref (EV_A);
1500} 1552}
1501 1553
1502void inline_size 1554void inline_size
1977 2029
1978 ev_stop (EV_A_ (W)w); 2030 ev_stop (EV_A_ (W)w);
1979} 2031}
1980#endif 2032#endif
1981 2033
2034#if EV_IDLE_ENABLE
1982void 2035void
1983ev_idle_start (EV_P_ ev_idle *w) 2036ev_idle_start (EV_P_ ev_idle *w)
1984{ 2037{
1985 if (expect_false (ev_is_active (w))) 2038 if (expect_false (ev_is_active (w)))
1986 return; 2039 return;
1987 2040
2041 pri_adjust (EV_A_ (W)w);
2042
2043 {
2044 int active = ++idlecnt [ABSPRI (w)];
2045
2046 ++idleall;
1988 ev_start (EV_A_ (W)w, ++idlecnt); 2047 ev_start (EV_A_ (W)w, active);
2048
1989 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); 2049 array_needsize (ev_idle *, idles [ABSPRI (w)], idlemax [ABSPRI (w)], active, EMPTY2);
1990 idles [idlecnt - 1] = w; 2050 idles [ABSPRI (w)][active - 1] = w;
2051 }
1991} 2052}
1992 2053
1993void 2054void
1994ev_idle_stop (EV_P_ ev_idle *w) 2055ev_idle_stop (EV_P_ ev_idle *w)
1995{ 2056{
1997 if (expect_false (!ev_is_active (w))) 2058 if (expect_false (!ev_is_active (w)))
1998 return; 2059 return;
1999 2060
2000 { 2061 {
2001 int active = ((W)w)->active; 2062 int active = ((W)w)->active;
2002 idles [active - 1] = idles [--idlecnt]; 2063
2064 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2003 ((W)idles [active - 1])->active = active; 2065 ((W)idles [ABSPRI (w)][active - 1])->active = active;
2066
2067 ev_stop (EV_A_ (W)w);
2068 --idleall;
2004 } 2069 }
2005
2006 ev_stop (EV_A_ (W)w);
2007} 2070}
2071#endif
2008 2072
2009void 2073void
2010ev_prepare_start (EV_P_ ev_prepare *w) 2074ev_prepare_start (EV_P_ ev_prepare *w)
2011{ 2075{
2012 if (expect_false (ev_is_active (w))) 2076 if (expect_false (ev_is_active (w)))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines