ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.7
Committed: Wed Oct 31 00:24:16 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.6: +109 -11 lines
Log Message:
signal support

File Contents

# Content
1 #include <math.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <signal.h>
6
7 #include <stdio.h>
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <sys/time.h>
12 #include <time.h>
13
14 #ifdef CLOCK_MONOTONIC
15 # define HAVE_MONOTONIC 1
16 #endif
17
18 #define HAVE_REALTIME 1
19 #define HAVE_EPOLL 1
20 #define HAVE_SELECT 1
21
22 #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23 #define MAX_BLOCKTIME 60.
24
25 #include "ev.h"
26
27 struct ev_watcher {
28 EV_WATCHER (ev_watcher);
29 };
30
31 struct ev_watcher_list {
32 EV_WATCHER_LIST (ev_watcher_list);
33 };
34
35 static ev_tstamp now, diff; /* monotonic clock */
36 ev_tstamp ev_now;
37 int ev_method;
38
39 static int have_monotonic; /* runtime */
40
41 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42 static void (*method_modify)(int fd, int oev, int nev);
43 static void (*method_poll)(ev_tstamp timeout);
44
45 ev_tstamp
46 ev_time (void)
47 {
48 #if HAVE_REALTIME
49 struct timespec ts;
50 clock_gettime (CLOCK_REALTIME, &ts);
51 return ts.tv_sec + ts.tv_nsec * 1e-9;
52 #else
53 struct timeval tv;
54 gettimeofday (&tv, 0);
55 return tv.tv_sec + tv.tv_usec * 1e-6;
56 #endif
57 }
58
59 static ev_tstamp
60 get_clock (void)
61 {
62 #if HAVE_MONOTONIC
63 if (have_monotonic)
64 {
65 struct timespec ts;
66 clock_gettime (CLOCK_MONOTONIC, &ts);
67 return ts.tv_sec + ts.tv_nsec * 1e-9;
68 }
69 #endif
70
71 return ev_time ();
72 }
73
74 #define array_needsize(base,cur,cnt,init) \
75 if ((cnt) > cur) \
76 { \
77 int newcnt = cur ? cur << 1 : 16; \
78 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
79 base = realloc (base, sizeof (*base) * (newcnt)); \
80 init (base + cur, newcnt - cur); \
81 cur = newcnt; \
82 }
83
84 typedef struct
85 {
86 struct ev_io *head;
87 unsigned char wev, rev; /* want, received event set */
88 } ANFD;
89
90 static ANFD *anfds;
91 static int anfdmax;
92
93 static int *fdchanges;
94 static int fdchangemax, fdchangecnt;
95
96 static void
97 anfds_init (ANFD *base, int count)
98 {
99 while (count--)
100 {
101 base->head = 0;
102 base->wev = base->rev = EV_NONE;
103 ++base;
104 }
105 }
106
107 typedef struct
108 {
109 struct ev_watcher *w;
110 int events;
111 } ANPENDING;
112
113 static ANPENDING *pendings;
114 static int pendingmax, pendingcnt;
115
116 static void
117 event (struct ev_watcher *w, int events)
118 {
119 w->pending = ++pendingcnt;
120 array_needsize (pendings, pendingmax, pendingcnt, );
121 pendings [pendingcnt - 1].w = w;
122 pendings [pendingcnt - 1].events = events;
123 }
124
125 static void
126 fd_event (int fd, int events)
127 {
128 ANFD *anfd = anfds + fd;
129 struct ev_io *w;
130
131 for (w = anfd->head; w; w = w->next)
132 {
133 int ev = w->events & events;
134
135 if (ev)
136 event ((struct ev_watcher *)w, ev);
137 }
138 }
139
140 static struct ev_timer **atimers;
141 static int atimermax, atimercnt;
142
143 static struct ev_timer **rtimers;
144 static int rtimermax, rtimercnt;
145
146 static void
147 upheap (struct ev_timer **timers, int k)
148 {
149 struct ev_timer *w = timers [k];
150
151 while (k && timers [k >> 1]->at > w->at)
152 {
153 timers [k] = timers [k >> 1];
154 timers [k]->active = k + 1;
155 k >>= 1;
156 }
157
158 timers [k] = w;
159 timers [k]->active = k + 1;
160
161 }
162
163 static void
164 downheap (struct ev_timer **timers, int N, int k)
165 {
166 struct ev_timer *w = timers [k];
167
168 while (k < (N >> 1))
169 {
170 int j = k << 1;
171
172 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
173 ++j;
174
175 if (w->at <= timers [j]->at)
176 break;
177
178 timers [k] = timers [j];
179 timers [k]->active = k + 1;
180 k = j;
181 }
182
183 timers [k] = w;
184 timers [k]->active = k + 1;
185 }
186
187 typedef struct
188 {
189 struct ev_signal *head;
190 sig_atomic_t gotsig;
191 } ANSIG;
192
193 static ANSIG *signals;
194 static int signalmax;
195
196 static int sigpipe [2];
197 static sig_atomic_t gotsig;
198 static struct ev_io sigev;
199
200 static void
201 signals_init (ANSIG *base, int count)
202 {
203 while (count--)
204 {
205 base->head = 0;
206 base->gotsig = 0;
207 ++base;
208 }
209 }
210
211 static void
212 sighandler (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
223 static void
224 sigcb (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
242 static void
243 siginit (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);
254 }
255
256 #if HAVE_EPOLL
257 # include "ev_epoll.c"
258 #endif
259 #if HAVE_SELECT
260 # include "ev_select.c"
261 #endif
262
263 int ev_init (int flags)
264 {
265 #if HAVE_MONOTONIC
266 {
267 struct timespec ts;
268 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
269 have_monotonic = 1;
270 }
271 #endif
272
273 ev_now = ev_time ();
274 now = get_clock ();
275 diff = ev_now - now;
276
277 if (pipe (sigpipe))
278 return 0;
279
280 ev_method = EVMETHOD_NONE;
281 #if HAVE_EPOLL
282 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
283 #endif
284 #if HAVE_SELECT
285 if (ev_method == EVMETHOD_NONE) select_init (flags);
286 #endif
287
288 if (ev_method)
289 {
290 evw_init (&sigev, sigcb, 0);
291 siginit ();
292 }
293
294 return ev_method;
295 }
296
297 void ev_prefork (void)
298 {
299 }
300
301 void ev_postfork_parent (void)
302 {
303 }
304
305 void ev_postfork_child (void)
306 {
307 #if HAVE_EPOLL
308 if (ev_method == EVMETHOD_EPOLL)
309 epoll_postfork_child ();
310 #endif
311
312 evio_stop (&sigev);
313 close (sigpipe [0]);
314 close (sigpipe [1]);
315 pipe (sigpipe);
316 siginit ();
317 }
318
319 static void
320 fd_reify (void)
321 {
322 int i;
323
324 for (i = 0; i < fdchangecnt; ++i)
325 {
326 int fd = fdchanges [i];
327 ANFD *anfd = anfds + fd;
328 struct ev_io *w;
329
330 int wev = 0;
331
332 for (w = anfd->head; w; w = w->next)
333 wev |= w->events;
334
335 if (anfd->wev != wev)
336 {
337 method_modify (fd, anfd->wev, wev);
338 anfd->wev = wev;
339 }
340 }
341
342 fdchangecnt = 0;
343 }
344
345 static void
346 call_pending ()
347 {
348 int i;
349
350 for (i = 0; i < pendingcnt; ++i)
351 {
352 ANPENDING *p = pendings + i;
353
354 if (p->w)
355 {
356 p->w->pending = 0;
357 p->w->cb (p->w, p->events);
358 }
359 }
360
361 pendingcnt = 0;
362 }
363
364 static void
365 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
366 {
367 while (timercnt && timers [0]->at <= now)
368 {
369 struct ev_timer *w = timers [0];
370
371 /* first reschedule or stop timer */
372 if (w->repeat)
373 {
374 if (w->is_abs)
375 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
376 else
377 w->at = now + w->repeat;
378
379 assert (w->at > now);
380
381 downheap (timers, timercnt, 0);
382 }
383 else
384 {
385 evtimer_stop (w); /* nonrepeating: stop timer */
386 --timercnt; /* maybe pass by reference instead? */
387 }
388
389 event ((struct ev_watcher *)w, EV_TIMEOUT);
390 }
391 }
392
393 static void
394 time_update ()
395 {
396 int i;
397 ev_now = ev_time ();
398
399 if (have_monotonic)
400 {
401 ev_tstamp odiff = diff;
402
403 /* detecting time jumps is much more difficult */
404 for (i = 2; --i; ) /* loop a few times, before making important decisions */
405 {
406 now = get_clock ();
407 diff = ev_now - now;
408
409 if (fabs (odiff - diff) < MIN_TIMEJUMP)
410 return; /* all is well */
411
412 ev_now = ev_time ();
413 }
414
415 /* time jump detected, reschedule atimers */
416 for (i = 0; i < atimercnt; ++i)
417 {
418 struct ev_timer *w = atimers [i];
419 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
420 }
421 }
422 else
423 {
424 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
425 /* time jump detected, adjust rtimers */
426 for (i = 0; i < rtimercnt; ++i)
427 rtimers [i]->at += ev_now - now;
428
429 now = ev_now;
430 }
431 }
432
433 int ev_loop_done;
434
435 void ev_loop (int flags)
436 {
437 double block;
438 ev_loop_done = flags & EVLOOP_ONESHOT;
439
440 do
441 {
442 /* update fd-related kernel structures */
443 fd_reify ();
444
445 /* calculate blocking time */
446 if (flags & EVLOOP_NONBLOCK)
447 block = 0.;
448 else
449 {
450 block = MAX_BLOCKTIME;
451
452 if (rtimercnt)
453 {
454 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge;
455 if (block > to) block = to;
456 }
457
458 if (atimercnt)
459 {
460 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge;
461 if (block > to) block = to;
462 }
463
464 if (block < 0.) block = 0.;
465 }
466
467 method_poll (block);
468
469 /* update ev_now, do magic */
470 time_update ();
471
472 /* put pending timers into pendign queue and reschedule them */
473 /* absolute timers first */
474 timers_reify (atimers, atimercnt, ev_now);
475 /* relative timers second */
476 timers_reify (rtimers, rtimercnt, now);
477
478 call_pending ();
479 }
480 while (!ev_loop_done);
481 }
482
483 static void
484 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
485 {
486 elem->next = *head;
487 *head = elem;
488 }
489
490 static void
491 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
492 {
493 while (*head)
494 {
495 if (*head == elem)
496 {
497 *head = elem->next;
498 return;
499 }
500
501 head = &(*head)->next;
502 }
503 }
504
505 static void
506 ev_start (struct ev_watcher *w, int active)
507 {
508 w->pending = 0;
509 w->active = active;
510 }
511
512 static void
513 ev_stop (struct ev_watcher *w)
514 {
515 if (w->pending)
516 pendings [w->pending - 1].w = 0;
517
518 w->active = 0;
519 /* nop */
520 }
521
522 void
523 evio_start (struct ev_io *w)
524 {
525 if (ev_is_active (w))
526 return;
527
528 int fd = w->fd;
529
530 ev_start ((struct ev_watcher *)w, 1);
531 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
532 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
533
534 ++fdchangecnt;
535 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
536 fdchanges [fdchangecnt - 1] = fd;
537 }
538
539 void
540 evio_stop (struct ev_io *w)
541 {
542 if (!ev_is_active (w))
543 return;
544
545 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
546 ev_stop ((struct ev_watcher *)w);
547
548 ++fdchangecnt;
549 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
550 fdchanges [fdchangecnt - 1] = w->fd;
551 }
552
553 void
554 evtimer_start (struct ev_timer *w)
555 {
556 if (ev_is_active (w))
557 return;
558
559 if (w->is_abs)
560 {
561 /* this formula differs from the one in timer_reify becuse we do not round up */
562 if (w->repeat)
563 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
564
565 ev_start ((struct ev_watcher *)w, ++atimercnt);
566 array_needsize (atimers, atimermax, atimercnt, );
567 atimers [atimercnt - 1] = w;
568 upheap (atimers, atimercnt - 1);
569 }
570 else
571 {
572 w->at += now;
573
574 ev_start ((struct ev_watcher *)w, ++rtimercnt);
575 array_needsize (rtimers, rtimermax, rtimercnt, );
576 rtimers [rtimercnt - 1] = w;
577 upheap (rtimers, rtimercnt - 1);
578 }
579
580 }
581
582 void
583 evtimer_stop (struct ev_timer *w)
584 {
585 if (!ev_is_active (w))
586 return;
587
588 if (w->is_abs)
589 {
590 if (w->active < atimercnt--)
591 {
592 atimers [w->active - 1] = atimers [atimercnt];
593 downheap (atimers, atimercnt, w->active - 1);
594 }
595 }
596 else
597 {
598 if (w->active < rtimercnt--)
599 {
600 rtimers [w->active - 1] = rtimers [rtimercnt];
601 downheap (rtimers, rtimercnt, w->active - 1);
602 }
603 }
604
605 ev_stop ((struct ev_watcher *)w);
606 }
607
608 void
609 evsignal_start (struct ev_signal *w)
610 {
611 if (ev_is_active (w))
612 return;
613
614 ev_start ((struct ev_watcher *)w, 1);
615 array_needsize (signals, signalmax, w->signum, signals_init);
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 }
626 }
627
628 void
629 evsignal_stop (struct ev_signal *w)
630 {
631 if (!ev_is_active (w))
632 return;
633
634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w);
635 ev_stop ((struct ev_watcher *)w);
636
637 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL);
639 }
640
641 /*****************************************************************************/
642 #if 1
643
644 static void
645 sin_cb (struct ev_io *w, int revents)
646 {
647 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
648 }
649
650 static void
651 ocb (struct ev_timer *w, int revents)
652 {
653 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
654 evtimer_stop (w);
655 evtimer_start (w);
656 }
657
658 static void
659 scb (struct ev_signal *w, int revents)
660 {
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
662 }
663
664 int main (void)
665 {
666 struct ev_io sin;
667
668 ev_init (0);
669
670 evw_init (&sin, sin_cb, 55);
671 evio_set (&sin, 0, EV_READ);
672 evio_start (&sin);
673
674 struct ev_timer t[10000];
675
676 #if 0
677 int i;
678 for (i = 0; i < 10000; ++i)
679 {
680 struct ev_timer *w = t + i;
681 evw_init (w, ocb, i);
682 evtimer_set_abs (w, drand48 (), 0.99775533);
683 evtimer_start (w);
684 if (drand48 () < 0.5)
685 evtimer_stop (w);
686 }
687 #endif
688
689 struct ev_timer t1;
690 evw_init (&t1, ocb, 0);
691 evtimer_set_abs (&t1, 5, 10);
692 evtimer_start (&t1);
693
694 struct ev_signal sig;
695 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT);
697 evsignal_start (&sig);
698
699 ev_loop (0);
700
701 return 0;
702 }
703
704 #endif
705
706
707
708