diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/condvar_ut.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/condvar_ut.cpp')
-rw-r--r-- | util/system/condvar_ut.cpp | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/util/system/condvar_ut.cpp b/util/system/condvar_ut.cpp index 5130a18d32..bfc226c39f 100644 --- a/util/system/condvar_ut.cpp +++ b/util/system/condvar_ut.cpp @@ -1,17 +1,17 @@ -#include "mutex.h" -#include "guard.h" -#include "condvar.h" - +#include "mutex.h" +#include "guard.h" +#include "condvar.h" + #include <library/cpp/testing/unittest/registar.h> #include <util/system/atomic.h> #include <util/system/atomic_ops.h> #include <util/thread/pool.h> -class TCondVarTest: public TTestBase { +class TCondVarTest: public TTestBase { UNIT_TEST_SUITE(TCondVarTest); - UNIT_TEST(TestBasics) - UNIT_TEST(TestSyncronize) + UNIT_TEST(TestBasics) + UNIT_TEST(TestSyncronize) UNIT_TEST_SUITE_END(); struct TSharedData { @@ -23,7 +23,7 @@ class TCondVarTest: public TTestBase { , failed(false) { } - + TMutex mutex; TCondVar condVar1; TCondVar condVar2; @@ -38,47 +38,47 @@ class TCondVarTest: public TTestBase { bool failed; }; - class TThreadTask: public IObjectInQueue { - public: + class TThreadTask: public IObjectInQueue { + public: using PFunc = void (TThreadTask::*)(void); - TThreadTask(PFunc func, size_t id, size_t totalIds, TSharedData& data) - : Func_(func) - , Id_(id) - , TotalIds_(totalIds) - , Data_(data) - { - } + TThreadTask(PFunc func, size_t id, size_t totalIds, TSharedData& data) + : Func_(func) + , Id_(id) + , TotalIds_(totalIds) + , Data_(data) + { + } void Process(void*) override { - THolder<TThreadTask> This(this); - - (this->*Func_)(); - } - -#define FAIL_ASSERT(cond) \ - if (!(cond)) { \ - Data_.failed = true; \ + THolder<TThreadTask> This(this); + + (this->*Func_)(); + } + +#define FAIL_ASSERT(cond) \ + if (!(cond)) { \ + Data_.failed = true; \ } - void RunBasics() { + void RunBasics() { Y_ASSERT(TotalIds_ == 3); - if (Id_ < 2) { - TGuard<TMutex> guard(Data_.mutex); + if (Id_ < 2) { + TGuard<TMutex> guard(Data_.mutex); while (!AtomicGet(Data_.stopWaiting)) { bool res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } else { - usleep(100000); + FAIL_ASSERT(res == true); + } + } else { + usleep(100000); AtomicSet(Data_.stopWaiting, true); - - TGuard<TMutex> guard(Data_.mutex); - Data_.condVar1.Signal(); - Data_.condVar1.Signal(); - } - } - + + TGuard<TMutex> guard(Data_.mutex); + Data_.condVar1.Signal(); + Data_.condVar1.Signal(); + } + } + void RunBasicsWithPredicate() { Y_ASSERT(TotalIds_ == 3); @@ -98,14 +98,14 @@ class TCondVarTest: public TTestBase { } } - void RunSyncronize() { - for (size_t i = 0; i < 10; ++i) { - TGuard<TMutex> guard(Data_.mutex); + void RunSyncronize() { + for (size_t i = 0; i < 10; ++i) { + TGuard<TMutex> guard(Data_.mutex); AtomicIncrement(Data_.in); if (AtomicGet(Data_.in) == TotalIds_) { AtomicSet(Data_.out, 0); - Data_.condVar1.BroadCast(); - } else { + Data_.condVar1.BroadCast(); + } else { AtomicIncrement(Data_.waited); while (AtomicGet(Data_.in) < TotalIds_) { bool res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1)); @@ -116,14 +116,14 @@ class TCondVarTest: public TTestBase { AtomicIncrement(Data_.out); if (AtomicGet(Data_.out) == TotalIds_) { AtomicSet(Data_.in, 0); - Data_.condVar2.BroadCast(); - } else { + Data_.condVar2.BroadCast(); + } else { while (AtomicGet(Data_.out) < TotalIds_) { bool res = Data_.condVar2.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); + FAIL_ASSERT(res == true); } } - } + } FAIL_ASSERT(AtomicGet(Data_.waited) == (TotalIds_ - 1) * 10); } @@ -155,45 +155,45 @@ class TCondVarTest: public TTestBase { } } - FAIL_ASSERT(Data_.waited == (TotalIds_ - 1) * 10); - } + FAIL_ASSERT(Data_.waited == (TotalIds_ - 1) * 10); + } #undef FAIL_ASSERT - private: - PFunc Func_; - size_t Id_; + private: + PFunc Func_; + size_t Id_; TAtomicBase TotalIds_; - TSharedData& Data_; + TSharedData& Data_; }; -private: -#define RUN_CYCLE(what, count) \ - Q_.Start(count); \ - for (size_t i = 0; i < count; ++i) { \ - UNIT_ASSERT(Q_.Add(new TThreadTask(&TThreadTask::what, i, count, Data_))); \ - } \ - Q_.Stop(); \ - bool b = Data_.failed; \ - Data_.failed = false; \ - UNIT_ASSERT(!b); - - inline void TestBasics() { - RUN_CYCLE(RunBasics, 3); - } +private: +#define RUN_CYCLE(what, count) \ + Q_.Start(count); \ + for (size_t i = 0; i < count; ++i) { \ + UNIT_ASSERT(Q_.Add(new TThreadTask(&TThreadTask::what, i, count, Data_))); \ + } \ + Q_.Stop(); \ + bool b = Data_.failed; \ + Data_.failed = false; \ + UNIT_ASSERT(!b); + + inline void TestBasics() { + RUN_CYCLE(RunBasics, 3); + } inline void TestBasicsWithPredicate() { RUN_CYCLE(RunBasicsWithPredicate, 3); } - inline void TestSyncronize() { - RUN_CYCLE(RunSyncronize, 6); - } + inline void TestSyncronize() { + RUN_CYCLE(RunSyncronize, 6); + } inline void TestSyncronizeWithPredicate() { RUN_CYCLE(RunSyncronizeWithPredicate, 6); } -#undef RUN_CYCLE - TSharedData Data_; +#undef RUN_CYCLE + TSharedData Data_; TThreadPool Q_; }; |