Switch to C++11 threads, mutexes and lock_guards. Switching to C++11 condition_variables requires some more thought.

This commit is contained in:
Erik Hofman
2020-01-22 13:44:47 +01:00
parent 867cf6b60a
commit aab88b411e
27 changed files with 160 additions and 351 deletions

View File

@@ -20,9 +20,9 @@
#include "BVHPager.hxx"
#include <list>
#include <mutex>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include "BVHPageNode.hxx"
#include "BVHPageRequest.hxx"
@@ -37,12 +37,12 @@ struct BVHPager::_PrivateData : protected SGThread {
struct _LockedQueue {
void _push(const _Request& request)
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
_requestList.push_back(request);
}
_Request _pop()
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
if (_requestList.empty())
return _Request();
_Request request;
@@ -51,7 +51,7 @@ struct BVHPager::_PrivateData : protected SGThread {
return request;
}
private:
SGMutex _mutex;
std::mutex _mutex;
_RequestList _requestList;
};
@@ -62,7 +62,7 @@ struct BVHPager::_PrivateData : protected SGThread {
}
void _push(const _Request& request)
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
bool needSignal = _requestList.empty();
_requestList.push_back(request);
if (needSignal)
@@ -70,7 +70,7 @@ struct BVHPager::_PrivateData : protected SGThread {
}
_Request _pop()
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
while (_requestList.empty())
_waitCondition.wait(_mutex);
_Request request;
@@ -79,7 +79,7 @@ struct BVHPager::_PrivateData : protected SGThread {
return request;
}
private:
SGMutex _mutex;
std::mutex _mutex;
SGWaitCondition _waitCondition;
_RequestList _requestList;
};

View File

