diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/system/mutex.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/system/mutex.cpp')
-rw-r--r-- | util/system/mutex.cpp | 214 |
1 files changed, 107 insertions, 107 deletions
diff --git a/util/system/mutex.cpp b/util/system/mutex.cpp index a20aa20b11..4041402db9 100644 --- a/util/system/mutex.cpp +++ b/util/system/mutex.cpp @@ -1,146 +1,146 @@ -#include "mutex.h" - -#include <util/generic/yexception.h> +#include "mutex.h" + +#include <util/generic/yexception.h> #include <errno.h> - -#if defined(_win_) - #include "winint.h" -#else - #include <pthread.h> + +#if defined(_win_) + #include "winint.h" +#else + #include <pthread.h> #endif class TMutex::TImpl { -public: - inline TImpl() { -#if defined(_win_) - InitializeCriticalSection(&Obj); +public: + inline TImpl() { +#if defined(_win_) + InitializeCriticalSection(&Obj); #else - struct T { - pthread_mutexattr_t Attr; - - inline T() { - int result; + 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) << ")"; - } - } - + 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); + int result = pthread_mutexattr_destroy(&Attr); Y_VERIFY(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) << ")"; - } + } + } 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); +#if defined(_win_) + DeleteCriticalSection(&Obj); #else - int result = pthread_mutex_destroy(&Obj); + int result = pthread_mutex_destroy(&Obj); Y_VERIFY(result == 0, "mutex destroy failure (%s)", LastSystemErrorText(result)); #endif - } - + } + inline void Acquire() noexcept { -#if defined(_win_) - EnterCriticalSection(&Obj); +#if defined(_win_) + EnterCriticalSection(&Obj); #else - int result = pthread_mutex_lock(&Obj); + int result = pthread_mutex_lock(&Obj); Y_VERIFY(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 - } + } + +#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); +#if defined(_win_) + return TryEnterCriticalSectionInt(&Obj); #else - int result = pthread_mutex_trylock(&Obj); - if (result == 0 || result == EBUSY) { - return result == 0; - } + int result = pthread_mutex_trylock(&Obj); + if (result == 0 || result == EBUSY) { + return result == 0; + } Y_FAIL("mutex trylock failure (%s)", LastSystemErrorText(result)); #endif - } - + } + inline void Release() noexcept { -#if defined(_win_) - LeaveCriticalSection(&Obj); +#if defined(_win_) + LeaveCriticalSection(&Obj); #else - int result = pthread_mutex_unlock(&Obj); + int result = pthread_mutex_unlock(&Obj); Y_VERIFY(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 -}; - + return (void*)&Obj; + } + +private: +#ifdef _win_ + CRITICAL_SECTION Obj; +#else + pthread_mutex_t Obj; +#endif +}; + TMutex::TMutex() - : Impl_(new TImpl()) -{ + : Impl_(new TImpl()) +{ } TMutex::TMutex(TMutex&&) = default; TMutex::~TMutex() = default; - + void TMutex::Acquire() noexcept { - Impl_->Acquire(); -} - + Impl_->Acquire(); +} + bool TMutex::TryAcquire() noexcept { - return Impl_->TryAcquire(); -} - + return Impl_->TryAcquire(); +} + void TMutex::Release() noexcept { - Impl_->Release(); -} - + Impl_->Release(); +} + void* TMutex::Handle() const noexcept { - return Impl_->Handle(); -} + return Impl_->Handle(); +} |