]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#886, !508] added thread resource mgr
authorRazvan Becheriu <razvan@isc.org>
Wed, 11 Sep 2019 15:15:47 +0000 (18:15 +0300)
committerRazvan Becheriu <razvan@isc.org>
Mon, 4 Nov 2019 17:53:25 +0000 (19:53 +0200)
src/lib/dhcpsrv/Makefile.am
src/lib/dhcpsrv/tests/Makefile.am
src/lib/dhcpsrv/tests/thread_resource_mgr_unittest.cc [new file with mode: 0644]
src/lib/dhcpsrv/thread_resource_mgr.h [new file with mode: 0644]

index 948f01566aa3b0cf93cb3c28fc23fc29abd12e50..684e4655b37dca8e21ab82b0355b47e7812c2de3 100644 (file)
@@ -117,6 +117,7 @@ libkea_dhcpsrv_la_SOURCES += lease_mgr.cc lease_mgr.h
 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 += thread_resource_mgr.h
 
 if HAVE_MYSQL
 libkea_dhcpsrv_la_SOURCES += mysql_lease_mgr.cc mysql_lease_mgr.h
index d9cac94776fa096b9da89fee85c20e181f6ae346..8684f6379eaa0ee1743d69b0d7e72c8a5d385f44 100644 (file)
@@ -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 (file)
index 0000000..5561567
--- /dev/null
@@ -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 <config.h>
+
+#include <gtest/gtest.h>
+
+#include <dhcpsrv/thread_resource_mgr.h>
+
+#include <mutex>
+
+using namespace isc::dhcp;
+using namespace std;
+
+namespace {
+
+/// @brief Test Fixture for testing isc::dhcp::ThreadResourceMgr
+class ThreadResourceMgrTest : public ::testing::Test {
+};
+
+template <typename T>
+class Resource {
+public:
+    Resource() {
+        lock_guard<std::mutex> lk(mutex_);
+        Resource::count_++;
+        Resource::created_count_++;
+    }
+
+    virtual ~Resource() {
+        lock_guard<std::mutex> lk(mutex_);
+        Resource::count_--;
+        Resource::destroyed_count_++;
+    }
+
+    static uint32_t count() {
+        lock_guard<std::mutex> lk(mutex_);
+        return Resource::count_;
+    }
+
+    static uint32_t createdCount() {
+        lock_guard<std::mutex> lk(mutex_);
+        return Resource::created_count_;
+    }
+
+    static uint32_t destroyedCount() {
+        lock_guard<std::mutex> lk(mutex_);
+        return Resource::destroyed_count_;
+    }
+
+    static void reset() {
+        lock_guard<std::mutex> 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<uint32_t> integers;
+    ThreadResourceMgr<bool> bools;
+}
+
+}  // namespace
diff --git a/src/lib/dhcpsrv/thread_resource_mgr.h b/src/lib/dhcpsrv/thread_resource_mgr.h
new file mode 100644 (file)
index 0000000..50e7f62
--- /dev/null
@@ -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 <boost/shared_ptr.hpp>
+#include <mutex>
+#include <thread>
+#include <unordered_map>
+
+namespace isc {
+namespace dhcp {
+
+template <typename Resource>
+class ThreadResourceMgr {
+    typedef boost::shared_ptr<Resource> 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<std::mutex> lock(&mutex_);
+        auto id = std::this_thread::get_id();
+        if (map_.find(id) != map_.end()) {
+            return map_[id];
+        }
+        ResourcePtr result(std::make_shared<Resource>());
+        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<std::thread::id, ResourcePtr> map_;
+};
+
+}  // namespace dhcp
+}  // namespace isc
+
+#endif  // THREAD_RESOURCE_MGR_H