Switch to C++11 threads, mutexes and lock_guards. Switching to C++11 condition_variables requires some more thought.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
include (SimGearComponent)
|
||||
|
||||
set(HEADERS
|
||||
SGGuard.hxx
|
||||
SGQueue.hxx
|
||||
SGThread.hxx)
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#ifndef SGGUARD_HXX_INCLUDED
|
||||
#define SGGUARD_HXX_INCLUDED 1
|
||||
|
||||
/**
|
||||
* A scoped locking utility.
|
||||
* An SGGuard object locks its synchronization object during creation and
|
||||
* automatically unlocks it when it goes out of scope.
|
||||
*/
|
||||
template<class SGLOCK>
|
||||
class SGGuard
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create an SGGuard object and lock the passed lockable object.
|
||||
* @param l A lockable object.
|
||||
*/
|
||||
inline SGGuard( SGLOCK& l ) : lock(l) { lock.lock(); }
|
||||
|
||||
/**
|
||||
* Destroy this object and unlock the lockable object.
|
||||
*/
|
||||
inline ~SGGuard() { lock.unlock(); }
|
||||
|
||||
private:
|
||||
|
||||
SGLOCK& lock; //!< A lockable object
|
||||
|
||||
private:
|
||||
// Disable copying.
|
||||
SGGuard(const SGLOCK&);
|
||||
SGLOCK& operator= (const SGLOCK&);
|
||||
};
|
||||
|
||||
#endif // SGGUARD_HXX_INCLUDED
|
||||
+23
-23
@@ -5,7 +5,7 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <queue>
|
||||
#include "SGGuard.hxx"
|
||||
#include <mutex>
|
||||
#include "SGThread.hxx"
|
||||
|
||||
/**
|
||||
@@ -94,7 +94,7 @@ public:
|
||||
* @return True if queue is empty, otherwise false.
|
||||
*/
|
||||
virtual bool empty() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
return this->fifo.empty();
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ public:
|
||||
* @param item object to add.
|
||||
*/
|
||||
virtual void push( const T& item ) {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
this->fifo.push( item );
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T front() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
assert( ! this->fifo.empty() );
|
||||
T item = this->fifo.front();
|
||||
return item;
|
||||
@@ -126,7 +126,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T pop() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
if (this->fifo.empty()) return T(); // assumes T is default constructable
|
||||
|
||||
// if (fifo.empty())
|
||||
@@ -145,7 +145,7 @@ public:
|
||||
* @return Size of queue.
|
||||
*/
|
||||
virtual size_t size() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
return this->fifo.size();
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ private:
|
||||
/**
|
||||
* Mutex to serialise access.
|
||||
*/
|
||||
SGMutex mutex;
|
||||
std::mutex mutex;
|
||||
|
||||
private:
|
||||
// Prevent copying.
|
||||
@@ -184,7 +184,7 @@ public:
|
||||
*
|
||||
*/
|
||||
virtual bool empty() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
return this->fifo.empty();
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ public:
|
||||
* @param item The object to add.
|
||||
*/
|
||||
virtual void push( const T& item ) {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
this->fifo.push( item );
|
||||
not_empty.signal();
|
||||
}
|
||||
@@ -206,7 +206,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T front() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
|
||||
assert(this->fifo.empty() != true);
|
||||
//if (fifo.empty()) throw ??
|
||||
@@ -222,7 +222,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T pop() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
|
||||
while (this->fifo.empty())
|
||||
not_empty.wait(mutex);
|
||||
@@ -241,7 +241,7 @@ public:
|
||||
* @return Size of queue.
|
||||
*/
|
||||
virtual size_t size() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
return this->fifo.size();
|
||||
}
|
||||
|
||||
@@ -250,7 +250,7 @@ private:
|
||||
/**
|
||||
* Mutex to serialise access.
|
||||
*/
|
||||
SGMutex mutex;
|
||||
std::mutex mutex;
|
||||
|
||||
/**
|
||||
* Condition to signal when queue not empty.
|
||||
@@ -286,7 +286,7 @@ public:
|
||||
*
|
||||
*/
|
||||
virtual void clear() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
this->queue.clear();
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ public:
|
||||
*
|
||||
*/
|
||||
virtual bool empty() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
return this->queue.empty();
|
||||
}
|
||||
|
||||
@@ -304,7 +304,7 @@ public:
|
||||
* @param item The object to add.
|
||||
*/
|
||||
virtual void push_front( const T& item ) {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
this->queue.push_front( item );
|
||||
not_empty.signal();
|
||||
}
|
||||
@@ -315,7 +315,7 @@ public:
|
||||
* @param item The object to add.
|
||||
*/
|
||||
virtual void push_back( const T& item ) {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
this->queue.push_back( item );
|
||||
not_empty.signal();
|
||||
}
|
||||
@@ -327,7 +327,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T front() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
|
||||
assert(this->queue.empty() != true);
|
||||
//if (queue.empty()) throw ??
|
||||
@@ -343,7 +343,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T pop_front() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
|
||||
while (this->queue.empty())
|
||||
not_empty.wait(mutex);
|
||||
@@ -363,7 +363,7 @@ public:
|
||||
* @return The next available object.
|
||||
*/
|
||||
virtual T pop_back() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
|
||||
while (this->queue.empty())
|
||||
not_empty.wait(mutex);
|
||||
@@ -382,12 +382,12 @@ public:
|
||||
* @return Size of queue.
|
||||
*/
|
||||
virtual size_t size() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
return this->queue.size();
|
||||
}
|
||||
|
||||
void waitOnNotEmpty() {
|
||||
SGGuard<SGMutex> g(mutex);
|
||||
std::lock_guard<std::mutex> g(mutex);
|
||||
while (this->queue.empty())
|
||||
not_empty.wait(mutex);
|
||||
}
|
||||
@@ -396,7 +396,7 @@ private:
|
||||
/**
|
||||
* Mutex to serialise access.
|
||||
*/
|
||||
SGMutex mutex;
|
||||
std::mutex mutex;
|
||||
|
||||
/**
|
||||
* Condition to signal when queue not empty.
|
||||
|
||||
+48
-162
@@ -29,29 +29,26 @@
|
||||
|
||||
#include "SGThread.hxx"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
/// win32 threads
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <list>
|
||||
#include <windows.h>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <climits>
|
||||
|
||||
struct SGThread::PrivateData {
|
||||
PrivateData() :
|
||||
_handle(INVALID_HANDLE_VALUE)
|
||||
PrivateData()
|
||||
{
|
||||
}
|
||||
~PrivateData()
|
||||
{
|
||||
if (_handle == INVALID_HANDLE_VALUE)
|
||||
// If we are still having a started thread and nobody waited,
|
||||
// now detach ...
|
||||
if (!_started)
|
||||
return;
|
||||
CloseHandle(_handle);
|
||||
_handle = INVALID_HANDLE_VALUE;
|
||||
_thread.detach();
|
||||
}
|
||||
|
||||
static DWORD WINAPI start_routine(LPVOID data)
|
||||
static void *start_routine(void* data)
|
||||
{
|
||||
SGThread* thread = reinterpret_cast<SGThread*>(data);
|
||||
thread->run();
|
||||
@@ -60,55 +57,50 @@ struct SGThread::PrivateData {
|
||||
|
||||
bool start(SGThread& thread)
|
||||
{
|
||||
if (_handle != INVALID_HANDLE_VALUE)
|
||||
if (_started)
|
||||
return false;
|
||||
_handle = CreateThread(0, 0, start_routine, &thread, 0, 0);
|
||||
if (_handle == INVALID_HANDLE_VALUE)
|
||||
|
||||
try {
|
||||
_thread = std::thread(start_routine, &thread);
|
||||
} catch (std::runtime_error &ex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_started = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void join()
|
||||
{
|
||||
if (_handle == INVALID_HANDLE_VALUE)
|
||||
if (!_started)
|
||||
return;
|
||||
DWORD ret = WaitForSingleObject(_handle, INFINITE);
|
||||
if (ret != WAIT_OBJECT_0)
|
||||
return;
|
||||
CloseHandle(_handle);
|
||||
_handle = INVALID_HANDLE_VALUE;
|
||||
|
||||
_thread.join();
|
||||
_started = false;
|
||||
}
|
||||
|
||||
HANDLE _handle;
|
||||
std::thread _thread;
|
||||
bool _started = false;
|
||||
};
|
||||
|
||||
long SGThread::current( void ) {
|
||||
long SGThread::current( void )
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return (long)GetCurrentThreadId();
|
||||
#else
|
||||
return (long)pthread_self();
|
||||
#endif
|
||||
}
|
||||
|
||||
struct SGMutex::PrivateData {
|
||||
PrivateData()
|
||||
{
|
||||
InitializeCriticalSection((LPCRITICAL_SECTION)&_criticalSection);
|
||||
}
|
||||
|
||||
~PrivateData()
|
||||
{
|
||||
DeleteCriticalSection((LPCRITICAL_SECTION)&_criticalSection);
|
||||
}
|
||||
#if _WIN32
|
||||
|
||||
void lock(void)
|
||||
{
|
||||
EnterCriticalSection((LPCRITICAL_SECTION)&_criticalSection);
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
/// win32 threads
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void unlock(void)
|
||||
{
|
||||
LeaveCriticalSection((LPCRITICAL_SECTION)&_criticalSection);
|
||||
}
|
||||
|
||||
CRITICAL_SECTION _criticalSection;
|
||||
};
|
||||
#include <list>
|
||||
#include <windows.h>
|
||||
|
||||
struct SGWaitCondition::PrivateData {
|
||||
~PrivateData(void)
|
||||
@@ -138,7 +130,7 @@ struct SGWaitCondition::PrivateData {
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
bool wait(SGMutex::PrivateData& externalMutex, DWORD msec)
|
||||
bool wait(std::mutex& externalMutex, DWORD msec)
|
||||
{
|
||||
_mutex.lock();
|
||||
if (_pool.empty())
|
||||
@@ -163,13 +155,13 @@ struct SGWaitCondition::PrivateData {
|
||||
return result == WAIT_OBJECT_0;
|
||||
}
|
||||
|
||||
void wait(SGMutex::PrivateData& externalMutex)
|
||||
void wait(std::mutex& externalMutex)
|
||||
{
|
||||
wait(externalMutex, INFINITE);
|
||||
}
|
||||
|
||||
// Protect the list of waiters
|
||||
SGMutex::PrivateData _mutex;
|
||||
std::mutex _mutex;
|
||||
|
||||
std::list<HANDLE> _waiters;
|
||||
std::list<HANDLE> _pool;
|
||||
@@ -185,89 +177,6 @@ struct SGWaitCondition::PrivateData {
|
||||
#include <cerrno>
|
||||
#include <sys/time.h>
|
||||
|
||||
struct SGThread::PrivateData {
|
||||
PrivateData() :
|
||||
_started(false)
|
||||
{
|
||||
}
|
||||
~PrivateData()
|
||||
{
|
||||
// If we are still having a started thread and nobody waited,
|
||||
// now detach ...
|
||||
if (!_started)
|
||||
return;
|
||||
pthread_detach(_thread);
|
||||
}
|
||||
|
||||
static void *start_routine(void* data)
|
||||
{
|
||||
SGThread* thread = reinterpret_cast<SGThread*>(data);
|
||||
thread->run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool start(SGThread& thread)
|
||||
{
|
||||
if (_started)
|
||||
return false;
|
||||
|
||||
int ret = pthread_create(&_thread, 0, start_routine, &thread);
|
||||
if (0 != ret)
|
||||
return false;
|
||||
|
||||
_started = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void join()
|
||||
{
|
||||
if (!_started)
|
||||
return;
|
||||
|
||||
pthread_join(_thread, 0);
|
||||
_started = false;
|
||||
}
|
||||
|
||||
pthread_t _thread;
|
||||
bool _started;
|
||||
};
|
||||
|
||||
long SGThread::current( void ) {
|
||||
return (long)pthread_self();
|
||||
}
|
||||
|
||||
struct SGMutex::PrivateData {
|
||||
PrivateData()
|
||||
{
|
||||
int err = pthread_mutex_init(&_mutex, 0);
|
||||
assert(err == 0);
|
||||
(void)err;
|
||||
}
|
||||
|
||||
~PrivateData()
|
||||
{
|
||||
int err = pthread_mutex_destroy(&_mutex);
|
||||
assert(err == 0);
|
||||
(void)err;
|
||||
}
|
||||
|
||||
void lock(void)
|
||||
{
|
||||
int err = pthread_mutex_lock(&_mutex);
|
||||
assert(err == 0);
|
||||
(void)err;
|
||||
}
|
||||
|
||||
void unlock(void)
|
||||
{
|
||||
int err = pthread_mutex_unlock(&_mutex);
|
||||
assert(err == 0);
|
||||
(void)err;
|
||||
}
|
||||
|
||||
pthread_mutex_t _mutex;
|
||||
};
|
||||
|
||||
struct SGWaitCondition::PrivateData {
|
||||
PrivateData(void)
|
||||
{
|
||||
@@ -296,14 +205,14 @@ struct SGWaitCondition::PrivateData {
|
||||
(void)err;
|
||||
}
|
||||
|
||||
void wait(SGMutex::PrivateData& mutex)
|
||||
void wait(std::mutex& mutex)
|
||||
{
|
||||
int err = pthread_cond_wait(&_condition, &mutex._mutex);
|
||||
int err = pthread_cond_wait(&_condition, mutex.native_handle());
|
||||
assert(err == 0);
|
||||
(void)err;
|
||||
}
|
||||
|
||||
bool wait(SGMutex::PrivateData& mutex, unsigned msec)
|
||||
bool wait(std::mutex& mutex, unsigned msec)
|
||||
{
|
||||
struct timespec ts;
|
||||
#ifdef HAVE_CLOCK_GETTIME
|
||||
@@ -324,7 +233,7 @@ struct SGWaitCondition::PrivateData {
|
||||
}
|
||||
ts.tv_sec += msec / 1000;
|
||||
|
||||
int evalue = pthread_cond_timedwait(&_condition, &mutex._mutex, &ts);
|
||||
int evalue = pthread_cond_timedwait(&_condition, mutex.native_handle(), &ts);
|
||||
if (evalue == 0)
|
||||
return true;
|
||||
|
||||
@@ -360,29 +269,6 @@ SGThread::join()
|
||||
_privateData->join();
|
||||
}
|
||||
|
||||
SGMutex::SGMutex() :
|
||||
_privateData(new PrivateData)
|
||||
{
|
||||
}
|
||||
|
||||
SGMutex::~SGMutex()
|
||||
{
|
||||
delete _privateData;
|
||||
_privateData = 0;
|
||||
}
|
||||
|
||||
void
|
||||
SGMutex::lock()
|
||||
{
|
||||
_privateData->lock();
|
||||
}
|
||||
|
||||
void
|
||||
SGMutex::unlock()
|
||||
{
|
||||
_privateData->unlock();
|
||||
}
|
||||
|
||||
SGWaitCondition::SGWaitCondition() :
|
||||
_privateData(new PrivateData)
|
||||
{
|
||||
@@ -395,15 +281,15 @@ SGWaitCondition::~SGWaitCondition()
|
||||
}
|
||||
|
||||
void
|
||||
SGWaitCondition::wait(SGMutex& mutex)
|
||||
SGWaitCondition::wait(std::mutex& mutex)
|
||||
{
|
||||
_privateData->wait(*mutex._privateData);
|
||||
_privateData->wait(mutex);
|
||||
}
|
||||
|
||||
bool
|
||||
SGWaitCondition::wait(SGMutex& mutex, unsigned msec)
|
||||
SGWaitCondition::wait(std::mutex& mutex, unsigned msec)
|
||||
{
|
||||
return _privateData->wait(*mutex._privateData, msec);
|
||||
return _privateData->wait(mutex, msec);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -86,49 +86,6 @@ private:
|
||||
|
||||
class SGWaitCondition;
|
||||
|
||||
/**
|
||||
* A mutex is used to protect a section of code such that at any time
|
||||
* only a single thread can execute the code.
|
||||
*/
|
||||
class SGMutex {
|
||||
public:
|
||||
/**
|
||||
* Create a new mutex.
|
||||
* Under Linux this is a 'fast' mutex.
|
||||
*/
|
||||
SGMutex();
|
||||
|
||||
/**
|
||||
* Destroy a mutex object.
|
||||
* Note: it is the responsibility of the caller to ensure the mutex is
|
||||
* unlocked before destruction occurs.
|
||||
*/
|
||||
~SGMutex();
|
||||
|
||||
/**
|
||||
* Lock this mutex.
|
||||
* If the mutex is currently unlocked, it becomes locked and owned by
|
||||
* the calling thread. If the mutex is already locked by another thread,
|
||||
* the calling thread is suspended until the mutex is unlocked. If the
|
||||
* mutex is already locked and owned by the calling thread, the calling
|
||||
* thread is suspended until the mutex is unlocked, effectively causing
|
||||
* the calling thread to deadlock.
|
||||
*/
|
||||
void lock();
|
||||
|
||||
/**
|
||||
* Unlock this mutex.
|
||||
* It is assumed that the mutex is locked and owned by the calling thread.
|
||||
*/
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
struct PrivateData;
|
||||
PrivateData* _privateData;
|
||||
|
||||
friend class SGWaitCondition;
|
||||
};
|
||||
|
||||
/**
|
||||
* A condition variable is a synchronization device that allows threads to
|
||||
* suspend execution until some predicate on shared data is satisfied.
|
||||
@@ -152,7 +109,7 @@ public:
|
||||
*
|
||||
* @param mutex Reference to a locked mutex.
|
||||
*/
|
||||
void wait(SGMutex& mutex);
|
||||
void wait(std::mutex& mutex);
|
||||
|
||||
/**
|
||||
* Wait for this condition variable to be signaled for at most \a 'msec'
|
||||
@@ -163,7 +120,7 @@ public:
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
bool wait(SGMutex& mutex, unsigned msec);
|
||||
bool wait(std::mutex& mutex, unsigned msec);
|
||||
|
||||
/**
|
||||
* Wake one thread waiting on this condition variable.
|
||||
|
||||
Reference in New Issue
Block a user