From: Razvan Becheriu Date: Wed, 11 Sep 2019 15:15:47 +0000 (+0300) Subject: [#886, !508] added thread resource mgr X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f65d1a473e3a07a90e7bf6ddb96816177f185cd2;p=thirdparty%2Fkea.git [#886, !508] added thread resource mgr --- diff --git a/src/lib/dhcpsrv/Makefile.am b/src/lib/dhcpsrv/Makefile.am index d0076959dd..1974c9a9b2 100644 --- a/src/lib/dhcpsrv/Makefile.am +++ b/src/lib/dhcpsrv/Makefile.am @@ -118,6 +118,7 @@ libkea_dhcpsrv_la_SOURCES += lease_mgr_factory.cc lease_mgr_factory.h libkea_dhcpsrv_la_SOURCES += memfile_lease_mgr.cc memfile_lease_mgr.h libkea_dhcpsrv_la_SOURCES += memfile_lease_storage.h libkea_dhcpsrv_la_SOURCES += multi_threading_utils.h multi_threading_utils.cc +libkea_dhcpsrv_la_SOURCES += thread_resource_mgr.h if HAVE_MYSQL libkea_dhcpsrv_la_SOURCES += mysql_lease_mgr.cc mysql_lease_mgr.h diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index d9cac94776..8684f6379e 100644 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -124,6 +124,7 @@ libdhcpsrv_unittests_SOURCES += shared_networks_list_parser_unittest.cc libdhcpsrv_unittests_SOURCES += srv_config_unittest.cc libdhcpsrv_unittests_SOURCES += subnet_unittest.cc libdhcpsrv_unittests_SOURCES += test_get_callout_handle.cc test_get_callout_handle.h +libdhcpsrv_unittests_SOURCES += thread_resource_mgr_unittest.cc libdhcpsrv_unittests_SOURCES += triplet_unittest.cc libdhcpsrv_unittests_SOURCES += test_utils.cc test_utils.h libdhcpsrv_unittests_SOURCES += timer_mgr_unittest.cc diff --git a/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc b/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc new file mode 100644 index 0000000000..5561567a3a --- /dev/null +++ b/src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc @@ -0,0 +1,80 @@ +// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include + +#include + +#include + +using namespace isc::dhcp; +using namespace std; + +namespace { + +/// @brief Test Fixture for testing isc::dhcp::ThreadResourceMgr +class ThreadResourceMgrTest : public ::testing::Test { +}; + +template +class Resource { +public: + Resource() { + lock_guard lk(mutex_); + Resource::count_++; + Resource::created_count_++; + } + + virtual ~Resource() { + lock_guard lk(mutex_); + Resource::count_--; + Resource::destroyed_count_++; + } + + static uint32_t count() { + lock_guard lk(mutex_); + return Resource::count_; + } + + static uint32_t createdCount() { + lock_guard lk(mutex_); + return Resource::created_count_; + } + + static uint32_t destroyedCount() { + lock_guard lk(mutex_); + return Resource::destroyed_count_; + } + + static void reset() { + lock_guard lk(mutex_); + Resource::count_ = 0; + Resource::created_count_ = 0; + Resource::destroyed_count_ = 0; + } +private: + /// @brief total number of instances at any given time + static uint32_t count_; + + /// @brief total number of instances ever created + static uint32_t created_count_; + + /// @brief total number of instances ever destroyed + static uint32_t destroyed_count_; + + /// @brief mutex used to keep the internal state consistent + static std::mutex mutex_; +}; + +// This test verifies that each thread can access it's own allocated resource +TEST(ThreadResourceMgrTest, testThreadResources) { + ThreadResourceMgr integers; + ThreadResourceMgr bools; +} + +} // namespace diff --git a/src/lib/dhcpsrv/thread_resource_mgr.h b/src/lib/dhcpsrv/thread_resource_mgr.h new file mode 100644 index 0000000000..50e7f62899 --- /dev/null +++ b/src/lib/dhcpsrv/thread_resource_mgr.h @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef THREAD_RESOURCE_MGR_H +#define THREAD_RESOURCE_MGR_H + +#include +#include +#include +#include + +namespace isc { +namespace dhcp { + +template +class ThreadResourceMgr { + typedef boost::shared_ptr ResourcePtr; +public: + /// @brief function to retrieve the specific resource of calling thread + /// This function returns the resource of the calling thread from the map + /// container or, in case it is not found, it creates a resource and adds it + /// to the map container + /// + /// @return the specific resource of the calling thread + ResourcePtr resource() { + std::lock_guard lock(&mutex_); + auto id = std::this_thread::get_id(); + if (map_.find(id) != map_.end()) { + return map_[id]; + } + ResourcePtr result(std::make_shared()); + map_[id] = result; + return result; + } +private: + /// @brief mutex used to keep the internal state consistent + std::mutex mutex_; + + /// @brief map container which holds the resources for each thread + std::unordered_map map_; +}; + +} // namespace dhcp +} // namespace isc + +#endif // THREAD_RESOURCE_MGR_H