diff options
Diffstat (limited to 'src/threadengine.cpp')
-rw-r--r-- | src/threadengine.cpp | 119 |
1 files changed, 48 insertions, 71 deletions
diff --git a/src/threadengine.cpp b/src/threadengine.cpp index f392ac472..bfd7605c3 100644 --- a/src/threadengine.cpp +++ b/src/threadengine.cpp @@ -1,48 +1,26 @@ /* + * Anope IRC Services * - * (C) 2003-2016 Anope Team - * Contact us at team@anope.org + * Copyright (C) 2010-2016 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ #include "services.h" #include "threadengine.h" #include "anope.h" - -#ifndef _WIN32 -#include <pthread.h> -#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<Thread *>(parameter); - thread->Run(); - thread->SetExitState(); - pthread_exit(0); - return NULL; -} +#include <system_error> Thread::Thread() : exit(false) { @@ -55,7 +33,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() @@ -64,18 +50,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())); } } @@ -90,47 +86,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); -} |