aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2020-09-26 23:58:24 +0200
committerjohannst <johannes.stoelp@gmail.com>2020-09-26 23:58:24 +0200
commit1b1bd56777358ef2fa538a9b7b62c6e8c823489b (patch)
treeeeeb80ac415999857093631696c81b02d2217bcc
parent5cbdde2c97229a552852af57232c4e5bbe814a38 (diff)
downloadmatcha-threads-1b1bd56777358ef2fa538a9b7b62c6e8c823489b.tar.gz
matcha-threads-1b1bd56777358ef2fa538a9b7b62c6e8c823489b.zip
added finished flag to thread + catch exceptions in thread entry function as unwinding across entry fn would be catastrophic
-rw-r--r--lib/executor.cc3
-rw-r--r--lib/thread.cc16
-rw-r--r--lib/thread.h3
3 files changed, 19 insertions, 3 deletions
diff --git a/lib/executor.cc b/lib/executor.cc
index fb79b49..af49f0e 100644
--- a/lib/executor.cc
+++ b/lib/executor.cc
@@ -12,6 +12,9 @@ namespace nMatcha {
void Executor::run() {
for (const std::unique_ptr<Thread>& t : mThreads) {
+ if (t->isFinished()) {
+ continue;
+ }
yield_to(t.get());
}
}
diff --git a/lib/thread.cc b/lib/thread.cc
index d665f49..ec7fd64 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -6,7 +6,9 @@
#include "executor.h"
#include <cassert>
-#include <cstdint> // uintN_t
+#include <cstdint> // uintN_t
+#include <exception>
+#include <iostream>
#include <sys/mman.h> // mmap
#include <unistd.h> // sysconf
@@ -15,7 +17,7 @@ namespace {
} // namespace
namespace nMatcha {
- Thread::Thread() : mStackPtr(nullptr), mExecutor(nullptr) {
+ Thread::Thread() : mStackPtr(nullptr), mFinished(false), mExecutor(nullptr) {
const long PAGE_SIZE = get_pagesize();
const long STACK_SIZE = 8 * PAGE_SIZE;
@@ -64,7 +66,15 @@ namespace nMatcha {
void Thread::entry(void* obj) {
Thread* t = static_cast<Thread*>(obj);
- t->threadFn();
+ try {
+ t->threadFn();
+ } catch (const std::exception& e) {
+ std::cerr << "Thread: caught unhandled std::exception!\n" << e.what() << std::endl;
+ } catch (...) {
+ std::cerr << "Thread: caught unhandled unknown exception!" << std::endl;
+ }
+ t->mFinished = true;
+ t->yield();
}
void Thread::yield() {
diff --git a/lib/thread.h b/lib/thread.h
index e392052..d983734 100644
--- a/lib/thread.h
+++ b/lib/thread.h
@@ -13,12 +13,15 @@ namespace nMatcha {
virtual void threadFn() = 0;
+ bool isFinished() const { return mFinished; }
+
protected:
void yield();
private:
static void entry(void* obj);
void* mStackPtr;
+ bool mFinished;
friend struct Executor;
const Executor* mExecutor;