diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/matcha.cc | 12 | ||||
-rw-r--r-- | lib/matcha.h | 18 |
2 files changed, 17 insertions, 13 deletions
diff --git a/lib/matcha.cc b/lib/matcha.cc index 3f7cd7b..561a08f 100644 --- a/lib/matcha.cc +++ b/lib/matcha.cc @@ -14,7 +14,7 @@ long get_pagesize() { return sysconf(_SC_PAGESIZE); } -Thread::Thread(void (*fn)()) : mUserFn(fn) { +Thread::Thread() { const long PAGE_SIZE = get_pagesize(); const long STACK_SIZE = 8 * PAGE_SIZE; @@ -53,16 +53,16 @@ void Thread::entry(void* obj) { Thread* t = static_cast<Thread*>(obj); puts("thread entry"); - t->mUserFn(); + t->threadFn(); puts("thread done"); } void* gOriginalStack; -void yield_to(const Thread& t) { - yield(t.mStackPtr, &gOriginalStack); +void Thread::yield() { + ::yield(gOriginalStack, &mStackPtr); } -void yield_from(Thread& t) { - yield(gOriginalStack, &t.mStackPtr); +void Thread::yield_to() const { + ::yield(mStackPtr, &gOriginalStack); } diff --git a/lib/matcha.h b/lib/matcha.h index 905107f..1506081 100644 --- a/lib/matcha.h +++ b/lib/matcha.h @@ -1,14 +1,18 @@ struct Thread { Thread(const Thread&) = delete; Thread& operator=(const Thread&) = delete; - Thread(void (*fn)()); + Thread(); - static void entry(void* obj); + virtual void threadFn() = 0; + + // use from executor + void yield_to() const; + + protected: + // use in thread + void yield(); - // private: + private: + static void entry(void* obj); void* mStackPtr; - void (*mUserFn)(); }; - -void yield_to(const Thread& t); -void yield_from(Thread& t); |