1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Use C++11 mutexes only (remove compat code) (#5922)

* Fix event LINT & remove default constructor/destructors
* remove compat code & modernize autolock header
This commit is contained in:
Loïc Blot 2017-06-06 16:29:28 +02:00 committed by GitHub
parent 8bdde45895
commit d4c0f91275
36 changed files with 59 additions and 363 deletions

View file

@ -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)

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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>;

View file

@ -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;