]> 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>
Wed, 6 Nov 2019 17:32:52 +0000 (19:32 +0200)
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

index 86bc606cf217f589540e89e9bd221ddf11967847..c93a91743fb1feca78a5ca69c906cb90f7cb0473 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 += thread_pool_unittest.cc
 libdhcpsrv_unittests_SOURCES += test_utils.cc test_utils.h
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
index 52685d5010277376a459a3903f2443131baa85ee..405e92f6114b7af41d0c502fdb5a2b8fa4caa7a4 100644 (file)
@@ -17,20 +17,29 @@ namespace dhcp {
 
 template <typename Resource>
 class ThreadResourceMgr {
-    typedef boost::shared_ptr<Resource> ResourcePtr;
+    typedef std::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(new Resource());
+        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_;
 };