From b8cc2491d8d2f239df29a6f85ad8b21e0221e8d1 Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 19 Sep 2020 19:16:39 +0200 Subject: added basic executor --- lib/matcha.h | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'lib/matcha.h') diff --git a/lib/matcha.h b/lib/matcha.h index 1506081..9fed7c2 100644 --- a/lib/matcha.h +++ b/lib/matcha.h @@ -1,18 +1,48 @@ +#include +#include + +struct Executor; + struct Thread { Thread(const Thread&) = delete; Thread& operator=(const Thread&) = delete; Thread(); + virtual ~Thread() {} virtual void threadFn() = 0; - // use from executor - void yield_to() const; - protected: - // use in thread void yield(); private: static void entry(void* obj); void* mStackPtr; + + friend struct Executor; + const Executor* mExecutor; +}; + + +struct Executor { + Executor(const Executor&) = delete; + Executor& operator=(const Executor&) = delete; + Executor() = default; + + const void* getStackPtr() const { return mStackPtr; } + + void spawn(std::unique_ptr t) { + mThreads.push_back(std::move(t)); + mThreads.back()->mExecutor = this; + } + void run() { + for (const std::unique_ptr& t : mThreads) { + yield_to(t.get()); + } + } + + private: + void* mStackPtr; + std::vector> mThreads; + + void yield_to(const Thread* t) const; }; -- cgit v1.2.3