@@ -26,9 +26,9 @@
#include <simgear/sg_inlines.h>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <cstdlib> // for malloc
#include <mutex>
namespace simgear
{
@@ -36,7 +36,7 @@ namespace simgear
class BufferedLogCallback::BufferedLogCallbackPrivate
{
public:
SGMutex m_mutex;
std::mutex m_mutex;
vector_cstring m_buffer;
unsigned int m_stamp;
unsigned int m_maxLength;
@@ -74,7 +74,7 @@ void BufferedLogCallback::operator()(sgDebugClass c, sgDebugPriority p,
msg = (vector_cstring::value_type) strdup(aMessage.c_str());
}
SGGuard<SGMutex> g(d->m_mutex);
std::lock_guard<std::mutex> g(d->m_mutex);
d->m_buffer.push_back(msg);
d->m_stamp++;
}
@@ -86,7 +86,7 @@ unsigned int BufferedLogCallback::stamp() const
unsigned int BufferedLogCallback::threadsafeCopy(vector_cstring& aOutput)
{
SGGuard<SGMutex> g(d->m_mutex);
std::lock_guard<std::mutex> g(d->m_mutex);
size_t sz = d->m_buffer.size();
aOutput.resize(sz);
memcpy(aOutput.data(), d->m_buffer.data(), sz * sizeof(vector_cstring::value_type));

View File

@@ -28,13 +28,13 @@
#include <fstream>
#include <sstream>
#include <algorithm>
#include <mutex>
#include <boost/foreach.hpp>
#include <simgear/sg_inlines.h>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGQueue.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/misc/sg_path.hxx>
@@ -398,7 +398,7 @@ public:
removeCallbacks();
}
SGMutex m_lock;
std::mutex m_lock;
SGBlockingQueue<LogEntry> m_entries;
// log entries posted during startup
@@ -427,7 +427,7 @@ public:
void startLog()
{
SGGuard<SGMutex> g(m_lock);
std::lock_guard<std::mutex> g(m_lock);
if (m_isRunning) return;
m_isRunning = true;
start();
@@ -440,7 +440,7 @@ public:
}
{
SGGuard<SGMutex> g(m_lock);
std::lock_guard<std::mutex> g(m_lock);
m_startupLogging = on;
m_startupEntries.clear();
}
@@ -456,7 +456,7 @@ public:
return;
}
{
SGGuard<SGMutex> g(m_lock);
std::lock_guard<std::mutex> g(m_lock);
if (m_startupLogging) {
// save to the startup list for not-yet-added callbacks to
// pull down on startup
@@ -474,7 +474,7 @@ public:
bool stop()
{
{
SGGuard<SGMutex> g(m_lock);
std::lock_guard<std::mutex> g(m_lock);
if (!m_isRunning) {
return false;
}
@@ -574,7 +574,7 @@ public:
/////////////////////////////////////////////////////////////////////////////
static std::unique_ptr<logstream> global_logstream;
static SGMutex global_logStreamLock;
static std::mutex global_logStreamLock;
logstream::logstream()
{
@@ -742,7 +742,7 @@ sglog()
// http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
// in the absence of portable memory barrier ops in Simgear,
// let's keep this correct & safe
SGGuard<SGMutex> g(global_logStreamLock);
std::lock_guard<std::mutex> g(global_logStreamLock);
if( !global_logstream )
global_logstream.reset(new logstream);
@@ -823,7 +823,7 @@ void requestConsole()
void shutdownLogging()
{
SGGuard<SGMutex> g(global_logStreamLock);
std::lock_guard<std::mutex> g(global_logStreamLock);
global_logstream.reset();
}

View File

@@ -210,9 +210,11 @@ logstream& sglog();
} } while(0)
#ifdef FG_NDEBUG
# define SG_LOG(C,P,M) do { if((P) == SG_POPUP) SG_LOGX(C,P,M) } while(0)
# define SG_LOG_NAN(C,P,M) SG_LOG(C,P,M)
# define SG_HEXDUMP(C,P,MEM,LEN)
#else
# define SG_LOG(C,P,M) SG_LOGX(C,P,M)
# define SG_LOG_NAN(C,P,M) do { SG_LOGX(C,P,M); throw std::overflow_error(M); } while(0)
# define SG_LOG_HEXDUMP(C,P,MEM,LEN) if(sglog().would_log(C,P)) sglog().hexdump(C, P, __FILE__, __LINE__, MEM, LEN)
#endif

View File

@@ -27,7 +27,7 @@
#include <set>
#include <vector>
#include <atomic>
#include <simgear/threads/SGThread.hxx>
#include <mutex>
namespace simgear
{
@@ -41,7 +41,7 @@ namespace simgear
RecipientList recipient_list;
RecipientList deleted_recipients;
int CurrentRecipientIndex = 0;
SGMutex _lock;
std::mutex _lock;
std::atomic<int> receiveDepth;
std::atomic<int> sentMessageCount;

View File

@@ -7,6 +7,7 @@
#include <iostream>
#include <simgear/threads/SGThread.hxx>
#include <simgear/emesary/Emesary.hxx>
using std::cout;

View File

@@ -19,11 +19,13 @@
# include <simgear_config.h>
#endif
#include <mutex>
#include <simgear/compiler.h>
#include "RTIFederateFactoryRegistry.hxx"
#include "simgear/threads/SGGuard.hxx"
#include "simgear/threads/std::lock_guard.hxx"
#include "RTIFederate.hxx"
namespace simgear {
@@ -39,7 +41,7 @@ RTIFederateFactoryRegistry::~RTIFederateFactoryRegistry()
SGSharedPtr<RTIFederate>
RTIFederateFactoryRegistry::create(const std::string& name, const std::list<std::string>& stringList) const
{
SGGuard<SGMutex> guard(_mutex);
std::lock_guard<std::mutex> guard(_mutex);
for (FederateFactoryList::const_iterator i = _federateFactoryList.begin(); i != _federateFactoryList.end(); ++i) {
SGSharedPtr<RTIFederate> federate = (*i)->create(name, stringList);
if (!federate.valid())
@@ -52,7 +54,7 @@ RTIFederateFactoryRegistry::create(const std::string& name, const std::list<std:
void
RTIFederateFactoryRegistry::registerFactory(RTIFederateFactory* factory)
{
SGGuard<SGMutex> guard(_mutex);
std::lock_guard<std::mutex> guard(_mutex);
_federateFactoryList.push_back(factory);
}

View File

@@ -33,6 +33,7 @@
#include <errno.h>
#include <map>
#include <stdexcept>
#include <mutex>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/case_conv.hpp>
@@ -48,8 +49,6 @@
#include <simgear/debug/logstream.hxx>
#include <simgear/timing/timestamp.hxx>
#include <simgear/structure/exception.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#if defined( HAVE_VERSION_H ) && HAVE_VERSION_H
#include "version.h"
@@ -125,9 +124,9 @@ Client::Client() :
setUserAgent("SimGear-" SG_STRINGIZE(SIMGEAR_VERSION));
static bool didInitCurlGlobal = false;
static SGMutex initMutex;
static std::mutex initMutex;
SGGuard<SGMutex> g(initMutex);
std::lock_guard<std::mutex> g(initMutex);
if (!didInitCurlGlobal) {
curl_global_init(CURL_GLOBAL_ALL);
didInitCurlGlobal = true;

View File

@@ -36,6 +36,7 @@
#include <cstring>
#include <cassert>
#include <cstdio> // for snprintf
#include <mutex>
#include <errno.h>
#if defined(WINSOCK)
@@ -213,7 +214,7 @@ private:
return ok;
}
SGMutex _lock;
std::mutex _lock;
SGWaitCondition _wait;
typedef std::map<string, simgear::IPAddress*> AddressCache;

View File

@@ -37,6 +37,7 @@
#include <map>
#include <list>
#include <utility>
#include <mutex>
#include <boost/optional.hpp>
#include <simgear/threads/SGThread.hxx>
@@ -48,7 +49,7 @@ namespace simgear
class lru_cache
{
public:
SGMutex _mutex;
std::mutex _mutex;
typedef Key key_type;
typedef Value value_type;
@@ -85,13 +86,13 @@ namespace simgear
bool contains(const key_type &key)
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
return m_map.find(key) != m_map.end();
}
void insert(const key_type &key, const value_type &value)
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
typename map_type::iterator i = m_map.find(key);
if (i == m_map.end()) {
// insert item into the cache, but first check if it is full
@@ -107,7 +108,7 @@ namespace simgear
}
boost::optional<key_type> findValue(const std::string &requiredValue)
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
for (typename map_type::iterator it = m_map.begin(); it != m_map.end(); ++it)
if (it->second.first == requiredValue)
return it->first;
@@ -115,7 +116,7 @@ namespace simgear
}
boost::optional<value_type> get(const key_type &key)
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
// lookup value in the cache
typename map_type::iterator i = m_map.find(key);
if (i == m_map.end()) {
@@ -148,7 +149,7 @@ namespace simgear
void clear()
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
m_map.clear();
m_list.clear();
}
@@ -156,7 +157,7 @@ namespace simgear
private:
void evict()
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
// evict item from the end of most recently used list
typename list_type::iterator i = --m_list.end();
m_map.erase(*i);

View File

@@ -33,6 +33,7 @@
#include <queue>
#include <utility>
#include <unordered_map>
#include <mutex>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
@@ -79,8 +80,6 @@
#include <simgear/structure/SGExpression.hxx>
#include <simgear/props/props_io.hxx>
#include <simgear/props/vectorPropTemplates.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
namespace simgear
{
@@ -104,7 +103,7 @@ ref_ptr<Uniform> UniformFactoryImpl::getUniform( Effect * effect,
SGConstPropertyNode_ptr valProp,
const SGReaderWriterOptions* options )
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
std::string val = "0";
if (valProp->nChildren() == 0) {
@@ -191,7 +190,7 @@ void UniformFactoryImpl::addListener(DeferredPropertyListener* listener)
void UniformFactoryImpl::updateListeners( SGPropertyNode* propRoot )
{
SGGuard<SGMutex> scopeLock(_mutex);
std::lock_guard<std::mutex> scopeLock(_mutex);
if (deferredListenerList.empty()) return;
@@ -1475,10 +1474,10 @@ void mergeSchemesFallbacks(Effect *effect, const SGReaderWriterOptions *options)
// Walk the techniques property tree, building techniques and
// passes.
static SGMutex realizeTechniques_lock;
static std::mutex realizeTechniques_lock;
bool Effect::realizeTechniques(const SGReaderWriterOptions* options)
{
SGGuard<SGMutex> g(realizeTechniques_lock);
std::lock_guard<std::mutex> g(realizeTechniques_lock);
if (getPropertyRoot()->getBoolValue("/sim/version/compositor-support", false))
mergeSchemesFallbacks(this, options);

View File

@@ -21,6 +21,7 @@
#include <string>
#include <unordered_map>
#include <queue>
#include <mutex>
#include <boost/functional/hash.hpp>
#include <boost/tuple/tuple.hpp>
@@ -32,7 +33,6 @@
#include <simgear/props/props.hxx>
#include <simgear/scene/util/UpdateOnceCallback.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/structure/Singleton.hxx>
namespace osg
@@ -189,7 +189,7 @@ private:
static const char* vec3Names[];
static const char* vec4Names[];
SGMutex _mutex;
std::mutex _mutex;
typedef boost::tuple<std::string, osg::Uniform::Type, std::string, std::string> UniformCacheKey;
typedef boost::tuple<osg::ref_ptr<osg::Uniform>, SGPropertyChangeListener*> UniformCacheValue;

View File

@@ -31,6 +31,7 @@
#include <map>
#include <vector>
#include <string>
#include <mutex>
#include <boost/foreach.hpp>
#include "mat.hxx"
@@ -50,7 +51,6 @@
#include <simgear/debug/logstream.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/scene/util/SGReaderWriterOptions.hxx>
#include <simgear/props/props_io.hxx>
@@ -463,7 +463,7 @@ Effect* SGMaterial::get_effect(int i)
Effect* SGMaterial::get_one_effect(int texIndex)
{
SGGuard<SGMutex> g(_lock);
std::lock_guard<std::mutex> g(_lock);
if (_status.empty()) {
SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
return 0;
@@ -475,7 +475,7 @@ Effect* SGMaterial::get_one_effect(int texIndex)
Effect* SGMaterial::get_effect()
{
SGGuard<SGMutex> g(_lock);
std::lock_guard<std::mutex> g(_lock);
return get_effect(0);
}

View File

@@ -31,6 +31,7 @@
#include <string> // Standard C++ string library
#include <vector>
#include <map>
#include <mutex>
#include "Effect.hxx"
@@ -517,7 +518,7 @@ private:
const SGPropertyNode* parameters;
// per-material lock for entrypoints called from multiple threads
SGMutex _lock;
std::mutex _lock;
////////////////////////////////////////////////////////////////////
// Internal constructors and methods.

View File

@@ -31,6 +31,7 @@
#include <string.h>
#include <string>
#include <mutex>
#include <osgDB/Registry>
@@ -41,8 +42,6 @@
#include <simgear/props/props_io.hxx>
#include <simgear/props/condition.hxx>
#include <simgear/scene/tgdb/userdata.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include "mat.hxx"
@@ -56,7 +55,7 @@ using std::string;
class SGMaterialLib::MatLibPrivate
{
public:
SGMutex mutex;
std::mutex mutex;
};
// Constructor

View File

@@ -61,7 +61,6 @@
#include <simgear/props/props_io.hxx>
#include <simgear/props/condition.hxx>
#include <simgear/io/sg_file.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/misc/lru_cache.hxx>
#include "BoundingVolumeBuildVisitor.hxx"

View File

@@ -57,7 +57,6 @@
#include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/threads/SGQueue.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/misc/sg_dir.hxx>
#include <simgear/debug/BufferedLogCallback.hxx>
@@ -242,31 +241,31 @@ public:
bool isIdle()
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
return !_state._busy;
}
bool isRunning()
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
return _running;
}
bool isStalled()
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
return _state._stalled;
}
bool hasServer()
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
return _state._hasServer;
}
bool hasServer( bool flag )
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
return (_state._hasServer = flag);
}
@@ -309,14 +308,14 @@ public:
void setAllowedErrorCount(int errors)
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_state._allowed_errors = errors;
}
void setCachePath(const SGPath& p) {_persistentCachePath = p;}
void setCacheHits(unsigned int hits)
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_state._cache_hits = hits;
}
@@ -324,7 +323,7 @@ public:
{
TerrasyncThreadState st;
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
st = _state;
}
return st;
@@ -332,7 +331,7 @@ public:
private:
void incrementCacheHits()
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_state._cache_hits++;
}
@@ -371,7 +370,7 @@ private:
string _dnsdn;
TerrasyncThreadState _state;
SGMutex _stateLock;
std::mutex _stateLock;
};
SGTerraSync::WorkerThread::WorkerThread() :
@@ -392,7 +391,7 @@ void SGTerraSync::WorkerThread::stop()
// set stop flag and wake up the thread with an empty request
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_stop = true;
}
@@ -520,7 +519,7 @@ bool SGTerraSync::WorkerThread::findServer()
void SGTerraSync::WorkerThread::run()
{
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_running = true;
}
@@ -529,7 +528,7 @@ void SGTerraSync::WorkerThread::run()
runInternal();
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_running = false;
}
}
@@ -640,7 +639,7 @@ void SGTerraSync::WorkerThread::runInternal()
}
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_state._transfer_rate = _http.transferRateBytesPerSec();
// convert from bytes to kbytes
_state._total_kb_downloaded = static_cast<int>(_http.totalBytesDownloaded() / 1024);
@@ -676,7 +675,7 @@ void SGTerraSync::WorkerThread::runInternal()
}
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
_state._totalKbPending = newPendingCount; // approximately atomic update
_state._busy = anySlotBusy;
}
@@ -714,7 +713,7 @@ SyncItem::Status SGTerraSync::WorkerThread::isPathCached(const SyncItem& next) c
void SGTerraSync::WorkerThread::fail(SyncItem failedItem)
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
time_t now = time(0);
_state._consecutive_errors++;
_state._fail_count++;
@@ -742,7 +741,7 @@ void SGTerraSync::WorkerThread::notFound(SyncItem item)
void SGTerraSync::WorkerThread::updated(SyncItem item, bool isNewDirectory)
{
{
SGGuard<SGMutex> g(_stateLock);
std::lock_guard<std::mutex> g(_stateLock);
time_t now = time(0);
_state._consecutive_errors = 0;
_state._success_count++;

View File

@@ -31,7 +31,7 @@
#elif defined(GCC_ATOMIC_BUILTINS_FOUND)
#elif defined(__GNUC__) && defined(__i386__)
#elif defined(SGATOMIC_USE_MUTEX)
# include <simgear/threads/SGGuard.hxx>
# include <mutex>
#else
# error
#endif
@@ -52,7 +52,7 @@ SGAtomic::operator++()
: "memory");
return result + 1;
#else
SGGuard<SGMutex> lock(mMutex);
std::lock_guard<std::mutex> lock(mMutex);
return ++mValue;
#endif
}
@@ -73,7 +73,7 @@ SGAtomic::operator--()
: "memory");
return result - 1;
#else
SGGuard<SGMutex> lock(mMutex);
std::lock_guard<std::mutex> lock(mMutex);
return --mValue;
#endif
}
@@ -89,7 +89,7 @@ SGAtomic::operator unsigned() const
__asm__ __volatile__("": : : "memory");
return mValue;
#else
SGGuard<SGMutex> lock(mMutex);
std::lock_guard<std::mutex> lock(mMutex);
return mValue;
#endif
}
@@ -111,7 +111,7 @@ SGAtomic::compareAndExchange(unsigned oldValue, unsigned newValue)
: "memory");
return before == oldValue;
#else
SGGuard<SGMutex> lock(mMutex);
std::lock_guard<std::mutex> lock(mMutex);
if (mValue != oldValue)
return false;
mValue = newValue;

