From 94fb4d0a1e95f140dd840535bb066fb7dbb3013c Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 25 Apr 2008 01:18:18 +0000 Subject: [PATCH] r15317@tombo: nickm | 2008-04-24 21:17:49 -0400 Add new functions to be more threadsafe (and structure-ignorant) than event_set. svn:r726 --- ChangeLog | 1 + event.c | 30 ++++++++++++++++++++++++++++++ include/event2/event.h | 7 +++++++ 3 files changed, 38 insertions(+) diff --git a/ChangeLog b/ChangeLog index 32b3bd4e..8659e100 100644 --- a/ChangeLog +++ b/ChangeLog @@ -69,6 +69,7 @@ Changes in current version: o Add new thread-safe interfaces to evdns functions. o Make all event_tagging interfaces threadsafe. o Rename internal memory management functions. + o New functions (event_assign, event_new, event_free) for use by apps that want to be safely threadsafe, or want to remain ignorant of the contents of struct event. Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. diff --git a/event.c b/event.c index 7d4b882c..3e36f736 100644 --- a/event.c +++ b/event.c @@ -664,6 +664,36 @@ event_base_set(struct event_base *base, struct event *ev) return (0); } +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); +} + +struct event * +event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) +{ + struct event *ev; + ev = mm_malloc(sizeof(struct event)); + if (!ev) + return NULL; + if (event_assign(ev, base, fd, events, cb, arg) < 0) { + mm_free(ev); + return NULL; + } + return ev; +} + +void +event_free(struct event *ev) +{ + /* make sure that this event won't be coming back to haunt us. + * XXX this is safe, right? */ + event_del(ev); + mm_free(ev); +} + /* * Set's the priority of an event - if an event is already scheduled * changing the priority is going to fail. diff --git a/include/event2/event.h b/include/event2/event.h index 2644cd4b..d66295cf 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -285,6 +285,13 @@ int event_base_loopbreak(struct event_base *); */ void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); +/*XXX document this function, deprecate previous one. */ +int event_assign(struct event *, struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); + +struct event *event_new(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); + +void event_free(struct event *); + /** Schedule a one-time event (threadsafe variant) -- 2.40.0