1
0
Fork 0

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:45:28 +01:00
parent 94b12aa992
commit 2d2d5dbb76
2 changed files with 18 additions and 18 deletions

View file

@ -30,6 +30,7 @@
#include <cassert> #include <cassert>
#include <stdint.h> // for int64_t #include <stdint.h> // for int64_t
#include <sstream> // for std::ostringstream #include <sstream> // for std::ostringstream
#include <mutex>
#ifdef SYSTEM_SQLITE #ifdef SYSTEM_SQLITE
// the standard sqlite3.h doesn't give a way to set SQLITE_UINT64_TYPE, // the standard sqlite3.h doesn't give a way to set SQLITE_UINT64_TYPE,
@ -54,7 +55,6 @@
#include <simgear/misc/sg_dir.hxx> #include <simgear/misc/sg_dir.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
#include <simgear/threads/SGThread.hxx> #include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <Main/globals.hxx> #include <Main/globals.hxx>
#include <Main/fg_props.hxx> #include <Main/fg_props.hxx>
@ -172,7 +172,7 @@ public:
bool isFinished() const bool isFinished() const
{ {
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
return _isFinished; return _isFinished;
} }
@ -183,7 +183,7 @@ public:
_cache->doRebuild(); _cache->doRebuild();
SG_LOG(SG_NAVCACHE, SG_INFO, "cache rebuild took:" << st.elapsedMSec() << "msec"); SG_LOG(SG_NAVCACHE, SG_INFO, "cache rebuild took:" << st.elapsedMSec() << "msec");
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
_isFinished = true; _isFinished = true;
_phase = NavDataCache::REBUILD_DONE; _phase = NavDataCache::REBUILD_DONE;
} }
@ -191,7 +191,7 @@ public:
NavDataCache::RebuildPhase currentPhase() const NavDataCache::RebuildPhase currentPhase() const
{ {
NavDataCache::RebuildPhase ph; NavDataCache::RebuildPhase ph;
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
ph = _phase; ph = _phase;
return ph; return ph;
} }
@ -199,14 +199,14 @@ public:
unsigned int completionPercent() const unsigned int completionPercent() const
{ {
unsigned int perc = 0; unsigned int perc = 0;
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
perc = _completionPercent; perc = _completionPercent;
return perc; return perc;
} }
void setProgress(NavDataCache::RebuildPhase ph, unsigned int percent) void setProgress(NavDataCache::RebuildPhase ph, unsigned int percent)
{ {
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
_phase = ph; _phase = ph;
_completionPercent = percent; _completionPercent = percent;
} }
@ -215,7 +215,7 @@ private:
NavDataCache* _cache; NavDataCache* _cache;
NavDataCache::RebuildPhase _phase; NavDataCache::RebuildPhase _phase;
unsigned int _completionPercent; unsigned int _completionPercent;
mutable SGMutex _lock; mutable std::mutex _lock;
bool _isFinished; bool _isFinished;
}; };
@ -2529,7 +2529,7 @@ public:
break; break;
} else if (err == SQLITE_ROW) { } else if (err == SQLITE_ROW) {
PositionedID r = sqlite3_column_int64(query, 0); PositionedID r = sqlite3_column_int64(query, 0);
SGGuard<SGMutex> g(lock); std::lock_guard<std::mutex> g(lock);
results.push_back(r); results.push_back(r);
} else if (err == SQLITE_BUSY) { } else if (err == SQLITE_BUSY) {
// sleep a tiny amount // sleep a tiny amount
@ -2540,11 +2540,11 @@ public:
} }
} }
SGGuard<SGMutex> g(lock); std::lock_guard<std::mutex> g(lock);
isComplete = true; isComplete = true;
} }
SGMutex lock; std::mutex lock;
sqlite3* db; sqlite3* db;
sqlite3_stmt_ptr query; sqlite3_stmt_ptr query;
PositionedIDVec results; PositionedIDVec results;
@ -2576,7 +2576,7 @@ NavDataCache::ThreadedGUISearch::ThreadedGUISearch(const std::string& term, bool
NavDataCache::ThreadedGUISearch::~ThreadedGUISearch() NavDataCache::ThreadedGUISearch::~ThreadedGUISearch()
{ {
{ {
SGGuard<SGMutex> g(d->lock); std::lock_guard<std::mutex> g(d->lock);
d->quit = true; d->quit = true;
} }
@ -2589,7 +2589,7 @@ PositionedIDVec NavDataCache::ThreadedGUISearch::results() const
{ {
PositionedIDVec r; PositionedIDVec r;
{ {
SGGuard<SGMutex> g(d->lock); std::lock_guard<std::mutex> g(d->lock);
r = std::move(d->results); r = std::move(d->results);
} }
return r; return r;
@ -2597,7 +2597,7 @@ PositionedIDVec NavDataCache::ThreadedGUISearch::results() const
bool NavDataCache::ThreadedGUISearch::isComplete() const bool NavDataCache::ThreadedGUISearch::isComplete() const
{ {
SGGuard<SGMutex> g(d->lock); std::lock_guard<std::mutex> g(d->lock);
return d->isComplete; return d->isComplete;
} }

View file

@ -45,6 +45,7 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <mutex>
#include <string> #include <string>
@ -59,11 +60,10 @@
#include <simgear/props/props.hxx> #include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx> #include <simgear/structure/subsystem_mgr.hxx>
#include <simgear/structure/exception.hxx> #include <simgear/structure/exception.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/timing/sg_time.hxx> #include <simgear/timing/sg_time.hxx>
#include <simgear/xml/easyxml.hxx> #include <simgear/xml/easyxml.hxx>
#include <simgear/threads/SGThread.hxx>
#include <simgear/threads/SGGuard.hxx>
#include <simgear/scene/tsync/terrasync.hxx> #include <simgear/scene/tsync/terrasync.hxx>
#include <AIModel/AIAircraft.hxx> #include <AIModel/AIAircraft.hxx>
@ -124,7 +124,7 @@ public:
bool isFinished() const bool isFinished() const
{ {
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
return _isFinished; return _isFinished;
} }
@ -137,7 +137,7 @@ public:
} }
} }
SGGuard<SGMutex> g(_lock); std::lock_guard<std::mutex> g(_lock);
_isFinished = true; _isFinished = true;
} }
@ -394,7 +394,7 @@ private:
} }
FGTrafficManager* _trafficManager; FGTrafficManager* _trafficManager;
mutable SGMutex _lock; mutable std::mutex _lock;
bool _isFinished; bool _isFinished;
bool _cancelThread; bool _cancelThread;
simgear::PathList _trafficDirPaths; simgear::PathList _trafficDirPaths;