View File

@@ -111,7 +111,7 @@ private:
SGAtomic& operator=(const SGAtomic&);
#if defined(SGATOMIC_USE_MUTEX)
mutable SGMutex mMutex;
mutable std::mutex mMutex;
#endif
unsigned mValue;
};

View File

@@ -1,6 +1,5 @@
#include "StringTable.hxx"
#include <simgear/threads/SGGuard.hxx>
namespace simgear
{
@@ -8,7 +7,7 @@ using namespace std;
const string* StringTable::insert(const string& str)
{
SGGuard<SGMutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_mutex);
StringContainer::iterator it = _strings.insert(str).first;
return &*it;
}

View File

@@ -2,8 +2,8 @@
#define SIMGEAR_STRINGTABLE_HXX 1
#include <string>
#include <mutex>
#include <simgear/threads/SGThread.hxx>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
@@ -21,7 +21,7 @@ class StringTable
{
const std::string* insert(const std::string& str);
private:
SGMutex _mutex;
std::mutex _mutex;
StringContainer _strings;
};
}

View File

@@ -8,6 +8,7 @@
#include <memory>
#include <cassert>
#include <mutex>
#include <simgear/props/props_io.hxx>
@@ -15,7 +16,6 @@
#include <simgear/structure/exception.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/debug/logstream.hxx>
struct Invocation
@@ -32,7 +32,7 @@ public:
using InvocactionVec = std::vector<Invocation>;
InvocactionVec _queue;
SGMutex _mutex;
std::mutex _mutex;
using command_map = std::map<std::string,Command*> ;
command_map _commands;
@@ -179,7 +179,7 @@ void SGCommandMgr::queuedExecute(const std::string &name, const SGPropertyNode*
{
Invocation invoke = {name, new SGPropertyNode};
copyProperties(arg, invoke.args);
SGGuard<SGMutex> g(d->_mutex);
std::lock_guard<std::mutex> g(d->_mutex);
d->_queue.push_back(invoke);
}
@@ -192,7 +192,7 @@ void SGCommandMgr::executedQueuedCommands()
// locked swap with the shared queue
Private::InvocactionVec q;
{
SGGuard<SGMutex> g(d->_mutex);
std::lock_guard<std::mutex> g(d->_mutex);
d->_queue.swap(q);
}

View File

@@ -1,7 +1,6 @@
include (SimGearComponent)
set(HEADERS
SGGuard.hxx
SGQueue.hxx
SGThread.hxx)

View File

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

View File

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

View File

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

View File

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