From 8020b411cdd534a6cf6df60880f6c0980d428365 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 2 Apr 2014 14:24:32 -0400 Subject: Use std::thread for the threadengine --- src/CMakeLists.txt | 1 - src/threadengine.cpp | 101 +++++++++++++----------------------- src/win32/anope_windows.h | 1 - src/win32/pthread/pthread.cpp | 118 ------------------------------------------ src/win32/pthread/pthread.h | 34 ------------ 5 files changed, 35 insertions(+), 220 deletions(-) delete mode 100644 src/win32/pthread/pthread.cpp delete mode 100644 src/win32/pthread/pthread.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fd834fc16..8dadd2122 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,6 @@ if(WIN32) list(APPEND SRC_SRCS win32/windows.cpp) list(APPEND SRC_SRCS win32/dl/dl.cpp) list(APPEND SRC_SRCS win32/pipe/pipe.cpp) - list(APPEND SRC_SRCS win32/pthread/pthread.cpp) list(APPEND SRC_SRCS win32/sigaction/sigaction.cpp) endif(WIN32) diff --git a/src/threadengine.cpp b/src/threadengine.cpp index 40f2be094..3b709ddb6 100644 --- a/src/threadengine.cpp +++ b/src/threadengine.cpp @@ -13,37 +13,7 @@ #include "services.h" #include "threadengine.h" #include "anope.h" - -#ifndef _WIN32 -#include -#endif - -static inline pthread_attr_t *get_engine_attr() -{ - /* Threadengine attributes used by this thread engine */ - static pthread_attr_t attr; - static bool inited = false; - - if (inited == false) - { - if (pthread_attr_init(&attr)) - throw CoreException("Error calling pthread_attr_init"); - if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) - throw CoreException("Unable to mark threads as joinable"); - inited = true; - } - - return &attr; -} - -static void *entry_point(void *parameter) -{ - Thread *thread = static_cast(parameter); - thread->Run(); - thread->SetExitState(); - pthread_exit(0); - return NULL; -} +#include Thread::Thread() : exit(false) { @@ -56,7 +26,15 @@ Thread::~Thread() void Thread::Join() { this->SetExitState(); - pthread_join(handle, NULL); + try + { + if (this->handle.joinable()) + this->handle.join(); + } + catch (const std::system_error &error) + { + throw CoreException("Unable to join thread: " + Anope::string(error.what())); + } } void Thread::SetExitState() @@ -65,18 +43,28 @@ void Thread::SetExitState() exit = true; } -void Thread::Exit() -{ - this->SetExitState(); - pthread_exit(0); -} - void Thread::Start() { - if (pthread_create(&this->handle, get_engine_attr(), entry_point, this)) + try + { + this->handle = std::thread([this]() + { + try + { + this->Run(); + } + catch (...) + { + this->SetExitState(); + throw; + } + this->SetExitState(); + }); + } + catch (const std::system_error &error) { this->flags[SF_DEAD] = true; - throw CoreException("Unable to create thread: " + Anope::LastError()); + throw CoreException("Unable to create thread: " + Anope::string(error.what())); } } @@ -91,47 +79,28 @@ void Thread::OnNotify() this->flags[SF_DEAD] = true; } -Mutex::Mutex() -{ - pthread_mutex_init(&mutex, NULL); -} - -Mutex::~Mutex() -{ - pthread_mutex_destroy(&mutex); -} - void Mutex::Lock() { - pthread_mutex_lock(&mutex); -} - -void Mutex::Unlock() -{ - pthread_mutex_unlock(&mutex); + this->m.lock(); } bool Mutex::TryLock() { - return pthread_mutex_trylock(&mutex) == 0; + return this->m.try_lock(); } -Condition::Condition() : Mutex() +void Mutex::Unlock() { - pthread_cond_init(&cond, NULL); + this->m.unlock(); } -Condition::~Condition() +void Condition::Wait() { - pthread_cond_destroy(&cond); + this->cv.wait(this->m); } void Condition::Wakeup() { - pthread_cond_signal(&cond); + this->cv.notify_one(); } -void Condition::Wait() -{ - pthread_cond_wait(&cond, &mutex); -} diff --git a/src/win32/anope_windows.h b/src/win32/anope_windows.h index d08969ceb..8d6e7ef9e 100644 --- a/src/win32/anope_windows.h +++ b/src/win32/anope_windows.h @@ -63,7 +63,6 @@ #include "dir/dir.h" #include "dl/dl.h" #include "pipe/pipe.h" -#include "pthread/pthread.h" #include "sigaction/sigaction.h" typedef int ssize_t; diff --git a/src/win32/pthread/pthread.cpp b/src/win32/pthread/pthread.cpp deleted file mode 100644 index 3bb8b6d7b..000000000 --- a/src/win32/pthread/pthread.cpp +++ /dev/null @@ -1,118 +0,0 @@ - /* POSIX emulation layer for Windows. - * - * Copyright (C) 2008-2014 Anope Team - * - * Please read COPYING and README for further details. - */ - -#include "pthread.h" - -struct ThreadInfo -{ - void *(*entry)(void *); - void *param; -}; - -static DWORD WINAPI entry_point(void *parameter) -{ - ThreadInfo *ti = static_cast(parameter); - ti->entry(ti->param); - delete ti; - return 0; -} - -int pthread_attr_init(pthread_attr_t *) -{ - /* No need for this */ - return 0; -} - -int pthread_attr_setdetachstate(pthread_attr_t *, int) -{ - /* No need for this */ - return 0; -} - -int pthread_create(pthread_t *thread, const pthread_attr_t *, void *(*entry)(void *), void *param) -{ - ThreadInfo *ti = new ThreadInfo; - ti->entry = entry; - ti->param = param; - - *thread = CreateThread(NULL, 0, entry_point, ti, 0, NULL); - if (!*thread) - { - delete ti; - return -1; - } - - return 0; -} - -int pthread_join(pthread_t thread, void **) -{ - if (WaitForSingleObject(thread, INFINITE) == WAIT_FAILED) - return -1; - CloseHandle(thread); - return 0; -} - -void pthread_exit(int i) -{ - ExitThread(i); -} - -int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *) -{ - InitializeCriticalSection(mutex); - return 0; -} - -int pthread_mutex_destroy(pthread_mutex_t *mutex) -{ - DeleteCriticalSection(mutex); - return 0; -} - -int pthread_mutex_lock(pthread_mutex_t *mutex) -{ - EnterCriticalSection(mutex); - return 0; -} - -int pthread_mutex_trylock(pthread_mutex_t *mutex) -{ - return !TryEnterCriticalSection(mutex); -} - -int pthread_mutex_unlock(pthread_mutex_t *mutex) -{ - LeaveCriticalSection(mutex); - return 0; -} - -int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *) -{ - *cond = CreateEvent(NULL, false, false, NULL); - if (*cond == NULL) - return -1; - return 0; -} - -int pthread_cond_destroy(pthread_cond_t *cond) -{ - return !CloseHandle(*cond); -} - -int pthread_cond_signal(pthread_cond_t *cond) -{ - return !PulseEvent(*cond); -} - -int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) -{ - LeaveCriticalSection(mutex); - WaitForSingleObject(*cond, INFINITE); - EnterCriticalSection(mutex); - return 0; -} diff --git a/src/win32/pthread/pthread.h b/src/win32/pthread/pthread.h deleted file mode 100644 index 234a2a027..000000000 --- a/src/win32/pthread/pthread.h +++ /dev/null @@ -1,34 +0,0 @@ - /* POSIX emulation layer for Windows. - * - * Copyright (C) 2008-2014 Anope Team - * - * Please read COPYING and README for further details. - */ - -#include - -typedef HANDLE pthread_t; -typedef CRITICAL_SECTION pthread_mutex_t; -typedef HANDLE pthread_cond_t; -typedef int pthread_attr_t; -typedef void pthread_mutexattr_t; -typedef void pthread_condattr_t; - -#define PTHREAD_CREATE_JOINABLE 0 - -extern int pthread_attr_init(pthread_attr_t *); -extern int pthread_attr_setdetachstate(pthread_attr_t *, int); -extern int pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *); -extern int pthread_join(pthread_t, void **); -extern void pthread_exit(int); - -extern int pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); -extern int pthread_mutex_destroy(pthread_mutex_t *); -extern int pthread_mutex_lock(pthread_mutex_t *); -extern int pthread_mutex_trylock(pthread_mutex_t *); -extern int pthread_mutex_unlock(pthread_mutex_t *); - -extern int pthread_cond_init(pthread_cond_t *, const pthread_condattr_t *); -extern int pthread_cond_destroy(pthread_cond_t *); -extern int pthread_cond_signal(pthread_cond_t *); -extern int pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); -- cgit