From 8d267b9a2838d0d08434eb20ae14c83270cc9515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mirza=20Halil=C4=8Devi=C4=87?= <109971222+mirza-halilcevic@users.noreply.github.com> Date: Mon, 19 Sep 2022 17:24:45 +0200 Subject: [PATCH] EXSWHTEC-5, EXSWHTEC-7 - Implement abstraction for multithreaded tests (#2887) * Implement abstraction for multithreaded tests. Signed-off-by: Dino Music Co-authored-by: Mirza Halilcevic --- tests/catch/include/threaded_zig_zag_test.hh | 110 +++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 tests/catch/include/threaded_zig_zag_test.hh diff --git a/tests/catch/include/threaded_zig_zag_test.hh b/tests/catch/include/threaded_zig_zag_test.hh new file mode 100644 index 0000000000..96fe250869 --- /dev/null +++ b/tests/catch/include/threaded_zig_zag_test.hh @@ -0,0 +1,110 @@ +/* +Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#pragma once + +#include +#include +#include + +/* +Guarantees total ordering between parent and child thread +PARENT CHILD +THREAD THREAD +TestPart1 + \ + \ + \ + TestPart2 + / + / + / +TestPart3 + \ + \ + \ + TestPart4 +Usage: +Define a derived class which inherits from ThreadedZigZagTest instantiated with that selfsame class, +which implements the appropriate test methods +class DerivedTestClass : public ThreadedZigZagTest { + void TestPart1() {...} + void TestPart2() {...} + void TestPart3() {...} + void TestPart4() {...} +}; +The derived class can contain state that the test requires. +*/ + +template class ThreadedZigZagTest { + public: + void run() { + // 1. + static_cast(this)->TestPart1(); + + auto t = std::thread([this] { + // 2. + static_cast(this)->TestPart2(); + + { + std::lock_guard lock(mtx_); + ready_ = true; + } + cv_.notify_one(); + + { + std::unique_lock lock(mtx_); + cv_.wait(lock, [this] { return !ready_; }); + } + + // 4. + static_cast(this)->TestPart4(); + }); + + { + std::unique_lock lock(mtx_); + cv_.wait(lock, [this] { return ready_; }); + } + + // 3. + static_cast(this)->TestPart3(); + + { + std::lock_guard lock(mtx_); + ready_ = false; + } + cv_.notify_one(); + + // Finalize + t.join(); + HIP_CHECK_THREAD_FINALIZE(); + } + + void TestPart1() const {} + void TestPart2() const {} + void TestPart3() const {} + void TestPart4() const {} + + private: + std::mutex mtx_; + std::condition_variable cv_; + bool ready_ = false; +}; \ No newline at end of file