aboutsummaryrefslogtreecommitdiff
path: root/lib/matcha.h
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2020-09-19 19:16:39 +0200
committerjohannst <johannes.stoelp@gmail.com>2020-09-19 19:39:01 +0200
commitb8cc2491d8d2f239df29a6f85ad8b21e0221e8d1 (patch)
treeb8f1f354586198d78168fc8f6e0905a4cce128cb /lib/matcha.h
parent1f415f839ccdcc0f455d8b4c9adacbf0f60fc89f (diff)
downloadmatcha-threads-b8cc2491d8d2f239df29a6f85ad8b21e0221e8d1.tar.gz
matcha-threads-b8cc2491d8d2f239df29a6f85ad8b21e0221e8d1.zip
added basic executor
Diffstat (limited to 'lib/matcha.h')
-rw-r--r--lib/matcha.h38
1 files changed, 34 insertions, 4 deletions
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 <memory>
+#include <vector>
+
+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<Thread> t) {
+ mThreads.push_back(std::move(t));
+ mThreads.back()->mExecutor = this;
+ }
+ void run() {
+ for (const std::unique_ptr<Thread>& t : mThreads) {
+ yield_to(t.get());
+ }
+ }
+
+ private:
+ void* mStackPtr;
+ std::vector<std::unique_ptr<Thread>> mThreads;
+
+ void yield_to(const Thread* t) const;
};