diff options
author | kimkim <kimkim@yandex-team.ru> | 2022-02-10 16:49:28 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:28 +0300 |
commit | 10807864acf73d00f425a23b442aac2cf34403a8 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /util/system/mutex_ut.cpp | |
parent | 13f84424ed9975f6827d9786087c6fe6ea265cda (diff) | |
download | ydb-10807864acf73d00f425a23b442aac2cf34403a8.tar.gz |
Restoring authorship annotation for <kimkim@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/system/mutex_ut.cpp')
-rw-r--r-- | util/system/mutex_ut.cpp | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/util/system/mutex_ut.cpp b/util/system/mutex_ut.cpp index 0fc27c191d..c8d7caafa1 100644 --- a/util/system/mutex_ut.cpp +++ b/util/system/mutex_ut.cpp @@ -2,52 +2,52 @@ #include "atomic.h" #include <library/cpp/testing/unittest/registar.h> - + #include <util/thread/pool.h> -#include <util/random/random.h> - +#include <util/random/random.h> + class TMutexTest: public TTestBase { - UNIT_TEST_SUITE(TMutexTest); + UNIT_TEST_SUITE(TMutexTest); UNIT_TEST(TestBasics) UNIT_TEST(TestFake) UNIT_TEST(TestRecursive) - UNIT_TEST_SUITE_END(); - - struct TSharedData { - TSharedData() - : sharedCounter(0) - , failed(false) - { - } - - volatile ui32 sharedCounter; - TMutex mutex; - TFakeMutex fakeMutex; - - bool failed; - }; - + UNIT_TEST_SUITE_END(); + + struct TSharedData { + TSharedData() + : sharedCounter(0) + , failed(false) + { + } + + volatile ui32 sharedCounter; + TMutex mutex; + TFakeMutex fakeMutex; + + bool failed; + }; + class TThreadTask: public IObjectInQueue { public: using PFunc = void (TThreadTask::*)(void); - + TThreadTask(PFunc func, TSharedData& data, size_t id) : Func_(func) , Data_(data) , Id_(id) { } - + void Process(void*) override { THolder<TThreadTask> This(this); (this->*Func_)(); } - + #define FAIL_ASSERT(cond) \ if (!(cond)) { \ Data_.failed = true; \ - } + } void RunBasics() { Data_.mutex.Acquire(); @@ -55,10 +55,10 @@ class TMutexTest: public TTestBase { Data_.sharedCounter = oldCounter; usleep(10 + RandomNumber<ui32>() % 10); FAIL_ASSERT(Data_.sharedCounter == oldCounter); - + Data_.mutex.Release(); } - + void RunFakeMutex() { bool res = Data_.fakeMutex.TryAcquire(); FAIL_ASSERT(res); @@ -69,9 +69,9 @@ class TMutexTest: public TTestBase { Data_.mutex.Acquire(); ++Data_.sharedCounter; usleep(1); - } + } FAIL_ASSERT(Data_.sharedCounter == Id_ + 1); - + bool res = Data_.mutex.TryAcquire(); FAIL_ASSERT(res); Data_.mutex.Release(); @@ -79,20 +79,20 @@ class TMutexTest: public TTestBase { for (size_t i = 0; i < Id_; ++i) { --Data_.sharedCounter; Data_.mutex.Release(); - } + } FAIL_ASSERT(Data_.sharedCounter == 1); --Data_.sharedCounter; Data_.mutex.Release(); } - -#undef FAIL_ASSERT - + +#undef FAIL_ASSERT + private: PFunc Func_; TSharedData& Data_; size_t Id_; - }; - + }; + private: #define RUN_CYCLE(what, count) \ Q_.Start(count); \ @@ -103,27 +103,27 @@ private: bool b = Data_.failed; \ Data_.failed = false; \ UNIT_ASSERT(!b); - + void TestBasics() { RUN_CYCLE(RunBasics, 5); - + UNIT_ASSERT(Data_.sharedCounter == 10); Data_.sharedCounter = 0; } - + void TestFake() { RUN_CYCLE(RunFakeMutex, 3); } - + void TestRecursive() { RUN_CYCLE(RunRecursiveMutex, 4); } - -#undef RUN_CYCLE - + +#undef RUN_CYCLE + private: TSharedData Data_; TThreadPool Q_; -}; - -UNIT_TEST_SUITE_REGISTRATION(TMutexTest) +}; + +UNIT_TEST_SUITE_REGISTRATION(TMutexTest) |