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

Comparing libev/ev.c (file contents):
Revision 1.23 by root, Wed Oct 31 20:10:17 2007 UTC vs.
Revision 1.27 by root, Wed Oct 31 22:16:36 2007 UTC

130/*****************************************************************************/ 130/*****************************************************************************/
131 131
132typedef struct 132typedef struct
133{ 133{
134 struct ev_io *head; 134 struct ev_io *head;
135 unsigned char wev, rev; /* want, received event set */ 135 int events;
136} ANFD; 136} ANFD;
137 137
138static ANFD *anfds; 138static ANFD *anfds;
139static int anfdmax; 139static int anfdmax;
140 140
141static int *fdchanges;
142static int fdchangemax, fdchangecnt;
143
144static void 141static void
145anfds_init (ANFD *base, int count) 142anfds_init (ANFD *base, int count)
146{ 143{
147 while (count--) 144 while (count--)
148 { 145 {
149 base->head = 0; 146 base->head = 0;
150 base->wev = base->rev = EV_NONE; 147 base->events = EV_NONE;
151 ++base; 148 ++base;
152 } 149 }
153} 150}
154 151
155typedef struct 152typedef struct
172 pendings [pendingcnt - 1].events = events; 169 pendings [pendingcnt - 1].events = events;
173 } 170 }
174} 171}
175 172
176static void 173static void
174queue_events (W *events, int eventcnt, int type)
175{
176 int i;
177
178 for (i = 0; i < eventcnt; ++i)
179 event (events [i], type);
180}
181
182static void
177fd_event (int fd, int events) 183fd_event (int fd, int events)
178{ 184{
179 ANFD *anfd = anfds + fd; 185 ANFD *anfd = anfds + fd;
180 struct ev_io *w; 186 struct ev_io *w;
181 187
186 if (ev) 192 if (ev)
187 event ((W)w, ev); 193 event ((W)w, ev);
188 } 194 }
189} 195}
190 196
197/*****************************************************************************/
198
199static int *fdchanges;
200static int fdchangemax, fdchangecnt;
201
191static void 202static void
192queue_events (W *events, int eventcnt, int type) 203fd_reify (void)
193{ 204{
194 int i; 205 int i;
195 206
196 for (i = 0; i < eventcnt; ++i) 207 for (i = 0; i < fdchangecnt; ++i)
197 event (events [i], type); 208 {
209 int fd = fdchanges [i];
210 ANFD *anfd = anfds + fd;
211 struct ev_io *w;
212
213 int events = 0;
214
215 for (w = anfd->head; w; w = w->next)
216 events |= w->events;
217
218 anfd->events &= ~EV_REIFY;
219
220 if (anfd->events != events)
221 {
222 method_modify (fd, anfd->events, events);
223 anfd->events = events;
224 }
225 }
226
227 fdchangecnt = 0;
228}
229
230static void
231fd_change (int fd)
232{
233 if (anfds [fd].events & EV_REIFY)
234 return;
235
236 anfds [fd].events |= EV_REIFY;
237
238 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd;
198} 241}
199 242
200/* called on EBADF to verify fds */ 243/* called on EBADF to verify fds */
201static void 244static void
202fd_recheck () 245fd_recheck (void)
203{ 246{
204 int fd; 247 int fd;
205 248
206 for (fd = 0; fd < anfdmax; ++fd) 249 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 250 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 252 while (anfds [fd].head)
253 {
254 event ((W)anfds [fd].head, EV_ERROR);
210 evio_stop (anfds [fd].head); 255 evio_stop (anfds [fd].head);
256 }
211} 257}
212 258
213/*****************************************************************************/ 259/*****************************************************************************/
214 260
215static struct ev_timer **timers; 261static struct ev_timer **timers;
371# include "ev_epoll.c" 417# include "ev_epoll.c"
372#endif 418#endif
373#if HAVE_SELECT 419#if HAVE_SELECT
374# include "ev_select.c" 420# include "ev_select.c"
375#endif 421#endif
422
423int
424ev_version_major (void)
425{
426 return EV_VERSION_MAJOR;
427}
428
429int
430ev_version_minor (void)
431{
432 return EV_VERSION_MINOR;
433}
376 434
377int ev_init (int flags) 435int ev_init (int flags)
378{ 436{
379 if (!ev_method) 437 if (!ev_method)
380 { 438 {
414 return ev_method; 472 return ev_method;
415} 473}
416 474
417/*****************************************************************************/ 475/*****************************************************************************/
418 476
477void
419void ev_prefork (void) 478ev_prefork (void)
420{ 479{
421 /* nop */ 480 /* nop */
422} 481}
423 482
483void
424void ev_postfork_parent (void) 484ev_postfork_parent (void)
425{ 485{
426 /* nop */ 486 /* nop */
427} 487}
428 488
489void
429void ev_postfork_child (void) 490ev_postfork_child (void)
430{ 491{
431#if HAVE_EPOLL 492#if HAVE_EPOLL
432 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
433 epoll_postfork_child (); 494 epoll_postfork_child ();
434#endif 495#endif
441} 502}
442 503
443/*****************************************************************************/ 504/*****************************************************************************/
444 505
445static void 506static void
446fd_reify (void)
447{
448 int i;
449
450 for (i = 0; i < fdchangecnt; ++i)
451 {
452 int fd = fdchanges [i];
453 ANFD *anfd = anfds + fd;
454 struct ev_io *w;
455
456 int wev = 0;
457
458 for (w = anfd->head; w; w = w->next)
459 wev |= w->events;
460
461 if (anfd->wev != wev)
462 {
463 method_modify (fd, anfd->wev, wev);
464 anfd->wev = wev;
465 }
466 }
467
468 fdchangecnt = 0;
469}
470
471static void
472call_pending () 507call_pending (void)
473{ 508{
474 while (pendingcnt) 509 while (pendingcnt)
475 { 510 {
476 ANPENDING *p = pendings + --pendingcnt; 511 ANPENDING *p = pendings + --pendingcnt;
477 512
482 } 517 }
483 } 518 }
484} 519}
485 520
486static void 521static void
487timers_reify () 522timers_reify (void)
488{ 523{
489 while (timercnt && timers [0]->at <= now) 524 while (timercnt && timers [0]->at <= now)
490 { 525 {
491 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
492 527
503 evtimer_stop (w); /* nonrepeating: stop timer */ 538 evtimer_stop (w); /* nonrepeating: stop timer */
504 } 539 }
505} 540}
506 541
507static void 542static void
508periodics_reify () 543periodics_reify (void)
509{ 544{
510 while (periodiccnt && periodics [0]->at <= ev_now) 545 while (periodiccnt && periodics [0]->at <= ev_now)
511 { 546 {
512 struct ev_periodic *w = periodics [0]; 547 struct ev_periodic *w = periodics [0];
513 548
549 } 584 }
550 } 585 }
551} 586}
552 587
553static void 588static void
554time_update () 589time_update (void)
555{ 590{
556 int i; 591 int i;
557 592
558 ev_now = ev_time (); 593 ev_now = ev_time ();
559 594
593int ev_loop_done; 628int ev_loop_done;
594 629
595void ev_loop (int flags) 630void ev_loop (int flags)
596{ 631{
597 double block; 632 double block;
598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
599 634
600 do 635 do
601 { 636 {
602 /* queue check watchers (and execute them) */ 637 /* queue check watchers (and execute them) */
603 if (preparecnt) 638 if (preparecnt)
719 754
720 ev_start ((W)w, 1); 755 ev_start ((W)w, 1);
721 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
722 wlist_add ((WL *)&anfds[fd].head, (WL)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
723 758
724 ++fdchangecnt; 759 fd_change (fd);
725 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
726 fdchanges [fdchangecnt - 1] = fd;
727} 760}
728 761
729void 762void
730evio_stop (struct ev_io *w) 763evio_stop (struct ev_io *w)
731{ 764{
734 return; 767 return;
735 768
736 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
737 ev_stop ((W)w); 770 ev_stop ((W)w);
738 771
739 ++fdchangecnt; 772 fd_change (w->fd);
740 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
741 fdchanges [fdchangecnt - 1] = w->fd;
742} 773}
743 774
744void 775void
745evtimer_start (struct ev_timer *w) 776evtimer_start (struct ev_timer *w)
746{ 777{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines