]> granicus.if.org Git - libevent/commitdiff
Add event_self_cbarg() to be used in conjunction with event_new().
authorRoss Lagerwall <rosslagerwall@gmail.com>
Mon, 12 Mar 2012 18:42:39 +0000 (20:42 +0200)
committerRoss Lagerwall <rosslagerwall@gmail.com>
Mon, 12 Mar 2012 18:42:39 +0000 (20:42 +0200)
event_self_cbarg() returns a magic value which makes event_new()
pass the event itself as the callback argument.

event.c
include/event2/event.h

diff --git a/event.c b/event.c
index 8935ad69ca22dec984f89e6a7ecd44a735857949..b98ba953371a4894a74bc22817685ed4f7cfcfa5 100644 (file)
--- a/event.c
+++ b/event.c
@@ -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);
index 54871eeb7e4301f74575d18f8c625c03efb85da8..9b5848364e3080909b7e696cfb11ead10e4cca48 100644 (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.