threading: rename CSemaphore methods to match std::semaphore

This commit is contained in:
Cory Fields 2025-05-08 16:12:48 +00:00
parent 66c968b4b4
commit 7b816c4e00
3 changed files with 17 additions and 17 deletions

View File

@ -3428,13 +3428,13 @@ void CConnman::Interrupt()
if (semOutbound) {
for (int i=0; i<m_max_automatic_outbound; i++) {
semOutbound->post();
semOutbound->release();
}
}
if (semAddnode) {
for (int i=0; i<m_max_addnode; i++) {
semAddnode->post();
semAddnode->release();
}
}
}

View File

@ -321,14 +321,14 @@ public:
CSemaphore& operator=(const CSemaphore&) = delete;
CSemaphore& operator=(CSemaphore&&) = delete;
void wait() noexcept
void acquire() noexcept
{
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]() { return value >= 1; });
value--;
}
bool try_wait() noexcept
bool try_acquire() noexcept
{
std::lock_guard<std::mutex> lock(mutex);
if (value < 1) {
@ -338,7 +338,7 @@ public:
return true;
}
void post() noexcept
void release() noexcept
{
{
std::lock_guard<std::mutex> lock(mutex);
@ -361,7 +361,7 @@ public:
if (fHaveGrant) {
return;
}
sem->wait();
sem->acquire();
fHaveGrant = true;
}
@ -370,13 +370,13 @@ public:
if (!fHaveGrant) {
return;
}
sem->post();
sem->release();
fHaveGrant = false;
}
bool TryAcquire() noexcept
{
if (!fHaveGrant && sem->try_wait()) {
if (!fHaveGrant && sem->try_acquire()) {
fHaveGrant = true;
}
return fHaveGrant;

View File

@ -445,7 +445,7 @@ void SQLiteBatch::Close()
try {
m_database.Open();
// If TxnAbort failed and we refreshed the connection, the semaphore was not released, so release it here to avoid deadlocks on future writes.
m_database.m_write_semaphore.post();
m_database.m_write_semaphore.release();
} catch (const std::runtime_error&) {
// If open fails, cleanup this object and rethrow the exception
m_database.Close();
@ -498,7 +498,7 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
if (!BindBlobToStatement(stmt, 2, value, "value")) return false;
// Acquire semaphore if not previously acquired when creating a transaction.
if (!m_txn) m_database.m_write_semaphore.wait();
if (!m_txn) m_database.m_write_semaphore.acquire();
// Execute
int res = sqlite3_step(stmt);
@ -508,7 +508,7 @@ bool SQLiteBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
}
if (!m_txn) m_database.m_write_semaphore.post();
if (!m_txn) m_database.m_write_semaphore.release();
return res == SQLITE_DONE;
}
@ -522,7 +522,7 @@ bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> b
if (!BindBlobToStatement(stmt, 1, blob, "key")) return false;
// Acquire semaphore if not previously acquired when creating a transaction.
if (!m_txn) m_database.m_write_semaphore.wait();
if (!m_txn) m_database.m_write_semaphore.acquire();
// Execute
int res = sqlite3_step(stmt);
@ -532,7 +532,7 @@ bool SQLiteBatch::ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> b
LogPrintf("%s: Unable to execute statement: %s\n", __func__, sqlite3_errstr(res));
}
if (!m_txn) m_database.m_write_semaphore.post();
if (!m_txn) m_database.m_write_semaphore.release();
return res == SQLITE_DONE;
}
@ -651,12 +651,12 @@ std::unique_ptr<DatabaseCursor> SQLiteBatch::GetNewPrefixCursor(std::span<const
bool SQLiteBatch::TxnBegin()
{
if (!m_database.m_db || m_txn) return false;
m_database.m_write_semaphore.wait();
m_database.m_write_semaphore.acquire();
Assert(!m_database.HasActiveTxn());
int res = Assert(m_exec_handler)->Exec(m_database, "BEGIN TRANSACTION");
if (res != SQLITE_OK) {
LogPrintf("SQLiteBatch: Failed to begin the transaction\n");
m_database.m_write_semaphore.post();
m_database.m_write_semaphore.release();
} else {
m_txn = true;
}
@ -672,7 +672,7 @@ bool SQLiteBatch::TxnCommit()
LogPrintf("SQLiteBatch: Failed to commit the transaction\n");
} else {
m_txn = false;
m_database.m_write_semaphore.post();
m_database.m_write_semaphore.release();
}
return res == SQLITE_OK;
}
@ -686,7 +686,7 @@ bool SQLiteBatch::TxnAbort()
LogPrintf("SQLiteBatch: Failed to abort the transaction\n");
} else {
m_txn = false;
m_database.m_write_semaphore.post();
m_database.m_write_semaphore.release();
}
return res == SQLITE_OK;
}