From 42cde1b459c832f24ee3c49896fcfbd027c0ea29 Mon Sep 17 00:00:00 2001 From: Eddy Ashton Date: Thu, 17 Jul 2025 14:32:46 +0000 Subject: [PATCH] Remove unnecessarily public + repeated singleton initialisation --- src/consensus/aft/test/driver.cpp | 8 -------- src/consensus/aft/test/main.cpp | 3 --- src/ds/thread_messaging.h | 10 ++++++++-- src/enclave/main.cpp | 3 --- src/indexing/test/indexing.cpp | 5 ----- src/node/rpc/test/frontend_test.cpp | 6 ------ src/node/test/channels.cpp | 3 --- src/node/test/historical_queries.cpp | 3 --- src/node/test/history.cpp | 3 --- src/node/test/history_bench.cpp | 8 -------- src/node/test/snapshot.cpp | 3 --- src/node/test/snapshotter.cpp | 5 ----- 12 files changed, 8 insertions(+), 52 deletions(-) diff --git a/src/consensus/aft/test/driver.cpp b/src/consensus/aft/test/driver.cpp index 1bae98be99a1..391f93d5888d 100644 --- a/src/consensus/aft/test/driver.cpp +++ b/src/consensus/aft/test/driver.cpp @@ -15,14 +15,6 @@ using namespace std; -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - -namespace threading -{ - std::map thread_ids; -} - constexpr auto shash = ccf::ds::fnv_1a; int main(int argc, char** argv) diff --git a/src/consensus/aft/test/main.cpp b/src/consensus/aft/test/main.cpp index 8bdf43430014..d42bfd51d2d0 100644 --- a/src/consensus/aft/test/main.cpp +++ b/src/consensus/aft/test/main.cpp @@ -7,9 +7,6 @@ #define DOCTEST_CONFIG_IMPLEMENT #include -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - using ms = std::chrono::milliseconds; DOCTEST_TEST_CASE("Single node startup" * doctest::test_suite("single")) diff --git a/src/ds/thread_messaging.h b/src/ds/thread_messaging.h index 2823d597d1ee..40ff49285ee4 100644 --- a/src/ds/thread_messaging.h +++ b/src/ds/thread_messaging.h @@ -237,7 +237,11 @@ namespace threading return tasks[task_id]; } - static std::unique_ptr singleton; + static std::unique_ptr& get_singleton() + { + static std::unique_ptr singleton = nullptr; + return singleton; + } public: static constexpr uint16_t max_num_threads = 24; @@ -262,6 +266,7 @@ namespace threading static void init(uint16_t num_task_queues) { + auto& singleton = get_singleton(); if (singleton != nullptr) { throw std::logic_error("Called init() multiple times"); @@ -272,11 +277,12 @@ namespace threading static void shutdown() { - singleton.reset(); + get_singleton().reset(); } static ThreadMessaging& instance() { + auto& singleton = get_singleton(); if (singleton == nullptr) { throw std::logic_error( diff --git a/src/enclave/main.cpp b/src/enclave/main.cpp index 3b9756405c73..76302564681d 100644 --- a/src/enclave/main.cpp +++ b/src/enclave/main.cpp @@ -21,9 +21,6 @@ static std::atomic e; std::atomic num_pending_threads = 0; std::atomic num_complete_threads = 0; -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - constexpr size_t min_gap_between_initiation_attempts_us = 2'000'000; // 2 seconds std::chrono::microseconds ccf::Channel::min_gap_between_initiation_attempts( diff --git a/src/indexing/test/indexing.cpp b/src/indexing/test/indexing.cpp index 2b7735ec6d7e..6f01b0d21d22 100644 --- a/src/indexing/test/indexing.cpp +++ b/src/indexing/test/indexing.cpp @@ -18,11 +18,6 @@ #define DOCTEST_CONFIG_IMPLEMENT #include -// Transitively see a header that tries to use ThreadMessaging, so need to -// create static singleton -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - using IndexA = ccf::indexing::strategies::SeqnosByKey_InMemory; using LazyIndexA = ccf::indexing::LazyStrategy; diff --git a/src/node/rpc/test/frontend_test.cpp b/src/node/rpc/test/frontend_test.cpp index 24e3731cf131..04b29c7edb9c 100644 --- a/src/node/rpc/test/frontend_test.cpp +++ b/src/node/rpc/test/frontend_test.cpp @@ -26,12 +26,6 @@ #include #include -namespace threading -{ - std::unique_ptr<::threading::ThreadMessaging> ThreadMessaging::singleton = - nullptr; -}; - using namespace ccf; using namespace std; diff --git a/src/node/test/channels.cpp b/src/node/test/channels.cpp index bcb6c7b0ec85..c2ec5c423e99 100644 --- a/src/node/test/channels.cpp +++ b/src/node/test/channels.cpp @@ -40,9 +40,6 @@ void sleep_to_reinitiate() 2 * ccf::Channel::min_gap_between_initiation_attempts); } -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - class IORingbuffersFixture { protected: diff --git a/src/node/test/historical_queries.cpp b/src/node/test/historical_queries.cpp index 9c5d66fc4afe..f51e954fb4fc 100644 --- a/src/node/test/historical_queries.cpp +++ b/src/node/test/historical_queries.cpp @@ -23,9 +23,6 @@ #define DOCTEST_CONFIG_IMPLEMENT #include -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - using NumToString = ccf::kv::Map; constexpr size_t certificate_validity_period_days = 365; diff --git a/src/node/test/history.cpp b/src/node/test/history.cpp index 2db9394023d4..07ada86a6adc 100644 --- a/src/node/test/history.cpp +++ b/src/node/test/history.cpp @@ -18,9 +18,6 @@ #include #undef FAIL -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - using MapT = ccf::kv::Map; constexpr size_t certificate_validity_period_days = 365; diff --git a/src/node/test/history_bench.cpp b/src/node/test/history_bench.cpp index 2bf85d0c4ccc..6e83c5965c44 100644 --- a/src/node/test/history_bench.cpp +++ b/src/node/test/history_bench.cpp @@ -10,14 +10,6 @@ #define PICOBENCH_IMPLEMENT #include -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - -namespace threading -{ - std::map thread_ids; -} - using namespace ccf; class DummyConsensus : public ccf::kv::test::StubConsensus diff --git a/src/node/test/snapshot.cpp b/src/node/test/snapshot.cpp index 3ed6815a817d..ab176034ddde 100644 --- a/src/node/test/snapshot.cpp +++ b/src/node/test/snapshot.cpp @@ -14,9 +14,6 @@ #undef FAIL #include -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - TEST_CASE("Snapshot with merkle tree" * doctest::test_suite("snapshot")) { auto source_consensus = std::make_shared(); diff --git a/src/node/test/snapshotter.cpp b/src/node/test/snapshotter.cpp index 0631efd91928..6869788c6818 100644 --- a/src/node/test/snapshotter.cpp +++ b/src/node/test/snapshotter.cpp @@ -15,11 +15,6 @@ #include #include -// Because snapshot serialisation is costly, the snapshotter serialises -// snapshots asynchronously. -std::unique_ptr - threading::ThreadMessaging::singleton = nullptr; - constexpr auto buffer_size = 1024 * 16; auto node_kp = ccf::crypto::make_key_pair();