Lines Matching +full:tv +full:- +full:set

2  * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
56 #include "event-internal.h"
118 /* Handle signals - This is a deprecated interface */
119 int (*event_sigcb)(void); /* Signal callback when gotsig is set */
120 volatile sig_atomic_t event_gotsig; /* Set in signal handler */
136 if (timercmp(&a->ev_timeout, &b->ev_timeout, <)) in compare()
137 return (-1); in compare()
138 else if (timercmp(&a->ev_timeout, &b->ev_timeout, >)) in compare()
141 return (-1); in compare()
154 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) in gettime()
156 if (clock_gettime(CLOCK_REALTIME, &ts) == -1) in gettime()
158 return (-1); in gettime()
159 tp->tv_sec = ts.tv_sec; in gettime()
160 tp->tv_usec = ts.tv_nsec / 1000; in gettime()
183 gettime(&current_base->event_tv); in event_init()
185 RB_INIT(&current_base->timetree); in event_init()
186 TAILQ_INIT(&current_base->eventqueue); in event_init()
189 current_base->evbase = NULL; in event_init()
190 for (i = 0; eventops[i] && !current_base->evbase; i++) { in event_init()
191 current_base->evsel = eventops[i]; in event_init()
193 current_base->evbase = current_base->evsel->init(); in event_init()
196 if (current_base->evbase == NULL) in event_init()
201 current_base->evsel->name); in event_init()
220 assert(TAILQ_EMPTY(&base->eventqueue)); in event_base_free()
221 for (i=0; i < base->nactivequeues; ++i) in event_base_free()
222 assert(TAILQ_EMPTY(base->activequeues[i])); in event_base_free()
224 assert(RB_EMPTY(&base->timetree)); in event_base_free()
226 for (i = 0; i < base->nactivequeues; ++i) in event_base_free()
227 free(base->activequeues[i]); in event_base_free()
228 free(base->activequeues); in event_base_free()
230 if (base->evsel->dealloc != NULL) in event_base_free()
231 base->evsel->dealloc(base->evbase); in event_base_free()
247 if (base->event_count_active) in event_base_priority_init()
248 return (-1); in event_base_priority_init()
250 if (base->nactivequeues && npriorities != base->nactivequeues) { in event_base_priority_init()
251 for (i = 0; i < base->nactivequeues; ++i) { in event_base_priority_init()
252 free(base->activequeues[i]); in event_base_priority_init()
254 free(base->activequeues); in event_base_priority_init()
258 base->nactivequeues = npriorities; in event_base_priority_init()
259 base->activequeues = (struct event_list **)calloc(base->nactivequeues, in event_base_priority_init()
261 if (base->activequeues == NULL) in event_base_priority_init()
264 for (i = 0; i < base->nactivequeues; ++i) { in event_base_priority_init()
265 base->activequeues[i] = malloc(sizeof(struct event_list)); in event_base_priority_init()
266 if (base->activequeues[i] == NULL) in event_base_priority_init()
268 TAILQ_INIT(base->activequeues[i]); in event_base_priority_init()
277 return (base->event_count > 0); in event_haveevents()
294 if (!base->event_count_active) in event_process_active()
297 for (i = 0; i < base->nactivequeues; ++i) { in event_process_active()
298 if (TAILQ_FIRST(base->activequeues[i]) != NULL) { in event_process_active()
299 activeq = base->activequeues[i]; in event_process_active()
310 ncalls = ev->ev_ncalls; in event_process_active()
311 ev->ev_pncalls = &ncalls; in event_process_active()
313 ncalls--; in event_process_active()
314 ev->ev_ncalls = ncalls; in event_process_active()
315 (*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg); in event_process_active()
342 base->event_gotterm = 1; in event_loopexit_cb()
348 event_loopexit(struct timeval *tv) in event_loopexit() argument
350 return (event_once(-1, EV_TIMEOUT, event_loopexit_cb, in event_loopexit()
351 current_base, tv)); in event_loopexit()
355 event_base_loopexit(struct event_base *event_base, struct timeval *tv) in event_base_loopexit() argument
357 return (event_once(-1, EV_TIMEOUT, event_loopexit_cb, in event_base_loopexit()
358 event_base, tv)); in event_base_loopexit()
372 const struct eventop *evsel = base->evsel; in event_base_loop()
373 void *evbase = base->evbase; in event_base_loop()
374 struct timeval tv; in event_base_loop() local
380 if (evsel->recalc(base, evbase, 0) == -1) in event_base_loop()
381 return (-1); in event_base_loop()
384 if (base->event_gotterm) { in event_base_loop()
385 base->event_gotterm = 0; in event_base_loop()
389 /* You cannot use this interface for multi-threaded apps */ in event_base_loop()
394 if (res == -1) { in event_base_loop()
396 return (-1); in event_base_loop()
402 gettime(&tv); in event_base_loop()
403 if (timercmp(&tv, &base->event_tv, <)) { in event_base_loop()
407 timersub(&base->event_tv, &tv, &off); in event_base_loop()
410 base->event_tv = tv; in event_base_loop()
412 if (!base->event_count_active && !(flags & EVLOOP_NONBLOCK)) in event_base_loop()
413 timeout_next(base, &tv); in event_base_loop()
415 timerclear(&tv); in event_base_loop()
423 res = evsel->dispatch(base, evbase, &tv); in event_base_loop()
425 if (res == -1) in event_base_loop()
426 return (-1); in event_base_loop()
430 if (base->event_count_active) { in event_base_loop()
432 if (!base->event_count_active && (flags & EVLOOP_ONCE)) in event_base_loop()
451 /* One-time callback, it deletes itself */
458 (*eonce->cb)(fd, events, eonce->arg); in event_once_cb()
466 void (*callback)(int, short, void *), void *arg, struct timeval *tv) in event_once() argument
474 return (-1); in event_once()
477 return (-1); in event_once()
479 eonce->cb = callback; in event_once()
480 eonce->arg = arg; in event_once()
483 if (tv == NULL) { in event_once()
485 tv = &etv; in event_once()
488 evtimer_set(&eonce->ev, event_once_cb, eonce); in event_once()
492 event_set(&eonce->ev, fd, events, event_once_cb, eonce); in event_once()
496 return (-1); in event_once()
499 res = event_add(&eonce->ev, tv); in event_once()
512 /* Take the current base - caller needs to set the real base later */ in event_set()
513 ev->ev_base = current_base; in event_set()
515 ev->ev_callback = callback; in event_set()
516 ev->ev_arg = arg; in event_set()
517 ev->ev_fd = fd; in event_set()
518 ev->ev_events = events; in event_set()
519 ev->ev_flags = EVLIST_INIT; in event_set()
520 ev->ev_ncalls = 0; in event_set()
521 ev->ev_pncalls = NULL; in event_set()
524 ev->ev_pri = current_base->nactivequeues/2; in event_set()
531 if (ev->ev_flags != EVLIST_INIT) in event_base_set()
532 return (-1); in event_base_set()
534 ev->ev_base = base; in event_base_set()
535 ev->ev_pri = base->nactivequeues/2; in event_base_set()
541 * Set's the priority of an event - if an event is already scheduled
548 if (ev->ev_flags & EVLIST_ACTIVE) in event_priority_set()
549 return (-1); in event_priority_set()
550 if (pri < 0 || pri >= ev->ev_base->nactivequeues) in event_priority_set()
551 return (-1); in event_priority_set()
553 ev->ev_pri = pri; in event_priority_set()
563 event_pending(struct event *ev, short event, struct timeval *tv) in event_pending() argument
568 if (ev->ev_flags & EVLIST_INSERTED) in event_pending()
569 flags |= (ev->ev_events & (EV_READ|EV_WRITE)); in event_pending()
570 if (ev->ev_flags & EVLIST_ACTIVE) in event_pending()
571 flags |= ev->ev_res; in event_pending()
572 if (ev->ev_flags & EVLIST_TIMEOUT) in event_pending()
574 if (ev->ev_flags & EVLIST_SIGNAL) in event_pending()
580 if (tv != NULL && (flags & event & EV_TIMEOUT)) { in event_pending()
582 timersub(&ev->ev_timeout, &now, &res); in event_pending()
585 timeradd(&now, &res, tv); in event_pending()
592 event_add(struct event *ev, struct timeval *tv) in event_add() argument
594 struct event_base *base = ev->ev_base; in event_add()
595 const struct eventop *evsel = base->evsel; in event_add()
596 void *evbase = base->evbase; in event_add()
601 ev->ev_events & EV_READ ? "EV_READ " : " ", in event_add()
602 ev->ev_events & EV_WRITE ? "EV_WRITE " : " ", in event_add()
603 tv ? "EV_TIMEOUT " : " ", in event_add()
604 ev->ev_callback)); in event_add()
606 assert(!(ev->ev_flags & ~EVLIST_ALL)); in event_add()
608 if (tv != NULL) { in event_add()
611 if (ev->ev_flags & EVLIST_TIMEOUT) in event_add()
617 if ((ev->ev_flags & EVLIST_ACTIVE) && in event_add()
618 (ev->ev_res & EV_TIMEOUT)) { in event_add()
622 if (ev->ev_ncalls && ev->ev_pncalls) { in event_add()
624 *ev->ev_pncalls = 0; in event_add()
631 timeradd(&now, tv, &ev->ev_timeout); in event_add()
635 tv->tv_sec, ev->ev_callback)); in event_add()
640 if ((ev->ev_events & (EV_READ|EV_WRITE)) && in event_add()
641 !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) { in event_add()
644 return (evsel->add(evbase, ev)); in event_add()
645 } else if ((ev->ev_events & EV_SIGNAL) && in event_add()
646 !(ev->ev_flags & EVLIST_SIGNAL)) { in event_add()
649 return (evsel->add(evbase, ev)); in event_add()
663 ev, ev->ev_callback)); in event_del()
666 if (ev->ev_base == NULL) in event_del()
667 return (-1); in event_del()
669 base = ev->ev_base; in event_del()
670 evsel = base->evsel; in event_del()
671 evbase = base->evbase; in event_del()
673 assert(!(ev->ev_flags & ~EVLIST_ALL)); in event_del()
676 if (ev->ev_ncalls && ev->ev_pncalls) { in event_del()
678 *ev->ev_pncalls = 0; in event_del()
681 if (ev->ev_flags & EVLIST_TIMEOUT) in event_del()
684 if (ev->ev_flags & EVLIST_ACTIVE) in event_del()
687 if (ev->ev_flags & EVLIST_INSERTED) { in event_del()
689 return (evsel->del(evbase, ev)); in event_del()
690 } else if (ev->ev_flags & EVLIST_SIGNAL) { in event_del()
692 return (evsel->del(evbase, ev)); in event_del()
702 if (ev->ev_flags & EVLIST_ACTIVE) { in event_active()
703 ev->ev_res |= res; in event_active()
707 ev->ev_res = res; in event_active()
708 ev->ev_ncalls = ncalls; in event_active()
709 ev->ev_pncalls = NULL; in event_active()
710 event_queue_insert(ev->ev_base, ev, EVLIST_ACTIVE); in event_active()
714 timeout_next(struct event_base *base, struct timeval *tv) in timeout_next() argument
721 if ((ev = RB_MIN(event_tree, &base->timetree)) == NULL) { in timeout_next()
722 *tv = dflt; in timeout_next()
726 if (gettime(&now) == -1) in timeout_next()
727 return (-1); in timeout_next()
729 if (timercmp(&ev->ev_timeout, &now, <=)) { in timeout_next()
730 timerclear(tv); in timeout_next()
734 timersub(&ev->ev_timeout, &now, tv); in timeout_next()
736 assert(tv->tv_sec >= 0); in timeout_next()
737 assert(tv->tv_usec >= 0); in timeout_next()
739 event_debug(("timeout_next: in %d seconds", tv->tv_sec)); in timeout_next()
752 RB_FOREACH(ev, event_tree, &base->timetree) in timeout_correct()
753 timersub(&ev->ev_timeout, off, &ev->ev_timeout); in timeout_correct()
764 for (ev = RB_MIN(event_tree, &base->timetree); ev; ev = next) { in timeout_process()
765 if (timercmp(&ev->ev_timeout, &now, >)) in timeout_process()
767 next = RB_NEXT(event_tree, &base->timetree, ev); in timeout_process()
775 ev->ev_callback)); in timeout_process()
785 if (!(ev->ev_flags & queue)) in event_queue_remove()
787 ev, ev->ev_fd, queue); in event_queue_remove()
789 if (ev->ev_flags & EVLIST_INTERNAL) in event_queue_remove()
793 base->event_count--; in event_queue_remove()
795 ev->ev_flags &= ~queue; in event_queue_remove()
799 base->event_count_active--; in event_queue_remove()
800 TAILQ_REMOVE(base->activequeues[ev->ev_pri], in event_queue_remove()
807 RB_REMOVE(event_tree, &base->timetree, ev); in event_queue_remove()
810 TAILQ_REMOVE(&base->eventqueue, ev, ev_next); in event_queue_remove()
822 if (ev->ev_flags & queue) { in event_queue_insert()
828 ev, ev->ev_fd, queue); in event_queue_insert()
831 if (ev->ev_flags & EVLIST_INTERNAL) in event_queue_insert()
835 base->event_count++; in event_queue_insert()
837 ev->ev_flags |= queue; in event_queue_insert()
841 base->event_count_active++; in event_queue_insert()
842 TAILQ_INSERT_TAIL(base->activequeues[ev->ev_pri], in event_queue_insert()
849 struct event *tmp = RB_INSERT(event_tree, &base->timetree, ev); in event_queue_insert()
854 TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next); in event_queue_insert()
870 * No thread-safe interface needed - the information should be the same
877 return (current_base->evsel->name); in event_get_method()