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.232 by root, Tue May 6 15:29:58 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 {
1522periodics_reschedule (EV_P) 1525periodics_reschedule (EV_P)
1523{ 1526{
1524 int i; 1527 int i;
1525 1528
1526 /* adjust periodics after time jump */ 1529 /* adjust periodics after time jump */
1527 for (i = 0; i < periodiccnt; ++i) 1530 for (i = 1; i <= periodiccnt; ++i)
1528 { 1531 {
1529 ev_periodic *w = (ev_periodic *)periodics [i]; 1532 ev_periodic *w = (ev_periodic *)periodics [i];
1530 1533
1531 if (w->reschedule_cb) 1534 if (w->reschedule_cb)
1532 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1535 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1894 ev_start (EV_A_ (W)w, ++timercnt); 1897 ev_start (EV_A_ (W)w, ++timercnt);
1895 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1898 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1896 timers [timercnt] = (WT)w; 1899 timers [timercnt] = (WT)w;
1897 upheap (timers, timercnt); 1900 upheap (timers, timercnt);
1898 1901
1899 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ 1902 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1900} 1903}
1901 1904
1902void noinline 1905void noinline
1903ev_timer_stop (EV_P_ ev_timer *w) 1906ev_timer_stop (EV_P_ ev_timer *w)
1904{ 1907{
1905 clear_pending (EV_A_ (W)w); 1908 clear_pending (EV_A_ (W)w);
1906 if (expect_false (!ev_is_active (w))) 1909 if (expect_false (!ev_is_active (w)))
1907 return; 1910 return;
1908 1911
1909 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1910
1911 { 1912 {
1912 int active = ((W)w)->active; 1913 int active = ev_active (w);
1914
1915 assert (("internal timer heap corruption", timers [active] == (WT)w));
1913 1916
1914 if (expect_true (active < timercnt)) 1917 if (expect_true (active < timercnt))
1915 { 1918 {
1916 timers [active] = timers [timercnt]; 1919 timers [active] = timers [timercnt];
1917 adjustheap (timers, timercnt, active); 1920 adjustheap (timers, timercnt, active);
1931 if (ev_is_active (w)) 1934 if (ev_is_active (w))
1932 { 1935 {
1933 if (w->repeat) 1936 if (w->repeat)
1934 { 1937 {
1935 ev_at (w) = mn_now + w->repeat; 1938 ev_at (w) = mn_now + w->repeat;
1936 adjustheap (timers, timercnt, ((W)w)->active); 1939 adjustheap (timers, timercnt, ev_active (w));
1937 } 1940 }
1938 else 1941 else
1939 ev_timer_stop (EV_A_ w); 1942 ev_timer_stop (EV_A_ w);
1940 } 1943 }
1941 else if (w->repeat) 1944 else if (w->repeat)
1942 { 1945 {
1943 w->at = w->repeat; 1946 ev_at (w) = w->repeat;
1944 ev_timer_start (EV_A_ w); 1947 ev_timer_start (EV_A_ w);
1945 } 1948 }
1946} 1949}
1947 1950
1948#if EV_PERIODIC_ENABLE 1951#if EV_PERIODIC_ENABLE
1966 ev_start (EV_A_ (W)w, ++periodiccnt); 1969 ev_start (EV_A_ (W)w, ++periodiccnt);
1967 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 1970 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1968 periodics [periodiccnt] = (WT)w; 1971 periodics [periodiccnt] = (WT)w;
1969 upheap (periodics, periodiccnt); 1972 upheap (periodics, periodiccnt);
1970 1973
1971 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1974 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1972} 1975}
1973 1976
1974void noinline 1977void noinline
1975ev_periodic_stop (EV_P_ ev_periodic *w) 1978ev_periodic_stop (EV_P_ ev_periodic *w)
1976{ 1979{
1977 clear_pending (EV_A_ (W)w); 1980 clear_pending (EV_A_ (W)w);
1978 if (expect_false (!ev_is_active (w))) 1981 if (expect_false (!ev_is_active (w)))
1979 return; 1982 return;
1980 1983
1981 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1982
1983 { 1984 {
1984 int active = ((W)w)->active; 1985 int active = ev_active (w);
1986
1987 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1985 1988
1986 if (expect_true (active < periodiccnt)) 1989 if (expect_true (active < periodiccnt))
1987 { 1990 {
1988 periodics [active] = periodics [periodiccnt]; 1991 periodics [active] = periodics [periodiccnt];
1989 adjustheap (periodics, periodiccnt, active); 1992 adjustheap (periodics, periodiccnt, active);
2367 clear_pending (EV_A_ (W)w); 2370 clear_pending (EV_A_ (W)w);
2368 if (expect_false (!ev_is_active (w))) 2371 if (expect_false (!ev_is_active (w)))
2369 return; 2372 return;
2370 2373
2371 { 2374 {
2372 int active = ((W)w)->active; 2375 int active = ev_active (w);
2373 2376
2374 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2377 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2375 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2378 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2376 2379
2377 ev_stop (EV_A_ (W)w); 2380 ev_stop (EV_A_ (W)w);
2378 --idleall; 2381 --idleall;
2379 } 2382 }
2380} 2383}
2397 clear_pending (EV_A_ (W)w); 2400 clear_pending (EV_A_ (W)w);
2398 if (expect_false (!ev_is_active (w))) 2401 if (expect_false (!ev_is_active (w)))
2399 return; 2402 return;
2400 2403
2401 { 2404 {
2402 int active = ((W)w)->active; 2405 int active = ev_active (w);
2406
2403 prepares [active - 1] = prepares [--preparecnt]; 2407 prepares [active - 1] = prepares [--preparecnt];
2404 ((W)prepares [active - 1])->active = active; 2408 ev_active (prepares [active - 1]) = active;
2405 } 2409 }
2406 2410
2407 ev_stop (EV_A_ (W)w); 2411 ev_stop (EV_A_ (W)w);
2408} 2412}
2409 2413
2424 clear_pending (EV_A_ (W)w); 2428 clear_pending (EV_A_ (W)w);
2425 if (expect_false (!ev_is_active (w))) 2429 if (expect_false (!ev_is_active (w)))
2426 return; 2430 return;
2427 2431
2428 { 2432 {
2429 int active = ((W)w)->active; 2433 int active = ev_active (w);
2434
2430 checks [active - 1] = checks [--checkcnt]; 2435 checks [active - 1] = checks [--checkcnt];
2431 ((W)checks [active - 1])->active = active; 2436 ev_active (checks [active - 1]) = active;
2432 } 2437 }
2433 2438
2434 ev_stop (EV_A_ (W)w); 2439 ev_stop (EV_A_ (W)w);
2435} 2440}
2436 2441
2532 clear_pending (EV_A_ (W)w); 2537 clear_pending (EV_A_ (W)w);
2533 if (expect_false (!ev_is_active (w))) 2538 if (expect_false (!ev_is_active (w)))
2534 return; 2539 return;
2535 2540
2536 { 2541 {
2537 int active = ((W)w)->active; 2542 int active = ev_active (w);
2543
2538 forks [active - 1] = forks [--forkcnt]; 2544 forks [active - 1] = forks [--forkcnt];
2539 ((W)forks [active - 1])->active = active; 2545 ev_active (forks [active - 1]) = active;
2540 } 2546 }
2541 2547
2542 ev_stop (EV_A_ (W)w); 2548 ev_stop (EV_A_ (W)w);
2543} 2549}
2544#endif 2550#endif
2563 clear_pending (EV_A_ (W)w); 2569 clear_pending (EV_A_ (W)w);
2564 if (expect_false (!ev_is_active (w))) 2570 if (expect_false (!ev_is_active (w)))
2565 return; 2571 return;
2566 2572
2567 { 2573 {
2568 int active = ((W)w)->active; 2574 int active = ev_active (w);
2575
2569 asyncs [active - 1] = asyncs [--asynccnt]; 2576 asyncs [active - 1] = asyncs [--asynccnt];
2570 ((W)asyncs [active - 1])->active = active; 2577 ev_active (asyncs [active - 1]) = active;
2571 } 2578 }
2572 2579
2573 ev_stop (EV_A_ (W)w); 2580 ev_stop (EV_A_ (W)w);
2574} 2581}
2575 2582

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines