Skip to content

Commit ae5dcf5

Browse files
committed
lock more carefully
1 parent 6edb4e8 commit ae5dcf5

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

CommonData/databaseinterface.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,11 +2051,14 @@ sqlite3 * DatabaseInterface::_db()
20512051
return _dbCreated;
20522052

20532053
_dbCheckMutex.lock();
2054-
if(!_dbs.count(id))
2054+
bool itsNotThereYet = !_dbs.count(id); //tip toe around the map
2055+
_dbCheckMutex.unlock();
2056+
2057+
if(itsNotThereYet)
20552058
load();
20562059

2060+
_dbCheckMutex.lock();
20572061
sqlite3 * dbFound = _dbs.at(id);
2058-
20592062
_dbCheckMutex.unlock();
20602063

20612064
return dbFound;
@@ -2169,7 +2172,9 @@ void DatabaseInterface::load()
21692172
else
21702173
Log::log() << "Opened internal sqlite database for loading at '" << dbFile() << "'. This is for thread " << std::this_thread::get_id() << std::endl;
21712174

2175+
_dbCheckMutex.lock();
21722176
_dbs[std::this_thread::get_id()] = db;
2177+
_dbCheckMutex.unlock();
21732178

21742179
sqlite3_busy_timeout(db, 100);
21752180

@@ -2211,6 +2216,8 @@ void DatabaseInterface::close()
22112216

22122217
std::set<sqlite3*> waitingFor;
22132218

2219+
_dbCheckMutex.lock();
2220+
22142221
for(auto & idDb : _dbs)
22152222
waitingFor.insert(idDb.second);
22162223

@@ -2231,6 +2238,8 @@ void DatabaseInterface::close()
22312238
while(waitingFor.size() > 0);
22322239

22332240
_dbs.clear();
2241+
2242+
_dbCheckMutex.unlock();
22342243

22352244
while(sqlite3_close(_dbCreated) != SQLITE_OK)
22362245
{

CommonData/databaseinterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ class DatabaseInterface
208208
std::thread::id _dbCreator;
209209
sqlite3* _dbCreated = nullptr;
210210
bool _inMemory;
211-
std::mutex _loadMutex;
211+
std::mutex _loadMutex,
212+
_dbCheckMutex;
212213

213214
static std::string _wrap_sqlite3_column_text(sqlite3_stmt * stmt, int iCol);
214215
static const std::string _dbConstructionSql;

0 commit comments

Comments
 (0)