Skip to content

Commit

Permalink
Establish the scheduler in CallbackBase's constructor.
Browse files Browse the repository at this point in the history
There's no need to wait until we schedule a callback to set its
scheduler.
  • Loading branch information
caladri committed Jan 28, 2016
1 parent 0b9fd94 commit 24bddec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
27 changes: 19 additions & 8 deletions event/callback.cc
Expand Up @@ -26,6 +26,25 @@
#include <event/event_callback.h>
#include <event/event_system.h>

CallbackBase::CallbackBase(CallbackScheduler *scheduler, Lock *xlock)
: scheduler_(scheduler),
lock_(xlock),
scheduled_(false)
{
/*
* Use the default scheduler if we haven't been given one.
*
* This uses the legacy callback thread, as it were, which
* does not distribute callbacks over all available workers,
* but rather runs naive callbacks all in the same thread.
*
* Eventually this needs to pick a worker using the lock
* as an index into the available pool.
*/
if (scheduler_ == NULL)
scheduler_ = EventSystem::instance()->scheduler();
}

void
CallbackBase::cancel(void)
{
Expand All @@ -35,11 +54,3 @@ CallbackBase::cancel(void)
scheduler_->cancel(this);
scheduled_ = false;
}

Action *
CallbackBase::schedule(void)
{
if (scheduler_ != NULL)
return (scheduler_->schedule(this));
return (EventSystem::instance()->schedule(this));
}
15 changes: 6 additions & 9 deletions event/callback.h
Expand Up @@ -49,11 +49,7 @@ class CallbackBase : private Action {
Lock *lock_;
bool scheduled_;
protected:
CallbackBase(CallbackScheduler *scheduler, Lock *xlock)
: scheduler_(scheduler),
lock_(xlock),
scheduled_(false)
{ }
CallbackBase(CallbackScheduler *, Lock *);

virtual ~CallbackBase()
{ }
Expand All @@ -63,13 +59,14 @@ class CallbackBase : private Action {

virtual void execute(void) = 0;

Action *schedule(void);
Action *schedule(void)
{
ASSERT_NON_NULL("/callback/base", scheduler_);
return (scheduler_->schedule(this));
}

Action *scheduled(CallbackScheduler *scheduler)
{
/* Pin anything not already pinned. */
if (scheduler_ == NULL)
scheduler_ = scheduler;
/* It must not be pinned elsewhere. */
ASSERT("/callback/base", scheduler_ == scheduler);
/* It must not already be scheduled. */
Expand Down
10 changes: 5 additions & 5 deletions event/event_system.h
Expand Up @@ -70,11 +70,6 @@ class EventSystem {

Action *register_interest(const EventInterest&, SimpleCallback *);

Action *schedule(CallbackBase *cb)
{
return (td_.schedule(cb));
}

Action *timeout(unsigned ms, SimpleCallback *cb)
{
return (timeout_.timeout(ms, cb));
Expand All @@ -85,6 +80,11 @@ class EventSystem {
threads_.push_back(td);
}

CallbackScheduler *scheduler(void)
{
return (&td_);
}

CallbackScheduler *worker(void);

void start(void)
Expand Down

0 comments on commit 24bddec

Please sign in to comment.