diff --git a/include/envoy/event/dispatcher.h b/include/envoy/event/dispatcher.h index 1cbd3e212f1d5..eca8369801026 100644 --- a/include/envoy/event/dispatcher.h +++ b/include/envoy/event/dispatcher.h @@ -41,11 +41,15 @@ struct DispatcherStats { ALL_DISPATCHER_STATS(GENERATE_HISTOGRAM_STRUCT) }; +using DispatcherStatsPtr = std::unique_ptr; + /** * Callback invoked when a dispatcher post() runs. */ using PostCb = std::function; +using PostCbSharedPtr = std::shared_ptr; + /** * Abstract event dispatching loop. */ diff --git a/source/common/event/dispatcher_impl.h b/source/common/event/dispatcher_impl.h index 104791708d584..143ff4eb065ce 100644 --- a/source/common/event/dispatcher_impl.h +++ b/source/common/event/dispatcher_impl.h @@ -105,7 +105,7 @@ class DispatcherImpl : Logger::Loggable, const std::string name_; Api::Api& api_; std::string stats_prefix_; - std::unique_ptr stats_; + DispatcherStatsPtr stats_; Thread::ThreadId run_tid_; Buffer::WatermarkFactoryPtr buffer_factory_; LibeventScheduler base_scheduler_; diff --git a/source/common/stats/thread_local_store.cc b/source/common/stats/thread_local_store.cc index 328163801cb95..2b7574e42484d 100644 --- a/source/common/stats/thread_local_store.cc +++ b/source/common/stats/thread_local_store.cc @@ -401,8 +401,10 @@ StatType& ThreadLocalStoreImpl::ScopeImpl::safeMakeStat( } template -absl::optional> -ThreadLocalStoreImpl::ScopeImpl::findStatLockHeld( +using StatTypeOptConstRef = absl::optional>; + +template +StatTypeOptConstRef ThreadLocalStoreImpl::ScopeImpl::findStatLockHeld( StatName name, StatNameHashMap>& central_cache_map) const { auto iter = central_cache_map.find(name); if (iter == central_cache_map.end()) { diff --git a/source/common/stats/thread_local_store.h b/source/common/stats/thread_local_store.h index 3702a9bd77eef..3496c0790e152 100644 --- a/source/common/stats/thread_local_store.h +++ b/source/common/stats/thread_local_store.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "envoy/stats/tag.h" @@ -380,6 +381,9 @@ class ThreadLocalStoreImpl : Logger::Loggable, public StoreRo MakeStatFn make_stat, StatRefMap* tls_cache, StatNameHashSet* tls_rejected_stats, StatType& null_stat); + template + using StatTypeOptConstRef = absl::optional>; + /** * Looks up an existing stat, populating the local cache if necessary. Does * not check the TLS or rejects, and does not create a stat if it does not @@ -390,7 +394,7 @@ class ThreadLocalStoreImpl : Logger::Loggable, public StoreRo * @return a reference to the stat, if it exists. */ template - absl::optional> + StatTypeOptConstRef findStatLockHeld(StatName name, StatNameHashMap>& central_cache_map) const; @@ -463,5 +467,7 @@ class ThreadLocalStoreImpl : Logger::Loggable, public StoreRo StatNameSetPtr well_known_tags_; }; +using ThreadLocalStoreImplPtr = std::unique_ptr; + } // namespace Stats } // namespace Envoy diff --git a/source/common/thread_local/thread_local_impl.cc b/source/common/thread_local/thread_local_impl.cc index 8bfb093befae7..d4d02f8b2f5f0 100644 --- a/source/common/thread_local/thread_local_impl.cc +++ b/source/common/thread_local/thread_local_impl.cc @@ -26,7 +26,7 @@ SlotPtr InstanceImpl::allocateSlot() { ASSERT(!shutdown_); if (free_slot_indexes_.empty()) { - std::unique_ptr slot(new SlotImpl(*this, slots_.size())); + SlotImplPtr slot(new SlotImpl(*this, slots_.size())); auto wrapper = std::make_unique(*this, std::move(slot)); slots_.push_back(wrapper->slot_.get()); return wrapper; @@ -34,7 +34,7 @@ SlotPtr InstanceImpl::allocateSlot() { const uint32_t idx = free_slot_indexes_.front(); free_slot_indexes_.pop_front(); ASSERT(idx < slots_.size()); - std::unique_ptr slot(new SlotImpl(*this, idx)); + SlotImplPtr slot(new SlotImpl(*this, idx)); slots_[idx] = slot.get(); return std::make_unique(*this, std::move(slot)); } @@ -56,7 +56,7 @@ ThreadLocalObjectSharedPtr InstanceImpl::SlotImpl::get() { return thread_local_data_.data_[index_]; } -InstanceImpl::Bookkeeper::Bookkeeper(InstanceImpl& parent, std::unique_ptr&& slot) +InstanceImpl::Bookkeeper::Bookkeeper(InstanceImpl& parent, SlotImplPtr&& slot) : parent_(parent), slot_(std::move(slot)), ref_count_(/*not used.*/ nullptr, [slot = slot_.get(), &parent = this->parent_](uint32_t* /* not used */) { @@ -117,7 +117,7 @@ void InstanceImpl::registerThread(Event::Dispatcher& dispatcher, bool main_threa // Puts the slot into a deferred delete container, the slot will be destructed when its out-going // callback reference count goes to 0. -void InstanceImpl::recycle(std::unique_ptr&& slot) { +void InstanceImpl::recycle(SlotImplPtr&& slot) { ASSERT(std::this_thread::get_id() == main_thread_id_); ASSERT(slot != nullptr); auto* slot_addr = slot.get(); @@ -194,11 +194,11 @@ void InstanceImpl::runOnAllThreads(Event::PostCb cb, Event::PostCb all_threads_c // for programming simplicity here. cb(); - std::shared_ptr cb_guard(new Event::PostCb(cb), - [this, all_threads_complete_cb](Event::PostCb* cb) { - main_thread_dispatcher_->post(all_threads_complete_cb); - delete cb; - }); + Event::PostCbSharedPtr cb_guard(new Event::PostCb(cb), + [this, all_threads_complete_cb](Event::PostCb* cb) { + main_thread_dispatcher_->post(all_threads_complete_cb); + delete cb; + }); for (Event::Dispatcher& dispatcher : registered_threads_) { dispatcher.post([cb_guard]() -> void { (*cb_guard)(); }); diff --git a/source/common/thread_local/thread_local_impl.h b/source/common/thread_local/thread_local_impl.h index b451c4eb236a1..71153107fb3dd 100644 --- a/source/common/thread_local/thread_local_impl.h +++ b/source/common/thread_local/thread_local_impl.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "envoy/thread_local/thread_local.h" @@ -50,10 +51,12 @@ class InstanceImpl : Logger::Loggable, public NonCopyable, pub const uint64_t index_; }; + using SlotImplPtr = std::unique_ptr; + // A Wrapper of SlotImpl which on destruction returns the SlotImpl to the deferred delete queue // (detaches it). struct Bookkeeper : public Slot { - Bookkeeper(InstanceImpl& parent, std::unique_ptr&& slot); + Bookkeeper(InstanceImpl& parent, SlotImplPtr&& slot); ~Bookkeeper() override { parent_.recycle(std::move(slot_)); } // ThreadLocal::Slot @@ -66,7 +69,7 @@ class InstanceImpl : Logger::Loggable, public NonCopyable, pub void set(InitializeCb cb) override; InstanceImpl& parent_; - std::unique_ptr slot_; + SlotImplPtr slot_; std::shared_ptr ref_count_; }; @@ -75,7 +78,7 @@ class InstanceImpl : Logger::Loggable, public NonCopyable, pub std::vector data_; }; - void recycle(std::unique_ptr&& slot); + void recycle(SlotImplPtr&& slot); // Cleanup the deferred deletes queue. void scheduleCleanup(SlotImpl* slot); @@ -89,7 +92,7 @@ class InstanceImpl : Logger::Loggable, public NonCopyable, pub // A indexed container for Slots that has to be deferred to delete due to out-going callbacks // pointing to the Slot. To let the ref_count_ deleter find the SlotImpl by address, the container // is defined as a map of SlotImpl address to the unique_ptr. - absl::flat_hash_map> deferred_deletes_; + absl::flat_hash_map deferred_deletes_; std::vector slots_; // A list of index of freed slots. @@ -104,5 +107,7 @@ class InstanceImpl : Logger::Loggable, public NonCopyable, pub friend class ThreadLocalInstanceImplTest; }; +using InstanceImplPtr = std::unique_ptr; + } // namespace ThreadLocal } // namespace Envoy diff --git a/source/exe/main_common.h b/source/exe/main_common.h index e548efc5c4913..91ea197def3c2 100644 --- a/source/exe/main_common.h +++ b/source/exe/main_common.h @@ -78,9 +78,9 @@ class MainCommonBase { Stats::SymbolTablePtr symbol_table_; Stats::AllocatorImpl stats_allocator_; - std::unique_ptr tls_; + ThreadLocal::InstanceImplPtr tls_; std::unique_ptr restarter_; - std::unique_ptr stats_store_; + Stats::ThreadLocalStoreImplPtr stats_store_; std::unique_ptr logging_context_; std::unique_ptr init_manager_{std::make_unique("Server")}; std::unique_ptr server_; diff --git a/source/extensions/filters/common/lua/lua.h b/source/extensions/filters/common/lua/lua.h index 7071b375303fa..726b6c149e166 100644 --- a/source/extensions/filters/common/lua/lua.h +++ b/source/extensions/filters/common/lua/lua.h @@ -420,6 +420,8 @@ class ThreadLocalState : Logger::Loggable { uint64_t current_global_slot_{}; }; +using ThreadLocalStatePtr = std::unique_ptr; + /** * An exception specific to Lua errors. */ diff --git a/test/common/stats/stat_test_utility.cc b/test/common/stats/stat_test_utility.cc index 55670f7942d54..7cdbc08ab4dc5 100644 --- a/test/common/stats/stat_test_utility.cc +++ b/test/common/stats/stat_test_utility.cc @@ -203,9 +203,12 @@ Histogram& TestStore::histogramFromStatNameWithTags(const StatName& stat_name, } template -static absl::optional> +using StatTypeOptConstRef = absl::optional>; + +template +static StatTypeOptConstRef findByString(const std::string& name, const absl::flat_hash_map& map) { - absl::optional> ret; + StatTypeOptConstRef ret; auto iter = map.find(name); if (iter != map.end()) { ret = *iter->second; diff --git a/test/common/stats/thread_local_store_speed_test.cc b/test/common/stats/thread_local_store_speed_test.cc index e2e86e0fb6035..6e2c62ace9ef2 100644 --- a/test/common/stats/thread_local_store_speed_test.cc +++ b/test/common/stats/thread_local_store_speed_test.cc @@ -68,7 +68,7 @@ class ThreadLocalStorePerf { Event::SimulatedTimeSystem time_system_; Stats::AllocatorImpl heap_alloc_; Event::DispatcherPtr dispatcher_; - std::unique_ptr tls_; + ThreadLocal::InstanceImplPtr tls_; Stats::ThreadLocalStoreImpl store_; Api::ApiPtr api_; envoy::config::metrics::v3::StatsConfig stats_config_; diff --git a/test/common/stats/thread_local_store_test.cc b/test/common/stats/thread_local_store_test.cc index 3aa877ca4121e..a7906ff2d7eed 100644 --- a/test/common/stats/thread_local_store_test.cc +++ b/test/common/stats/thread_local_store_test.cc @@ -58,7 +58,7 @@ class StatsThreadLocalStoreTest : public testing::Test { NiceMock tls_; AllocatorImpl alloc_; MockSink sink_; - std::unique_ptr store_; + ThreadLocalStoreImplPtr store_; }; class HistogramWrapper { @@ -176,7 +176,7 @@ class HistogramTest : public testing::Test { NiceMock tls_; AllocatorImpl alloc_; MockSink sink_; - std::unique_ptr store_; + ThreadLocalStoreImplPtr store_; InSequence s; std::vector h1_cumulative_values_, h2_cumulative_values_, h1_interval_values_, h2_interval_values_; @@ -587,7 +587,7 @@ class ThreadLocalStoreNoMocksTestBase : public testing::Test { SymbolTablePtr symbol_table_; AllocatorImpl alloc_; - std::unique_ptr store_; + ThreadLocalStoreImplPtr store_; StatNamePool pool_; }; @@ -1080,7 +1080,7 @@ class StatsThreadLocalStoreTestNoFixture : public testing::Test { MockSink sink_; SymbolTablePtr symbol_table_; std::unique_ptr alloc_; - std::unique_ptr store_; + ThreadLocalStoreImplPtr store_; NiceMock main_thread_dispatcher_; NiceMock tls_; TestUtil::SymbolTableCreatorTestPeer symbol_table_creator_test_peer_; @@ -1514,7 +1514,7 @@ class ClusterShutdownCleanupStarvationTest : public ThreadLocalStoreNoMocksTestB Event::DispatcherPtr main_dispatcher_; std::vector thread_dispatchers_; Thread::ThreadFactory& thread_factory_; - std::unique_ptr tls_; + ThreadLocal::InstanceImplPtr tls_; Thread::ThreadPtr main_thread_; std::vector threads_; StatNamePool pool_; diff --git a/test/extensions/filters/common/lua/lua_test.cc b/test/extensions/filters/common/lua/lua_test.cc index 5f4462e7d3c4f..27c9e35f8cfe6 100644 --- a/test/extensions/filters/common/lua/lua_test.cc +++ b/test/extensions/filters/common/lua/lua_test.cc @@ -48,7 +48,7 @@ class LuaTest : public testing::Test { } NiceMock tls_; - std::unique_ptr state_; + ThreadLocalStatePtr state_; std::function yield_callback_; ReadyWatcher on_yield_; }; diff --git a/test/extensions/filters/common/lua/lua_wrappers.h b/test/extensions/filters/common/lua/lua_wrappers.h index 4791f9e5109a9..4b2e7f1f8b0a7 100644 --- a/test/extensions/filters/common/lua/lua_wrappers.h +++ b/test/extensions/filters/common/lua/lua_wrappers.h @@ -41,7 +41,7 @@ template class LuaWrappersTestBase : public testing::Test { MOCK_METHOD(void, testPrint, (const std::string&)); NiceMock tls_; - std::unique_ptr state_; + ThreadLocalStatePtr state_; std::function yield_callback_; CoroutinePtr coroutine_; }; diff --git a/test/server/admin/stats_handler_test.cc b/test/server/admin/stats_handler_test.cc index ce80844b635e8..623438013b97e 100644 --- a/test/server/admin/stats_handler_test.cc +++ b/test/server/admin/stats_handler_test.cc @@ -39,7 +39,7 @@ class AdminStatsTest : public testing::TestWithParam tls_; Stats::AllocatorImpl alloc_; Stats::MockSink sink_; - std::unique_ptr store_; + Stats::ThreadLocalStoreImplPtr store_; }; INSTANTIATE_TEST_SUITE_P(IpVersions, AdminStatsTest, diff --git a/test/server/server_test.cc b/test/server/server_test.cc index 6a9522e80bf59..b849689ef63fc 100644 --- a/test/server/server_test.cc +++ b/test/server/server_test.cc @@ -258,7 +258,7 @@ class ServerInstanceImplTestBase { testing::NiceMock options_; DefaultListenerHooks hooks_; testing::NiceMock restart_; - std::unique_ptr thread_local_; + ThreadLocal::InstanceImplPtr thread_local_; Stats::TestIsolatedStoreImpl stats_store_; Thread::MutexBasicLockable fakelock_; TestComponentFactory component_factory_;