From: Razvan Becheriu Date: Wed, 6 Nov 2019 17:20:51 +0000 (+0200) Subject: fixed rebase X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1fefcaf0bbff05f5e9494b010297e2c1925a371b;p=thirdparty%2Fkea.git fixed rebase --- diff --git a/src/lib/util/tests/thread_resource_unittest.cc b/src/lib/util/tests/thread_resource_unittest.cc index 9ce1c122a5..4977ced90e 100644 --- a/src/lib/util/tests/thread_resource_unittest.cc +++ b/src/lib/util/tests/thread_resource_unittest.cc @@ -37,8 +37,7 @@ public: Resource::count_++; // increase the total number of instances ever created Resource::created_count_++; - // check that this instance in new and should not be found in the - // verification set + // check that this instance is not found in the verification set EXPECT_TRUE(Resource::set_.find(&data_) == Resource::set_.end()); // add this instance to the verification set Resource::set_.emplace(&data_); @@ -107,7 +106,7 @@ private: /// @brief mutex used to keep the internal state consistent static std::mutex mutex_; - /// @brief set to fold + /// @brief set to hold the distinct identification data of each instance static std::set set_; }; @@ -133,14 +132,29 @@ public: ~ThreadResourceTest() { } + /// @brief flag which indicates if main thread should wait for the test + /// thread to start + /// + /// @return the wait flag + bool waitThread() { + return wait_thread_; + } + /// @brief flag which indicates if working thread should wait for main /// thread signal /// /// @return the wait flag - bool wait() { + bool waitMain() { return wait_; } + /// @brief block main thread until testing thread has processed the task + void wait() { + unique_lock lck(mutex_); + // wait for the testing thread to process + cv_.wait(lck, [&]{ return (waitThread() == false); }); + } + /// @brief function used by main thread to unblock processing threads void signalThreads() { lock_guard lk(wait_mutex_); @@ -160,9 +174,15 @@ public: get() = make_shared>>(); // perform sanity checks sanityCheck(); + // reset the wait flag wait_ = true; } + /// @brief reset wait thread flag + void resetWaitThread() { + wait_thread_ = true; + } + /// @brief check statistics /// /// @param expected_count check equality of this value with the number of @@ -217,14 +237,39 @@ public: // sequential requests for the same resource checkInstances(expected_count, expected_created, expected_destroyed); + { + // make sure this thread has started + lock_guard lk(mutex_); + // reset wait thread flag + wait_thread_ = false; + // wake main thread if it is waiting for this thread to process + cv_.notify_all(); + } + if (signal) { unique_lock lk(wait_mutex_); // if specified, wait for signal from main thread - wait_cv_.wait(lk, [&]{ return (wait() == false); }); + wait_cv_.wait(lk, [&]{ return (waitMain() == false); }); } } private: + /// @brief sanity check that the number of created instances is equal to the + /// number of destroyed instances + template + void sanityCheck() { + // the number of created instances should match the number of destroyed + // instances + ASSERT_EQ(Resource::createdCount(), Resource::destroyedCount()); + } + + /// @brief mutex used to keep the internal state consistent + std::mutex mutex_; + + /// @brief condition variable used to signal main thread that test thread + /// has started processing + condition_variable cv_; + /// @brief mutex used to keep the internal state consistent /// related to the control of the main thread over the working threads exit std::mutex wait_mutex_; @@ -232,6 +277,10 @@ private: /// @brief condition variable used to signal working threads to exit condition_variable wait_cv_; + /// @brief flag which indicates if main thread should wait for test thread + /// to start + bool wait_thread_; + /// @brief flag which indicates if working thread should wait for main /// thread signal bool wait_; @@ -251,6 +300,8 @@ TEST_F(ThreadResourceTest, testThreadResources) { reset(); // call run function on main thread and verify statistics run(1, 1, 0); + // configure wait for test thread + resetWaitThread(); // call run on a different thread and verify statistics threads.push_back(std::make_shared(std::bind( &ThreadResourceTest::run, this, 2, 2, 0, true))); @@ -280,6 +331,8 @@ TEST_F(ThreadResourceTest, testThreadResources) { reset(); // call run function on main thread and verify statistics run(1, 1, 0); + // configure wait for test thread + resetWaitThread(); // call run on a different thread and verify statistics threads.push_back(std::make_shared(std::bind( &ThreadResourceTest::run, this, 2, 2, 0, true))); diff --git a/src/lib/util/thread_resource.h b/src/lib/util/thread_resource.h index bd360cbc7c..9a8e44a604 100644 --- a/src/lib/util/thread_resource.h +++ b/src/lib/util/thread_resource.h @@ -4,8 +4,8 @@ // 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 +#ifndef THREAD_RESOURCE_H +#define THREAD_RESOURCE_H #include #include @@ -46,4 +46,4 @@ private: } // namespace dhcp } // namespace isc -#endif // THREAD_RESOURCE_MGR_H +#endif // THREAD_RESOURCE_H