ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.5
Committed: Tue Oct 30 23:54:38 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +31 -4 lines
Log Message:
implement select method

File Contents

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