Commit 67b9ea88 authored by Kenton Varda's avatar Kenton Varda

Refactor kj::Thread to share more code cross-platform.

parent cc74158d
...@@ -34,7 +34,7 @@ namespace kj { ...@@ -34,7 +34,7 @@ namespace kj {
#if _WIN32 #if _WIN32
Thread::Thread(Function<void()> func): state(new ThreadState { kj::mv(func), nullptr, 2 }) { Thread::Thread(Function<void()> func): state(new ThreadState(kj::mv(func))) {
threadHandle = CreateThread(nullptr, 0, &runThread, state, 0, nullptr); threadHandle = CreateThread(nullptr, 0, &runThread, state, 0, nullptr);
if (threadHandle == nullptr) { if (threadHandle == nullptr) {
state->unref(); state->unref();
...@@ -61,20 +61,9 @@ void Thread::detach() { ...@@ -61,20 +61,9 @@ void Thread::detach() {
detached = true; detached = true;
} }
DWORD Thread::runThread(void* ptr) {
ThreadState* state = reinterpret_cast<ThreadState*>(ptr);
KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() {
state->func();
})) {
state->exception = kj::mv(*exception);
}
state->unref();
return 0;
}
#else // _WIN32 #else // _WIN32
Thread::Thread(Function<void()> func): state(new ThreadState { kj::mv(func), nullptr, 2 }) { Thread::Thread(Function<void()> func): state(new ThreadState(kj::mv(func))) {
static_assert(sizeof(threadId) >= sizeof(pthread_t), static_assert(sizeof(threadId) >= sizeof(pthread_t),
"pthread_t is larger than a long long on your platform. Please port."); "pthread_t is larger than a long long on your platform. Please port.");
...@@ -119,19 +108,13 @@ void Thread::detach() { ...@@ -119,19 +108,13 @@ void Thread::detach() {
state->unref(); state->unref();
} }
void* Thread::runThread(void* ptr) {
ThreadState* state = reinterpret_cast<ThreadState*>(ptr);
KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() {
state->func();
})) {
state->exception = kj::mv(*exception);
}
state->unref();
return nullptr;
}
#endif // _WIN32, else #endif // _WIN32, else
Thread::ThreadState::ThreadState(Function<void()> func)
: func(kj::mv(func)),
exception(nullptr),
refcount(2) {}
void Thread::ThreadState::unref() { void Thread::ThreadState::unref() {
#if _MSC_VER #if _MSC_VER
if (_InterlockedDecrement(&refcount) == 0) { if (_InterlockedDecrement(&refcount) == 0) {
...@@ -148,4 +131,19 @@ void Thread::ThreadState::unref() { ...@@ -148,4 +131,19 @@ void Thread::ThreadState::unref() {
} }
} }
#if _WIN32
DWORD Thread::runThread(void* ptr) {
#else
void* Thread::runThread(void* ptr) {
#endif
ThreadState* state = reinterpret_cast<ThreadState*>(ptr);
KJ_IF_MAYBE(exception, kj::runCatchingExceptions([&]() {
state->func();
})) {
state->exception = kj::mv(*exception);
}
state->unref();
return 0;
}
} // namespace kj } // namespace kj
...@@ -53,6 +53,8 @@ public: ...@@ -53,6 +53,8 @@ public:
private: private:
struct ThreadState { struct ThreadState {
ThreadState(Function<void()> func);
Function<void()> func; Function<void()> func;
kj::Maybe<kj::Exception> exception; kj::Maybe<kj::Exception> exception;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment