From 5fbc7f0aeedcc5e57ac40aad01211bce7a7c7a0d Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 2 May 2008 16:28:25 +0000 Subject: [PATCH] r15439@tombo: nickm | 2008-05-02 12:28:08 -0400 use event_assign internall; switch uses of event_set to use event_assign instead. svn:r755 --- ChangeLog | 4 ++- evbuffer.c | 8 ++---- evdns.c | 45 +++++++++++++------------------- event.c | 14 +++++----- evrpc.c | 4 +-- http.c | 16 ++++-------- include/event2/event.h | 58 +++++++++++++++++++++++++++++++++--------- signal.c | 4 +-- 8 files changed, 85 insertions(+), 68 deletions(-) diff --git a/ChangeLog b/ChangeLog index b6356f0f..1420a900 100644 --- a/ChangeLog +++ b/ChangeLog @@ -81,7 +81,9 @@ Changes in current version: o Remove the never-exported, never-used evhttp_hostportfile function. o Support input/output filters for bufferevents; somewhat similar to libio's model. This will allow us to implement SSL, compression, etc, transparently to users of bufferevents such as the http layer. o allow connections to be removed from an rpc pool - + o add new evtimer_assign, signal_assign, evtimer_new, and signal_new functions to manipulate timer and signal events, analagous to the now-recommended event_assign and event_new + o switch internal uses of event_set over to use event_assign. + Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. o demote most http warnings to debug messages diff --git a/evbuffer.c b/evbuffer.c index faf6cdb1..36a182ec 100644 --- a/evbuffer.c +++ b/evbuffer.c @@ -338,12 +338,8 @@ bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd) event_del(&bufev->ev_read); event_del(&bufev->ev_write); - event_set(&bufev->ev_read, fd, EV_READ, bufferevent_readcb, bufev); - event_set(&bufev->ev_write, fd, EV_WRITE, bufferevent_writecb, bufev); - if (bufev->ev_base != NULL) { - event_base_set(bufev->ev_base, &bufev->ev_read); - event_base_set(bufev->ev_base, &bufev->ev_write); - } + event_assign(&bufev->ev_read, bufev->ev_base, fd, EV_READ, bufferevent_readcb, bufev); + event_assign(&bufev->ev_write, bufev->ev_base, fd, EV_WRITE, bufferevent_writecb, bufev); /* we need to free all filter contexts and then init them again */ TAILQ_FOREACH(filter, &bufev->input_filters, next) { diff --git a/evdns.c b/evdns.c index 26dc8026..109e0409 100644 --- a/evdns.c +++ b/evdns.c @@ -505,9 +505,8 @@ nameserver_probe_failed(struct nameserver *const ns) { global_nameserver_timeouts_length - 1)]; ns->failed_times++; - evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns); - if (ns->base->event_base) - event_base_set(ns->base->event_base, &ns->timeout_event); + evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns); + if (evtimer_add(&ns->timeout_event, (struct timeval *) timeout) < 0) { log(EVDNS_LOG_WARN, "Error from libevent when adding timer event for %s", @@ -538,9 +537,8 @@ nameserver_failed(struct nameserver *const ns, const char *msg) { ns->state = 0; ns->failed_times = 1; - evtimer_set(&ns->timeout_event, nameserver_prod_callback, ns); - if (ns->base->event_base) - event_base_set(ns->base->event_base, &ns->timeout_event); + evtimer_assign(&ns->timeout_event, ns->base->event_base, nameserver_prod_callback, ns); + if (evtimer_add(&ns->timeout_event, (struct timeval *) &global_nameserver_timeouts[0]) < 0) { log(EVDNS_LOG_WARN, "Error from libevent when adding timer event for %s", @@ -1230,10 +1228,10 @@ server_port_flush(struct evdns_server_port *port) /* We have no more pending requests; stop listening for 'writeable' events. */ (void) event_del(&port->event); - event_set(&port->event, port->socket, EV_READ | EV_PERSIST, - server_port_ready_callback, port); - if (port->event_base) - event_base_set(port->event_base, &port->event); + event_assign(&port->event, port->event_base, + port->socket, EV_READ | EV_PERSIST, + server_port_ready_callback, port); + if (event_add(&port->event, NULL) < 0) { log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server."); /* ???? Do more? */ @@ -1249,10 +1247,9 @@ nameserver_write_waiting(struct nameserver *ns, char waiting) { ns->write_waiting = waiting; (void) event_del(&ns->event); - event_set(&ns->event, ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST, - nameserver_ready_callback, ns); - if (ns->base->event_base) - event_base_set(ns->base->event_base, &ns->event); + event_assign(&ns->event, ns->base->event_base, + ns->socket, EV_READ | (waiting ? EV_WRITE : 0) | EV_PERSIST, + nameserver_ready_callback, ns); if (event_add(&ns->event, NULL) < 0) { log(EVDNS_LOG_WARN, "Error from libevent when adding event for %s", debug_ntoa(ns->address)); @@ -1490,10 +1487,9 @@ evdns_add_server_port_with_base(struct event_base *base, int socket, int is_tcp, port->pending_replies = NULL; port->event_base = base; - event_set(&port->event, port->socket, EV_READ | EV_PERSIST, - server_port_ready_callback, port); - if (port->event_base) - event_base_set(port->event_base, &port->event); + event_assign(&port->event, port->event_base, + port->socket, EV_READ | EV_PERSIST, + server_port_ready_callback, port); event_add(&port->event, NULL); /* check return. */ return port; } @@ -1758,9 +1754,7 @@ evdns_server_request_respond(struct evdns_server_request *_req, int err) port->choked = 1; (void) event_del(&port->event); - event_set(&port->event, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port); - if (port->event_base) - event_base_set(port->event_base, &port->event); + event_assign(&port->event, port->event_base, port->socket, (port->closing?0:EV_READ) | EV_WRITE | EV_PERSIST, server_port_ready_callback, port); if (event_add(&port->event, NULL) < 0) { log(EVDNS_LOG_WARN, "Error from libevent when adding event for DNS server"); @@ -1970,9 +1964,8 @@ evdns_request_transmit(struct request *req) { /* all ok */ log(EVDNS_LOG_DEBUG, "Setting timeout for request %lx", (unsigned long) req); - evtimer_set(&req->timeout_event, evdns_request_timeout_callback, req); - if (req->base->event_base) - event_base_set(req->base->event_base, &req->timeout_event); + evtimer_assign(&req->timeout_event, req->base->event_base, evdns_request_timeout_callback, req); + if (evtimer_add(&req->timeout_event, &req->base->global_timeout) < 0) { log(EVDNS_LOG_WARN, "Error from libevent when adding timer for request %lx", @@ -2173,9 +2166,7 @@ _evdns_nameserver_add_impl(struct evdns_base *base, unsigned long int address, i ns->address = address; ns->state = 1; - event_set(&ns->event, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns); - if (ns->base->event_base) - event_base_set(ns->base->event_base, &ns->event); + event_assign(&ns->event, ns->base->event_base, ns->socket, EV_READ | EV_PERSIST, nameserver_ready_callback, ns); if (event_add(&ns->event, NULL) < 0) { err = 2; goto out2; diff --git a/event.c b/event.c index ed357580..7f387d33 100644 --- a/event.c +++ b/event.c @@ -606,18 +606,17 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, tv = &etv; } - evtimer_set(&eonce->ev, event_once_cb, eonce); + res = evtimer_assign(&eonce->ev, base, event_once_cb, eonce); } else if (events & (EV_READ|EV_WRITE)) { events &= EV_READ|EV_WRITE; - event_set(&eonce->ev, fd, events, event_once_cb, eonce); + res = event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce); } else { /* Bad event combination */ mm_free(eonce); return (-1); } - res = event_base_set(base, &eonce->ev); if (res == 0) res = event_add(&eonce->ev, tv); if (res != 0) { @@ -668,7 +667,10 @@ int event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) { event_set(ev, fd, events, cb, arg); - return event_base_set(base, ev); + if (base) + return event_base_set(base, ev); + else + return (0); } struct event * @@ -1234,9 +1236,9 @@ evthread_set_id_callback(struct event_base *base, evutil_make_socket_nonblocking(base->th_notify_fd[1]); /* prepare an event that we can use for wakeup */ - event_set(&base->th_notify, base->th_notify_fd[0], EV_READ, + event_assign(&base->th_notify, base, base->th_notify_fd[0], EV_READ, evthread_ignore_fd, base); - event_base_set(base, &base->th_notify); + /* we need to mark this as internal event */ base->th_notify.ev_flags |= EVLIST_INTERNAL; diff --git a/evrpc.c b/evrpc.c index 191075d5..78ccc46f 100644 --- a/evrpc.c +++ b/evrpc.c @@ -764,9 +764,7 @@ evrpc_make_request(struct evrpc_request_wrapper *ctx) struct evrpc_pool *pool = ctx->pool; /* initialize the event structure for this rpc */ - evtimer_set(&ctx->ev_timeout, evrpc_request_timeout, ctx); - if (pool->base != NULL) - event_base_set(pool->base, &ctx->ev_timeout); + evtimer_assign(&ctx->ev_timeout, pool->base, evrpc_request_timeout, ctx); /* we better have some available connections on the pool */ assert(TAILQ_FIRST(&pool->connections) != NULL); diff --git a/http.c b/http.c index e78954b6..8713ea31 100644 --- a/http.c +++ b/http.c @@ -145,11 +145,6 @@ fake_freeaddrinfo(struct addrinfo *ai) #define MIN(a,b) (((a)<(b))?(a):(b)) #endif -/* wrapper for setting the base from the http server */ -#define EVHTTP_BASE_SET(x, y) do { \ - if ((x)->base != NULL) event_base_set((x)->base, y); \ -} while (0) - extern int debug; static int socket_connect(evutil_socket_t kefd, const char *address, unsigned short port); @@ -843,9 +838,9 @@ evhttp_connection_start_detectclose(struct evhttp_connection *evcon) if (event_initialized(&evcon->close_ev)) event_del(&evcon->close_ev); - event_set(&evcon->close_ev, evcon->fd, EV_READ, + event_assign(&evcon->close_ev, evcon->base, evcon->fd, EV_READ, evhttp_detect_close_cb, evcon); - EVHTTP_BASE_SET(evcon, &evcon->close_ev); + event_add(&evcon->close_ev, NULL); } @@ -870,8 +865,7 @@ static void evhttp_connection_cb_cleanup(struct evhttp_connection *evcon) { if (evcon->retry_max < 0 || evcon->retry_cnt < evcon->retry_max) { - evtimer_set(&evcon->retry_ev, evhttp_connection_retry, evcon); - EVHTTP_BASE_SET(evcon, &evcon->retry_ev); + evtimer_assign(&evcon->retry_ev, evcon->base, evhttp_connection_retry, evcon); evhttp_add_event(&evcon->retry_ev, MIN(3600, 2 << evcon->retry_cnt), HTTP_CONNECT_TIMEOUT); @@ -1985,8 +1979,8 @@ evhttp_accept_socket(struct evhttp *http, evutil_socket_t fd) struct event *ev = &http->bind_ev; /* Schedule the socket for accepting */ - event_set(ev, fd, EV_READ | EV_PERSIST, accept_socket, http); - EVHTTP_BASE_SET(http, ev); + event_assign(ev, http->base, fd, EV_READ | EV_PERSIST, accept_socket, http); + return (event_add(ev, NULL)); } diff --git a/include/event2/event.h b/include/event2/event.h index d66295cf..b85bc496 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -192,6 +192,8 @@ int event_base_loopbreak(struct event_base *); @param arg argument that will be passed to the callback function */ #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) +#define evtimer_assign(ev, b, cb, arg) event_assign(ev, b, -1, 0, cb, arg) +#define evtimer_new(b, cb, arg) event_new(b, -1, 0, cb, arg) /** Add a timer event. @@ -201,16 +203,6 @@ int event_base_loopbreak(struct event_base *); */ #define evtimer_add(ev, tv) event_add(ev, tv) -/** - Define a timer event. - - @param ev event struct to be modified - @param cb callback function - @param arg argument that will be passed to the callback function - */ -#define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) - - /** * Delete a timer event. * @@ -238,7 +230,6 @@ int event_base_loopbreak(struct event_base *); */ #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) - /** * Disable a timeout event. * @@ -252,6 +243,10 @@ int event_base_loopbreak(struct event_base *); #define signal_add(ev, tv) event_add(ev, tv) #define signal_set(ev, x, cb, arg) \ event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg) +#define signal_assign(ev, b, x, cb, arg) \ + event_assign(ev, b, x, EV_SIGNAL|EV_PERSIST, cb, arg) +#define signal_new(b, x, cb, arg) \ + event_new(b, x, EV_SIGNAL|EV_PERSIST, cb, arg) #define signal_del(ev) event_del(ev) #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv) #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) @@ -282,14 +277,53 @@ int event_base_loopbreak(struct event_base *); @see event_add(), event_del(), event_once() + @deprecated event_set() is deprecated. Use event_assign() instead. + */ void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); -/*XXX document this function, deprecate previous one. */ + +/** + Prepare an event structure to be added. + + The function event_assign() prepares the event structure ev to be used in + future calls to event_add() and event_del(). The event will be prepared to + call the function specified by the fn argument with an int argument + indicating the file descriptor, a short argument indicating the type of + event, and a void * argument given in the arg argument. The fd indicates + the file descriptor that should be monitored for events. The events can be + either EV_READ, EV_WRITE, or both. Indicating that an application can read + or write from the file descriptor respectively without blocking. + + The function fn will be called with the file descriptor that triggered the + event and the type of event which will be either EV_TIMEOUT, EV_SIGNAL, + EV_READ, or EV_WRITE. The additional flag EV_PERSIST makes an event_add() + persistent until event_del() has been called. + + @param ev an event struct to be modified + @param base the event base to which ev should be attached. + @param fd the file descriptor to be monitored + @param event desired events to monitor; can be EV_READ and/or EV_WRITE + @param fn callback function to be invoked when the event occurs + @param arg an argument to be passed to the callback function + + @see event_add(), event_del(), event_once() + + */ int event_assign(struct event *, struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); +/** + Create and allocate a new event structure, ready to be added. + + Arguments are as for event_assign; returns a newly allocated struct event * + that must later be deallocated with event_free(). + + */ struct event *event_new(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); +/** + Deallocate a struct event * returned by event_new(). + */ void event_free(struct event *); /** diff --git a/signal.c b/signal.c index c7798b35..4bfe9bcd 100644 --- a/signal.c +++ b/signal.c @@ -114,9 +114,9 @@ evsignal_init(struct event_base *base) evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]); - event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1], + event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[1], EV_READ | EV_PERSIST, evsignal_cb, &base->sig.ev_signal); - base->sig.ev_signal.ev_base = base; + base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL; } -- 2.40.0