-
Notifications
You must be signed in to change notification settings - Fork 11
Custom executor support #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,10 +43,11 @@ class AlicaEngine | |
| ~AlicaEngine(); | ||
|
|
||
| // State modifiers: | ||
| bool init(AlicaCreators&& creatorCtx); | ||
| bool init(AlicaCreators&& creatorCtx, const std::shared_ptr<IExecutor>& executor); | ||
| void start(); | ||
| void terminate(); | ||
| void stepNotify(); | ||
| void step(); | ||
|
|
||
| // Parameter Access: | ||
| // bool getStepEngine() const; | ||
|
|
@@ -134,6 +135,7 @@ class AlicaEngine | |
| ExpressionHandler _expressionHandler; | ||
| AuthorityManager _auth; | ||
| DefaultTransitionConditionCreator _defaultTransitionConditionCreator; | ||
| std::shared_ptr<IExecutor> _executor; | ||
|
||
|
|
||
| /** | ||
| * TODO: Make VariableSyncModule a stack variable. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #pragma once | ||
| #include "engine/IExecutor.h" | ||
| #include <atomic> | ||
| #include <memory> | ||
| #include <thread> | ||
|
|
||
| namespace alica | ||
| { | ||
|
|
||
| class AsyncExecutor : public IExecutor | ||
| { | ||
|
|
||
| public: | ||
| AsyncExecutor(){}; | ||
| ~AsyncExecutor() override { stop(); }; | ||
|
|
||
| void start() override; | ||
| void stop() override; | ||
| void run() override; | ||
|
|
||
| private: | ||
| std::unique_ptr<std::thread> _thread; | ||
| std::atomic<bool> _running{false}; | ||
| }; | ||
|
|
||
| class DrivenExecutor : public IExecutor | ||
| { | ||
| public: | ||
| DrivenExecutor(){}; | ||
| ~DrivenExecutor() override = default; | ||
|
|
||
| void start() override{}; | ||
| void stop() override{}; | ||
| void run() override; | ||
| }; | ||
|
|
||
| } // namespace alica |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
| #include <functional> | ||
|
|
||
| namespace alica | ||
| { | ||
| class IExecutor | ||
| { | ||
| public: | ||
| virtual ~IExecutor() = default; | ||
| virtual void start() = 0; | ||
| virtual void stop() = 0; | ||
| virtual void run() = 0; // only for driven executor | ||
|
||
| void attachRunCb(std::function<void()>&& runCb); | ||
|
|
||
| protected: | ||
| std::function<void()> _run_cb = nullptr; | ||
| }; | ||
| } // namespace alica | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #include "engine/Executor.h" | ||
| #include <cassert> | ||
|
|
||
| namespace alica | ||
| { | ||
|
|
||
| void AsyncExecutor::start() | ||
| { | ||
| assert(_run_cb); | ||
| _running = true; | ||
| _thread = std::make_unique<std::thread>(&AsyncExecutor::run, this); | ||
| } | ||
|
|
||
| void AsyncExecutor::stop() | ||
| { | ||
| _running = false; | ||
| if (_thread) { | ||
| _thread->join(); | ||
| _thread.reset(); | ||
| } | ||
| } | ||
|
|
||
| void AsyncExecutor::run() | ||
| { | ||
| assert(_run_cb); | ||
| while (_running) { | ||
| _run_cb(); | ||
|
||
| } | ||
| } | ||
|
|
||
| void DrivenExecutor::run() | ||
| { | ||
| assert(_run_cb); | ||
| _run_cb(); | ||
| } | ||
|
|
||
| } // namespace alica | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #include "engine/IExecutor.h" | ||
|
|
||
| namespace alica | ||
| { | ||
|
|
||
| void IExecutor::attachRunCb(std::function<void()>&& runCb) | ||
| { | ||
| _run_cb = std::move(runCb); | ||
| } | ||
|
|
||
| } // namespace alica |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a setter for it like with the other injected dependencies?