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

Comparing libev/ev.c (file contents):
Revision 1.6 by root, Tue Oct 30 23:55:29 2007 UTC vs.
Revision 1.7 by root, Wed Oct 31 00:24:16 2007 UTC

1#include <math.h> 1#include <math.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <signal.h>
3 6
4#include <stdio.h> 7#include <stdio.h>
5 8
6#include <assert.h> 9#include <assert.h>
7#include <errno.h> 10#include <errno.h>
179 182
180 timers [k] = w; 183 timers [k] = w;
181 timers [k]->active = k + 1; 184 timers [k]->active = k + 1;
182} 185}
183 186
184static struct ev_signal **signals; 187typedef struct
188{
189 struct ev_signal *head;
190 sig_atomic_t gotsig;
191} ANSIG;
192
193static ANSIG *signals;
185static int signalmax; 194static int signalmax;
186 195
196static int sigpipe [2];
197static sig_atomic_t gotsig;
198static struct ev_io sigev;
199
187static void 200static void
188signals_init (struct ev_signal **base, int count) 201signals_init (ANSIG *base, int count)
189{ 202{
190 while (count--) 203 while (count--)
191 *base++ = 0; 204 {
205 base->head = 0;
206 base->gotsig = 0;
207 ++base;
208 }
209}
210
211static void
212sighandler (int signum)
213{
214 signals [signum - 1].gotsig = 1;
215
216 if (!gotsig)
217 {
218 gotsig = 1;
219 write (sigpipe [1], &gotsig, 1);
220 }
221}
222
223static void
224sigcb (struct ev_io *iow, int revents)
225{
226 struct ev_signal *w;
227 int sig;
228
229 gotsig = 0;
230 read (sigpipe [0], &revents, 1);
231
232 for (sig = signalmax; sig--; )
233 if (signals [sig].gotsig)
234 {
235 signals [sig].gotsig = 0;
236
237 for (w = signals [sig].head; w; w = w->next)
238 event ((struct ev_watcher *)w, EV_SIGNAL);
239 }
240}
241
242static void
243siginit (void)
244{
245 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
246 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
247
248 /* rather than sort out wether we really need nb, set it */
249 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
250 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
251
252 evio_set (&sigev, sigpipe [0], EV_READ);
253 evio_start (&sigev);
192} 254}
193 255
194#if HAVE_EPOLL 256#if HAVE_EPOLL
195# include "ev_epoll.c" 257# include "ev_epoll.c"
196#endif 258#endif
210 272
211 ev_now = ev_time (); 273 ev_now = ev_time ();
212 now = get_clock (); 274 now = get_clock ();
213 diff = ev_now - now; 275 diff = ev_now - now;
214 276
277 if (pipe (sigpipe))
278 return 0;
279
280 ev_method = EVMETHOD_NONE;
215#if HAVE_EPOLL 281#if HAVE_EPOLL
216 if (epoll_init (flags)) 282 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
217 return ev_method;
218#endif 283#endif
219#if HAVE_SELECT 284#if HAVE_SELECT
220 if (select_init (flags)) 285 if (ev_method == EVMETHOD_NONE) select_init (flags);
221 return ev_method;
222#endif 286#endif
223 287
224 ev_method = EVMETHOD_NONE; 288 if (ev_method)
289 {
290 evw_init (&sigev, sigcb, 0);
291 siginit ();
292 }
293
225 return ev_method; 294 return ev_method;
226} 295}
227 296
228void ev_prefork (void) 297void ev_prefork (void)
229{ 298{
237{ 306{
238#if HAVE_EPOLL 307#if HAVE_EPOLL
239 if (ev_method == EVMETHOD_EPOLL) 308 if (ev_method == EVMETHOD_EPOLL)
240 epoll_postfork_child (); 309 epoll_postfork_child ();
241#endif 310#endif
311
312 evio_stop (&sigev);
313 close (sigpipe [0]);
314 close (sigpipe [1]);
315 pipe (sigpipe);
316 siginit ();
242} 317}
243 318
244static void 319static void
245fd_reify (void) 320fd_reify (void)
246{ 321{
536 if (ev_is_active (w)) 611 if (ev_is_active (w))
537 return; 612 return;
538 613
539 ev_start ((struct ev_watcher *)w, 1); 614 ev_start ((struct ev_watcher *)w, 1);
540 array_needsize (signals, signalmax, w->signum, signals_init); 615 array_needsize (signals, signalmax, w->signum, signals_init);
541 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 616 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w);
617
618 if (!w->next)
619 {
620 struct sigaction sa;
621 sa.sa_handler = sighandler;
622 sigfillset (&sa.sa_mask);
623 sa.sa_flags = 0;
624 sigaction (w->signum, &sa, 0);
625 }
542} 626}
543 627
544void 628void
545evsignal_stop (struct ev_signal *w) 629evsignal_stop (struct ev_signal *w)
546{ 630{
547 if (!ev_is_active (w)) 631 if (!ev_is_active (w))
548 return; 632 return;
549 633
550 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w);
551 ev_stop ((struct ev_watcher *)w); 635 ev_stop ((struct ev_watcher *)w);
636
637 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL);
552} 639}
553 640
554/*****************************************************************************/ 641/*****************************************************************************/
555#if 1 642#if 1
556 643
566 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 653 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
567 evtimer_stop (w); 654 evtimer_stop (w);
568 evtimer_start (w); 655 evtimer_start (w);
569} 656}
570 657
658static void
659scb (struct ev_signal *w, int revents)
660{
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
662}
663
571int main (void) 664int main (void)
572{ 665{
573 struct ev_io sin; 666 struct ev_io sin;
574 667
575 ev_init (0); 668 ev_init (0);
578 evio_set (&sin, 0, EV_READ); 671 evio_set (&sin, 0, EV_READ);
579 evio_start (&sin); 672 evio_start (&sin);
580 673
581 struct ev_timer t[10000]; 674 struct ev_timer t[10000];
582 675
583#if 1 676#if 0
584 int i; 677 int i;
585 for (i = 0; i < 10000; ++i) 678 for (i = 0; i < 10000; ++i)
586 { 679 {
587 struct ev_timer *w = t + i; 680 struct ev_timer *w = t + i;
588 evw_init (w, ocb, i); 681 evw_init (w, ocb, i);
596 struct ev_timer t1; 689 struct ev_timer t1;
597 evw_init (&t1, ocb, 0); 690 evw_init (&t1, ocb, 0);
598 evtimer_set_abs (&t1, 5, 10); 691 evtimer_set_abs (&t1, 5, 10);
599 evtimer_start (&t1); 692 evtimer_start (&t1);
600 693
694 struct ev_signal sig;
695 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT);
697 evsignal_start (&sig);
698
601 ev_loop (0); 699 ev_loop (0);
602 700
603 return 0; 701 return 0;
604} 702}
605 703

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines