Skip to content

Commit 7b816c4

Browse files
committed
threading: rename CSemaphore methods to match std::semaphore
1 parent 66c968b commit 7b816c4

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3428,13 +3428,13 @@ void CConnman::Interrupt()
34283428

34293429
if (semOutbound) {
34303430
for (int i=0; i<m_max_automatic_outbound; i++) {
3431-
semOutbound->post();
3431+
semOutbound->release();
34323432
}
34333433
}
34343434

34353435
if (semAddnode) {
34363436
for (int i=0; i<m_max_addnode; i++) {
3437-
semAddnode->post();
3437+
semAddnode->release();
34383438
}
34393439
}
34403440
}

src/sync.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,14 @@ class CSemaphore
321321
CSemaphore& operator=(const CSemaphore&) = delete;
322322
CSemaphore& operator=(CSemaphore&&) = delete;
323323

324-
void wait() noexcept
324+
void acquire() noexcept
325325
{
326326
std::unique_lock<std::mutex> lock(mutex);
327327
condition.wait(lock, [&]() { return value >= 1; });
328328
value--;
329329
}
330330

331-
bool try_wait() noexcept
331+
bool try_acquire() noexcept
332332
{
333333
std::lock_guard<std::mutex> lock(mutex);
334334
if (value < 1) {
@@ -338,7 +338,7 @@ class CSemaphore
338338
return true;
339339
}
340340

341-
void post() noexcept
341+
void release() noexcept
342342
{
343343
{
344344
std::lock_guard<std::mutex> lock(mutex);
@@ -361,7 +361,7 @@ class CSemaphoreGrant
361361
if (fHaveGrant) {
362362
return;
363363
}
364-
sem->wait();
364+
sem->acquire();
365365
fHaveGrant = true;
366366
}
367367

@@ -370,13 +370,13 @@ class CSemaphoreGrant
370370
if (!fHaveGrant) {
371371
return;
372372
}
373-
sem->post();
373+
sem->release();
374374
fHaveGrant = false;
375375
}
376376

377377
bool TryAcquire() noexcept
378378
{
379-
if (!fHaveGrant && sem->try_wait()) {
379+
if (!fHaveGrant && sem->try_acquire()) {
380380
fHaveGrant = true;
381381
}
382382
return fHaveGrant;

src/wallet/sqlite.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void SQLiteBatch::Close()
445445
try {
446446
m_database.Open();
447447
// If TxnAbort failed and we refreshed the connection, the semaphore was not released, so release it here to avoid deadlocks on future writes.
448-
m_database.m_write_semaphore.post();
448+
m_database.m_write_semaphore.release();
449449
} catch (const std::runtime_error&) {
450450
// If open fails, cleanup this object and rethrow the exception
451451
m_database.Close();
@@ -498,7 +498,7 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
498498
if (!BindBlobToStatement(stmt, 2, value, "value")) return false;
499499

500500
// Acquire semaphore if not previously acquired when creating a transaction.
501-
if (!m_txn) m_database.m_write_semaphore.wait();
501+
if (!m_txn) m_database.m_write_semaphore.acquire();
502502

503503
// Execute
504504
int res = sqlite3_step(stmt);
@@ -508,7 +508,7 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
508508
LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
509509
}
510510

511-
if (!m_txn) m_database.m_write_semaphore.post();
511+
if (!m_txn) m_database.m_write_semaphore.release();
512512

513513
return res == SQLITE_DONE;
514514
}
@@ -522,7 +522,7 @@ bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> b
522522
if (!BindBlobToStatement(stmt, 1, blob, "key")) return false;
523523

524524
// Acquire semaphore if not previously acquired when creating a transaction.
525-
if (!m_txn) m_database.m_write_semaphore.wait();
525+
if (!m_txn) m_database.m_write_semaphore.acquire();
526526

527527
// Execute
528528
int res = sqlite3_step(stmt);
@@ -532,7 +532,7 @@ bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> b
532532
LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
533533
}
534534

535-
if (!m_txn) m_database.m_write_semaphore.post();
535+
if (!m_txn) m_database.m_write_semaphore.release();
536536

537537
return res == SQLITE_DONE;
538538
}
@@ -651,12 +651,12 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(std::span<const
651651
bool SQLiteBatch::TxnBegin()
652652
{
653653
if (!m_database.m_db || m_txn) return false;
654-
m_database.m_write_semaphore.wait();
654+
m_database.m_write_semaphore.acquire();
655655
Assert(!m_database.HasActiveTxn());
656656
int res = Assert(m_exec_handler)->Exec(m_database, "BEGIN TRANSACTION");
657657
if (res != SQLITE_OK) {
658658
LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
659-
m_database.m_write_semaphore.post();
659+
m_database.m_write_semaphore.release();
660660
} else {
661661
m_txn = true;
662662
}
@@ -672,7 +672,7 @@ bool SQLiteBatch::TxnCommit()
672672
LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
673673
} else {
674674
m_txn = false;
675-
m_database.m_write_semaphore.post();
675+
m_database.m_write_semaphore.release();
676676
}
677677
return res == SQLITE_OK;
678678
}
@@ -686,7 +686,7 @@ bool SQLiteBatch::TxnAbort()
686686
LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
687687
} else {
688688
m_txn = false;
689-
m_database.m_write_semaphore.post();
689+
m_database.m_write_semaphore.release();
690690
}
691691
return res == SQLITE_OK;
692692
}

0 commit comments

Comments
 (0)