Created
November 2, 2018 18:08
-
-
Save MonsieurNicolas/6576a0c58504453433c5ec483ee2ebc4 to your computer and use it in GitHub Desktop.
sketch for how to remove wakeup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/work/BasicWork.cpp b/src/work/BasicWork.cpp | |
index f968d383..9ad06553 100644 | |
--- a/src/work/BasicWork.cpp | |
+++ b/src/work/BasicWork.cpp | |
@@ -195,11 +195,6 @@ BasicWork::onFailureRaise() | |
{ | |
} | |
-void | |
-BasicWork::onWakeUp() | |
-{ | |
-} | |
- | |
BasicWork::State | |
BasicWork::getState() const | |
{ | |
@@ -269,7 +264,6 @@ BasicWork::wakeUp() | |
return; | |
} | |
setState(InternalState::RUNNING); | |
- onWakeUp(); | |
if (mNotifyCallback) | |
{ | |
mNotifyCallback(); | |
@@ -277,15 +271,19 @@ BasicWork::wakeUp() | |
} | |
std::function<void()> | |
-BasicWork::wakeUpCallback() | |
+BasicWork::wakeUpCallback(std::function<void()> cb) | |
{ | |
std::weak_ptr<BasicWork> weak = shared_from_this(); | |
- auto callback = [weak]() { | |
+ auto callback = [weak, cb]() { | |
auto self = weak.lock(); | |
if (self) | |
{ | |
self->wakeUp(); | |
} | |
+ if (cb) | |
+ { | |
+ cb(); | |
+ } | |
}; | |
return callback; | |
} | |
diff --git a/src/work/BasicWork.h b/src/work/BasicWork.h | |
index 3f1fb096..93b36394 100644 | |
--- a/src/work/BasicWork.h | |
+++ b/src/work/BasicWork.h | |
@@ -87,7 +87,6 @@ class BasicWork : public std::enable_shared_from_this<BasicWork>, | |
virtual void onReset(); | |
virtual State onRun() = 0; | |
- virtual void onWakeUp(); | |
virtual void onFailureRetry(); | |
virtual void onFailureRaise(); | |
virtual void onSuccess(); | |
@@ -101,7 +100,7 @@ class BasicWork : public std::enable_shared_from_this<BasicWork>, | |
void wakeUp(); | |
// Default wakeUp callback that implementers can use | |
- std::function<void()> wakeUpCallback(); | |
+ std::function<void()> wakeUpCallback(std::function<void()> cb); | |
Application& mApp; | |
diff --git a/src/work/Work.cpp b/src/work/Work.cpp | |
index f016b1b2..0650368d 100644 | |
--- a/src/work/Work.cpp | |
+++ b/src/work/Work.cpp | |
@@ -194,7 +194,7 @@ WorkSequence::doWork() | |
assert(w); | |
if (!hasChildren()) | |
{ | |
- w->startWork(wakeUpCallback()); | |
+ w->startWork(wakeUpCallback(nullptr)); | |
addChild(w); | |
return State::WORK_RUNNING; | |
} | |
diff --git a/src/work/Work.h b/src/work/Work.h | |
index d4f8939b..cb06901d 100644 | |
--- a/src/work/Work.h | |
+++ b/src/work/Work.h | |
@@ -6,6 +6,7 @@ | |
#include "util/Timer.h" | |
#include "work/BasicWork.h" | |
+#include <functional> | |
#include <list> | |
#include <map> | |
#include <memory> | |
@@ -55,15 +56,22 @@ class Work : public BasicWork | |
// the reference). | |
template <typename T, typename... Args> | |
std::shared_ptr<T> | |
- addWork(Args&&... args) | |
+ addWorkCB(std::function<void()> wakeUpCB, Args&&... args) | |
{ | |
auto child = std::make_shared<T>(mApp, std::forward<Args>(args)...); | |
addChild(child); | |
- child->startWork(wakeUpCallback()); | |
+ child->startWork(wakeUpCallback(wakeUpCB)); | |
wakeUp(); | |
return child; | |
} | |
+ template <typename T, typename... Args> | |
+ std::shared_ptr<T> | |
+ addWork(Args&&... args) | |
+ { | |
+ return addWorkCB<T>(nullptr, std::forward<Args>(args)...); | |
+ } | |
+ | |
State onRun() final; | |
void onReset() final; | |
diff --git a/src/work/WorkScheduler.cpp b/src/work/WorkScheduler.cpp | |
index cff2ce80..36ace6ee 100644 | |
--- a/src/work/WorkScheduler.cpp | |
+++ b/src/work/WorkScheduler.cpp | |
@@ -16,26 +16,30 @@ WorkScheduler::~WorkScheduler() | |
} | |
std::shared_ptr<WorkScheduler> | |
-WorkScheduler::create(Application& app, std::function<void()> wakeUpCallback) | |
+WorkScheduler::create(Application& app) | |
{ | |
auto work = std::shared_ptr<WorkScheduler>(new WorkScheduler(app)); | |
- work->startWork(wakeUpCallback); | |
+ work->startWork(nullptr); | |
work->crankWork(); | |
return work; | |
}; | |
void | |
-WorkScheduler::onWakeUp() | |
+WorkScheduler::scheduleOne(std::weak_ptr<WorkScheduler> weak) | |
{ | |
- if (mScheduled) | |
+ auto self = weak.lock(); | |
+ if (!self) | |
{ | |
return; | |
} | |
- std::weak_ptr<WorkScheduler> weak( | |
- std::static_pointer_cast<WorkScheduler>(shared_from_this())); | |
- mScheduled = true; | |
- mApp.getClock().getIOService().post([weak]() { | |
+ if (self->mScheduled) | |
+ { | |
+ return; | |
+ } | |
+ self->mScheduled = true; | |
+ | |
+ self->mApp.getClock().getIOService().post([weak]() { | |
auto self = weak.lock(); | |
if (!self) | |
{ | |
@@ -45,7 +49,7 @@ WorkScheduler::onWakeUp() | |
self->crankWork(); | |
if (self->getState() == State::WORK_RUNNING) | |
{ | |
- self->onWakeUp(); | |
+ scheduleOne(weak); | |
} | |
}); | |
} | |
diff --git a/src/work/WorkScheduler.h b/src/work/WorkScheduler.h | |
index debb9a8f..95fda770 100644 | |
--- a/src/work/WorkScheduler.h | |
+++ b/src/work/WorkScheduler.h | |
@@ -11,8 +11,7 @@ namespace stellar | |
/** | |
* WorkScheduler is a top level Work, that is in charge of scheduling | |
- * cranks (posting to the IO service); this is done via custom | |
- * implementation of `onWakeUp`, which schedules a crank if necessary. | |
+ * cranks (posting to the IO service). | |
* | |
* WorkScheduler attempts fair scheduling by doing round-robin among | |
* works that wish to execute. | |
@@ -24,14 +23,26 @@ class WorkScheduler : public Work | |
public: | |
virtual ~WorkScheduler(); | |
- static std::shared_ptr<WorkScheduler> | |
- create(Application& app, std::function<void()> wakeUpCallback = nullptr); | |
+ static std::shared_ptr<WorkScheduler> create(Application& app); | |
+ | |
+ template <typename T, typename... Args> | |
+ std::shared_ptr<T> | |
+ scheduleWork(Args&&... args) | |
+ { | |
+ std::weak_ptr<WorkScheduler> weak( | |
+ std::static_pointer_cast<WorkScheduler>(shared_from_this())); | |
+ | |
+ auto work = addWorkCB<T>([weak]() { scheduleOne(weak); }, | |
+ std::forward<Args>(args)...); | |
+ | |
+ return work; | |
+ } | |
template <typename T, typename... Args> | |
std::shared_ptr<T> | |
executeWork(Args&&... args) | |
{ | |
- auto work = addWork<T>(std::forward<Args>(args)...); | |
+ auto work = scheduleWork<T>(std::forward<Args>(args)...); | |
auto& clock = mApp.getClock(); | |
while (!clock.getIOService().stopped() && !allChildrenDone()) | |
{ | |
@@ -40,15 +51,8 @@ class WorkScheduler : public Work | |
return work; | |
} | |
- template <typename T, typename... Args> | |
- std::shared_ptr<T> | |
- scheduleWork(Args&&... args) | |
- { | |
- return addWork<T>(std::forward<Args>(args)...); | |
- } | |
- | |
protected: | |
- void onWakeUp() override; | |
+ static void scheduleOne(std::weak_ptr<WorkScheduler> weak); | |
State doWork() override; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment