Skip to content

Commit 1f89e2a

Browse files
committed
scripted-diff: threading: semaphore: use direct types rather than the temporary convenience ones
-BEGIN VERIFY SCRIPT- sed -i 's|BinarySemaphore|std::binary_semaphore|g' src/wallet/sqlite.h sed -i 's|SemaphoreGrant|CountingGrant|g' src/net.h src/net.cpp sed -i 's|Semaphore|std::counting_semaphore<>|g' src/net.h src/net.cpp sed -i 's|CountingGrant|CountingSemaphoreGrant<>|g' src/net.h src/net.cpp -END VERIFY SCRIPT-
1 parent f21365c commit 1f89e2a

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/net.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ
18931893
if (max_connections != std::nullopt && existing_connections >= max_connections) return false;
18941894

18951895
// Max total outbound connections already exist
1896-
SemaphoreGrant grant(*semOutbound, true);
1896+
CountingSemaphoreGrant<> grant(*semOutbound, true);
18971897
if (!grant) return false;
18981898

18991899
OpenNetworkConnection(CAddress(), false, std::move(grant), address.c_str(), conn_type, /*use_v2transport=*/use_v2transport);
@@ -2409,7 +2409,7 @@ void CConnman::ProcessAddrFetch()
24092409
// peer doesn't support it or immediately disconnects us for another reason.
24102410
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
24112411
CAddress addr;
2412-
SemaphoreGrant grant(*semOutbound, /*fTry=*/true);
2412+
CountingSemaphoreGrant<> grant(*semOutbound, /*fTry=*/true);
24132413
if (grant) {
24142414
OpenNetworkConnection(addr, false, std::move(grant), strDest.c_str(), ConnectionType::ADDR_FETCH, use_v2transport);
24152415
}
@@ -2583,7 +2583,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, std
25832583

25842584
PerformReconnections();
25852585

2586-
SemaphoreGrant grant(*semOutbound);
2586+
CountingSemaphoreGrant<> grant(*semOutbound);
25872587
if (interruptNet)
25882588
return;
25892589

@@ -2961,7 +2961,7 @@ void CConnman::ThreadOpenAddedConnections()
29612961
AssertLockNotHeld(m_reconnections_mutex);
29622962
while (true)
29632963
{
2964-
SemaphoreGrant grant(*semAddnode);
2964+
CountingSemaphoreGrant<> grant(*semAddnode);
29652965
std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo(/*include_connected=*/false);
29662966
bool tried = false;
29672967
for (const AddedNodeInfo& info : vInfo) {
@@ -2974,7 +2974,7 @@ void CConnman::ThreadOpenAddedConnections()
29742974
CAddress addr(CService(), NODE_NONE);
29752975
OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
29762976
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
2977-
grant = SemaphoreGrant(*semAddnode, /*fTry=*/true);
2977+
grant = CountingSemaphoreGrant<>(*semAddnode, /*fTry=*/true);
29782978
}
29792979
// See if any reconnections are desired.
29802980
PerformReconnections();
@@ -2985,7 +2985,7 @@ void CConnman::ThreadOpenAddedConnections()
29852985
}
29862986

29872987
// if successful, this moves the passed grant to the constructed node
2988-
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, SemaphoreGrant&& grant_outbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport)
2988+
void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CountingSemaphoreGrant<>&& grant_outbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport)
29892989
{
29902990
AssertLockNotHeld(m_unused_i2p_sessions_mutex);
29912991
assert(conn_type != ConnectionType::INBOUND);
@@ -3336,11 +3336,11 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
33363336

33373337
if (semOutbound == nullptr) {
33383338
// initialize semaphore
3339-
semOutbound = std::make_unique<Semaphore>(std::min(m_max_automatic_outbound, m_max_automatic_connections));
3339+
semOutbound = std::make_unique<std::counting_semaphore<>>(std::min(m_max_automatic_outbound, m_max_automatic_connections));
33403340
}
33413341
if (semAddnode == nullptr) {
33423342
// initialize semaphore
3343-
semAddnode = std::make_unique<Semaphore>(m_max_addnode);
3343+
semAddnode = std::make_unique<std::counting_semaphore<>>(m_max_addnode);
33443344
}
33453345

33463346
//

src/net.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ class CNode
729729
// Setting fDisconnect to true will cause the node to be disconnected the
730730
// next time DisconnectNodes() runs
731731
std::atomic_bool fDisconnect{false};
732-
SemaphoreGrant grantOutbound;
732+
CountingSemaphoreGrant<> grantOutbound;
733733
std::atomic<int> nRefCount{0};
734734

735735
const uint64_t nKeyedNetGroup;
@@ -1136,7 +1136,7 @@ class CConnman
11361136
bool GetNetworkActive() const { return fNetworkActive; };
11371137
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
11381138
void SetNetworkActive(bool active);
1139-
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, SemaphoreGrant&& grant_outbound, const char* strDest, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
1139+
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CountingSemaphoreGrant<>&& grant_outbound, const char* strDest, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
11401140
bool CheckIncomingNonce(uint64_t nonce);
11411141
void ASMapHealthCheck();
11421142

@@ -1491,8 +1491,8 @@ class CConnman
14911491
*/
14921492
std::atomic<ServiceFlags> m_local_services;
14931493

1494-
std::unique_ptr<Semaphore> semOutbound;
1495-
std::unique_ptr<Semaphore> semAddnode;
1494+
std::unique_ptr<std::counting_semaphore<>> semOutbound;
1495+
std::unique_ptr<std::counting_semaphore<>> semAddnode;
14961496

14971497
/**
14981498
* Maximum number of automatic connections permitted, excluding manual
@@ -1614,7 +1614,7 @@ class CConnman
16141614
struct ReconnectionInfo
16151615
{
16161616
CAddress addr_connect;
1617-
SemaphoreGrant grant;
1617+
CountingSemaphoreGrant<> grant;
16181618
std::string destination;
16191619
ConnectionType conn_type;
16201620
bool use_v2transport;

src/wallet/sqlite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class SQLiteDatabase : public WalletDatabase
127127

128128
// Batches must acquire this semaphore on writing, and release when done writing.
129129
// This ensures that only one batch is modifying the database at a time.
130-
BinarySemaphore m_write_semaphore;
130+
std::binary_semaphore m_write_semaphore;
131131

132132
bool Verify(bilingual_str& error);
133133

0 commit comments

Comments
 (0)