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

Comparing libev/ev.c (file contents):
Revision 1.228 by root, Fri May 2 08:07:37 2008 UTC vs.
Revision 1.234 by root, Tue May 6 23:42:16 2008 UTC

325 325
326typedef ev_watcher *W; 326typedef ev_watcher *W;
327typedef ev_watcher_list *WL; 327typedef ev_watcher_list *WL;
328typedef ev_watcher_time *WT; 328typedef ev_watcher_time *WT;
329 329
330#define ev_active(w) ((W)(w))->active
330#define ev_at(w) ((WT)(w))->at 331#define ev_at(w) ((WT)(w))->at
331 332
332#if EV_USE_MONOTONIC 333#if EV_USE_MONOTONIC
333/* sig_atomic_t is used to avoid per-thread variables or locking but still */ 334/* sig_atomic_t is used to avoid per-thread variables or locking but still */
334/* giving it a reasonably high chance of working on typical architetcures */ 335/* giving it a reasonably high chance of working on typical architetcures */
519 } 520 }
520} 521}
521 522
522/*****************************************************************************/ 523/*****************************************************************************/
523 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
524int inline_size 527int inline_size
525array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
526{ 529{
527 int ncur = cur + 1; 530 int ncur = cur + 1;
528 531
529 do 532 do
530 ncur <<= 1; 533 ncur <<= 1;
531 while (cnt > ncur); 534 while (cnt > ncur);
532 535
533 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 536 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
534 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
535 { 538 {
536 ncur *= elem; 539 ncur *= elem;
537 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
538 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
539 ncur /= elem; 542 ncur /= elem;
540 } 543 }
541 544
542 return ncur; 545 return ncur;
771 /* maybe we could use a dummy element at heap [0]? */ 774 /* maybe we could use a dummy element at heap [0]? */
772 if (!p || heap [p]->at <= w->at) 775 if (!p || heap [p]->at <= w->at)
773 break; 776 break;
774 777
775 heap [k] = heap [p]; 778 heap [k] = heap [p];
776 ((W)heap [k])->active = k; 779 ev_active (heap [k]) = k;
777 k = p; 780 k = p;
778 } 781 }
779 782
780 heap [k] = w; 783 heap [k] = w;
781 ((W)heap [k])->active = k; 784 ev_active (heap [k]) = k;
782} 785}
783 786
784/* away from the root */ 787/* away from the root */
785void inline_speed 788void inline_speed
786downheap (WT *heap, int N, int k) 789downheap (WT *heap, int N, int k)
799 802
800 if (w->at <= heap [c]->at) 803 if (w->at <= heap [c]->at)
801 break; 804 break;
802 805
803 heap [k] = heap [c]; 806 heap [k] = heap [c];
804 ((W)heap [k])->active = k; 807 ev_active (heap [k]) = k;
805 808
806 k = c; 809 k = c;
807 } 810 }
808 811
809 heap [k] = w; 812 heap [k] = w;
810 ((W)heap [k])->active = k; 813 ev_active (heap [k]) = k;
811} 814}
812 815
813void inline_size 816void inline_size
814adjustheap (WT *heap, int N, int k) 817adjustheap (WT *heap, int N, int k)
815{ 818{
911pipecb (EV_P_ ev_io *iow, int revents) 914pipecb (EV_P_ ev_io *iow, int revents)
912{ 915{
913#if EV_USE_EVENTFD 916#if EV_USE_EVENTFD
914 if (evfd >= 0) 917 if (evfd >= 0)
915 { 918 {
916 uint64_t counter = 1; 919 uint64_t counter;
917 read (evfd, &counter, sizeof (uint64_t)); 920 read (evfd, &counter, sizeof (uint64_t));
918 } 921 }
919 else 922 else
920#endif 923#endif
921 { 924 {
1367void 1370void
1368ev_loop_fork (EV_P) 1371ev_loop_fork (EV_P)
1369{ 1372{
1370 postfork = 1; /* must be in line with ev_default_fork */ 1373 postfork = 1; /* must be in line with ev_default_fork */
1371} 1374}
1372
1373#endif 1375#endif
1374 1376
1375#if EV_MULTIPLICITY 1377#if EV_MULTIPLICITY
1376struct ev_loop * 1378struct ev_loop *
1377ev_default_loop_init (unsigned int flags) 1379ev_default_loop_init (unsigned int flags)
1457 p->w->pending = 0; 1459 p->w->pending = 0;
1458 EV_CB_INVOKE (p->w, p->events); 1460 EV_CB_INVOKE (p->w, p->events);
1459 } 1461 }
1460 } 1462 }
1461} 1463}
1464
1465#if EV_IDLE_ENABLE
1466void inline_size
1467idle_reify (EV_P)
1468{
1469 if (expect_false (idleall))
1470 {
1471 int pri;
1472
1473 for (pri = NUMPRI; pri--; )
1474 {
1475 if (pendingcnt [pri])
1476 break;
1477
1478 if (idlecnt [pri])
1479 {
1480 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1481 break;
1482 }
1483 }
1484 }
1485}
1486#endif
1462 1487
1463void inline_size 1488void inline_size
1464timers_reify (EV_P) 1489timers_reify (EV_P)
1465{ 1490{
1466 while (timercnt && ev_at (timers [1]) <= mn_now) 1491 while (timercnt && ev_at (timers [1]) <= mn_now)
1522periodics_reschedule (EV_P) 1547periodics_reschedule (EV_P)
1523{ 1548{
1524 int i; 1549 int i;
1525 1550
1526 /* adjust periodics after time jump */ 1551 /* adjust periodics after time jump */
1527 for (i = 0; i < periodiccnt; ++i) 1552 for (i = 1; i <= periodiccnt; ++i)
1528 { 1553 {
1529 ev_periodic *w = (ev_periodic *)periodics [i]; 1554 ev_periodic *w = (ev_periodic *)periodics [i];
1530 1555
1531 if (w->reschedule_cb) 1556 if (w->reschedule_cb)
1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1557 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1535 } 1560 }
1536 1561
1537 /* now rebuild the heap */ 1562 /* now rebuild the heap */
1538 for (i = periodiccnt >> 1; i--; ) 1563 for (i = periodiccnt >> 1; i--; )
1539 downheap (periodics, periodiccnt, i); 1564 downheap (periodics, periodiccnt, i);
1540}
1541#endif
1542
1543#if EV_IDLE_ENABLE
1544void inline_size
1545idle_reify (EV_P)
1546{
1547 if (expect_false (idleall))
1548 {
1549 int pri;
1550
1551 for (pri = NUMPRI; pri--; )
1552 {
1553 if (pendingcnt [pri])
1554 break;
1555
1556 if (idlecnt [pri])
1557 {
1558 queue_events (EV_A_ (W *)idles [pri], idlecnt [pri], EV_IDLE);
1559 break;
1560 }
1561 }
1562 }
1563} 1565}
1564#endif 1566#endif
1565 1567
1566void inline_speed 1568void inline_speed
1567time_update (EV_P_ ev_tstamp max_block) 1569time_update (EV_P_ ev_tstamp max_block)
1596 */ 1598 */
1597 for (i = 4; --i; ) 1599 for (i = 4; --i; )
1598 { 1600 {
1599 rtmn_diff = ev_rt_now - mn_now; 1601 rtmn_diff = ev_rt_now - mn_now;
1600 1602
1601 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1603 if (expect_true (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP))
1602 return; /* all is well */ 1604 return; /* all is well */
1603 1605
1604 ev_rt_now = ev_time (); 1606 ev_rt_now = ev_time ();
1605 mn_now = get_clock (); 1607 mn_now = get_clock ();
1606 now_floor = mn_now; 1608 now_floor = mn_now;
1894 ev_start (EV_A_ (W)w, ++timercnt); 1896 ev_start (EV_A_ (W)w, ++timercnt);
1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1897 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1896 timers [timercnt] = (WT)w; 1898 timers [timercnt] = (WT)w;
1897 upheap (timers, timercnt); 1899 upheap (timers, timercnt);
1898 1900
1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ 1901 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1900} 1902}
1901 1903
1902void noinline 1904void noinline
1903ev_timer_stop (EV_P_ ev_timer *w) 1905ev_timer_stop (EV_P_ ev_timer *w)
1904{ 1906{
1905 clear_pending (EV_A_ (W)w); 1907 clear_pending (EV_A_ (W)w);
1906 if (expect_false (!ev_is_active (w))) 1908 if (expect_false (!ev_is_active (w)))
1907 return; 1909 return;
1908 1910
1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1910
1911 { 1911 {
1912 int active = ((W)w)->active; 1912 int active = ev_active (w);
1913
1914 assert (("internal timer heap corruption", timers [active] == (WT)w));
1913 1915
1914 if (expect_true (active < timercnt)) 1916 if (expect_true (active < timercnt))
1915 { 1917 {
1916 timers [active] = timers [timercnt]; 1918 timers [active] = timers [timercnt];
1917 adjustheap (timers, timercnt, active); 1919 adjustheap (timers, timercnt, active);
1931 if (ev_is_active (w)) 1933 if (ev_is_active (w))
1932 { 1934 {
1933 if (w->repeat) 1935 if (w->repeat)
1934 { 1936 {
1935 ev_at (w) = mn_now + w->repeat; 1937 ev_at (w) = mn_now + w->repeat;
1936 adjustheap (timers, timercnt, ((W)w)->active); 1938 adjustheap (timers, timercnt, ev_active (w));
1937 } 1939 }
1938 else 1940 else
1939 ev_timer_stop (EV_A_ w); 1941 ev_timer_stop (EV_A_ w);
1940 } 1942 }
1941 else if (w->repeat) 1943 else if (w->repeat)
1942 { 1944 {
1943 w->at = w->repeat; 1945 ev_at (w) = w->repeat;
1944 ev_timer_start (EV_A_ w); 1946 ev_timer_start (EV_A_ w);
1945 } 1947 }
1946} 1948}
1947 1949
1948#if EV_PERIODIC_ENABLE 1950#if EV_PERIODIC_ENABLE
1966 ev_start (EV_A_ (W)w, ++periodiccnt); 1968 ev_start (EV_A_ (W)w, ++periodiccnt);
1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 1969 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1968 periodics [periodiccnt] = (WT)w; 1970 periodics [periodiccnt] = (WT)w;
1969 upheap (periodics, periodiccnt); 1971 upheap (periodics, periodiccnt);
1970 1972
1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1973 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1972} 1974}
1973 1975
1974void noinline 1976void noinline
1975ev_periodic_stop (EV_P_ ev_periodic *w) 1977ev_periodic_stop (EV_P_ ev_periodic *w)
1976{ 1978{
1977 clear_pending (EV_A_ (W)w); 1979 clear_pending (EV_A_ (W)w);
1978 if (expect_false (!ev_is_active (w))) 1980 if (expect_false (!ev_is_active (w)))
1979 return; 1981 return;
1980 1982
1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1982
1983 { 1983 {
1984 int active = ((W)w)->active; 1984 int active = ev_active (w);
1985
1986 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1985 1987
1986 if (expect_true (active < periodiccnt)) 1988 if (expect_true (active < periodiccnt))
1987 { 1989 {
1988 periodics [active] = periodics [periodiccnt]; 1990 periodics [active] = periodics [periodiccnt];
1989 adjustheap (periodics, periodiccnt, active); 1991 adjustheap (periodics, periodiccnt, active);
2113 if (w->wd < 0) 2115 if (w->wd < 0)
2114 { 2116 {
2115 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2117 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2116 2118
2117 /* monitor some parent directory for speedup hints */ 2119 /* monitor some parent directory for speedup hints */
2120 /* note that exceeding the hardcoded limit is not a correctness issue, */
2121 /* but an efficiency issue only */
2118 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2122 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2119 { 2123 {
2120 char path [4096]; 2124 char path [4096];
2121 strcpy (path, w->path); 2125 strcpy (path, w->path);
2122 2126
2367 clear_pending (EV_A_ (W)w); 2371 clear_pending (EV_A_ (W)w);
2368 if (expect_false (!ev_is_active (w))) 2372 if (expect_false (!ev_is_active (w)))
2369 return; 2373 return;
2370 2374
2371 { 2375 {
2372 int active = ((W)w)->active; 2376 int active = ev_active (w);
2373 2377
2374 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2378 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2375 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2379 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2376 2380
2377 ev_stop (EV_A_ (W)w); 2381 ev_stop (EV_A_ (W)w);
2378 --idleall; 2382 --idleall;
2379 } 2383 }
2380} 2384}
2397 clear_pending (EV_A_ (W)w); 2401 clear_pending (EV_A_ (W)w);
2398 if (expect_false (!ev_is_active (w))) 2402 if (expect_false (!ev_is_active (w)))
2399 return; 2403 return;
2400 2404
2401 { 2405 {
2402 int active = ((W)w)->active; 2406 int active = ev_active (w);
2407
2403 prepares [active - 1] = prepares [--preparecnt]; 2408 prepares [active - 1] = prepares [--preparecnt];
2404 ((W)prepares [active - 1])->active = active; 2409 ev_active (prepares [active - 1]) = active;
2405 } 2410 }
2406 2411
2407 ev_stop (EV_A_ (W)w); 2412 ev_stop (EV_A_ (W)w);
2408} 2413}
2409 2414
2424 clear_pending (EV_A_ (W)w); 2429 clear_pending (EV_A_ (W)w);
2425 if (expect_false (!ev_is_active (w))) 2430 if (expect_false (!ev_is_active (w)))
2426 return; 2431 return;
2427 2432
2428 { 2433 {
2429 int active = ((W)w)->active; 2434 int active = ev_active (w);
2435
2430 checks [active - 1] = checks [--checkcnt]; 2436 checks [active - 1] = checks [--checkcnt];
2431 ((W)checks [active - 1])->active = active; 2437 ev_active (checks [active - 1]) = active;
2432 } 2438 }
2433 2439
2434 ev_stop (EV_A_ (W)w); 2440 ev_stop (EV_A_ (W)w);
2435} 2441}
2436 2442
2532 clear_pending (EV_A_ (W)w); 2538 clear_pending (EV_A_ (W)w);
2533 if (expect_false (!ev_is_active (w))) 2539 if (expect_false (!ev_is_active (w)))
2534 return; 2540 return;
2535 2541
2536 { 2542 {
2537 int active = ((W)w)->active; 2543 int active = ev_active (w);
2544
2538 forks [active - 1] = forks [--forkcnt]; 2545 forks [active - 1] = forks [--forkcnt];
2539 ((W)forks [active - 1])->active = active; 2546 ev_active (forks [active - 1]) = active;
2540 } 2547 }
2541 2548
2542 ev_stop (EV_A_ (W)w); 2549 ev_stop (EV_A_ (W)w);
2543} 2550}
2544#endif 2551#endif
2563 clear_pending (EV_A_ (W)w); 2570 clear_pending (EV_A_ (W)w);
2564 if (expect_false (!ev_is_active (w))) 2571 if (expect_false (!ev_is_active (w)))
2565 return; 2572 return;
2566 2573
2567 { 2574 {
2568 int active = ((W)w)->active; 2575 int active = ev_active (w);
2576
2569 asyncs [active - 1] = asyncs [--asynccnt]; 2577 asyncs [active - 1] = asyncs [--asynccnt];
2570 ((W)asyncs [active - 1])->active = active; 2578 ev_active (asyncs [active - 1]) = active;
2571 } 2579 }
2572 2580
2573 ev_stop (EV_A_ (W)w); 2581 ev_stop (EV_A_ (W)w);
2574} 2582}
2575 2583

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines