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

Comparing libev/ev.c (file contents):
Revision 1.139 by root, Sun Nov 25 09:24:37 2007 UTC vs.
Revision 1.140 by root, Mon Nov 26 19:49:36 2007 UTC

111#include <time.h> 111#include <time.h>
112 112
113#include <signal.h> 113#include <signal.h>
114 114
115#ifndef _WIN32 115#ifndef _WIN32
116# include <unistd.h>
117# include <sys/time.h> 116# include <sys/time.h>
118# include <sys/wait.h> 117# include <sys/wait.h>
118# include <unistd.h>
119#else 119#else
120# define WIN32_LEAN_AND_MEAN 120# define WIN32_LEAN_AND_MEAN
121# include <windows.h> 121# include <windows.h>
122# ifndef EV_SELECT_IS_WINSOCKET 122# ifndef EV_SELECT_IS_WINSOCKET
123# define EV_SELECT_IS_WINSOCKET 1 123# define EV_SELECT_IS_WINSOCKET 1
187# include "ev.h" 187# include "ev.h"
188#endif 188#endif
189 189
190#if __GNUC__ >= 3 190#if __GNUC__ >= 3
191# define expect(expr,value) __builtin_expect ((expr),(value)) 191# define expect(expr,value) __builtin_expect ((expr),(value))
192# define inline_size static inline /* inline for codesize */
193# if EV_MINIMAL
194# define noinline __attribute__ ((noinline))
195# define inline_speed static noinline
196# else
197# define noinline
192# define inline static inline 198# define inline_speed static inline
199# endif
193#else 200#else
194# define expect(expr,value) (expr) 201# define expect(expr,value) (expr)
195# define inline static 202# define inline_speed static
203# define inline_minimal static
204# define noinline
196#endif 205#endif
197 206
198#define expect_false(expr) expect ((expr) != 0, 0) 207#define expect_false(expr) expect ((expr) != 0, 0)
199#define expect_true(expr) expect ((expr) != 0, 1) 208#define expect_true(expr) expect ((expr) != 0, 1)
200 209
306 315
307#endif 316#endif
308 317
309/*****************************************************************************/ 318/*****************************************************************************/
310 319
311ev_tstamp 320ev_tstamp noinline
312ev_time (void) 321ev_time (void)
313{ 322{
314#if EV_USE_REALTIME 323#if EV_USE_REALTIME
315 struct timespec ts; 324 struct timespec ts;
316 clock_gettime (CLOCK_REALTIME, &ts); 325 clock_gettime (CLOCK_REALTIME, &ts);
320 gettimeofday (&tv, 0); 329 gettimeofday (&tv, 0);
321 return tv.tv_sec + tv.tv_usec * 1e-6; 330 return tv.tv_sec + tv.tv_usec * 1e-6;
322#endif 331#endif
323} 332}
324 333
325inline ev_tstamp 334ev_tstamp inline_size
326get_clock (void) 335get_clock (void)
327{ 336{
328#if EV_USE_MONOTONIC 337#if EV_USE_MONOTONIC
329 if (expect_true (have_monotonic)) 338 if (expect_true (have_monotonic))
330 { 339 {
373#define array_free(stem, idx) \ 382#define array_free(stem, idx) \
374 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 383 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
375 384
376/*****************************************************************************/ 385/*****************************************************************************/
377 386
378static void 387void inline_size
379anfds_init (ANFD *base, int count) 388anfds_init (ANFD *base, int count)
380{ 389{
381 while (count--) 390 while (count--)
382 { 391 {
383 base->head = 0; 392 base->head = 0;
386 395
387 ++base; 396 ++base;
388 } 397 }
389} 398}
390 399
391void 400void noinline
392ev_feed_event (EV_P_ void *w, int revents) 401ev_feed_event (EV_P_ void *w, int revents)
393{ 402{
394 W w_ = (W)w; 403 W w_ = (W)w;
395 404
396 if (expect_false (w_->pending)) 405 if (expect_false (w_->pending))
412 421
413 for (i = 0; i < eventcnt; ++i) 422 for (i = 0; i < eventcnt; ++i)
414 ev_feed_event (EV_A_ events [i], type); 423 ev_feed_event (EV_A_ events [i], type);
415} 424}
416 425
417inline void 426void inline_speed
418fd_event (EV_P_ int fd, int revents) 427fd_event (EV_P_ int fd, int revents)
419{ 428{
420 ANFD *anfd = anfds + fd; 429 ANFD *anfd = anfds + fd;
421 ev_io *w; 430 ev_io *w;
422 431
435 fd_event (EV_A_ fd, revents); 444 fd_event (EV_A_ fd, revents);
436} 445}
437 446
438/*****************************************************************************/ 447/*****************************************************************************/
439 448
440inline void 449void inline_size
441fd_reify (EV_P) 450fd_reify (EV_P)
442{ 451{
443 int i; 452 int i;
444 453
445 for (i = 0; i < fdchangecnt; ++i) 454 for (i = 0; i < fdchangecnt; ++i)
469 } 478 }
470 479
471 fdchangecnt = 0; 480 fdchangecnt = 0;
472} 481}
473 482
474static void 483void inline_size
475fd_change (EV_P_ int fd) 484fd_change (EV_P_ int fd)
476{ 485{
477 if (expect_false (anfds [fd].reify)) 486 if (expect_false (anfds [fd].reify))
478 return; 487 return;
479 488
482 ++fdchangecnt; 491 ++fdchangecnt;
483 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); 492 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
484 fdchanges [fdchangecnt - 1] = fd; 493 fdchanges [fdchangecnt - 1] = fd;
485} 494}
486 495
487static void 496void inline_speed
488fd_kill (EV_P_ int fd) 497fd_kill (EV_P_ int fd)
489{ 498{
490 ev_io *w; 499 ev_io *w;
491 500
492 while ((w = (ev_io *)anfds [fd].head)) 501 while ((w = (ev_io *)anfds [fd].head))
494 ev_io_stop (EV_A_ w); 503 ev_io_stop (EV_A_ w);
495 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 504 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
496 } 505 }
497} 506}
498 507
499inline int 508int inline_size
500fd_valid (int fd) 509fd_valid (int fd)
501{ 510{
502#ifdef _WIN32 511#ifdef _WIN32
503 return _get_osfhandle (fd) != -1; 512 return _get_osfhandle (fd) != -1;
504#else 513#else
505 return fcntl (fd, F_GETFD) != -1; 514 return fcntl (fd, F_GETFD) != -1;
506#endif 515#endif
507} 516}
508 517
509/* called on EBADF to verify fds */ 518/* called on EBADF to verify fds */
510static void 519static void noinline
511fd_ebadf (EV_P) 520fd_ebadf (EV_P)
512{ 521{
513 int fd; 522 int fd;
514 523
515 for (fd = 0; fd < anfdmax; ++fd) 524 for (fd = 0; fd < anfdmax; ++fd)
517 if (!fd_valid (fd) == -1 && errno == EBADF) 526 if (!fd_valid (fd) == -1 && errno == EBADF)
518 fd_kill (EV_A_ fd); 527 fd_kill (EV_A_ fd);
519} 528}
520 529
521/* called on ENOMEM in select/poll to kill some fds and retry */ 530/* called on ENOMEM in select/poll to kill some fds and retry */
522static void 531static void noinline
523fd_enomem (EV_P) 532fd_enomem (EV_P)
524{ 533{
525 int fd; 534 int fd;
526 535
527 for (fd = anfdmax; fd--; ) 536 for (fd = anfdmax; fd--; )
531 return; 540 return;
532 } 541 }
533} 542}
534 543
535/* usually called after fork if backend needs to re-arm all fds from scratch */ 544/* usually called after fork if backend needs to re-arm all fds from scratch */
536static void 545static void noinline
537fd_rearm_all (EV_P) 546fd_rearm_all (EV_P)
538{ 547{
539 int fd; 548 int fd;
540 549
541 /* this should be highly optimised to not do anything but set a flag */ 550 /* this should be highly optimised to not do anything but set a flag */
547 } 556 }
548} 557}
549 558
550/*****************************************************************************/ 559/*****************************************************************************/
551 560
552static void 561void inline_speed
553upheap (WT *heap, int k) 562upheap (WT *heap, int k)
554{ 563{
555 WT w = heap [k]; 564 WT w = heap [k];
556 565
557 while (k && heap [k >> 1]->at > w->at) 566 while (k && heap [k >> 1]->at > w->at)
564 heap [k] = w; 573 heap [k] = w;
565 ((W)heap [k])->active = k + 1; 574 ((W)heap [k])->active = k + 1;
566 575
567} 576}
568 577
569static void 578void inline_speed
570downheap (WT *heap, int N, int k) 579downheap (WT *heap, int N, int k)
571{ 580{
572 WT w = heap [k]; 581 WT w = heap [k];
573 582
574 while (k < (N >> 1)) 583 while (k < (N >> 1))
588 597
589 heap [k] = w; 598 heap [k] = w;
590 ((W)heap [k])->active = k + 1; 599 ((W)heap [k])->active = k + 1;
591} 600}
592 601
593inline void 602void inline_size
594adjustheap (WT *heap, int N, int k) 603adjustheap (WT *heap, int N, int k)
595{ 604{
596 upheap (heap, k); 605 upheap (heap, k);
597 downheap (heap, N, k); 606 downheap (heap, N, k);
598} 607}
610 619
611static int sigpipe [2]; 620static int sigpipe [2];
612static sig_atomic_t volatile gotsig; 621static sig_atomic_t volatile gotsig;
613static ev_io sigev; 622static ev_io sigev;
614 623
615static void 624void inline_size
616signals_init (ANSIG *base, int count) 625signals_init (ANSIG *base, int count)
617{ 626{
618 while (count--) 627 while (count--)
619 { 628 {
620 base->head = 0; 629 base->head = 0;
640 write (sigpipe [1], &signum, 1); 649 write (sigpipe [1], &signum, 1);
641 errno = old_errno; 650 errno = old_errno;
642 } 651 }
643} 652}
644 653
645void 654void noinline
646ev_feed_signal_event (EV_P_ int signum) 655ev_feed_signal_event (EV_P_ int signum)
647{ 656{
648 WL w; 657 WL w;
649 658
650#if EV_MULTIPLICITY 659#if EV_MULTIPLICITY
673 for (signum = signalmax; signum--; ) 682 for (signum = signalmax; signum--; )
674 if (signals [signum].gotsig) 683 if (signals [signum].gotsig)
675 ev_feed_signal_event (EV_A_ signum + 1); 684 ev_feed_signal_event (EV_A_ signum + 1);
676} 685}
677 686
678static void 687void inline_size
679fd_intern (int fd) 688fd_intern (int fd)
680{ 689{
681#ifdef _WIN32 690#ifdef _WIN32
682 int arg = 1; 691 int arg = 1;
683 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 692 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
685 fcntl (fd, F_SETFD, FD_CLOEXEC); 694 fcntl (fd, F_SETFD, FD_CLOEXEC);
686 fcntl (fd, F_SETFL, O_NONBLOCK); 695 fcntl (fd, F_SETFL, O_NONBLOCK);
687#endif 696#endif
688} 697}
689 698
690static void 699static void noinline
691siginit (EV_P) 700siginit (EV_P)
692{ 701{
693 fd_intern (sigpipe [0]); 702 fd_intern (sigpipe [0]);
694 fd_intern (sigpipe [1]); 703 fd_intern (sigpipe [1]);
695 704
708 717
709#ifndef WCONTINUED 718#ifndef WCONTINUED
710# define WCONTINUED 0 719# define WCONTINUED 0
711#endif 720#endif
712 721
713static void 722void inline_speed
714child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) 723child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
715{ 724{
716 ev_child *w; 725 ev_child *w;
717 726
718 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 727 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
772{ 781{
773 return EV_VERSION_MINOR; 782 return EV_VERSION_MINOR;
774} 783}
775 784
776/* return true if we are running with elevated privileges and should ignore env variables */ 785/* return true if we are running with elevated privileges and should ignore env variables */
777static int 786int inline_size
778enable_secure (void) 787enable_secure (void)
779{ 788{
780#ifdef _WIN32 789#ifdef _WIN32
781 return 0; 790 return 0;
782#else 791#else
904 array_free (pending, [i]); 913 array_free (pending, [i]);
905 914
906 /* have to use the microsoft-never-gets-it-right macro */ 915 /* have to use the microsoft-never-gets-it-right macro */
907 array_free (fdchange, EMPTY0); 916 array_free (fdchange, EMPTY0);
908 array_free (timer, EMPTY0); 917 array_free (timer, EMPTY0);
909#if EV_PERIODICS 918#if EV_PERIODIC_ENABLE
910 array_free (periodic, EMPTY0); 919 array_free (periodic, EMPTY0);
911#endif 920#endif
912 array_free (idle, EMPTY0); 921 array_free (idle, EMPTY0);
913 array_free (prepare, EMPTY0); 922 array_free (prepare, EMPTY0);
914 array_free (check, EMPTY0); 923 array_free (check, EMPTY0);
1050 postfork = 1; 1059 postfork = 1;
1051} 1060}
1052 1061
1053/*****************************************************************************/ 1062/*****************************************************************************/
1054 1063
1055static int 1064int inline_size
1056any_pending (EV_P) 1065any_pending (EV_P)
1057{ 1066{
1058 int pri; 1067 int pri;
1059 1068
1060 for (pri = NUMPRI; pri--; ) 1069 for (pri = NUMPRI; pri--; )
1062 return 1; 1071 return 1;
1063 1072
1064 return 0; 1073 return 0;
1065} 1074}
1066 1075
1067inline void 1076void inline_speed
1068call_pending (EV_P) 1077call_pending (EV_P)
1069{ 1078{
1070 int pri; 1079 int pri;
1071 1080
1072 for (pri = NUMPRI; pri--; ) 1081 for (pri = NUMPRI; pri--; )
1082 EV_CB_INVOKE (p->w, p->events); 1091 EV_CB_INVOKE (p->w, p->events);
1083 } 1092 }
1084 } 1093 }
1085} 1094}
1086 1095
1087inline void 1096void inline_size
1088timers_reify (EV_P) 1097timers_reify (EV_P)
1089{ 1098{
1090 while (timercnt && ((WT)timers [0])->at <= mn_now) 1099 while (timercnt && ((WT)timers [0])->at <= mn_now)
1091 { 1100 {
1092 ev_timer *w = timers [0]; 1101 ev_timer *w = timers [0];
1109 1118
1110 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1119 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1111 } 1120 }
1112} 1121}
1113 1122
1114#if EV_PERIODICS 1123#if EV_PERIODIC_ENABLE
1115inline void 1124void inline_size
1116periodics_reify (EV_P) 1125periodics_reify (EV_P)
1117{ 1126{
1118 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1127 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1119 { 1128 {
1120 ev_periodic *w = periodics [0]; 1129 ev_periodic *w = periodics [0];
1139 1148
1140 ev_feed_event (EV_A_ (W)w, EV_PERIODIC); 1149 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
1141 } 1150 }
1142} 1151}
1143 1152
1144static void 1153static void noinline
1145periodics_reschedule (EV_P) 1154periodics_reschedule (EV_P)
1146{ 1155{
1147 int i; 1156 int i;
1148 1157
1149 /* adjust periodics after time jump */ 1158 /* adjust periodics after time jump */
1161 for (i = periodiccnt >> 1; i--; ) 1170 for (i = periodiccnt >> 1; i--; )
1162 downheap ((WT *)periodics, periodiccnt, i); 1171 downheap ((WT *)periodics, periodiccnt, i);
1163} 1172}
1164#endif 1173#endif
1165 1174
1166inline int 1175int inline_size
1167time_update_monotonic (EV_P) 1176time_update_monotonic (EV_P)
1168{ 1177{
1169 mn_now = get_clock (); 1178 mn_now = get_clock ();
1170 1179
1171 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 1180 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
1179 ev_rt_now = ev_time (); 1188 ev_rt_now = ev_time ();
1180 return 1; 1189 return 1;
1181 } 1190 }
1182} 1191}
1183 1192
1184inline void 1193void inline_size
1185time_update (EV_P) 1194time_update (EV_P)
1186{ 1195{
1187 int i; 1196 int i;
1188 1197
1189#if EV_USE_MONOTONIC 1198#if EV_USE_MONOTONIC
1211 ev_rt_now = ev_time (); 1220 ev_rt_now = ev_time ();
1212 mn_now = get_clock (); 1221 mn_now = get_clock ();
1213 now_floor = mn_now; 1222 now_floor = mn_now;
1214 } 1223 }
1215 1224
1216# if EV_PERIODICS 1225# if EV_PERIODIC_ENABLE
1217 periodics_reschedule (EV_A); 1226 periodics_reschedule (EV_A);
1218# endif 1227# endif
1219 /* no timer adjustment, as the monotonic clock doesn't jump */ 1228 /* no timer adjustment, as the monotonic clock doesn't jump */
1220 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1229 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1221 } 1230 }
1225 { 1234 {
1226 ev_rt_now = ev_time (); 1235 ev_rt_now = ev_time ();
1227 1236
1228 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1237 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1229 { 1238 {
1230#if EV_PERIODICS 1239#if EV_PERIODIC_ENABLE
1231 periodics_reschedule (EV_A); 1240 periodics_reschedule (EV_A);
1232#endif 1241#endif
1233 1242
1234 /* adjust timers. this is easy, as the offset is the same for all */ 1243 /* adjust timers. this is easy, as the offset is the same for all */
1235 for (i = 0; i < timercnt; ++i) 1244 for (i = 0; i < timercnt; ++i)
1302 { 1311 {
1303 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1312 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1304 if (block > to) block = to; 1313 if (block > to) block = to;
1305 } 1314 }
1306 1315
1307#if EV_PERIODICS 1316#if EV_PERIODIC_ENABLE
1308 if (periodiccnt) 1317 if (periodiccnt)
1309 { 1318 {
1310 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1319 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1311 if (block > to) block = to; 1320 if (block > to) block = to;
1312 } 1321 }
1321 /* update ev_rt_now, do magic */ 1330 /* update ev_rt_now, do magic */
1322 time_update (EV_A); 1331 time_update (EV_A);
1323 1332
1324 /* queue pending timers and reschedule them */ 1333 /* queue pending timers and reschedule them */
1325 timers_reify (EV_A); /* relative timers called last */ 1334 timers_reify (EV_A); /* relative timers called last */
1326#if EV_PERIODICS 1335#if EV_PERIODIC_ENABLE
1327 periodics_reify (EV_A); /* absolute timers called first */ 1336 periodics_reify (EV_A); /* absolute timers called first */
1328#endif 1337#endif
1329 1338
1330 /* queue idle watchers unless other events are pending */ 1339 /* queue idle watchers unless other events are pending */
1331 if (idlecnt && !any_pending (EV_A)) 1340 if (idlecnt && !any_pending (EV_A))
1351 loop_done = how; 1360 loop_done = how;
1352} 1361}
1353 1362
1354/*****************************************************************************/ 1363/*****************************************************************************/
1355 1364
1356inline void 1365void inline_size
1357wlist_add (WL *head, WL elem) 1366wlist_add (WL *head, WL elem)
1358{ 1367{
1359 elem->next = *head; 1368 elem->next = *head;
1360 *head = elem; 1369 *head = elem;
1361} 1370}
1362 1371
1363inline void 1372void inline_size
1364wlist_del (WL *head, WL elem) 1373wlist_del (WL *head, WL elem)
1365{ 1374{
1366 while (*head) 1375 while (*head)
1367 { 1376 {
1368 if (*head == elem) 1377 if (*head == elem)
1373 1382
1374 head = &(*head)->next; 1383 head = &(*head)->next;
1375 } 1384 }
1376} 1385}
1377 1386
1378inline void 1387void inline_speed
1379ev_clear_pending (EV_P_ W w) 1388ev_clear_pending (EV_P_ W w)
1380{ 1389{
1381 if (w->pending) 1390 if (w->pending)
1382 { 1391 {
1383 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1392 pendings [ABSPRI (w)][w->pending - 1].w = 0;
1384 w->pending = 0; 1393 w->pending = 0;
1385 } 1394 }
1386} 1395}
1387 1396
1388inline void 1397void inline_speed
1389ev_start (EV_P_ W w, int active) 1398ev_start (EV_P_ W w, int active)
1390{ 1399{
1391 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1400 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
1392 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1401 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
1393 1402
1394 w->active = active; 1403 w->active = active;
1395 ev_ref (EV_A); 1404 ev_ref (EV_A);
1396} 1405}
1397 1406
1398inline void 1407void inline_size
1399ev_stop (EV_P_ W w) 1408ev_stop (EV_P_ W w)
1400{ 1409{
1401 ev_unref (EV_A); 1410 ev_unref (EV_A);
1402 w->active = 0; 1411 w->active = 0;
1403} 1412}
1492 w->at = w->repeat; 1501 w->at = w->repeat;
1493 ev_timer_start (EV_A_ w); 1502 ev_timer_start (EV_A_ w);
1494 } 1503 }
1495} 1504}
1496 1505
1497#if EV_PERIODICS 1506#if EV_PERIODIC_ENABLE
1498void 1507void
1499ev_periodic_start (EV_P_ ev_periodic *w) 1508ev_periodic_start (EV_P_ ev_periodic *w)
1500{ 1509{
1501 if (expect_false (ev_is_active (w))) 1510 if (expect_false (ev_is_active (w)))
1502 return; 1511 return;
1695 1704
1696 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1705 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1697 ev_stop (EV_A_ (W)w); 1706 ev_stop (EV_A_ (W)w);
1698} 1707}
1699 1708
1700#if EV_MULTIPLICITY 1709#if EV_EMBED_ENABLE
1701void 1710void noinline
1702ev_embed_sweep (EV_P_ ev_embed *w) 1711ev_embed_sweep (EV_P_ ev_embed *w)
1703{ 1712{
1704 ev_loop (w->loop, EVLOOP_NONBLOCK); 1713 ev_loop (w->loop, EVLOOP_NONBLOCK);
1705} 1714}
1706 1715
1727 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 1736 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1728 } 1737 }
1729 1738
1730 ev_set_priority (&w->io, ev_priority (w)); 1739 ev_set_priority (&w->io, ev_priority (w));
1731 ev_io_start (EV_A_ &w->io); 1740 ev_io_start (EV_A_ &w->io);
1741
1732 ev_start (EV_A_ (W)w, 1); 1742 ev_start (EV_A_ (W)w, 1);
1733} 1743}
1734 1744
1735void 1745void
1736ev_embed_stop (EV_P_ ev_embed *w) 1746ev_embed_stop (EV_P_ ev_embed *w)
1738 ev_clear_pending (EV_A_ (W)w); 1748 ev_clear_pending (EV_A_ (W)w);
1739 if (expect_false (!ev_is_active (w))) 1749 if (expect_false (!ev_is_active (w)))
1740 return; 1750 return;
1741 1751
1742 ev_io_stop (EV_A_ &w->io); 1752 ev_io_stop (EV_A_ &w->io);
1753
1754 ev_stop (EV_A_ (W)w);
1755}
1756#endif
1757
1758#if EV_STAT_ENABLE
1759
1760# ifdef _WIN32
1761# define lstat(a,b) stat(a,b)
1762# endif
1763
1764void
1765ev_stat_stat (EV_P_ ev_stat *w)
1766{
1767 if (lstat (w->path, &w->attr) < 0)
1768 w->attr.st_nlink = 0;
1769 else if (!w->attr.st_nlink)
1770 w->attr.st_nlink = 1;
1771}
1772
1773static void
1774stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1775{
1776 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1777
1778 /* we copy this here each the time so that */
1779 /* prev has the old value when the callback gets invoked */
1780 w->prev = w->attr;
1781 ev_stat_stat (EV_A_ w);
1782
1783 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata)))
1784 ev_feed_event (EV_A_ w, EV_STAT);
1785}
1786
1787void
1788ev_stat_start (EV_P_ ev_stat *w)
1789{
1790 if (expect_false (ev_is_active (w)))
1791 return;
1792
1793 /* since we use memcmp, we need to clear any padding data etc. */
1794 memset (&w->prev, 0, sizeof (ev_statdata));
1795 memset (&w->attr, 0, sizeof (ev_statdata));
1796
1797 ev_stat_stat (EV_A_ w);
1798
1799 ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval);
1800 ev_set_priority (&w->timer, ev_priority (w));
1801 ev_timer_start (EV_A_ &w->timer);
1802
1803 ev_start (EV_A_ (W)w, 1);
1804}
1805
1806void
1807ev_stat_stop (EV_P_ ev_stat *w)
1808{
1809 ev_clear_pending (EV_A_ (W)w);
1810 if (expect_false (!ev_is_active (w)))
1811 return;
1812
1813 ev_timer_stop (EV_A_ &w->timer);
1814
1743 ev_stop (EV_A_ (W)w); 1815 ev_stop (EV_A_ (W)w);
1744} 1816}
1745#endif 1817#endif
1746 1818
1747/*****************************************************************************/ 1819/*****************************************************************************/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines