Merge remote-tracking branch 'github/21_split_functions'

Conflicts:
	event.c

The conflicts were with the 21_faster_timeout_adj branch, which
added a "reinsert" function that needed to get renamed to
"reinsert_timeout".  Also, some of the code that 21_split_functions
changes got removed by 21_faster_timeout_adj.
This commit is contained in:
Nick Mathewson 2011-08-08 16:20:53 -04:00
commit e91d57f1f6

177
event.c
View File

@ -131,9 +131,13 @@ static inline int event_add_internal(struct event *ev,
const struct timeval *tv, int tv_is_absolute); const struct timeval *tv, int tv_is_absolute);
static inline int event_del_internal(struct event *ev); static inline int event_del_internal(struct event *ev);
static void event_queue_insert(struct event_base *, struct event *, int); static void event_queue_insert_active(struct event_base *, struct event *);
static void event_queue_remove(struct event_base *, struct event *, int); static void event_queue_insert_timeout(struct event_base *, struct event *);
static void event_queue_reinsert(struct event_base *,struct event *ev,int); static void event_queue_insert_inserted(struct event_base *, struct event *);
static void event_queue_remove_active(struct event_base *, struct event *);
static void event_queue_remove_timeout(struct event_base *, struct event *);
static void event_queue_remove_inserted(struct event_base *, struct event *);
static void event_queue_reinsert_timeout(struct event_base *,struct event *);
static int event_haveevents(struct event_base *); static int event_haveevents(struct event_base *);
@ -811,22 +815,18 @@ event_reinit(struct event_base *base)
if (base->sig.ev_signal_added) { if (base->sig.ev_signal_added) {
/* we cannot call event_del here because the base has /* we cannot call event_del here because the base has
* not been reinitialized yet. */ * not been reinitialized yet. */
event_queue_remove(base, &base->sig.ev_signal, event_queue_remove_inserted(base, &base->sig.ev_signal);
EVLIST_INSERTED);
if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE) if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE)
event_queue_remove(base, &base->sig.ev_signal, event_queue_remove_active(base, &base->sig.ev_signal);
EVLIST_ACTIVE);
base->sig.ev_signal_added = 0; base->sig.ev_signal_added = 0;
} }
if (base->th_notify_fd[0] != -1) { if (base->th_notify_fd[0] != -1) {
/* we cannot call event_del here because the base has /* we cannot call event_del here because the base has
* not been reinitialized yet. */ * not been reinitialized yet. */
was_notifiable = 1; was_notifiable = 1;
event_queue_remove(base, &base->th_notify, event_queue_remove_inserted(base, &base->th_notify);
EVLIST_INSERTED);
if (base->th_notify.ev_flags & EVLIST_ACTIVE) if (base->th_notify.ev_flags & EVLIST_ACTIVE)
event_queue_remove(base, &base->th_notify, event_queue_remove_active(base, &base->th_notify);
EVLIST_ACTIVE);
base->sig.ev_signal_added = 0; base->sig.ev_signal_added = 0;
EVUTIL_CLOSESOCKET(base->th_notify_fd[0]); EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
if (base->th_notify_fd[1] != -1) if (base->th_notify_fd[1] != -1)
@ -1288,7 +1288,7 @@ event_process_active_single_queue(struct event_base *base,
for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) { for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) {
if (ev->ev_events & EV_PERSIST) if (ev->ev_events & EV_PERSIST)
event_queue_remove(base, ev, EVLIST_ACTIVE); event_queue_remove_active(base, ev);
else else
event_del_internal(ev); event_del_internal(ev);
if (!(ev->ev_flags & EVLIST_INTERNAL)) if (!(ev->ev_flags & EVLIST_INTERNAL))
@ -2028,7 +2028,7 @@ event_add_internal(struct event *ev, const struct timeval *tv,
else if (ev->ev_events & EV_SIGNAL) else if (ev->ev_events & EV_SIGNAL)
res = evmap_signal_add(base, (int)ev->ev_fd, ev); res = evmap_signal_add(base, (int)ev->ev_fd, ev);
if (res != -1) if (res != -1)
event_queue_insert(base, ev, EVLIST_INSERTED); event_queue_insert_inserted(base, ev);
if (res == 1) { if (res == 1) {
/* evmap says we need to notify the main thread. */ /* evmap says we need to notify the main thread. */
notify = 1; notify = 1;
@ -2068,7 +2068,7 @@ event_add_internal(struct event *ev, const struct timeval *tv,
} }
} }
event_queue_remove(base, ev, EVLIST_ACTIVE); event_queue_remove_active(base, ev);
} }
gettime(base, &now); gettime(base, &now);
@ -2090,7 +2090,7 @@ event_add_internal(struct event *ev, const struct timeval *tv,
"event_add: timeout in %d seconds, call %p", "event_add: timeout in %d seconds, call %p",
(int)tv->tv_sec, ev->ev_callback)); (int)tv->tv_sec, ev->ev_callback));
event_queue_reinsert(base, ev, EVLIST_TIMEOUT); event_queue_reinsert_timeout(base, ev);
if (common_timeout) { if (common_timeout) {
struct common_timeout_list *ctl = struct common_timeout_list *ctl =
@ -2183,14 +2183,14 @@ event_del_internal(struct event *ev)
* dispatch loop early anyway, so we wouldn't gain anything by * dispatch loop early anyway, so we wouldn't gain anything by
* doing it. * doing it.
*/ */
event_queue_remove(base, ev, EVLIST_TIMEOUT); event_queue_remove_timeout(base, ev);
} }
if (ev->ev_flags & EVLIST_ACTIVE) if (ev->ev_flags & EVLIST_ACTIVE)
event_queue_remove(base, ev, EVLIST_ACTIVE); event_queue_remove_active(base, ev);
if (ev->ev_flags & EVLIST_INSERTED) { if (ev->ev_flags & EVLIST_INSERTED) {
event_queue_remove(base, ev, EVLIST_INSERTED); event_queue_remove_inserted(base, ev);
if (ev->ev_events & (EV_READ|EV_WRITE)) if (ev->ev_events & (EV_READ|EV_WRITE))
res = evmap_io_del(base, ev->ev_fd, ev); res = evmap_io_del(base, ev->ev_fd, ev);
else else
@ -2261,7 +2261,7 @@ event_active_nolock(struct event *ev, int res, short ncalls)
ev->ev_pncalls = NULL; ev->ev_pncalls = NULL;
} }
event_queue_insert(base, ev, EVLIST_ACTIVE); event_queue_insert_active(base, ev);
if (EVBASE_NEED_NOTIFY(base)) if (EVBASE_NEED_NOTIFY(base))
evthread_notify_base(base); evthread_notify_base(base);
@ -2439,32 +2439,58 @@ timeout_process(struct event_base *base)
} }
} }
/* Remove 'ev' from 'queue' (EVLIST_...) in base. */ #if (EVLIST_INTERNAL >> 4) != 1
#error "Mismatch for value of EVLIST_INTERNAL"
#endif
/* These are a fancy way to spell
if (~ev->ev_flags & EVLIST_INTERNAL)
base->event_count--/++;
*/
#define DECR_EVENT_COUNT(base,ev) \
((base)->event_count -= (~((ev)->ev_flags >> 4) & 1))
#define INCR_EVENT_COUNT(base, ev) \
((base)->event_count += (~((ev)->ev_flags >> 4) & 1))
static void static void
event_queue_remove(struct event_base *base, struct event *ev, int queue) event_queue_remove_inserted(struct event_base *base, struct event *ev)
{ {
EVENT_BASE_ASSERT_LOCKED(base); EVENT_BASE_ASSERT_LOCKED(base);
if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) {
if (!(ev->ev_flags & queue)) {
event_errx(1, "%s: %p(fd %d) not on queue %x", __func__, event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
ev, ev->ev_fd, queue); ev, ev->ev_fd, EVLIST_INSERTED);
return; return;
} }
DECR_EVENT_COUNT(base, ev);
if (~ev->ev_flags & EVLIST_INTERNAL) ev->ev_flags &= ~EVLIST_INSERTED;
base->event_count--;
ev->ev_flags &= ~queue;
switch (queue) {
case EVLIST_INSERTED:
TAILQ_REMOVE(&base->eventqueue, ev, ev_next); TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
break; }
case EVLIST_ACTIVE: static void
event_queue_remove_active(struct event_base *base, struct event *ev)
{
EVENT_BASE_ASSERT_LOCKED(base);
if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_ACTIVE))) {
event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
ev, ev->ev_fd, EVLIST_ACTIVE);
return;
}
DECR_EVENT_COUNT(base, ev);
ev->ev_flags &= ~EVLIST_ACTIVE;
base->event_count_active--; base->event_count_active--;
TAILQ_REMOVE(&base->activequeues[ev->ev_pri], TAILQ_REMOVE(&base->activequeues[ev->ev_pri],
ev, ev_active_next); ev, ev_active_next);
break; }
case EVLIST_TIMEOUT: static void
event_queue_remove_timeout(struct event_base *base, struct event *ev)
{
EVENT_BASE_ASSERT_LOCKED(base);
if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) {
event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
ev, ev->ev_fd, EVLIST_TIMEOUT);
return;
}
DECR_EVENT_COUNT(base, ev);
ev->ev_flags &= ~EVLIST_TIMEOUT;
if (is_common_timeout(&ev->ev_timeout, base)) { if (is_common_timeout(&ev->ev_timeout, base)) {
struct common_timeout_list *ctl = struct common_timeout_list *ctl =
get_common_timeout_list(base, &ev->ev_timeout); get_common_timeout_list(base, &ev->ev_timeout);
@ -2473,27 +2499,17 @@ event_queue_remove(struct event_base *base, struct event *ev, int queue)
} else { } else {
min_heap_erase(&base->timeheap, ev); min_heap_erase(&base->timeheap, ev);
} }
break;
default:
event_errx(1, "%s: unknown queue %x", __func__, queue);
}
} }
/* Remove and reinsert 'ev' into the appropriate queue. Only EVLIST_TIMEOUT /* Remove and reinsert 'ev' into the timeout queue. */
* is supported. */
static void static void
event_queue_reinsert(struct event_base *base, struct event *ev, int queue) event_queue_reinsert_timeout(struct event_base *base, struct event *ev)
{ {
if (!(ev->ev_flags & queue)) { if (!(ev->ev_flags & EVLIST_TIMEOUT)) {
event_queue_insert(base, ev, queue); event_queue_insert_timeout(base, ev);
return; return;
} }
if (queue != EVLIST_TIMEOUT) {
event_errx(1, "%s: Unsupported queue %x", __func__, queue);
return; /* unreached */
}
if (is_common_timeout(&ev->ev_timeout, base)) { if (is_common_timeout(&ev->ev_timeout, base)) {
struct common_timeout_list *ctl = struct common_timeout_list *ctl =
get_common_timeout_list(base, &ev->ev_timeout); get_common_timeout_list(base, &ev->ev_timeout);
@ -2537,44 +2553,63 @@ insert_common_timeout_inorder(struct common_timeout_list *ctl,
} }
static void static void
event_queue_insert(struct event_base *base, struct event *ev, int queue) event_queue_insert_inserted(struct event_base *base, struct event *ev)
{ {
EVENT_BASE_ASSERT_LOCKED(base); EVENT_BASE_ASSERT_LOCKED(base);
if (ev->ev_flags & queue) { if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) {
/* Double insertion is possible for active events */ event_errx(1, "%s: %p(fd %d) already inserted", __func__,
if (queue & EVLIST_ACTIVE) ev, ev->ev_fd);
return;
event_errx(1, "%s: %p(fd %d) already on queue %x", __func__,
ev, ev->ev_fd, queue);
return; return;
} }
if (~ev->ev_flags & EVLIST_INTERNAL) INCR_EVENT_COUNT(base, ev);
base->event_count++;
ev->ev_flags |= EVLIST_INSERTED;
ev->ev_flags |= queue;
switch (queue) {
case EVLIST_INSERTED:
TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next); TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
break; }
case EVLIST_ACTIVE:
static void
event_queue_insert_active(struct event_base *base, struct event *ev)
{
EVENT_BASE_ASSERT_LOCKED(base);
if (ev->ev_flags & EVLIST_ACTIVE) {
/* Double insertion is possible for active events */
return;
}
INCR_EVENT_COUNT(base, ev);
ev->ev_flags |= EVLIST_ACTIVE;
base->event_count_active++; base->event_count_active++;
TAILQ_INSERT_TAIL(&base->activequeues[ev->ev_pri], TAILQ_INSERT_TAIL(&base->activequeues[ev->ev_pri],
ev,ev_active_next); ev,ev_active_next);
break; }
case EVLIST_TIMEOUT: {
static void
event_queue_insert_timeout(struct event_base *base, struct event *ev)
{
EVENT_BASE_ASSERT_LOCKED(base);
if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) {
event_errx(1, "%s: %p(fd %d) already on timeout", __func__,
ev, ev->ev_fd);
return;
}
INCR_EVENT_COUNT(base, ev);
ev->ev_flags |= EVLIST_TIMEOUT;
if (is_common_timeout(&ev->ev_timeout, base)) { if (is_common_timeout(&ev->ev_timeout, base)) {
struct common_timeout_list *ctl = struct common_timeout_list *ctl =
get_common_timeout_list(base, &ev->ev_timeout); get_common_timeout_list(base, &ev->ev_timeout);
insert_common_timeout_inorder(ctl, ev); insert_common_timeout_inorder(ctl, ev);
} else } else {
min_heap_push(&base->timeheap, ev); min_heap_push(&base->timeheap, ev);
break;
}
default:
event_errx(1, "%s: unknown queue %x", __func__, queue);
} }
} }