ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.4
Committed: Tue Oct 30 23:10:33 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.3: +129 -35 lines
Log Message:
much better

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 0
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_reify)(void);
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 epoll_postfork_child ();
240 #endif
241 }
242
243 static void
244 call_pending ()
245 {
246 int i;
247
248 for (i = 0; i < pendingcnt; ++i)
249 {
250 ANPENDING *p = pendings + i;
251
252 if (p->w)
253 {
254 p->w->pending = 0;
255 p->w->cb (p->w, p->events);
256 }
257 }
258
259 pendingcnt = 0;
260 }
261
262 static void
263 timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now)
264 {
265 while (timercnt && timers [0]->at <= now)
266 {
267 struct ev_timer *w = timers [0];
268
269 /* first reschedule or stop timer */
270 if (w->repeat)
271 {
272 if (w->is_abs)
273 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
274 else
275 w->at = now + w->repeat;
276
277 assert (w->at > now);
278
279 downheap (timers, timercnt, 0);
280 }
281 else
282 {
283 evtimer_stop (w); /* nonrepeating: stop timer */
284 --timercnt; /* maybe pass by reference instead? */
285 }
286
287 event ((struct ev_watcher *)w, EV_TIMEOUT);
288 }
289 }
290
291 static void
292 time_update ()
293 {
294 int i;
295 ev_now = ev_time ();
296
297 if (have_monotonic)
298 {
299 ev_tstamp odiff = diff;
300
301 /* detecting time jumps is much more difficult */
302 for (i = 2; --i; ) /* loop a few times, before making important decisions */
303 {
304 now = get_clock ();
305 diff = ev_now - now;
306
307 if (fabs (odiff - diff) < MIN_TIMEJUMP)
308 return; /* all is well */
309
310 ev_now = ev_time ();
311 }
312
313 /* time jump detected, reschedule atimers */
314 for (i = 0; i < atimercnt; ++i)
315 {
316 struct ev_timer *w = atimers [i];
317 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
318 }
319 }
320 else
321 {
322 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
323 /* time jump detected, adjust rtimers */
324 for (i = 0; i < rtimercnt; ++i)
325 rtimers [i]->at += ev_now - now;
326
327 now = ev_now;
328 }
329 }
330
331 int ev_loop_done;
332
333 void ev_loop (int flags)
334 {
335 double block;
336 ev_loop_done = flags & EVLOOP_ONESHOT;
337
338 do
339 {
340 /* update fd-related kernel structures */
341 method_reify (); fdchangecnt = 0;
342
343 /* calculate blocking time */
344 if (flags & EVLOOP_NONBLOCK)
345 block = 0.;
346 else
347 {
348 block = MAX_BLOCKTIME;
349
350 if (rtimercnt)
351 {
352 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge;
353 if (block > to) block = to;
354 }
355
356 if (atimercnt)
357 {
358 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge;
359 if (block > to) block = to;
360 }
361
362 if (block < 0.) block = 0.;
363 }
364
365 method_poll (block);
366
367 /* update ev_now, do magic */
368 time_update ();
369
370 /* put pending timers into pendign queue and reschedule them */
371 /* absolute timers first */
372 timers_reify (atimers, atimercnt, ev_now);
373 /* relative timers second */
374 timers_reify (rtimers, rtimercnt, now);
375
376 call_pending ();
377 }
378 while (!ev_loop_done);
379 }
380
381 static void
382 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
383 {
384 elem->next = *head;
385 *head = elem;
386 }
387
388 static void
389 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
390 {
391 while (*head)
392 {
393 if (*head == elem)
394 {
395 *head = elem->next;
396 return;
397 }
398
399 head = &(*head)->next;
400 }
401 }
402
403 static void
404 ev_start (struct ev_watcher *w, int active)
405 {
406 w->pending = 0;
407 w->active = active;
408 }
409
410 static void
411 ev_stop (struct ev_watcher *w)
412 {
413 if (w->pending)
414 pendings [w->pending - 1].w = 0;
415
416 w->active = 0;
417 /* nop */
418 }
419
420 void
421 evio_start (struct ev_io *w)
422 {
423 if (ev_is_active (w))
424 return;
425
426 int fd = w->fd;
427
428 ev_start ((struct ev_watcher *)w, 1);
429 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
430 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
431
432 ++fdchangecnt;
433 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
434 fdchanges [fdchangecnt - 1] = fd;
435 }
436
437 void
438 evio_stop (struct ev_io *w)
439 {
440 if (!ev_is_active (w))
441 return;
442
443 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
444 ev_stop ((struct ev_watcher *)w);
445
446 ++fdchangecnt;
447 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
448 fdchanges [fdchangecnt - 1] = w->fd;
449 }
450
451 void
452 evtimer_start (struct ev_timer *w)
453 {
454 if (ev_is_active (w))
455 return;
456
457 if (w->is_abs)
458 {
459 /* this formula differs from the one in timer_reify becuse we do not round up */
460 if (w->repeat)
461 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
462
463 ev_start ((struct ev_watcher *)w, ++atimercnt);
464 array_needsize (atimers, atimermax, atimercnt, );
465 atimers [atimercnt - 1] = w;
466 upheap (atimers, atimercnt - 1);
467 }
468 else
469 {
470 w->at += now;
471
472 ev_start ((struct ev_watcher *)w, ++rtimercnt);
473 array_needsize (rtimers, rtimermax, rtimercnt, );
474 rtimers [rtimercnt - 1] = w;
475 upheap (rtimers, rtimercnt - 1);
476 }
477
478 }
479
480 void
481 evtimer_stop (struct ev_timer *w)
482 {
483 if (!ev_is_active (w))
484 return;
485
486 if (w->is_abs)
487 {
488 if (w->active < atimercnt--)
489 {
490 atimers [w->active - 1] = atimers [atimercnt];
491 downheap (atimers, atimercnt, w->active - 1);
492 }
493 }
494 else
495 {
496 if (w->active < rtimercnt--)
497 {
498 rtimers [w->active - 1] = rtimers [rtimercnt];
499 downheap (rtimers, rtimercnt, w->active - 1);
500 }
501 }
502
503 ev_stop ((struct ev_watcher *)w);
504 }
505
506 void
507 evsignal_start (struct ev_signal *w)
508 {
509 if (ev_is_active (w))
510 return;
511
512 ev_start ((struct ev_watcher *)w, 1);
513 array_needsize (signals, signalmax, w->signum, signals_init);
514 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
515 }
516
517 void
518 evsignal_stop (struct ev_signal *w)
519 {
520 if (!ev_is_active (w))
521 return;
522
523 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
524 ev_stop ((struct ev_watcher *)w);
525 }
526
527 /*****************************************************************************/
528 #if 1
529
530 static void
531 sin_cb (struct ev_io *w, int revents)
532 {
533 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
534 }
535
536 static void
537 ocb (struct ev_timer *w, int revents)
538 {
539 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
540 evtimer_stop (w);
541 evtimer_start (w);
542 }
543
544 int main (void)
545 {
546 struct ev_io sin;
547
548 ev_init (0);
549
550 evw_init (&sin, sin_cb, 55);
551 evio_set (&sin, 0, EV_READ);
552 evio_start (&sin);
553
554 struct ev_timer t[10000];
555
556 #if 1
557 int i;
558 for (i = 0; i < 10000; ++i)
559 {
560 struct ev_timer *w = t + i;
561 evw_init (w, ocb, i);
562 evtimer_set_abs (w, drand48 (), 0.99775533);
563 evtimer_start (w);
564 if (drand48 () < 0.5)
565 evtimer_stop (w);
566 }
567 #endif
568
569 struct ev_timer t1;
570 evw_init (&t1, ocb, 0);
571 evtimer_set_abs (&t1, 5, 10);
572 evtimer_start (&t1);
573
574 ev_loop (0);
575
576 return 0;
577 }
578
579 #endif
580
581
582
583