403 lines
9.0 KiB
C++
403 lines
9.0 KiB
C++
// SGThread - Simple pthread class wrappers.
|
|
//
|
|
// Written by Bernie Bright, started April 2001.
|
|
//
|
|
// Copyright (C) 2001 Bernard Bright - bbright@bigpond.net.au
|
|
// Copyright (C) 2011 Mathias Froehlich
|
|
//
|
|
// This program 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; either version 2 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// 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, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
//
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include <simgear_config.h>
|
|
#endif
|
|
|
|
#include <simgear/compiler.h>
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include "SGThread.hxx"
|
|
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
#include <chrono>
|
|
#include <climits>
|
|
|
|
struct SGThread::PrivateData {
|
|
PrivateData()
|
|
{
|
|
}
|
|
~PrivateData()
|
|
{
|
|
// If we are still having a started thread and nobody waited,
|
|
// now detach ...
|
|
if (!_started)
|
|
return;
|
|
_thread.detach();
|
|
}
|
|
|
|
static void *start_routine(void* data)
|
|
{
|
|
SGThread* thread = reinterpret_cast<SGThread*>(data);
|
|
thread->run();
|
|
return 0;
|
|
}
|
|
|
|
bool start(SGThread& thread)
|
|
{
|
|
if (_started)
|
|
return false;
|
|
|
|
try {
|
|
_thread = std::thread(start_routine, &thread);
|
|
} catch (std::runtime_error &ex) {
|
|
return false;
|
|
}
|
|
|
|
_started = true;
|
|
return true;
|
|
}
|
|
|
|
void join()
|
|
{
|
|
if (!_started)
|
|
return;
|
|
|
|
_thread.join();
|
|
_started = false;
|
|
}
|
|
|
|
std::thread _thread;
|
|
bool _started = false;
|
|
};
|
|
|
|
long SGThread::current( void )
|
|
{
|
|
#ifdef _WIN32
|
|
return (long)GetCurrentThreadId();
|
|
#else
|
|
return (long)pthread_self();
|
|
#endif
|
|
}
|
|
|
|
|
|
#if _WIN32
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/// win32 threads
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <list>
|
|
#include <windows.h>
|
|
|
|
struct SGWaitCondition::PrivateData {
|
|
~PrivateData(void)
|
|
{
|
|
// The waiters list should be empty anyway
|
|
_mutex.lock();
|
|
while (!_pool.empty()) {
|
|
CloseHandle(_pool.front());
|
|
_pool.pop_front();
|
|
}
|
|
_mutex.unlock();
|
|
}
|
|
|
|
void signal(void)
|
|
{
|
|
_mutex.lock();
|
|
if (!_waiters.empty())
|
|
SetEvent(_waiters.back());
|
|
_mutex.unlock();
|
|
}
|
|
|
|
void broadcast(void)
|
|
{
|
|
_mutex.lock();
|
|
for (std::list<HANDLE>::iterator i = _waiters.begin(); i != _waiters.end(); ++i)
|
|
SetEvent(*i);
|
|
_mutex.unlock();
|
|
}
|
|
|
|
bool wait(std::mutex& externalMutex, DWORD msec)
|
|
{
|
|
_mutex.lock();
|
|
if (_pool.empty())
|
|
_waiters.push_front(CreateEvent(NULL, FALSE, FALSE, NULL));
|
|
else
|
|
_waiters.splice(_waiters.begin(), _pool, _pool.begin());
|
|
std::list<HANDLE>::iterator i = _waiters.begin();
|
|
_mutex.unlock();
|
|
|
|
externalMutex.unlock();
|
|
|
|
DWORD result = WaitForSingleObject(*i, msec);
|
|
|
|
externalMutex.lock();
|
|
|
|
_mutex.lock();
|
|
if (result != WAIT_OBJECT_0)
|
|
result = WaitForSingleObject(*i, 0);
|
|
_pool.splice(_pool.begin(), _waiters, i);
|
|
_mutex.unlock();
|
|
|
|
return result == WAIT_OBJECT_0;
|
|
}
|
|
|
|
void wait(std::mutex& externalMutex)
|
|
{
|
|
wait(externalMutex, INFINITE);
|
|
}
|
|
|
|
// Protect the list of waiters
|
|
std::mutex _mutex;
|
|
|
|
std::list<HANDLE> _waiters;
|
|
std::list<HANDLE> _pool;
|
|
};
|
|
|
|
#else
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/// posix threads
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <pthread.h>
|
|
#include <cassert>
|
|
#include <cerrno>
|
|
#include <sys/time.h>
|
|
|
|
struct SGWaitCondition::PrivateData {
|
|
PrivateData(void)
|
|
{
|
|
int err = pthread_cond_init(&_condition, NULL);
|
|
assert(err == 0);
|
|
(void)err;
|
|
}
|
|
~PrivateData(void)
|
|
{
|
|
int err = pthread_cond_destroy(&_condition);
|
|
assert(err == 0);
|
|
(void)err;
|
|
}
|
|
|
|
void signal(void)
|
|
{
|
|
int err = pthread_cond_signal(&_condition);
|
|
assert(err == 0);
|
|
(void)err;
|
|
}
|
|
|
|
void broadcast(void)
|
|
{
|
|
int err = pthread_cond_broadcast(&_condition);
|
|
assert(err == 0);
|
|
(void)err;
|
|
}
|
|
|
|
void wait(std::mutex& mutex)
|
|
{
|
|
int err = pthread_cond_wait(&_condition, mutex.native_handle());
|
|
assert(err == 0);
|
|
(void)err;
|
|
}
|
|
|
|
bool wait(std::mutex& mutex, unsigned msec)
|
|
{
|
|
struct timespec ts;
|
|
#ifdef HAVE_CLOCK_GETTIME
|
|
if (0 != clock_gettime(CLOCK_REALTIME, &ts))
|
|
return false;
|
|
#else
|
|
struct timeval tv;
|
|
if (0 != gettimeofday(&tv, NULL))
|
|
return false;
|
|
ts.tv_sec = tv.tv_sec;
|
|
ts.tv_nsec = tv.tv_usec * 1000;
|
|
#endif
|
|
|
|
ts.tv_nsec += 1000000*(msec % 1000);
|
|
if (1000000000 <= ts.tv_nsec) {
|
|
ts.tv_nsec -= 1000000000;
|
|
ts.tv_sec += 1;
|
|
}
|
|
ts.tv_sec += msec / 1000;
|
|
|
|
int evalue = pthread_cond_timedwait(&_condition, mutex.native_handle(), &ts);
|
|
if (evalue == 0)
|
|
return true;
|
|
|
|
assert(evalue == ETIMEDOUT);
|
|
return false;
|
|
}
|
|
|
|
pthread_cond_t _condition;
|
|
};
|
|
|
|
#endif
|
|
|
|
SGThread::SGThread() :
|
|
_privateData(new PrivateData)
|
|
{
|
|
}
|
|
|
|
SGThread::~SGThread()
|
|
{
|
|
delete _privateData;
|
|
_privateData = 0;
|
|
}
|
|
|
|
bool
|
|
SGThread::start()
|
|
{
|
|
return _privateData->start(*this);
|
|
}
|
|
|
|
void
|
|
SGThread::join()
|
|
{
|
|
_privateData->join();
|
|
}
|
|
|
|
SGWaitCondition::SGWaitCondition() :
|
|
_privateData(new PrivateData)
|
|
{
|
|
}
|
|
|
|
SGWaitCondition::~SGWaitCondition()
|
|
{
|
|
delete _privateData;
|
|
_privateData = 0;
|
|
}
|
|
|
|
void
|
|
SGWaitCondition::wait(std::mutex& mutex)
|
|
{
|
|
_privateData->wait(mutex);
|
|
}
|
|
|
|
bool
|
|
SGWaitCondition::wait(std::mutex& mutex, unsigned msec)
|
|
{
|
|
return _privateData->wait(mutex, msec);
|
|
}
|
|
|
|
void
|
|
SGWaitCondition::signal()
|
|
{
|
|
_privateData->signal();
|
|
}
|
|
|
|
void
|
|
SGWaitCondition::broadcast()
|
|
{
|
|
_privateData->broadcast();
|
|
}
|
|
|
|
|
|
SGExclusiveThread::SGExclusiveThread() :
|
|
_started(false), _terminated(false), last_await_time(0),
|
|
dataReady(false), complete(true), process_ran(false), process_running(false)
|
|
{
|
|
}
|
|
|
|
SGExclusiveThread::~SGExclusiveThread()
|
|
{
|
|
|
|
}
|
|
|
|
void SGExclusiveThread::release() {
|
|
std::unique_lock<std::mutex> lck(mutex_);
|
|
if (!complete) {
|
|
SG_LOG(SG_NASAL, SG_ALERT, "[SGExclusiveThread] not finished - skipping");
|
|
return;
|
|
}
|
|
if (!complete.exchange(false))
|
|
SG_LOG(SG_NASAL, SG_ALERT, "[SGExclusiveThread] concurrent failure (2)");
|
|
if (dataReady.exchange(true))
|
|
SG_LOG(SG_NASAL, SG_ALERT, "[SGExclusiveThread] concurrent failure (1)");
|
|
condVar.notify_one();
|
|
}
|
|
void SGExclusiveThread::wait() {
|
|
std::unique_lock<std::mutex> lck(mutex_);
|
|
if (!dataReady)
|
|
{
|
|
do
|
|
{
|
|
condVar.wait(lck);
|
|
} while (!dataReady);
|
|
}
|
|
}
|
|
void SGExclusiveThread::clearAwaitCompletionTime() {
|
|
last_await_time = 0;
|
|
}
|
|
void SGExclusiveThread::awaitCompletion() {
|
|
timestamp.stamp();
|
|
std::unique_lock<std::mutex> lck(Cmutex_);
|
|
if (!complete)
|
|
{
|
|
do {
|
|
CcondVar.wait(lck);
|
|
} while (!complete.load());
|
|
}
|
|
|
|
if (process_ran) {
|
|
last_await_time = timestamp.elapsedUSec();
|
|
process_ran = 0;
|
|
}
|
|
}
|
|
|
|
void SGExclusiveThread::setCompletion() {
|
|
std::unique_lock<std::mutex> lck(Cmutex_);
|
|
if (!dataReady.exchange(false))
|
|
SG_LOG(SG_NASAL, SG_ALERT, "[SGExclusiveThread] atomic operation on dataReady failed (5)\n");
|
|
|
|
if (complete.exchange(true))
|
|
SG_LOG(SG_NASAL, SG_ALERT, "[SGExclusiveThread] atomic operation on complete failed (5)\n");
|
|
CcondVar.notify_one();
|
|
}
|
|
void SGExclusiveThread::run()
|
|
{
|
|
process_running = true;
|
|
while (!_terminated) {
|
|
wait();
|
|
process_ran = process();
|
|
setCompletion();
|
|
}
|
|
process_running = false;
|
|
_terminated = false;
|
|
_started = false;
|
|
}
|
|
|
|
void SGExclusiveThread::terminate() {
|
|
_terminated = true;
|
|
release();
|
|
join();
|
|
}
|
|
bool SGExclusiveThread::stop()
|
|
{
|
|
return true;
|
|
}
|
|
void SGExclusiveThread::ensure_running()
|
|
{
|
|
if (!_started)
|
|
{
|
|
_started = true;
|
|
start();
|
|
}
|
|
}
|
|
bool SGExclusiveThread::is_running()
|
|
{
|
|
return process_running;
|
|
}
|