From 2c5041237b61ee806d7cb36a4e1742bd7beeb0af Mon Sep 17 00:00:00 2001 From: Bulat Gayazov Date: Thu, 7 Mar 2024 09:20:37 +0000 Subject: [PATCH] Removed TCondVar and TMutex --- library/cpp/coroutine/engine/sockpool.h | 1 - library/cpp/http/fetch/sockhandler.h | 1 - util/CMakeLists.darwin-arm64.txt | 10 +- util/CMakeLists.darwin-x86_64.txt | 10 +- util/CMakeLists.linux-aarch64.txt | 10 +- util/CMakeLists.linux-x86_64.txt | 3 +- util/CMakeLists.windows-x86_64.txt | 10 +- util/network/pollerimpl.h | 2 +- util/system/condvar.cpp | 145 ------------------ util/system/condvar.h | 71 --------- util/system/condvar_ut.cpp | 191 ------------------------ util/system/fake_mutex.cpp | 1 + util/system/fake_mutex.h | 28 ++++ util/system/mutex.cpp | 146 ------------------ util/system/mutex.h | 64 -------- util/system/mutex_ut.cpp | 128 ---------------- util/system/shellcommand.h | 1 - 17 files changed, 35 insertions(+), 787 deletions(-) delete mode 100644 util/system/condvar.cpp delete mode 100644 util/system/condvar.h delete mode 100644 util/system/condvar_ut.cpp create mode 100644 util/system/fake_mutex.cpp create mode 100644 util/system/fake_mutex.h delete mode 100644 util/system/mutex.cpp delete mode 100644 util/system/mutex.h delete mode 100644 util/system/mutex_ut.cpp diff --git a/library/cpp/coroutine/engine/sockpool.h b/library/cpp/coroutine/engine/sockpool.h index 7bd4c9a330f..67c588f25b3 100644 --- a/library/cpp/coroutine/engine/sockpool.h +++ b/library/cpp/coroutine/engine/sockpool.h @@ -5,7 +5,6 @@ #include #include -#include #include diff --git a/library/cpp/http/fetch/sockhandler.h b/library/cpp/http/fetch/sockhandler.h index 68a73f6d476..94f97ff6d0e 100644 --- a/library/cpp/http/fetch/sockhandler.h +++ b/library/cpp/http/fetch/sockhandler.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #include diff --git a/util/CMakeLists.darwin-arm64.txt b/util/CMakeLists.darwin-arm64.txt index 23ae0ba543b..964f1f19156 100644 --- a/util/CMakeLists.darwin-arm64.txt +++ b/util/CMakeLists.darwin-arm64.txt @@ -91,8 +91,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp - ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/maybe.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp ${CMAKE_SOURCE_DIR}/util/generic/noncopyable.cpp @@ -107,15 +105,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -217,7 +212,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -255,7 +249,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -288,8 +282,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.darwin-x86_64.txt b/util/CMakeLists.darwin-x86_64.txt index e159ab06c8b..8be835bbca7 100644 --- a/util/CMakeLists.darwin-x86_64.txt +++ b/util/CMakeLists.darwin-x86_64.txt @@ -89,8 +89,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/maybe.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp @@ -106,15 +104,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -216,7 +211,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -254,7 +248,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -287,8 +281,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.linux-aarch64.txt b/util/CMakeLists.linux-aarch64.txt index eee1114ed8d..b3788b4a5d7 100644 --- a/util/CMakeLists.linux-aarch64.txt +++ b/util/CMakeLists.linux-aarch64.txt @@ -93,8 +93,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/maybe.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp @@ -110,15 +108,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -220,7 +215,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -258,7 +252,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -291,8 +285,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/CMakeLists.linux-x86_64.txt b/util/CMakeLists.linux-x86_64.txt index 9569e53379f..37952dd29b8 100644 --- a/util/CMakeLists.linux-x86_64.txt +++ b/util/CMakeLists.linux-x86_64.txt @@ -215,7 +215,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -253,7 +252,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp diff --git a/util/CMakeLists.windows-x86_64.txt b/util/CMakeLists.windows-x86_64.txt index ff38abd2066..8f25c221575 100644 --- a/util/CMakeLists.windows-x86_64.txt +++ b/util/CMakeLists.windows-x86_64.txt @@ -96,8 +96,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/iterator.cpp ${CMAKE_SOURCE_DIR}/util/generic/iterator_range.cpp ${CMAKE_SOURCE_DIR}/util/generic/lazy_value.cpp - ${CMAKE_SOURCE_DIR}/util/generic/list.cpp - ${CMAKE_SOURCE_DIR}/util/generic/map.cpp ${CMAKE_SOURCE_DIR}/util/generic/mapfindptr.cpp ${CMAKE_SOURCE_DIR}/util/generic/maybe.cpp ${CMAKE_SOURCE_DIR}/util/generic/mem_copy.cpp @@ -113,15 +111,12 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/generic/size_literals.cpp ${CMAKE_SOURCE_DIR}/util/generic/stack.cpp ${CMAKE_SOURCE_DIR}/util/generic/store_policy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/strbuf.cpp ${CMAKE_SOURCE_DIR}/util/generic/strfcpy.cpp - ${CMAKE_SOURCE_DIR}/util/generic/string.cpp ${CMAKE_SOURCE_DIR}/util/generic/typelist.cpp ${CMAKE_SOURCE_DIR}/util/generic/typetraits.cpp ${CMAKE_SOURCE_DIR}/util/generic/utility.cpp ${CMAKE_SOURCE_DIR}/util/generic/va_args.cpp ${CMAKE_SOURCE_DIR}/util/generic/variant.cpp - ${CMAKE_SOURCE_DIR}/util/generic/vector.cpp ${CMAKE_SOURCE_DIR}/util/generic/xrange.cpp ${CMAKE_SOURCE_DIR}/util/generic/yexception.cpp ${CMAKE_SOURCE_DIR}/util/generic/ylimits.cpp @@ -223,7 +218,6 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/atexit.cpp ${CMAKE_SOURCE_DIR}/util/system/backtrace.cpp ${CMAKE_SOURCE_DIR}/util/system/compat.cpp - ${CMAKE_SOURCE_DIR}/util/system/condvar.cpp ${CMAKE_SOURCE_DIR}/util/system/context.cpp ${CMAKE_SOURCE_DIR}/util/system/daemon.cpp ${CMAKE_SOURCE_DIR}/util/system/datetime.cpp @@ -261,7 +255,7 @@ target_joined_source(yutil ${CMAKE_SOURCE_DIR}/util/system/mincore.cpp ${CMAKE_SOURCE_DIR}/util/system/mktemp.cpp ${CMAKE_SOURCE_DIR}/util/system/mlock.cpp - ${CMAKE_SOURCE_DIR}/util/system/mutex.cpp + ${CMAKE_SOURCE_DIR}/util/system/fake_mutex.cpp ${CMAKE_SOURCE_DIR}/util/system/nice.cpp ${CMAKE_SOURCE_DIR}/util/system/pipe.cpp ${CMAKE_SOURCE_DIR}/util/system/platform.cpp @@ -294,8 +288,6 @@ target_joined_source(yutil all_thread.cpp ${CMAKE_SOURCE_DIR}/util/thread/factory.cpp ${CMAKE_SOURCE_DIR}/util/thread/fwd.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfqueue.cpp - ${CMAKE_SOURCE_DIR}/util/thread/lfstack.cpp ${CMAKE_SOURCE_DIR}/util/thread/pool.cpp ${CMAKE_SOURCE_DIR}/util/thread/singleton.cpp ) diff --git a/util/network/pollerimpl.h b/util/network/pollerimpl.h index ed03e09c8dd..a4fff8a7fa5 100644 --- a/util/network/pollerimpl.h +++ b/util/network/pollerimpl.h @@ -3,7 +3,7 @@ #include "socket.h" #include -#include +#include #include #include #include diff --git a/util/system/condvar.cpp b/util/system/condvar.cpp deleted file mode 100644 index b4d748e4e96..00000000000 --- a/util/system/condvar.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include "event.h" -#include "mutex.h" -#include "yassert.h" -#include "condvar.h" -#include "datetime.h" -#include "spinlock.h" - -#include -#include -#include - -#if defined(_unix_) - #include - #include - #include -#endif - -namespace { - class TCondVarImpl { - using TLock = TAdaptiveLock; - - struct TWaitEvent: public TIntrusiveListItem, public TSystemEvent { - }; - - using TWaitEvents = TIntrusiveList; - - public: - inline ~TCondVarImpl() { - Y_ASSERT(Events_.Empty()); - } - - inline void Signal() noexcept { - with_lock (Lock_) { - if (!Events_.Empty()) { - Events_.PopFront()->Signal(); - } - } - } - - inline void BroadCast() noexcept { - with_lock (Lock_) { - //TODO - while (!Events_.Empty()) { - Events_.PopFront()->Signal(); - } - } - } - - inline bool WaitD(TMutex& m, TInstant deadLine) noexcept { - TWaitEvent event; - - with_lock (Lock_) { - Events_.PushBack(&event); - } - - m.Release(); - - const bool signalled = event.WaitD(deadLine); - - m.Acquire(); - - with_lock (Lock_) { - event.Unlink(); - } - - return signalled; - } - - private: - TWaitEvents Events_; - TLock Lock_; - }; -} - -#if defined(_win_) -class TCondVar::TImpl: public TCondVarImpl { -}; -#else -class TCondVar::TImpl { -public: - inline TImpl() { - if (pthread_cond_init(&Cond_, nullptr)) { - ythrow yexception() << "can not create condvar(" << LastSystemErrorText() << ")"; - } - } - - inline ~TImpl() { - int ret = pthread_cond_destroy(&Cond_); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_destroy failed: %s", LastSystemErrorText(ret)); - } - - inline void Signal() noexcept { - int ret = pthread_cond_signal(&Cond_); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_signal failed: %s", LastSystemErrorText(ret)); - } - - inline bool WaitD(TMutex& lock, TInstant deadLine) noexcept { - if (deadLine == TInstant::Max()) { - int ret = pthread_cond_wait(&Cond_, (pthread_mutex_t*)lock.Handle()); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_wait failed: %s", LastSystemErrorText(ret)); - return true; - } else { - struct timespec spec; - - Zero(spec); - - spec.tv_sec = deadLine.Seconds(); - spec.tv_nsec = deadLine.NanoSecondsOfSecond(); - - int ret = pthread_cond_timedwait(&Cond_, (pthread_mutex_t*)lock.Handle(), &spec); - - Y_ABORT_UNLESS(ret == 0 || ret == ETIMEDOUT, "pthread_cond_timedwait failed: %s", LastSystemErrorText(ret)); - - return ret == 0; - } - } - - inline void BroadCast() noexcept { - int ret = pthread_cond_broadcast(&Cond_); - Y_ABORT_UNLESS(ret == 0, "pthread_cond_broadcast failed: %s", LastSystemErrorText(ret)); - } - -private: - pthread_cond_t Cond_; -}; -#endif - -TCondVar::TCondVar() - : Impl_(new TImpl) -{ -} - -TCondVar::~TCondVar() = default; - -void TCondVar::BroadCast() noexcept { - Impl_->BroadCast(); -} - -void TCondVar::Signal() noexcept { - Impl_->Signal(); -} - -bool TCondVar::WaitD(TMutex& mutex, TInstant deadLine) noexcept { - return Impl_->WaitD(mutex, deadLine); -} diff --git a/util/system/condvar.h b/util/system/condvar.h deleted file mode 100644 index 569162717cb..00000000000 --- a/util/system/condvar.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include "mutex.h" - -#include -#include -#include - -#include - -class TCondVar { -public: - TCondVar(); - ~TCondVar(); - - void BroadCast() noexcept; - void Signal() noexcept; - - /* - * returns false if failed by timeout - */ - bool WaitD(TMutex& m, TInstant deadline) noexcept; - - template - inline bool WaitD(TMutex& m, TInstant deadline, P pred) noexcept { - while (!pred()) { - if (!WaitD(m, deadline)) { - return pred(); - } - } - return true; - } - - /* - * returns false if failed by timeout - */ - inline bool WaitT(TMutex& m, TDuration timeout) noexcept { - return WaitD(m, timeout.ToDeadLine()); - } - - template - inline bool WaitT(TMutex& m, TDuration timeout, P pred) noexcept { - return WaitD(m, timeout.ToDeadLine(), std::move(pred)); - } - - /* - * infinite wait - */ - inline void WaitI(TMutex& m) noexcept { - WaitD(m, TInstant::Max()); - } - - template - inline void WaitI(TMutex& m, P pred) noexcept { - WaitD(m, TInstant::Max(), std::move(pred)); - } - - //deprecated - inline void Wait(TMutex& m) noexcept { - WaitI(m); - } - - template - inline void Wait(TMutex& m, P pred) noexcept { - WaitI(m, std::move(pred)); - } - -private: - class TImpl; - THolder Impl_; -}; diff --git a/util/system/condvar_ut.cpp b/util/system/condvar_ut.cpp deleted file mode 100644 index c6cce1c0a7a..00000000000 --- a/util/system/condvar_ut.cpp +++ /dev/null @@ -1,191 +0,0 @@ -#include "mutex.h" -#include "guard.h" -#include "condvar.h" - -#include - -#include - -#include - -class TCondVarTest: public TTestBase { - UNIT_TEST_SUITE(TCondVarTest); - UNIT_TEST(TestBasics) - UNIT_TEST(TestSyncronize) - UNIT_TEST_SUITE_END(); - - struct TSharedData { - TMutex mutex; - TCondVar condVar1; - TCondVar condVar2; - - std::atomic stopWaiting = false; - - std::atomic in = 0; - std::atomic out = 0; - - std::atomic waited = 0; - - bool failed = false; - }; - - 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) - { - } - - void Process(void*) override { - THolder This(this); - - (this->*Func_)(); - } - -#define FAIL_ASSERT(cond) \ - if (!(cond)) { \ - Data_.failed = true; \ - } - void RunBasics() { - Y_ASSERT(TotalIds_ == 3); - - if (Id_ < 2) { - TGuard guard(Data_.mutex); - while (!Data_.stopWaiting.load()) { - bool res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } else { - usleep(100000); - Data_.stopWaiting.store(true); - - TGuard guard(Data_.mutex); - Data_.condVar1.Signal(); - Data_.condVar1.Signal(); - } - } - - void RunBasicsWithPredicate() { - Y_ASSERT(TotalIds_ == 3); - - if (Id_ < 2) { - TGuard guard(Data_.mutex); - const auto res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1), [&] { - return Data_.stopWaiting.load(); - }); - FAIL_ASSERT(res == true); - } else { - usleep(100000); - Data_.stopWaiting.store(true); - - TGuard guard(Data_.mutex); - Data_.condVar1.Signal(); - Data_.condVar1.Signal(); - } - } - - void RunSyncronize() { - for (size_t i = 0; i < 10; ++i) { - TGuard guard(Data_.mutex); - ++Data_.in; - if (Data_.in.load() == TotalIds_) { - Data_.out.store(0); - Data_.condVar1.BroadCast(); - } else { - ++Data_.waited; - while (Data_.in.load() < TotalIds_) { - bool res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } - - ++Data_.out; - if (Data_.out.load() == TotalIds_) { - Data_.in.store(0); - Data_.condVar2.BroadCast(); - } else { - while (Data_.out.load() < TotalIds_) { - bool res = Data_.condVar2.WaitT(Data_.mutex, TDuration::Seconds(1)); - FAIL_ASSERT(res == true); - } - } - } - - FAIL_ASSERT(Data_.waited.load() == (TotalIds_ - 1) * 10); - } - - void RunSyncronizeWithPredicate() { - for (size_t i = 0; i < 10; ++i) { - TGuard guard(Data_.mutex); - ++Data_.in; - if (Data_.in.load() == TotalIds_) { - Data_.out.store(0); - Data_.condVar1.BroadCast(); - } else { - ++Data_.waited; - const auto res = Data_.condVar1.WaitT(Data_.mutex, TDuration::Seconds(1), [&] { - return Data_.in.load() >= TotalIds_; - }); - FAIL_ASSERT(res == true); - } - - ++Data_.out; - if (Data_.out.load() == TotalIds_) { - Data_.in.store(0); - Data_.condVar2.BroadCast(); - } else { - const auto res = Data_.condVar2.WaitT(Data_.mutex, TDuration::Seconds(1), [&] { - return Data_.out.load() >= TotalIds_; - }); - FAIL_ASSERT(res == true); - } - } - - FAIL_ASSERT(Data_.waited == (TotalIds_ - 1) * 10); - } -#undef FAIL_ASSERT - - private: - PFunc Func_; - size_t Id_; - size_t TotalIds_; - 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); - } - - inline void TestBasicsWithPredicate() { - RUN_CYCLE(RunBasicsWithPredicate, 3); - } - - inline void TestSyncronize() { - RUN_CYCLE(RunSyncronize, 6); - } - - inline void TestSyncronizeWithPredicate() { - RUN_CYCLE(RunSyncronizeWithPredicate, 6); - } -#undef RUN_CYCLE - TSharedData Data_; - TThreadPool Q_; -}; - -UNIT_TEST_SUITE_REGISTRATION(TCondVarTest); diff --git a/util/system/fake_mutex.cpp b/util/system/fake_mutex.cpp new file mode 100644 index 00000000000..bc04bd4a6e3 --- /dev/null +++ b/util/system/fake_mutex.cpp @@ -0,0 +1 @@ +#include "fake_mutex.h" \ No newline at end of file diff --git a/util/system/fake_mutex.h b/util/system/fake_mutex.h new file mode 100644 index 00000000000..b76760f2deb --- /dev/null +++ b/util/system/fake_mutex.h @@ -0,0 +1,28 @@ +#include + +class TFakeMutex: public TNonCopyable { +public: + inline void Acquire() noexcept { + } + + inline bool TryAcquire() noexcept { + return true; + } + + inline void Release() noexcept { + } + + inline void lock() noexcept { + Acquire(); + } + + inline bool try_lock() noexcept { + return TryAcquire(); + } + + inline void unlock() noexcept { + Release(); + } + + ~TFakeMutex() = default; +}; \ No newline at end of file diff --git a/util/system/mutex.cpp b/util/system/mutex.cpp deleted file mode 100644 index 66f9a15f294..00000000000 --- a/util/system/mutex.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "mutex.h" - -#include -#include - -#if defined(_win_) - #include "winint.h" -#else - #include -#endif - -class TMutex::TImpl { -public: - inline TImpl() { -#if defined(_win_) - InitializeCriticalSection(&Obj); -#else - struct T { - pthread_mutexattr_t Attr; - - inline T() { - int result; - - memset(&Attr, 0, sizeof(Attr)); - result = pthread_mutexattr_init(&Attr); - if (result != 0) { - ythrow yexception() << "mutexattr init failed(" << LastSystemErrorText(result) << ")"; - } - - result = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE); - if (result != 0) { - ythrow yexception() << "mutexattr set type failed(" << LastSystemErrorText(result) << ")"; - } - } - - inline ~T() { - int result = pthread_mutexattr_destroy(&Attr); - Y_ABORT_UNLESS(result == 0, "mutexattr destroy(%s)", LastSystemErrorText(result)); - } - } pma; - - int result = pthread_mutex_init(&Obj, &pma.Attr); - if (result != 0) { - ythrow yexception() << "mutex init failed(" << LastSystemErrorText(result) << ")"; - } -#endif - } - - inline ~TImpl() { -#if defined(_win_) - DeleteCriticalSection(&Obj); -#else - int result = pthread_mutex_destroy(&Obj); - Y_ABORT_UNLESS(result == 0, "mutex destroy failure (%s)", LastSystemErrorText(result)); -#endif - } - - inline void Acquire() noexcept { -#if defined(_win_) - EnterCriticalSection(&Obj); -#else - int result = pthread_mutex_lock(&Obj); - Y_ABORT_UNLESS(result == 0, "mutex lock failure (%s)", LastSystemErrorText(result)); -#endif - } - -#if defined(_win_) - static bool TryEnterCriticalSectionInt(CRITICAL_SECTION* obj) { - #if (_WIN32_WINNT < 0x0400) - if (-1L == ::InterlockedCompareExchange(&obj->LockCount, 0, -1)) { - obj->OwningThread = (HANDLE)(DWORD_PTR)::GetCurrentThreadId(); - obj->RecursionCount = 1; - - return true; - } - - if (obj->OwningThread == (HANDLE)(DWORD_PTR)::GetCurrentThreadId()) { - ::InterlockedIncrement(&obj->LockCount); - ++obj->RecursionCount; - return true; - } - - return false; - #else // _WIN32_WINNT < 0x0400 - return TryEnterCriticalSection(obj); - #endif // _WIN32_WINNT < 0x0400 - } -#endif // _win_ - - inline bool TryAcquire() noexcept { -#if defined(_win_) - return TryEnterCriticalSectionInt(&Obj); -#else - int result = pthread_mutex_trylock(&Obj); - if (result == 0 || result == EBUSY) { - return result == 0; - } - Y_ABORT("mutex trylock failure (%s)", LastSystemErrorText(result)); -#endif - } - - inline void Release() noexcept { -#if defined(_win_) - LeaveCriticalSection(&Obj); -#else - int result = pthread_mutex_unlock(&Obj); - Y_ABORT_UNLESS(result == 0, "mutex unlock failure (%s)", LastSystemErrorText(result)); -#endif - } - - inline void* Handle() const noexcept { - return (void*)&Obj; - } - -private: -#ifdef _win_ - CRITICAL_SECTION Obj; -#else - pthread_mutex_t Obj; -#endif -}; - -TMutex::TMutex() - : Impl_(new TImpl()) -{ -} - -TMutex::TMutex(TMutex&&) noexcept = default; - -TMutex::~TMutex() = default; - -void TMutex::Acquire() noexcept { - Impl_->Acquire(); -} - -bool TMutex::TryAcquire() noexcept { - return Impl_->TryAcquire(); -} - -void TMutex::Release() noexcept { - Impl_->Release(); -} - -void* TMutex::Handle() const noexcept { - return Impl_->Handle(); -} diff --git a/util/system/mutex.h b/util/system/mutex.h deleted file mode 100644 index 2899ed9a50b..00000000000 --- a/util/system/mutex.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include "guard.h" -#include "defaults.h" - -#include -#include - -class TFakeMutex: public TNonCopyable { -public: - inline void Acquire() noexcept { - } - - inline bool TryAcquire() noexcept { - return true; - } - - inline void Release() noexcept { - } - - inline void lock() noexcept { - Acquire(); - } - - inline bool try_lock() noexcept { - return TryAcquire(); - } - - inline void unlock() noexcept { - Release(); - } - - ~TFakeMutex() = default; -}; - -class TMutex { -public: - TMutex(); - TMutex(TMutex&&) noexcept; - ~TMutex(); - - void Acquire() noexcept; - bool TryAcquire() noexcept; - void Release() noexcept; - - inline void lock() noexcept { - Acquire(); - } - - inline bool try_lock() noexcept { - return TryAcquire(); - } - - inline void unlock() noexcept { - Release(); - } - - //return opaque pointer to real handler - void* Handle() const noexcept; - -private: - class TImpl; - THolder Impl_; -}; diff --git a/util/system/mutex_ut.cpp b/util/system/mutex_ut.cpp deleted file mode 100644 index a9c5c45123a..00000000000 --- a/util/system/mutex_ut.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include "mutex.h" - -#include - -#include -#include - -class TMutexTest: public TTestBase { - 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; - }; - - 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 This(this); - - (this->*Func_)(); - } - -#define FAIL_ASSERT(cond) \ - if (!(cond)) { \ - Data_.failed = true; \ - } - void RunBasics() { - Data_.mutex.Acquire(); - - ui32 oldCounter = ui32(Data_.sharedCounter + Id_); - Data_.sharedCounter = oldCounter; - usleep(10 + RandomNumber() % 10); - FAIL_ASSERT(Data_.sharedCounter == oldCounter); - - Data_.mutex.Release(); - } - - void RunFakeMutex() { - bool res = Data_.fakeMutex.TryAcquire(); - FAIL_ASSERT(res); - } - - void RunRecursiveMutex() { - for (size_t i = 0; i < Id_ + 1; ++i) { - Data_.mutex.Acquire(); - ++Data_.sharedCounter; - usleep(1); - } - FAIL_ASSERT(Data_.sharedCounter == Id_ + 1); - - bool res = Data_.mutex.TryAcquire(); - FAIL_ASSERT(res); - Data_.mutex.Release(); - - 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 - - private: - PFunc Func_; - TSharedData& Data_; - size_t Id_; - }; - -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, Data_, i))); \ - } \ - Q_.Stop(); \ - 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 - -private: - TSharedData Data_; - TThreadPool Q_; -}; - -UNIT_TEST_SUITE_REGISTRATION(TMutexTest) diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index 89c92668af1..e7fadf1d836 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -8,7 +8,6 @@ #include "file.h" #include "getpid.h" #include "thread.h" -#include "mutex.h" #include #include #include