Commit b4d403f5 authored by Kenton Varda's avatar Kenton Varda

kj::Thread

parent fc0c297c
...@@ -110,7 +110,8 @@ includekj_HEADERS = \ ...@@ -110,7 +110,8 @@ includekj_HEADERS = \
src/kj/arena.h \ src/kj/arena.h \
src/kj/io.h \ src/kj/io.h \
src/kj/tuple.h \ src/kj/tuple.h \
src/kj/mutex.h src/kj/mutex.h \
src/kj/thread.h
includekjparse_HEADERS = \ includekjparse_HEADERS = \
src/kj/parse/common.h \ src/kj/parse/common.h \
...@@ -148,6 +149,7 @@ libcapnp_la_SOURCES= \ ...@@ -148,6 +149,7 @@ libcapnp_la_SOURCES= \
src/kj/arena.c++ \ src/kj/arena.c++ \
src/kj/io.c++ \ src/kj/io.c++ \
src/kj/mutex.c++ \ src/kj/mutex.c++ \
src/kj/thread.c++ \
src/kj/parse/char.c++ \ src/kj/parse/char.c++ \
src/capnp/blob.c++ \ src/capnp/blob.c++ \
src/capnp/arena.h \ src/capnp/arena.h \
...@@ -198,6 +200,7 @@ capnp_test_SOURCES = \ ...@@ -198,6 +200,7 @@ capnp_test_SOURCES = \
src/kj/units-test.c++ \ src/kj/units-test.c++ \
src/kj/tuple-test.c++ \ src/kj/tuple-test.c++ \
src/kj/mutex-test.c++ \ src/kj/mutex-test.c++ \
src/kj/thread-test.c++ \
src/kj/parse/common-test.c++ \ src/kj/parse/common-test.c++ \
src/kj/parse/char-test.c++ \ src/kj/parse/char-test.c++ \
src/capnp/blob-test.c++ \ src/capnp/blob-test.c++ \
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "arena.h" #include "arena.h"
#include "debug.h" #include "debug.h"
#include "thread.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <stdint.h> #include <stdint.h>
...@@ -306,32 +307,6 @@ TEST(Arena, Strings) { ...@@ -306,32 +307,6 @@ TEST(Arena, Strings) {
EXPECT_EQ(quux.end() + 1, corge.begin()); EXPECT_EQ(quux.end() + 1, corge.begin());
} }
// I tried to use std::thread but it threw a pure-virtual exception. It's unclear if it's meant
// to be ready in GCC 4.7.
class Thread {
public:
template <typename Func>
explicit Thread(Func&& func) {
KJ_ASSERT(pthread_create(
&thread, nullptr, &runThread<Decay<Func>>,
new Decay<Func>(kj::fwd<Func>(func))) == 0);
}
~Thread() {
KJ_ASSERT(pthread_join(thread, nullptr) == 0);
}
private:
pthread_t thread;
template <typename Func>
static void* runThread(void* ptr) {
Func* func = reinterpret_cast<Func*>(ptr);
KJ_DEFER(delete func);
(*func)();
return nullptr;
}
};
struct ThreadTestObject { struct ThreadTestObject {
ThreadTestObject* next; ThreadTestObject* next;
void* owner; // points into the owning thread's stack void* owner; // points into the owning thread's stack
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "mutex.h" #include "mutex.h"
#include "debug.h" #include "debug.h"
#include "thread.h"
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -30,33 +31,6 @@ ...@@ -30,33 +31,6 @@
namespace kj { namespace kj {
namespace { namespace {
// I tried to use std::thread but it threw a pure-virtual exception. It's unclear if it's meant
// to be ready in GCC 4.7.
class Thread {
public:
template <typename Func>
explicit Thread(Func&& func) {
KJ_ASSERT(pthread_create(
&thread, nullptr, &runThread<Decay<Func>>,
new Decay<Func>(kj::fwd<Func>(func))) == 0);
}
~Thread() {
KJ_ASSERT(pthread_join(thread, nullptr) == 0);
}
private:
pthread_t thread;
template <typename Func>
static void* runThread(void* ptr) {
Func* func = reinterpret_cast<Func*>(ptr);
KJ_DEFER(delete func);
(*func)();
return nullptr;
}
};
inline void delay() { usleep(10000); } inline void delay() { usleep(10000); }
TEST(Mutex, MutexGuarded) { TEST(Mutex, MutexGuarded) {
......
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "thread.h"
#include "debug.h"
#include <pthread.h>
namespace kj {
Thread::Thread(void* (*run)(void*), void (*deleteArg)(void*), void* arg) {
static_assert(sizeof(threadId) >= sizeof(pthread_t),
"pthread_t is larger than a long long on your platform. Please port.");
int pthreadResult = pthread_create(reinterpret_cast<pthread_t*>(&threadId), nullptr, run, arg);
if (pthreadResult != 0) {
deleteArg(arg);
KJ_FAIL_SYSCALL("pthread_create", pthreadResult);
}
}
Thread::~Thread() {
KJ_ASSERT(pthread_join(threadId, nullptr) == 0);
}
} // namespace kj
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef KJ_THREAD_H_
#define KJ_THREAD_H_
#include "common.h"
namespace kj {
class Thread {
// A thread! Pass a lambda to the constructor. The destructor joins the thread.
public:
template <typename Func>
explicit Thread(Func&& func)
: Thread(&runThread<Decay<Func>>,
&deleteArg<Decay<Func>>,
new Decay<Func>(kj::fwd<Func>(func))) {}
~Thread();
private:
unsigned long long threadId; // actually pthread_t
Thread(void* (*run)(void*), void (*deleteArg)(void*), void* arg);
template <typename Func>
static void* runThread(void* ptr) {
// TODO(someday): Catch exceptions and propagate to the joiner.
Func* func = reinterpret_cast<Func*>(ptr);
KJ_DEFER(delete func);
(*func)();
return nullptr;
}
template <typename Func>
static void deleteArg(void* ptr) {
delete reinterpret_cast<Func*>(ptr);
}
};
} // namespace kj
#endif // KJ_THREAD_H_
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