| 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 |
/* first reschedule timer */ |
| 262 |
if (w->repeat) |
| 263 |
{ |
| 264 |
if (w->is_abs) |
| 265 |
w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
| 266 |
else |
| 267 |
w->at = ev_now + w->repeat; |
| 268 |
|
| 269 |
downheap (0); |
| 270 |
} |
| 271 |
else |
| 272 |
evtimer_stop (w); /* nonrepeating: stop timer */ |
| 273 |
|
| 274 |
event ((struct ev_watcher *)w, EV_TIMEOUT); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
int ev_loop_done; |
| 279 |
|
| 280 |
int ev_loop (int flags) |
| 281 |
{ |
| 282 |
double block; |
| 283 |
ev_loop_done = flags & EVLOOP_ONESHOT; |
| 284 |
|
| 285 |
do |
| 286 |
{ |
| 287 |
/* update fd-related kernel structures */ |
| 288 |
method_reify (); fdchangecnt = 0; |
| 289 |
|
| 290 |
/* calculate blocking time */ |
| 291 |
ev_now = ev_time (); |
| 292 |
|
| 293 |
if (flags & EVLOOP_NONBLOCK) |
| 294 |
block = 0.; |
| 295 |
else if (!timercnt) |
| 296 |
block = MAX_BLOCKTIME; |
| 297 |
else |
| 298 |
{ |
| 299 |
block = timers [0]->at - ev_now + method_fudge; |
| 300 |
if (block < 0.) block = 0.; |
| 301 |
else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME; |
| 302 |
} |
| 303 |
|
| 304 |
method_poll (block); |
| 305 |
|
| 306 |
/* put pending timers into pendign queue and reschedule them */ |
| 307 |
timer_reify (); |
| 308 |
|
| 309 |
ev_now = ev_time (); |
| 310 |
call_pending (); |
| 311 |
} |
| 312 |
while (!ev_loop_done); |
| 313 |
} |
| 314 |
|
| 315 |
static void |
| 316 |
wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
| 317 |
{ |
| 318 |
elem->next = *head; |
| 319 |
*head = elem; |
| 320 |
} |
| 321 |
|
| 322 |
static void |
| 323 |
wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
| 324 |
{ |
| 325 |
while (*head) |
| 326 |
{ |
| 327 |
if (*head == elem) |
| 328 |
{ |
| 329 |
*head = elem->next; |
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
head = &(*head)->next; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
static void |
| 338 |
ev_start (struct ev_watcher *w, int active) |
| 339 |
{ |
| 340 |
w->pending = 0; |
| 341 |
w->active = active; |
| 342 |
} |
| 343 |
|
| 344 |
static void |
| 345 |
ev_stop (struct ev_watcher *w) |
| 346 |
{ |
| 347 |
if (w->pending) |
| 348 |
pendings [w->pending - 1].w = 0; |
| 349 |
|
| 350 |
w->active = 0; |
| 351 |
/* nop */ |
| 352 |
} |
| 353 |
|
| 354 |
void |
| 355 |
evio_start (struct ev_io *w) |
| 356 |
{ |
| 357 |
if (ev_is_active (w)) |
| 358 |
return; |
| 359 |
|
| 360 |
int fd = w->fd; |
| 361 |
|
| 362 |
ev_start ((struct ev_watcher *)w, 1); |
| 363 |
array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
| 364 |
wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
| 365 |
|
| 366 |
++fdchangecnt; |
| 367 |
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
| 368 |
fdchanges [fdchangecnt - 1] = fd; |
| 369 |
} |
| 370 |
|
| 371 |
void |
| 372 |
evio_stop (struct ev_io *w) |
| 373 |
{ |
| 374 |
if (!ev_is_active (w)) |
| 375 |
return; |
| 376 |
|
| 377 |
wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
| 378 |
ev_stop ((struct ev_watcher *)w); |
| 379 |
|
| 380 |
++fdchangecnt; |
| 381 |
array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
| 382 |
fdchanges [fdchangecnt - 1] = w->fd; |
| 383 |
} |
| 384 |
|
| 385 |
void |
| 386 |
evtimer_start (struct ev_timer *w) |
| 387 |
{ |
| 388 |
if (ev_is_active (w)) |
| 389 |
return; |
| 390 |
|
| 391 |
if (w->is_abs) |
| 392 |
{ |
| 393 |
/* this formula differs from the one in timer_reify becuse we do not round up */ |
| 394 |
if (w->repeat) |
| 395 |
w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
| 396 |
} |
| 397 |
else |
| 398 |
w->at += ev_now; |
| 399 |
|
| 400 |
ev_start ((struct ev_watcher *)w, ++timercnt); |
| 401 |
array_needsize (timers, timermax, timercnt, ); |
| 402 |
timers [timercnt - 1] = w; |
| 403 |
upheap (timercnt - 1); |
| 404 |
} |
| 405 |
|
| 406 |
void |
| 407 |
evtimer_stop (struct ev_timer *w) |
| 408 |
{ |
| 409 |
if (!ev_is_active (w)) |
| 410 |
return; |
| 411 |
|
| 412 |
if (w->active < timercnt--) |
| 413 |
{ |
| 414 |
timers [w->active - 1] = timers [timercnt]; |
| 415 |
downheap (w->active - 1); |
| 416 |
} |
| 417 |
|
| 418 |
ev_stop ((struct ev_watcher *)w); |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
evsignal_start (struct ev_signal *w) |
| 423 |
{ |
| 424 |
if (ev_is_active (w)) |
| 425 |
return; |
| 426 |
|
| 427 |
ev_start ((struct ev_watcher *)w, 1); |
| 428 |
array_needsize (signals, signalmax, w->signum, signals_init); |
| 429 |
wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
| 430 |
} |
| 431 |
|
| 432 |
void |
| 433 |
evsignal_stop (struct ev_signal *w) |
| 434 |
{ |
| 435 |
if (!ev_is_active (w)) |
| 436 |
return; |
| 437 |
|
| 438 |
wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
| 439 |
ev_stop ((struct ev_watcher *)w); |
| 440 |
} |
| 441 |
|
| 442 |
/*****************************************************************************/ |
| 443 |
#if 1 |
| 444 |
|
| 445 |
static void |
| 446 |
sin_cb (struct ev_io *w, int revents) |
| 447 |
{ |
| 448 |
fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
ocb (struct ev_timer *w, int revents) |
| 453 |
{ |
| 454 |
fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
| 455 |
} |
| 456 |
|
| 457 |
int main (void) |
| 458 |
{ |
| 459 |
struct ev_io sin; |
| 460 |
|
| 461 |
ev_init (0); |
| 462 |
|
| 463 |
evw_init (&sin, sin_cb, 55); |
| 464 |
evio_set (&sin, 0, EV_READ); |
| 465 |
evio_start (&sin); |
| 466 |
|
| 467 |
struct ev_timer t[1000]; |
| 468 |
|
| 469 |
int i; |
| 470 |
for (i = 0; i < 1000; ++i) |
| 471 |
{ |
| 472 |
struct ev_timer *w = t + i; |
| 473 |
evw_init (w, ocb, i); |
| 474 |
evtimer_set_rel (w, drand48 (), 0); |
| 475 |
evtimer_start (w); |
| 476 |
if (drand48 () < 0.5) |
| 477 |
evtimer_stop (w); |
| 478 |
} |
| 479 |
|
| 480 |
ev_loop (0); |
| 481 |
|
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
#endif |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|