ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
Revision: 1.2
Committed: Tue Oct 30 21:42:12 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +29 -20 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <math.h>
2 #include <stdlib.h>
3
4 #include <stdio.h>
5
6 #include <errno.h>
7 #include <sys/time.h>
8 #include <time.h>
9
10 #ifdef CLOCK_MONOTONIC
11 # define HAVE_MONOTONIC 1
12 #endif
13
14 #define HAVE_EPOLL 1
15 #define HAVE_REALTIME 1
16 #define HAVE_SELECT 0
17
18 #define MAX_BLOCKTIME 60.
19
20 #include "ev.h"
21
22 struct ev_watcher {
23 EV_WATCHER (ev_watcher);
24 };
25
26 struct ev_watcher_list {
27 EV_WATCHER_LIST (ev_watcher_list);
28 };
29
30 ev_tstamp ev_now;
31 int ev_method;
32
33 static int have_monotonic; /* runtime */
34
35 static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
36 static void (*method_reify)(void);
37 static void (*method_poll)(ev_tstamp timeout);
38
39 ev_tstamp
40 ev_time (void)
41 {
42 #if HAVE_REALTIME
43 struct timespec ts;
44 clock_gettime (CLOCK_REALTIME, &ts);
45 return ts.tv_sec + ts.tv_nsec * 1e-9;
46 #else
47 struct timeval tv;
48 gettimeofday (&tv, 0);
49 return tv.tv_sec + tv.tv_usec * 1e-6;
50 #endif
51 }
52
53 static ev_tstamp
54 get_clock (void)
55 {
56 #if HAVE_MONOTONIC
57 if (have_monotonic)
58 {
59 struct timespec ts;
60 clock_gettime (CLOCK_MONOTONIC, &ts);
61 return ts.tv_sec + ts.tv_nsec * 1e-9;
62 }
63 #endif
64
65 return ev_time ();
66 }
67
68 #define array_needsize(base,cur,cnt,init) \
69 if ((cnt) > cur) \
70 { \
71 int newcnt = cur ? cur << 1 : 16; \
72 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
73 base = realloc (base, sizeof (*base) * (newcnt)); \
74 init (base + cur, newcnt - cur); \
75 cur = newcnt; \
76 }
77
78 typedef struct
79 {
80 struct ev_io *head;
81 unsigned char wev, rev; /* want, received event set */
82 } ANFD;
83
84 static ANFD *anfds;
85 static int anfdmax;
86
87 static int *fdchanges;
88 static int fdchangemax, fdchangecnt;
89
90 static void
91 anfds_init (ANFD *base, int count)
92 {
93 while (count--)
94 {
95 base->head = 0;
96 base->wev = base->rev = EV_NONE;
97 ++base;
98 }
99 }
100
101 typedef struct
102 {
103 struct ev_watcher *w;
104 int events;
105 } ANPENDING;
106
107 static ANPENDING *pendings;
108 static int pendingmax, pendingcnt;
109
110 static void
111 event (struct ev_watcher *w, int events)
112 {
113 w->pending = ++pendingcnt;
114 array_needsize (pendings, pendingmax, pendingcnt, );
115 pendings [pendingcnt - 1].w = w;
116 pendings [pendingcnt - 1].events = events;
117 }
118
119 static void
120 fd_event (int fd, int events)
121 {
122 ANFD *anfd = anfds + fd;
123 struct ev_io *w;
124
125 for (w = anfd->head; w; w = w->next)
126 {
127 int ev = w->events & events;
128
129 if (ev)
130 event ((struct ev_watcher *)w, ev);
131 }
132 }
133
134 static struct ev_timer **timers;
135 static int timermax, timercnt;
136
137 static void
138 upheap (int k)
139 {
140 struct ev_timer *w = timers [k];
141
142 while (k && timers [k >> 1]->at > w->at)
143 {
144 timers [k] = timers [k >> 1];
145 timers [k]->active = k + 1;
146 k >>= 1;
147 }
148
149 timers [k] = w;
150 timers [k]->active = k + 1;
151
152 }
153
154 static void
155 downheap (int k)
156 {
157 struct ev_timer *w = timers [k];
158
159 while (k < (timercnt >> 1))
160 {
161 int j = k << 1;
162
163 if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at)
164 ++j;
165
166 if (w->at <= timers [j]->at)
167 break;
168
169 timers [k] = timers [j];
170 timers [k]->active = k + 1;
171 k = j;
172 }
173
174 timers [k] = w;
175 timers [k]->active = k + 1;
176 }
177
178 static struct ev_signal **signals;
179 static int signalmax, signalcnt;
180
181 static void
182 signals_init (struct ev_signal **base, int count)
183 {
184 while (count--)
185 *base++ = 0;
186 }
187
188 #if HAVE_EPOLL
189 # include "ev_epoll.c"
190 #endif
191 #if HAVE_SELECT
192 # include "ev_select.c"
193 #endif
194
195 int ev_init (int flags)
196 {
197 #if HAVE_MONOTONIC
198 {
199 struct timespec ts;
200 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
201 have_monotonic = 1;
202 }
203 #endif
204
205 ev_now = ev_time ();
206
207 #if HAVE_EPOLL
208 if (epoll_init (flags))
209 return ev_method;
210 #endif
211 #if HAVE_SELECT
212 if (select_init (flags))
213 return ev_method;
214 #endif
215
216 ev_method = EVMETHOD_NONE;
217 return ev_method;
218 }
219
220 void ev_prefork (void)
221 {
222 }
223
224 void ev_postfork_parent (void)
225 {
226 }
227
228 void ev_postfork_child (void)
229 {
230 #if HAVE_EPOLL
231 epoll_postfork_child ();
232 #endif
233 }
234
235 static void
236 call_pending ()
237 {
238 int i;
239
240 for (i = 0; i < pendingcnt; ++i)
241 {
242 ANPENDING *p = pendings + i;
243
244 if (p->w)
245 {
246 p->w->pending = 0;
247 p->w->cb (p->w, p->events);
248 }
249 }
250
251 pendingcnt = 0;
252 }
253
254 static void
255 timer_reify (void)
256 {
257 while (timercnt && timers [0]->at <= ev_now)
258 {
259 struct ev_timer *w = timers [0];
260
261 fprintf (stderr, "0 %f, %d c%d\n", w->at, w->active, timercnt);//D
262 /* first reschedule timer */
263 if (w->repeat)
264 {
265 fprintf (stderr, "a %f now %f repeat %f, %f\n", w->at, ev_now, w->repeat, w->repeat *1e30);//D
266 if (w->is_abs)
267 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
268 else
269 w->at = ev_now + w->repeat;
270
271 fprintf (stderr, "b %f\n", w->at);//D
272
273 downheap (0);
274 }
275 else
276 {
277 fprintf (stderr, "c %f, %d c%d\n", w->at, w->active, timercnt);//D
278 evtimer_stop (w); /* nonrepeating: stop timer */
279 }
280
281 event ((struct ev_watcher *)w, EV_TIMEOUT);
282 }
283 }
284
285 int ev_loop_done;
286
287 int ev_loop (int flags)
288 {
289 double block;
290 ev_loop_done = flags & EVLOOP_ONESHOT;
291
292 do
293 {
294 /* update fd-related kernel structures */
295 method_reify (); fdchangecnt = 0;
296
297 /* calculate blocking time */
298 ev_now = ev_time ();
299
300 if (flags & EVLOOP_NONBLOCK)
301 block = 0.;
302 else if (!timercnt)
303 block = MAX_BLOCKTIME;
304 else
305 {
306 block = timers [0]->at - ev_now + method_fudge;
307 if (block < 0.) block = 0.;
308 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
309 }
310
311 fprintf (stderr, "block %f\n", block);//D
312 method_poll (block);
313
314 /* put pending timers into pendign queue and reschedule them */
315 timer_reify ();
316
317 ev_now = ev_time ();
318 call_pending ();
319 }
320 while (!ev_loop_done);
321 }
322
323 static void
324 wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem)
325 {
326 elem->next = *head;
327 *head = elem;
328 }
329
330 static void
331 wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem)
332 {
333 while (*head)
334 {
335 if (*head == elem)
336 {
337 *head = elem->next;
338 return;
339 }
340
341 head = &(*head)->next;
342 }
343 }
344
345 static void
346 ev_start (struct ev_watcher *w, int active)
347 {
348 w->pending = 0;
349 w->active = active;
350 }
351
352 static void
353 ev_stop (struct ev_watcher *w)
354 {
355 if (w->pending)
356 pendings [w->pending - 1].w = 0;
357
358 w->active = 0;
359 /* nop */
360 }
361
362 void
363 evio_start (struct ev_io *w)
364 {
365 if (ev_is_active (w))
366 return;
367
368 int fd = w->fd;
369
370 ev_start ((struct ev_watcher *)w, 1);
371 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
372 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w);
373
374 ++fdchangecnt;
375 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
376 fdchanges [fdchangecnt - 1] = fd;
377 }
378
379 void
380 evio_stop (struct ev_io *w)
381 {
382 if (!ev_is_active (w))
383 return;
384
385 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w);
386 ev_stop ((struct ev_watcher *)w);
387
388 ++fdchangecnt;
389 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
390 fdchanges [fdchangecnt - 1] = w->fd;
391 }
392
393 void
394 evtimer_start (struct ev_timer *w)
395 {
396 if (ev_is_active (w))
397 return;
398
399 fprintf (stderr, "t1 %f a %d\n", w->at, w->is_abs);//D
400 if (w->is_abs)
401 {
402 /* this formula differs from the one in timer_reify becuse we do not round up */
403 if (w->repeat)
404 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
405 }
406 else
407 w->at += ev_now;
408 fprintf (stderr, "t2 %f a %d\n", w->at, w->is_abs);//D
409
410 ev_start ((struct ev_watcher *)w, ++timercnt);
411 array_needsize (timers, timermax, timercnt, );
412 timers [timercnt - 1] = w;
413 upheap (timercnt - 1);
414 }
415
416 void
417 evtimer_stop (struct ev_timer *w)
418 {
419 fprintf (stderr, "-topping %d, %d\n", w->active, timercnt);//D
420 if (!ev_is_active (w))
421 return;
422
423 fprintf (stderr, "stopping %d, %d\n", w->active, timercnt);//D
424 if (w->active < timercnt)
425 {
426 timers [w->active - 1] = timers [--timercnt];
427 downheap (w->active - 1);
428 }
429
430 ev_stop ((struct ev_watcher *)w);
431 }
432
433 void
434 evsignal_start (struct ev_signal *w)
435 {
436 if (ev_is_active (w))
437 return;
438
439 ev_start ((struct ev_watcher *)w, 1);
440 array_needsize (signals, signalmax, w->signum, signals_init);
441 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
442 }
443
444 void
445 evsignal_stop (struct ev_signal *w)
446 {
447 if (!ev_is_active (w))
448 return;
449
450 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w);
451 ev_stop ((struct ev_watcher *)w);
452 }
453
454 /*****************************************************************************/
455 #if 1
456
457 static void
458 sin_cb (struct ev_io *w, int revents)
459 {
460 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
461 }
462
463 static void
464 ocb (struct ev_timer *w, int revents)
465 {
466 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
467 }
468
469 int main (void)
470 {
471 struct ev_io sin;
472
473 ev_init (0);
474
475 evw_init (&sin, sin_cb, 55);
476 evio_set (&sin, 0, EV_READ);
477 evio_start (&sin);
478
479 struct ev_timer t[1000];
480
481 int i;
482 for (i = 0; i < 1000; ++i)
483 {
484 struct ev_timer *w = t + i;
485 evw_init (w, ocb, i);
486 evtimer_set_rel (w, drand48 (), 0);
487 evtimer_start (w);
488 if (drand48 () < 0.5)
489 evtimer_stop (w);
490 }
491
492 ev_loop (0);
493
494 return 0;
495 }
496
497 #endif
498
499
500
501