Permalink
Browse files

Replace deferred_cbs with event_callback-based implementation.

  • Loading branch information...
Nick Mathewson
Nick Mathewson committed Apr 6, 2012
1 parent fec8bae commit ae2b84b2575be93d0aebba5c0b78453836f89f3c
Showing with 94 additions and 188 deletions.
  1. +6 −7 buffer.c
  2. +1 −0 bufferevent-internal.h
  3. +12 −11 bufferevent.c
  4. +10 −46 defer-internal.h
  5. +1 −0 evbuffer-internal.h
  6. +2 −1 evdns.c
  7. +5 −7 event-internal.h
  8. +49 −113 event.c
  9. +2 −1 http.c
  10. +4 −1 include/event2/event_struct.h
  11. +1 −0 test/regress_buffer.c
  12. +1 −1 test/regress_thread.c
View
@@ -404,7 +404,7 @@ evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base)
EVBUFFER_LOCK(buffer);
buffer->cb_queue = event_base_get_deferred_cb_queue_(base);
buffer->deferred_cbs = 1;
- event_deferred_cb_init_(&buffer->deferred,
+ event_deferred_cb_init_(base, &buffer->deferred,
evbuffer_deferred_callback, buffer);
EVBUFFER_UNLOCK(buffer);
return 0;
@@ -509,13 +509,12 @@ evbuffer_invoke_callbacks_(struct evbuffer *buffer)
}
if (buffer->deferred_cbs) {
- if (buffer->deferred.queued)
- return;
- evbuffer_incref_and_lock_(buffer);
- if (buffer->parent)
- bufferevent_incref_(buffer->parent);
+ if (event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred)) {
+ evbuffer_incref_and_lock_(buffer);
+ if (buffer->parent)
+ bufferevent_incref_(buffer->parent);
+ }
EVBUFFER_UNLOCK(buffer);
- event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred);
}
evbuffer_run_callbacks(buffer, 0);
View
@@ -31,6 +31,7 @@ extern "C" {
#endif
#include "event2/event-config.h"
+#include "event2/event_struct.h"
#include "evconfig-private.h"
#include "event2/util.h"
#include "defer-internal.h"
View
@@ -210,10 +210,10 @@ bufferevent_run_deferred_callbacks_unlocked(struct deferred_cb *cb, void *arg)
#define SCHEDULE_DEFERRED(bevp) \
do { \
- bufferevent_incref_(&(bevp)->bev); \
- event_deferred_cb_schedule_( \
+ if (event_deferred_cb_schedule_( \
event_base_get_deferred_cb_queue_((bevp)->bev.ev_base), \
- &(bevp)->deferred); \
+ &(bevp)->deferred)) \
+ bufferevent_incref_(&(bevp)->bev); \
} while (0)
@@ -227,8 +227,7 @@ bufferevent_run_readcb_(struct bufferevent *bufev)
return;
if (p->options & BEV_OPT_DEFER_CALLBACKS) {
p->readcb_pending = 1;
- if (!p->deferred.queued)
- SCHEDULE_DEFERRED(p);
+ SCHEDULE_DEFERRED(p);
} else {
bufev->readcb(bufev, bufev->cbarg);
}
@@ -244,8 +243,7 @@ bufferevent_run_writecb_(struct bufferevent *bufev)
return;
if (p->options & BEV_OPT_DEFER_CALLBACKS) {
p->writecb_pending = 1;
- if (!p->deferred.queued)
- SCHEDULE_DEFERRED(p);
+ SCHEDULE_DEFERRED(p);
} else {
bufev->writecb(bufev, bufev->cbarg);
}
@@ -262,8 +260,7 @@ bufferevent_run_eventcb_(struct bufferevent *bufev, short what)
if (p->options & BEV_OPT_DEFER_CALLBACKS) {
p->eventcb_pending |= what;
p->errno_pending = EVUTIL_SOCKET_ERROR();
- if (!p->deferred.queued)
- SCHEDULE_DEFERRED(p);
+ SCHEDULE_DEFERRED(p);
} else {
bufev->errorcb(bufev, what, bufev->cbarg);
}
@@ -326,11 +323,15 @@ bufferevent_init_common_(struct bufferevent_private *bufev_private,
}
if (options & BEV_OPT_DEFER_CALLBACKS) {
if (options & BEV_OPT_UNLOCK_CALLBACKS)
- event_deferred_cb_init_(&bufev_private->deferred,
+ event_deferred_cb_init_(
+ bufev->ev_base,
+ &bufev_private->deferred,
bufferevent_run_deferred_callbacks_unlocked,
bufev_private);
else
- event_deferred_cb_init_(&bufev_private->deferred,
+ event_deferred_cb_init_(
+ bufev->ev_base,
+ &bufev_private->deferred,
bufferevent_run_deferred_callbacks_locked,
bufev_private);
}
View
@@ -35,43 +35,11 @@ extern "C" {
#include <sys/queue.h>
-struct deferred_cb;
+#define deferred_cb event_callback
+#define deferred_cb_queue event_base
+struct event_callback;
-typedef void (*deferred_cb_fn)(struct deferred_cb *, void *);
-
-/** A deferred_cb is a callback that can be scheduled to run as part of
- * an event_base's event_loop, rather than running immediately. */
-struct deferred_cb {
- /** Links to the adjacent active (pending) deferred_cb objects. */
- TAILQ_ENTRY (deferred_cb) cb_next;
- /** True iff this deferred_cb is pending in an event_base. */
- unsigned queued : 1;
- /** The function to execute when the callback runs. */
- deferred_cb_fn cb;
- /** The function's second argument. */
- void *arg;
-};
-
-/** A deferred_cb_queue is a list of deferred_cb that we can add to and run. */
-struct deferred_cb_queue {
- /** Lock used to protect the queue. */
- void *lock;
-
- /** Which event_base does this queue associate itself with?
- * (Used for timing) */
- struct event_base *base;
-
- /** How many entries are in the queue? */
- int active_count;
-
- /** Function called when adding to the queue from another thread. */
- void (*notify_fn)(struct deferred_cb_queue *, void *);
- void *notify_arg;
-
- /** Deferred callback management: a list of deferred callbacks to
- * run active the active events. */
- TAILQ_HEAD (deferred_cb_list, deferred_cb) deferred_cb_list;
-};
+typedef void (*deferred_cb_fn)(struct event_callback *, void *);
/**
Initialize an empty, non-pending deferred_cb.
@@ -80,27 +48,23 @@ struct deferred_cb_queue {
@param cb The function to run when the deferred_cb executes.
@param arg The function's second argument.
*/
-void event_deferred_cb_init_(struct deferred_cb *, deferred_cb_fn, void *);
+void event_deferred_cb_init_(struct event_base *base, struct event_callback *, deferred_cb_fn, void *);
/**
Cancel a deferred_cb if it is currently scheduled in an event_base.
*/
-void event_deferred_cb_cancel_(struct deferred_cb_queue *, struct deferred_cb *);
+void event_deferred_cb_cancel_(struct event_base *, struct event_callback *);
/**
Activate a deferred_cb if it is not currently scheduled in an event_base.
- */
-void event_deferred_cb_schedule_(struct deferred_cb_queue *, struct deferred_cb *);
-#define LOCK_DEFERRED_QUEUE(q) \
- EVLOCK_LOCK((q)->lock, 0)
-#define UNLOCK_DEFERRED_QUEUE(q) \
- EVLOCK_UNLOCK((q)->lock, 0)
+ Return true iff it was not previously scheduled.
+ */
+int event_deferred_cb_schedule_(struct event_base *, struct event_callback *);
#ifdef __cplusplus
}
#endif
-void event_deferred_cb_queue_init_(struct deferred_cb_queue *);
-struct deferred_cb_queue *event_base_get_deferred_cb_queue_(struct event_base *);
+#define event_base_get_deferred_cb_queue_(x) (x)
#endif /* EVENT_INTERNAL_H_INCLUDED_ */
View
@@ -34,6 +34,7 @@ extern "C" {
#include "event2/event-config.h"
#include "evconfig-private.h"
#include "event2/util.h"
+#include "event2/event_struct.h"
#include "util-internal.h"
#include "defer-internal.h"
View
@@ -836,7 +836,8 @@ reply_schedule_callback(struct request *const req, u32 ttl, u32 err, struct repl
d->handle = req->handle;
}
- event_deferred_cb_init_(&d->deferred, reply_run_callback,
+ event_deferred_cb_init_(req->base->event_base,
+ &d->deferred, reply_run_callback,
req->user_pointer);
event_deferred_cb_schedule_(
event_base_get_deferred_cb_queue_(req->base->event_base),
View
@@ -56,13 +56,14 @@ extern "C" {
#define ev_pri ev_evcallback.evcb_pri
#define ev_flags ev_evcallback.evcb_flags
#define ev_closure ev_evcallback.evcb_closure
-#define ev_callback ev_evcallback.evcb_callback
+#define ev_callback ev_evcallback.evcb_cb_union.evcb_callback
#define ev_arg ev_evcallback.evcb_arg
/* Possible values for evcb_closure in struct event_callback */
#define EV_CLOSURE_EVENT 0
#define EV_CLOSURE_EVENT_SIGNAL 1
#define EV_CLOSURE_EVENT_PERSIST 2
+#define EV_CLOSURE_CB_SELF 3
/** Structure to define the backend of a given event_base. */
struct eventop {
@@ -239,10 +240,6 @@ struct event_base {
/** The total size of common_timeout_queues. */
int n_common_timeouts_allocated;
- /** List of defered_cb that are active. We run these after the active
- * events. */
- struct deferred_cb_queue defer_queue;
-
/** Mapping from file descriptors to enabled (added) events */
struct event_io_map io;
@@ -358,15 +355,16 @@ struct event_config {
#endif /* TAILQ_FOREACH */
#define N_ACTIVE_CALLBACKS(base) \
- ((base)->event_count_active + (base)->defer_queue.active_count)
+ ((base)->event_count_active)
int evsig_set_handler_(struct event_base *base, int evsignal,
void (*fn)(int));
int evsig_restore_handler_(struct event_base *base, int evsignal);
void event_active_nolock_(struct event *ev, int res, short count);
-void event_callback_activate_nolock_(struct event_base *, struct event_callback *);
+int event_callback_activate_(struct event_base *, struct event_callback *);
+int event_callback_activate_nolock_(struct event_base *, struct event_callback *);
int event_callback_cancel_(struct event_base *base,
struct event_callback *evcb);
Oops, something went wrong.

0 comments on commit ae2b84b

Please sign in to comment.