From 1f89e2a49a2170a57b14d993f181f29233b7d250 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Thu, 8 May 2025 19:39:47 +0000 Subject: [PATCH] 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- --- src/net.cpp | 16 ++++++++-------- src/net.h | 10 +++++----- src/wallet/sqlite.h | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 5e7b8bda351..00fec2bca3e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1893,7 +1893,7 @@ bool CConnman::AddConnection(const std::string& address, ConnectionType conn_typ if (max_connections != std::nullopt && existing_connections >= max_connections) return false; // Max total outbound connections already exist - SemaphoreGrant grant(*semOutbound, true); + CountingSemaphoreGrant<> grant(*semOutbound, true); if (!grant) return false; OpenNetworkConnection(CAddress(), false, std::move(grant), address.c_str(), conn_type, /*use_v2transport=*/use_v2transport); @@ -2409,7 +2409,7 @@ void CConnman::ProcessAddrFetch() // peer doesn't support it or immediately disconnects us for another reason. const bool use_v2transport(GetLocalServices() & NODE_P2P_V2); CAddress addr; - SemaphoreGrant grant(*semOutbound, /*fTry=*/true); + CountingSemaphoreGrant<> grant(*semOutbound, /*fTry=*/true); if (grant) { OpenNetworkConnection(addr, false, std::move(grant), strDest.c_str(), ConnectionType::ADDR_FETCH, use_v2transport); } @@ -2583,7 +2583,7 @@ void CConnman::ThreadOpenConnections(const std::vector connect, std PerformReconnections(); - SemaphoreGrant grant(*semOutbound); + CountingSemaphoreGrant<> grant(*semOutbound); if (interruptNet) return; @@ -2961,7 +2961,7 @@ void CConnman::ThreadOpenAddedConnections() AssertLockNotHeld(m_reconnections_mutex); while (true) { - SemaphoreGrant grant(*semAddnode); + CountingSemaphoreGrant<> grant(*semAddnode); std::vector vInfo = GetAddedNodeInfo(/*include_connected=*/false); bool tried = false; for (const AddedNodeInfo& info : vInfo) { @@ -2974,7 +2974,7 @@ void CConnman::ThreadOpenAddedConnections() CAddress addr(CService(), NODE_NONE); OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport); if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return; - grant = SemaphoreGrant(*semAddnode, /*fTry=*/true); + grant = CountingSemaphoreGrant<>(*semAddnode, /*fTry=*/true); } // See if any reconnections are desired. PerformReconnections(); @@ -2985,7 +2985,7 @@ void CConnman::ThreadOpenAddedConnections() } // if successful, this moves the passed grant to the constructed node -void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, SemaphoreGrant&& grant_outbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport) +void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CountingSemaphoreGrant<>&& grant_outbound, const char *pszDest, ConnectionType conn_type, bool use_v2transport) { AssertLockNotHeld(m_unused_i2p_sessions_mutex); assert(conn_type != ConnectionType::INBOUND); @@ -3336,11 +3336,11 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions) if (semOutbound == nullptr) { // initialize semaphore - semOutbound = std::make_unique(std::min(m_max_automatic_outbound, m_max_automatic_connections)); + semOutbound = std::make_unique>(std::min(m_max_automatic_outbound, m_max_automatic_connections)); } if (semAddnode == nullptr) { // initialize semaphore - semAddnode = std::make_unique(m_max_addnode); + semAddnode = std::make_unique>(m_max_addnode); } // diff --git a/src/net.h b/src/net.h index 9fab1a345b0..a017440c212 100644 --- a/src/net.h +++ b/src/net.h @@ -729,7 +729,7 @@ public: // Setting fDisconnect to true will cause the node to be disconnected the // next time DisconnectNodes() runs std::atomic_bool fDisconnect{false}; - SemaphoreGrant grantOutbound; + CountingSemaphoreGrant<> grantOutbound; std::atomic nRefCount{0}; const uint64_t nKeyedNetGroup; @@ -1136,7 +1136,7 @@ public: bool GetNetworkActive() const { return fNetworkActive; }; bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; }; void SetNetworkActive(bool active); - 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); + 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); bool CheckIncomingNonce(uint64_t nonce); void ASMapHealthCheck(); @@ -1491,8 +1491,8 @@ private: */ std::atomic m_local_services; - std::unique_ptr semOutbound; - std::unique_ptr semAddnode; + std::unique_ptr> semOutbound; + std::unique_ptr> semAddnode; /** * Maximum number of automatic connections permitted, excluding manual @@ -1614,7 +1614,7 @@ private: struct ReconnectionInfo { CAddress addr_connect; - SemaphoreGrant grant; + CountingSemaphoreGrant<> grant; std::string destination; ConnectionType conn_type; bool use_v2transport; diff --git a/src/wallet/sqlite.h b/src/wallet/sqlite.h index c17ca3a8c9f..dda1fab204c 100644 --- a/src/wallet/sqlite.h +++ b/src/wallet/sqlite.h @@ -127,7 +127,7 @@ public: // Batches must acquire this semaphore on writing, and release when done writing. // This ensures that only one batch is modifying the database at a time. - BinarySemaphore m_write_semaphore; + std::binary_semaphore m_write_semaphore; bool Verify(bilingual_str& error);