diff options
Diffstat (limited to 'src/threading')
-rw-r--r-- | src/threading/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/threading/event.cpp | 48 | ||||
-rw-r--r-- | src/threading/event.h | 19 | ||||
-rw-r--r-- | src/threading/mutex.cpp | 116 | ||||
-rw-r--r-- | src/threading/mutex.h | 84 | ||||
-rw-r--r-- | src/threading/mutex_auto_lock.h | 40 | ||||
-rw-r--r-- | src/threading/thread.h | 6 |
7 files changed, 9 insertions, 305 deletions
diff --git a/src/threading/CMakeLists.txt b/src/threading/CMakeLists.txt index 5dd60ef1a..8f86158be 100644 --- a/src/threading/CMakeLists.txt +++ b/src/threading/CMakeLists.txt @@ -1,6 +1,5 @@ set(JTHREAD_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/event.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/mutex.cpp ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp ${CMAKE_CURRENT_SOURCE_DIR}/semaphore.cpp PARENT_SCOPE) diff --git a/src/threading/event.cpp b/src/threading/event.cpp index a22c6628b..4e8d4bb3e 100644 --- a/src/threading/event.cpp +++ b/src/threading/event.cpp @@ -25,67 +25,19 @@ DEALINGS IN THE SOFTWARE. #include "threading/event.h" -Event::Event() -{ -#ifndef USE_CPP11_MUTEX -# if USE_WIN_MUTEX - event = CreateEvent(NULL, false, false, NULL); -# else - pthread_cond_init(&cv, NULL); - pthread_mutex_init(&mutex, NULL); - notified = false; -# endif -#elif USE_CPP11_MUTEX - notified = false; -#endif -} - -#ifndef USE_CPP11_MUTEX -Event::~Event() -{ -#if USE_WIN_MUTEX - CloseHandle(event); -#else - pthread_cond_destroy(&cv); - pthread_mutex_destroy(&mutex); -#endif -} -#endif - - void Event::wait() { -#if USE_CPP11_MUTEX MutexAutoLock lock(mutex); while (!notified) { cv.wait(lock); } notified = false; -#elif USE_WIN_MUTEX - WaitForSingleObject(event, INFINITE); -#else - pthread_mutex_lock(&mutex); - while (!notified) { - pthread_cond_wait(&cv, &mutex); - } - notified = false; - pthread_mutex_unlock(&mutex); -#endif } void Event::signal() { -#if USE_CPP11_MUTEX MutexAutoLock lock(mutex); notified = true; cv.notify_one(); -#elif USE_WIN_MUTEX - SetEvent(event); -#else - pthread_mutex_lock(&mutex); - notified = true; - pthread_cond_signal(&cv); - pthread_mutex_unlock(&mutex); -#endif } diff --git a/src/threading/event.h b/src/threading/event.h index 79a99ce1f..458864c82 100644 --- a/src/threading/event.h +++ b/src/threading/event.h @@ -28,11 +28,8 @@ DEALINGS IN THE SOFTWARE. #include "threads.h" -#if USE_CPP11_MUTEX #include <condition_variable> -#include "threading/mutex.h" #include "threading/mutex_auto_lock.h" -#endif /** A syncronization primitive that will wake up one waiting thread when signaled. * Calling @c signal() multiple times before a waiting thread has had a chance @@ -43,25 +40,13 @@ DEALINGS IN THE SOFTWARE. class Event { public: - Event(); -#ifndef USE_CPP11_MUTEX - ~Event(); -#endif void wait(); void signal(); private: -#if USE_CPP11_MUTEX std::condition_variable cv; - Mutex mutex; - bool notified; -#elif USE_WIN_MUTEX - HANDLE event; -#else - pthread_cond_t cv; - pthread_mutex_t mutex; - bool notified; -#endif + std::mutex mutex; + bool notified = false; }; #endif diff --git a/src/threading/mutex.cpp b/src/threading/mutex.cpp deleted file mode 100644 index d864f12c5..000000000 --- a/src/threading/mutex.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* -This file is a part of the JThread package, which contains some object- -oriented thread wrappers for different thread implementations. - -Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - -#include "threads.h" - -#ifndef USE_CPP11_MUTEX - -#include "threading/mutex.h" - -#include <cassert> - -#define UNUSED(expr) do { (void)(expr); } while (0) - -Mutex::Mutex() -{ - init_mutex(false); -} - - -Mutex::Mutex(bool recursive) -{ - init_mutex(recursive); -} - -void Mutex::init_mutex(bool recursive) -{ -#if USE_WIN_MUTEX - // Windows critical sections are recursive by default - UNUSED(recursive); - - InitializeCriticalSection(&mutex); -#else - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - - if (recursive) - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - - int ret = pthread_mutex_init(&mutex, &attr); - assert(!ret); - UNUSED(ret); - - pthread_mutexattr_destroy(&attr); -#endif -} - -Mutex::~Mutex() -{ -#if USE_WIN_MUTEX - DeleteCriticalSection(&mutex); -#else - int ret = pthread_mutex_destroy(&mutex); - assert(!ret); - UNUSED(ret); -#endif -} - -void Mutex::lock() -{ -#if USE_WIN_MUTEX - EnterCriticalSection(&mutex); -#else - int ret = pthread_mutex_lock(&mutex); - assert(!ret); - UNUSED(ret); -#endif -} - -bool Mutex::try_lock() -{ -#if USE_WIN_MUTEX - return TryEnterCriticalSection(&mutex) != 0; -#else - return pthread_mutex_trylock(&mutex) == 0; -#endif -} - -void Mutex::unlock() -{ -#if USE_WIN_MUTEX - LeaveCriticalSection(&mutex); -#else - int ret = pthread_mutex_unlock(&mutex); - assert(!ret); - UNUSED(ret); -#endif -} - -RecursiveMutex::RecursiveMutex() - : Mutex(true) -{} - -#endif // C++11 - diff --git a/src/threading/mutex.h b/src/threading/mutex.h deleted file mode 100644 index 6feb23c25..000000000 --- a/src/threading/mutex.h +++ /dev/null @@ -1,84 +0,0 @@ -/* -This file is a part of the JThread package, which contains some object- -oriented thread wrappers for different thread implementations. - -Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - -#ifndef THREADING_MUTEX_H -#define THREADING_MUTEX_H - -#include "threads.h" - -#if USE_CPP11_MUTEX - #include <mutex> - using Mutex = std::mutex; - using RecursiveMutex = std::recursive_mutex; -#else - -#if USE_WIN_MUTEX - #ifndef _WIN32_WINNT - #define _WIN32_WINNT 0x0501 - #endif - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - #include <windows.h> -#else - #include <pthread.h> -#endif - -#include "util/basic_macros.h" - -class Mutex -{ -public: - Mutex(); - ~Mutex(); - void lock(); - void unlock(); - - bool try_lock(); - -protected: - Mutex(bool recursive); - void init_mutex(bool recursive); -private: -#if USE_WIN_MUTEX - CRITICAL_SECTION mutex; -#else - pthread_mutex_t mutex; -#endif - - DISABLE_CLASS_COPY(Mutex); -}; - -class RecursiveMutex : public Mutex -{ -public: - RecursiveMutex(); - - DISABLE_CLASS_COPY(RecursiveMutex); -}; - -#endif // C++11 - -#endif diff --git a/src/threading/mutex_auto_lock.h b/src/threading/mutex_auto_lock.h index d79c68a93..c809ff8f5 100644 --- a/src/threading/mutex_auto_lock.h +++ b/src/threading/mutex_auto_lock.h @@ -23,40 +23,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef THREADING_MUTEX_AUTO_LOCK_H -#define THREADING_MUTEX_AUTO_LOCK_H - -#include "threads.h" - -#if USE_CPP11_MUTEX - #include <mutex> - using MutexAutoLock = std::unique_lock<std::mutex>; - using RecursiveMutexAutoLock = std::unique_lock<std::recursive_mutex>; -#else - -#include "threading/mutex.h" - - -class MutexAutoLock -{ -public: - MutexAutoLock(Mutex &m) : mutex(m) { mutex.lock(); } - ~MutexAutoLock() { mutex.unlock(); } - -private: - Mutex &mutex; -}; - -class RecursiveMutexAutoLock -{ -public: - RecursiveMutexAutoLock(RecursiveMutex &m) : mutex(m) { mutex.lock(); } - ~RecursiveMutexAutoLock() { mutex.unlock(); } - -private: - RecursiveMutex &mutex; -}; -#endif - -#endif +#pragma once +#include <mutex> +using MutexAutoLock = std::unique_lock<std::mutex>; +using RecursiveMutexAutoLock = std::unique_lock<std::recursive_mutex>; diff --git a/src/threading/thread.h b/src/threading/thread.h index 671a9be0b..ab943f094 100644 --- a/src/threading/thread.h +++ b/src/threading/thread.h @@ -27,11 +27,11 @@ DEALINGS IN THE SOFTWARE. #define THREADING_THREAD_H #include "util/basic_macros.h" -#include "threading/mutex.h" #include "threads.h" #include <string> #include <atomic> +#include <mutex> #ifdef _AIX #include <sys/thread.h> // for tid_t @@ -153,8 +153,8 @@ private: bool m_joinable; std::atomic<bool> m_request_stop; std::atomic<bool> m_running; - Mutex m_mutex; - Mutex m_start_finished_mutex; + std::mutex m_mutex; + std::mutex m_start_finished_mutex; #if USE_CPP11_THREADS std::thread *m_thread_obj; |