Add event_self_cbarg() to be used in conjunction with event_new().

event_self_cbarg() returns a magic value which makes event_new()
pass the event itself as the callback argument.
This commit is contained in:
Ross Lagerwall 2012-03-12 20:42:39 +02:00
parent 2d2aba18a0
commit ed36e6abea
2 changed files with 29 additions and 0 deletions

10
event.c
View File

@ -125,6 +125,8 @@ struct event_base *event_global_current_base_ = NULL;
static int use_monotonic;
static void *event_self_cbarg_ptr_ = NULL;
/* Prototypes */
static inline int event_add_internal(struct event *ev,
const struct timeval *tv, int tv_is_absolute);
@ -1910,6 +1912,12 @@ event_set(struct event *ev, evutil_socket_t fd, short events,
EVUTIL_ASSERT(r == 0);
}
void *
event_self_cbarg(void)
{
return &event_self_cbarg_ptr_;
}
struct event *
event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
{
@ -1917,6 +1925,8 @@ event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(
ev = mm_malloc(sizeof(struct event));
if (ev == NULL)
return (NULL);
if (arg == &event_self_cbarg_ptr_)
arg = ev;
if (event_assign(ev, base, fd, events, cb, arg) < 0) {
mm_free(ev);
return (NULL);

View File

@ -845,6 +845,25 @@ int event_base_got_break(struct event_base *);
*/
typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
/**
Return a value used to specify that the event itself must be used as the callback argument.
The function event_new() takes a callback argument which is passed
to the event's callback function. To specify that the argument to be
passed to the callback function is the event that event_new() returns,
pass in the return value of event_self_cbarg() as the callback argument
for event_new().
For example:
<pre>
struct event *ev = event_new(base, sock, events, callback, %event_self_cbarg());
</pre>
@return a value to be passed as the callback argument to event_new().
@see event_new()
*/
void *event_self_cbarg(void);
/**
Allocate and asssign a new event structure, ready to be added.