Skip to content

Commit 1f97ab0

Browse files
committed
Fix other potential mutex issues.
1 parent 2d03884 commit 1f97ab0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

cpp/ConnectionState.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ ConnectionState::~ConnectionState() {
3030
}
3131

3232
void ConnectionState::clearLock() {
33-
if (!workQueue.empty()) {
34-
waitFinished();
35-
}
33+
waitFinished();
3634
_currentLockId = EMPTY_LOCK_ID;
3735
}
3836

@@ -47,9 +45,7 @@ bool ConnectionState::matchesLock(const ConnectionLockId &lockId) {
4745
bool ConnectionState::isEmptyLock() { return _currentLockId == EMPTY_LOCK_ID; }
4846

4947
void ConnectionState::close() {
50-
if (!workQueue.empty()) {
51-
waitFinished();
52-
}
48+
waitFinished();
5349
// So that the thread can stop (if not already)
5450
threadDone = true;
5551
sqlite3_close_v2(connection);
@@ -94,12 +90,18 @@ void ConnectionState::doWork() {
9490
--threadBusy;
9591
// Need to notify in order for waitFinished to be updated when
9692
// the queue is empty and not busy
93+
{
94+
std::unique_lock<std::mutex> g(workQueueMutex);
9795
workQueueConditionVariable.notify_all();
96+
}
9897
}
9998
}
10099

101100
void ConnectionState::waitFinished() {
102101
std::unique_lock<std::mutex> g(workQueueMutex);
102+
if (workQueue.empty()) {
103+
return;
104+
}
103105
workQueueConditionVariable.wait(
104106
g, [&] { return workQueue.empty() && (threadBusy == 0); });
105107
}

0 commit comments

Comments
 (0)