diff options
author | gulin <gulin@yandex-team.ru> | 2022-02-10 16:47:32 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:32 +0300 |
commit | 7199613d295246a07c2f7b331fbb3128936039dc (patch) | |
tree | c0748b5dcbade83af788c0abfa89c0383d6b779c /util | |
parent | c807aae441c17fc7f577c35757a4b6e0bd909802 (diff) | |
download | ydb-7199613d295246a07c2f7b331fbb3128936039dc.tar.gz |
Restoring authorship annotation for <gulin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util')
-rw-r--r-- | util/datetime/cputimer.cpp | 2 | ||||
-rw-r--r-- | util/folder/dirent_win.c | 2 | ||||
-rw-r--r-- | util/folder/lstat_win.c | 2 | ||||
-rw-r--r-- | util/generic/guid.cpp | 4 | ||||
-rw-r--r-- | util/generic/ptr.h | 34 | ||||
-rw-r--r-- | util/generic/vector.h | 2 | ||||
-rw-r--r-- | util/network/init.cpp | 4 | ||||
-rw-r--r-- | util/network/init.h | 24 | ||||
-rw-r--r-- | util/network/socket.h | 2 | ||||
-rw-r--r-- | util/stream/ios_ut.cpp | 4 | ||||
-rw-r--r-- | util/system/atomic_win.h | 2 | ||||
-rw-r--r-- | util/system/filemap.cpp | 2 | ||||
-rw-r--r-- | util/system/fs.cpp | 4 | ||||
-rw-r--r-- | util/system/hostname.cpp | 2 | ||||
-rw-r--r-- | util/system/hp_timer.cpp | 34 | ||||
-rw-r--r-- | util/system/hp_timer.h | 4 | ||||
-rw-r--r-- | util/system/shmat.cpp | 132 | ||||
-rw-r--r-- | util/system/shmat.h | 16 | ||||
-rw-r--r-- | util/system/thread.cpp | 14 | ||||
-rw-r--r-- | util/system/yassert.h | 8 | ||||
-rw-r--r-- | util/thread/lfqueue.h | 38 | ||||
-rw-r--r-- | util/thread/lfstack.h | 82 | ||||
-rw-r--r-- | util/ysafeptr.cpp | 42 | ||||
-rw-r--r-- | util/ysafeptr.h | 96 |
24 files changed, 278 insertions, 278 deletions
diff --git a/util/datetime/cputimer.cpp b/util/datetime/cputimer.cpp index 7b35b95311..516d372c37 100644 --- a/util/datetime/cputimer.cpp +++ b/util/datetime/cputimer.cpp @@ -1,7 +1,7 @@ #include "cputimer.h" #include <util/system/defaults.h> -#include <util/system/hp_timer.h> +#include <util/system/hp_timer.h> #include <util/string/printf.h> #include <util/stream/output.h> #include <util/generic/singleton.h> diff --git a/util/folder/dirent_win.c b/util/folder/dirent_win.c index 399caff0fe..7e6db74ce5 100644 --- a/util/folder/dirent_win.c +++ b/util/folder/dirent_win.c @@ -1,4 +1,4 @@ -#include <util/system/defaults.h> +#include <util/system/defaults.h> #ifdef _win_ diff --git a/util/folder/lstat_win.c b/util/folder/lstat_win.c index 9c779af306..cf94cec01a 100644 --- a/util/folder/lstat_win.c +++ b/util/folder/lstat_win.c @@ -1,4 +1,4 @@ -#include <util/system/defaults.h> +#include <util/system/defaults.h> #ifdef _win_ #include <util/system/winint.h> diff --git a/util/generic/guid.cpp b/util/generic/guid.cpp index 7db115665a..8b907457bc 100644 --- a/util/generic/guid.cpp +++ b/util/generic/guid.cpp @@ -46,8 +46,8 @@ void CreateGuid(TGUID* res) { WriteUnaligned<ui64>(&dw[0], RandomNumber<ui64>()); WriteUnaligned<ui64>(&dw[1], RandomNumber<ui64>()); -} - +} + TGUID TGUID::CreateTimebased() { TGUID result; // GUID_EPOCH_OFFSET is the number of 100-ns intervals between the diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 6d17160a4b..19db0e3ec5 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -1124,22 +1124,22 @@ template <class T> class TPtrArg { T* Ptr; -public: +public: TPtrArg(T* p) - : Ptr(p) - { - } + : Ptr(p) + { + } TPtrArg(const TIntrusivePtr<T>& p) - : Ptr(p.Get()) - { - } - operator T*() const { - return Ptr; - } - T* operator->() const { - return Ptr; - } - T* Get() const { - return Ptr; - } -}; + : Ptr(p.Get()) + { + } + operator T*() const { + return Ptr; + } + T* operator->() const { + return Ptr; + } + T* Get() const { + return Ptr; + } +}; diff --git a/util/generic/vector.h b/util/generic/vector.h index f5b2a199b2..a5b258955a 100644 --- a/util/generic/vector.h +++ b/util/generic/vector.h @@ -109,7 +109,7 @@ public: inline yssize_t ysize() const noexcept { return (yssize_t)TBase::size(); } - + #ifdef _YNDX_LIBCXX_ENABLE_VECTOR_POD_RESIZE_UNINITIALIZED void yresize(size_type newSize) { if (std::is_pod<T>::value) { diff --git a/util/network/init.cpp b/util/network/init.cpp index d4dd413fb2..366e65682c 100644 --- a/util/network/init.cpp +++ b/util/network/init.cpp @@ -16,7 +16,7 @@ namespace { signal(SIGPIPE, SIG_IGN); #endif -#if defined(_win_) +#if defined(_win_) #pragma comment(lib, "ws2_32.lib") WSADATA wsaData; int result = WSAStartup(MAKEWORD(2, 2), &wsaData); @@ -31,4 +31,4 @@ namespace { void InitNetworkSubSystem() { (void)Singleton<TNetworkInit>(); -} +} diff --git a/util/network/init.h b/util/network/init.h index 7d5c1dc0ab..08a79c0fca 100644 --- a/util/network/init.h +++ b/util/network/init.h @@ -1,35 +1,35 @@ #pragma once - + #include <util/system/error.h> - -#if defined(_unix_) + +#if defined(_unix_) #include <fcntl.h> #include <netdb.h> #include <time.h> #include <unistd.h> #include <poll.h> - + #include <sys/uio.h> #include <sys/time.h> #include <sys/types.h> #include <sys/socket.h> - + #include <netinet/in.h> #include <netinet/tcp.h> #include <arpa/inet.h> - + using SOCKET = int; - + #define closesocket(s) close(s) #define SOCKET_ERROR -1 #define INVALID_SOCKET -1 #define WSAGetLastError() errno -#elif defined(_win_) +#elif defined(_win_) #include <util/system/winint.h> #include <io.h> #include <winsock2.h> #include <ws2tcpip.h> - + using nfds_t = ULONG; #undef Yield @@ -50,9 +50,9 @@ struct sockaddr_un { #define MSG_NOSIGNAL 0 #endif #endif // _win_ or _darwin_ - -void InitNetworkSubSystem(); - + +void InitNetworkSubSystem(); + static struct TNetworkInitializer { inline TNetworkInitializer() { InitNetworkSubSystem(); diff --git a/util/network/socket.h b/util/network/socket.h index 1128d5923a..40c8648b40 100644 --- a/util/network/socket.h +++ b/util/network/socket.h @@ -22,7 +22,7 @@ #define get_host_error() h_errno #elif defined(_win_) #pragma comment(lib, "Ws2_32.lib") - + #if _WIN32_WINNT < 0x0600 struct pollfd { SOCKET fd; diff --git a/util/stream/ios_ut.cpp b/util/stream/ios_ut.cpp index ceb2731870..139f4296e5 100644 --- a/util/stream/ios_ut.cpp +++ b/util/stream/ios_ut.cpp @@ -100,9 +100,9 @@ void TStreamsTest::TestStringStream() { << 123.1; UNIT_ASSERT_EQUAL(s.ReadLine(), "34"); - UNIT_ASSERT_EQUAL(s.ReadLine(), "123.1"); + UNIT_ASSERT_EQUAL(s.ReadLine(), "123.1"); - UNIT_ASSERT_EQUAL(s.Str(), "qw\r\n1234\n34\r\n123.1"); + UNIT_ASSERT_EQUAL(s.Str(), "qw\r\n1234\n34\r\n123.1"); // Test stream copying TStringStream sc = s; diff --git a/util/system/atomic_win.h b/util/system/atomic_win.h index 3e9bd6244b..65c290e6cc 100644 --- a/util/system/atomic_win.h +++ b/util/system/atomic_win.h @@ -11,7 +11,7 @@ #pragma intrinsic(_InterlockedExchangeAdd) #pragma intrinsic(_InterlockedExchange) #pragma intrinsic(_InterlockedCompareExchange) - + static inline intptr_t AtomicIncrement(TAtomic& a) { return _InterlockedIncrement((volatile long*)&a); } diff --git a/util/system/filemap.cpp b/util/system/filemap.cpp index 830a409077..7454a4cb94 100644 --- a/util/system/filemap.cpp +++ b/util/system/filemap.cpp @@ -76,7 +76,7 @@ TString TMemoryMapCommon::UnknownFileName() { } static inline i64 DownToGranularity(i64 offset) noexcept { - return offset & ~((i64)(GRANULARITY - 1)); + return offset & ~((i64)(GRANULARITY - 1)); } #if defined(_unix_) diff --git a/util/system/fs.cpp b/util/system/fs.cpp index 42abc86a15..d2611a8ccc 100644 --- a/util/system/fs.cpp +++ b/util/system/fs.cpp @@ -1,12 +1,12 @@ #include "fs.h" #include "defaults.h" -#if defined(_win_) +#if defined(_win_) #include "fs_win.h" #else #include <unistd.h> #include <errno.h> -#endif +#endif #include <util/generic/yexception.h> #include <util/memory/tempbuf.h> diff --git a/util/system/hostname.cpp b/util/system/hostname.cpp index dd4d4414cd..386f646d6b 100644 --- a/util/system/hostname.cpp +++ b/util/system/hostname.cpp @@ -27,7 +27,7 @@ namespace { } HostName = hostNameBuf.Data(); - } + } TString HostName; }; diff --git a/util/system/hp_timer.cpp b/util/system/hp_timer.cpp index 05ea5216bd..e4c3f21e6b 100644 --- a/util/system/hp_timer.cpp +++ b/util/system/hp_timer.cpp @@ -4,8 +4,8 @@ #include <util/generic/singleton.h> #include <util/datetime/cputimer.h> -using namespace NHPTimer; - +using namespace NHPTimer; + namespace { struct TFreq { inline TFreq() @@ -13,12 +13,12 @@ namespace { , Rate(1.0 / Freq) , CyclesPerSecond(static_cast<ui64>(Rate)) { - } + } static inline const TFreq& Instance() { return *SingletonWithPriority<TFreq, 1>(); - } - + } + static double EstimateCPUClock() { for (;;) { ui64 startCycle = 0; @@ -91,28 +91,28 @@ namespace { const double Rate; const ui64 CyclesPerSecond; }; -} - +} + double NHPTimer::GetSeconds(const STime& a) noexcept { return TFreq::Instance().GetSeconds(a); -} - +} + double NHPTimer::GetClockRate() noexcept { return TFreq::Instance().GetClockRate(); -} - +} + ui64 NHPTimer::GetCyclesPerSecond() noexcept { return TFreq::Instance().GetCyclesPerSecond(); } void NHPTimer::GetTime(STime* pTime) noexcept { - *pTime = GetCycleCount(); -} - + *pTime = GetCycleCount(); +} + double NHPTimer::GetTimePassed(STime* pTime) noexcept { - STime old(*pTime); + STime old(*pTime); *pTime = GetCycleCount(); - return GetSeconds(*pTime - old); -} + return GetSeconds(*pTime - old); +} diff --git a/util/system/hp_timer.h b/util/system/hp_timer.h index e482c413bd..0a4c252ec2 100644 --- a/util/system/hp_timer.h +++ b/util/system/hp_timer.h @@ -1,5 +1,5 @@ #pragma once - + #include "defaults.h" namespace NHPTimer { @@ -33,4 +33,4 @@ struct THPTimer { private: NHPTimer::STime Start; -}; +}; diff --git a/util/system/shmat.cpp b/util/system/shmat.cpp index a16108660f..07ff0d6caa 100644 --- a/util/system/shmat.cpp +++ b/util/system/shmat.cpp @@ -1,4 +1,4 @@ -#include "shmat.h" +#include "shmat.h" #include <util/generic/guid.h> @@ -37,8 +37,8 @@ extern "C" { BOOL WINAPI UnmapViewOfFile(LPCVOID lpBaseAddress); DWORD WINAPI GetLastError(void); } -#endif - +#endif + #if defined(_bionic_) namespace { #if !defined(__i386__) @@ -95,117 +95,117 @@ TSharedMemory::TSharedMemory() , Size(0) { } - + #if defined(_win_) static void FormatName(char* buf, const TGUID& id) { - sprintf(buf, "Global\\shmat-%s", GetGuidAsString(id).c_str()); -} - + sprintf(buf, "Global\\shmat-%s", GetGuidAsString(id).c_str()); +} + bool TSharedMemory::Open(const TGUID& id, int size) { //Y_ASSERT(Data == 0); - Id = id; - Size = size; - - char name[100]; - FormatName(name, Id); + Id = id; + Size = size; + + char name[100]; + FormatName(name, Id); Handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, name); if (Handle == 0) { - return false; + return false; } - + Data = MapViewOfFile(Handle, FILE_MAP_ALL_ACCESS, 0, 0, size); - if (Data == 0) { + if (Data == 0) { //Y_ASSERT(0); CloseHandle(Handle); Handle = INVALID_OS_HANDLE; - return false; - } + return false; + } + + return true; +} - return true; -} - bool TSharedMemory::Create(int size) { //Y_ASSERT(Data == 0); - Size = size; - - CreateGuid(&Id); - - char name[100]; - FormatName(name, Id); + Size = size; + + CreateGuid(&Id); + + char name[100]; + FormatName(name, Id); Handle = CreateFileMappingA(INVALID_OS_HANDLE, nullptr, PAGE_READWRITE, 0, size, name); if (Handle == 0) { //Y_ASSERT(0); - return false; - } - + return false; + } + Data = MapViewOfFile(Handle, FILE_MAP_ALL_ACCESS, 0, 0, size); - if (Data == 0) { + if (Data == 0) { //Y_ASSERT(0); CloseHandle(Handle); Handle = INVALID_OS_HANDLE; - return false; - } + return false; + } + + return true; +} - return true; -} - TSharedMemory::~TSharedMemory() { if (Data) { UnmapViewOfFile(Handle); } CloseHandle(Handle); -} -#else +} +#else static key_t GetKey(const TGUID& id) { i64 id64 = (ui64)(((ui64)id.dw[0] + (ui64)id.dw[2]) << 32) + (ui64)id.dw[1] + (ui64)id.dw[3]; - return id64; -} - + return id64; +} + bool TSharedMemory::Open(const TGUID& id, int size) { Y_VERIFY(id, "invalid shared memory guid: %s", GetGuidAsString(id).data()); //Y_ASSERT(Data == 0); - Size = size; - - key_t k = GetKey(id); + Size = size; + + key_t k = GetKey(id); int shmId = shmget(k, Size, 0777); // do not fill Handle, since IPC_RMID should be called by owner if (shmId < 0) { - return false; + return false; } - + Data = shmat(shmId, nullptr, 0); if (Data == nullptr) { //Y_ASSERT(0); - return false; - } + return false; + } + + return true; +} - return true; -} - bool TSharedMemory::Create(int size) { //Y_ASSERT(Data == 0); - Size = size; - - CreateGuid(&Id); - - key_t k = GetKey(Id); + Size = size; + + CreateGuid(&Id); + + key_t k = GetKey(Id); Handle = shmget(k, Size, IPC_CREAT | IPC_EXCL | 0777); if (Handle < 0) { //Y_ASSERT(0); - return false; - } - + return false; + } + Data = shmat(Handle, nullptr, 0); if (Data == (void*)-1) { @@ -213,19 +213,19 @@ bool TSharedMemory::Create(int size) { shmctl(Handle, IPC_RMID, nullptr); Handle = -1; - return false; - } + return false; + } + + return true; +} - return true; -} - TSharedMemory::~TSharedMemory() { if (Data) { - shmdt(Data); + shmdt(Data); } if (Handle >= 0) { shmctl(Handle, IPC_RMID, nullptr); } -} -#endif +} +#endif diff --git a/util/system/shmat.h b/util/system/shmat.h index ab860e30ca..d9da3c151a 100644 --- a/util/system/shmat.h +++ b/util/system/shmat.h @@ -1,21 +1,21 @@ -#pragma once - +#pragma once + #include "fhandle.h" #include <util/generic/ptr.h> #include <util/generic/guid.h> - + class TSharedMemory: public TThrRefBase { - TGUID Id; + TGUID Id; FHANDLE Handle; void* Data; - int Size; + int Size; -public: +public: TSharedMemory(); ~TSharedMemory() override; - bool Create(int Size); + bool Create(int Size); bool Open(const TGUID& id, int size); const TGUID& GetId() { @@ -29,4 +29,4 @@ public: int GetSize() const { return Size; } -}; +}; diff --git a/util/system/thread.cpp b/util/system/thread.cpp index 746c1eac30..6236746c2d 100644 --- a/util/system/thread.cpp +++ b/util/system/thread.cpp @@ -203,15 +203,15 @@ namespace { } } - { + { TParams* holdP = P_.Release(); - int err = pthread_create(&H_, pattrs, ThreadProxy, holdP); - if (err) { + int err = pthread_create(&H_, pattrs, ThreadProxy, holdP); + if (err) { H_ = {}; - P_.Reset(holdP); - PCHECK(err, "failed to create thread"); - } - } + P_.Reset(holdP); + PCHECK(err, "failed to create thread"); + } + } } private: diff --git a/util/system/yassert.h b/util/system/yassert.h index 490ca4a7d9..529823440c 100644 --- a/util/system/yassert.h +++ b/util/system/yassert.h @@ -52,7 +52,7 @@ inline bool YaIsDebuggerPresent() { inline void YaDebugBreak() { __debugbreak(); -} +} #undef Y_ASSERT @@ -79,7 +79,7 @@ inline void YaDebugBreak() { } \ } \ } while (false) -#else +#else #define Y_ASSERT(a) \ do { \ if (false) { \ @@ -87,8 +87,8 @@ inline void YaDebugBreak() { Y_UNUSED(__xxx); \ } \ } while (false) -#endif - +#endif + namespace NPrivate { /// method should not be used directly [[noreturn]] void Panic(const TStaticBuf& file, int line, const char* function, const char* expr, const char* format, ...) noexcept Y_PRINTF_FORMAT(5, 6); diff --git a/util/thread/lfqueue.h b/util/thread/lfqueue.h index 2717c56bb3..ab523631e4 100644 --- a/util/thread/lfqueue.h +++ b/util/thread/lfqueue.h @@ -10,14 +10,14 @@ struct TDefaultLFCounter { template <class T> void IncCount(const T& data) { - (void)data; - } + (void)data; + } template <class T> void DecCount(const T& data) { - (void)data; - } -}; - + (void)data; + } +}; + // @brief lockfree queue // @tparam T - the queue element, should be movable // @tparam TCounter, a observer class to count number of items in queue @@ -43,7 +43,7 @@ class TLockFreeQueue: public TNonCopyable { T Data; }; - // using inheritance to be able to use 0 bytes for TCounter when we don't need one + // using inheritance to be able to use 0 bytes for TCounter when we don't need one struct TRootNode: public TCounter { TListNode* volatile PushQueue; TListNode* volatile PopQueue; @@ -58,8 +58,8 @@ class TLockFreeQueue: public TNonCopyable { { } void CopyCounter(TRootNode* x) { - *(TCounter*)this = *(TCounter*)x; - } + *(TCounter*)this = *(TCounter*)x; + } }; static void EraseList(TListNode* n) { @@ -233,8 +233,8 @@ public: { } ~TLockFreeQueue() { - AsyncRef(); - AsyncUnref(); // should free FreeList + AsyncRef(); + AsyncUnref(); // should free FreeList EraseList(JobQueue->PushQueue); EraseList(JobQueue->PopQueue); delete JobQueue; @@ -285,8 +285,8 @@ public: AtomicSet(newRoot->PushQueue, AtomicGet(curRoot->PushQueue)); AtomicSet(newRoot->PopQueue, AtomicGet(tail->Next)); - newRoot->CopyCounter(curRoot); - newRoot->DecCount(tail->Data); + newRoot->CopyCounter(curRoot); + newRoot->DecCount(tail->Data); Y_ASSERT(AtomicGet(curRoot->PopQueue) == tail); if (AtomicCas(&JobQueue, newRoot, curRoot)) { *data = std::move(tail->Data); @@ -307,7 +307,7 @@ public: AtomicSet(newRoot->PushQueue, nullptr); listInvertor.DoCopy(AtomicGet(curRoot->PushQueue)); AtomicSet(newRoot->PopQueue, listInvertor.Copy); - newRoot->CopyCounter(curRoot); + newRoot->CopyCounter(curRoot); Y_ASSERT(AtomicGet(curRoot->PopQueue) == nullptr); if (AtomicCas(&JobQueue, newRoot, curRoot)) { newRoot = nullptr; @@ -351,12 +351,12 @@ public: return res; } TCounter GetCounter() { - AsyncRef(); + AsyncRef(); TRootNode* curRoot = AtomicGet(JobQueue); - TCounter res = *(TCounter*)curRoot; - AsyncUnref(); - return res; - } + TCounter res = *(TCounter*)curRoot; + AsyncUnref(); + return res; + } }; template <class T, class TCounter> diff --git a/util/thread/lfstack.h b/util/thread/lfstack.h index 98135e1ee9..ca3d95f3c3 100644 --- a/util/thread/lfstack.h +++ b/util/thread/lfstack.h @@ -121,66 +121,66 @@ public: AtomicAdd(DequeueCount, -1); return false; } - // add all elements to *res + // add all elements to *res // elements are returned in order of dequeue (top to bottom; see example in unittest) template <typename TCollection> void DequeueAll(TCollection* res) { - AtomicAdd(DequeueCount, 1); + AtomicAdd(DequeueCount, 1); for (TNode* current = AtomicGet(Head); current; current = AtomicGet(Head)) { if (AtomicCas(&Head, (TNode*)nullptr, current)) { for (TNode* x = current; x;) { res->push_back(std::move(x->Value)); - x = x->Next; - } - // EraseList(current); // ABA problem - // even more complex node deletion - TryToFreeMemory(); - if (AtomicAdd(DequeueCount, -1) == 0) { - // no other Dequeue()s, can safely reclaim memory - EraseList(current); - } else { - // Dequeue()s in progress, add nodes list to free list + x = x->Next; + } + // EraseList(current); // ABA problem + // even more complex node deletion + TryToFreeMemory(); + if (AtomicAdd(DequeueCount, -1) == 0) { + // no other Dequeue()s, can safely reclaim memory + EraseList(current); + } else { + // Dequeue()s in progress, add nodes list to free list TNode* currentLast = current; - while (currentLast->Next) { + while (currentLast->Next) { currentLast = currentLast->Next; - } - for (;;) { + } + for (;;) { AtomicSet(currentLast->Next, AtomicGet(FreePtr)); - if (AtomicCas(&FreePtr, current, currentLast->Next)) - break; - } - } - return; - } - } - TryToFreeMemory(); - AtomicAdd(DequeueCount, -1); - } + if (AtomicCas(&FreePtr, current, currentLast->Next)) + break; + } + } + return; + } + } + TryToFreeMemory(); + AtomicAdd(DequeueCount, -1); + } bool DequeueSingleConsumer(T* res) { for (TNode* current = AtomicGet(Head); current; current = AtomicGet(Head)) { - if (AtomicCas(&Head, current->Next, current)) { + if (AtomicCas(&Head, current->Next, current)) { *res = std::move(current->Value); - delete current; // with single consumer thread ABA does not happen - return true; - } - } - return false; - } - // add all elements to *res - // elements are returned in order of dequeue (top to bottom; see example in unittest) + delete current; // with single consumer thread ABA does not happen + return true; + } + } + return false; + } + // add all elements to *res + // elements are returned in order of dequeue (top to bottom; see example in unittest) template <typename TCollection> void DequeueAllSingleConsumer(TCollection* res) { for (TNode* current = AtomicGet(Head); current; current = AtomicGet(Head)) { if (AtomicCas(&Head, (TNode*)nullptr, current)) { for (TNode* x = current; x;) { res->push_back(std::move(x->Value)); - x = x->Next; - } - EraseList(current); // with single consumer thread ABA does not happen - return; - } - } - } + x = x->Next; + } + EraseList(current); // with single consumer thread ABA does not happen + return; + } + } + } bool IsEmpty() { AtomicAdd(DequeueCount, 0); // mem barrier return AtomicGet(Head) == nullptr; // without lock, so result is approximate diff --git a/util/ysafeptr.cpp b/util/ysafeptr.cpp index de00b6d8f0..96f2514067 100644 --- a/util/ysafeptr.cpp +++ b/util/ysafeptr.cpp @@ -1,33 +1,33 @@ #include "ysafeptr.h" -#ifdef CHECK_YPTR2 +#ifdef CHECK_YPTR2 Y_POD_THREAD(bool) IObjectBase::DisableThreadCheck; -#endif -//////////////////////////////////////////////////////////////////////////////////////////////////// +#endif +//////////////////////////////////////////////////////////////////////////////////////////////////// void IObjectBase::ReleaseObjComplete(int nMask) { - if ((ObjData & 0x3fffffff) == 0 && RefData == 0) { - assert((ObjData & 0x40000000) == 0); // object not being invalidated + if ((ObjData & 0x3fffffff) == 0 && RefData == 0) { + assert((ObjData & 0x40000000) == 0); // object not being invalidated delete this; - } else if ((ObjData & nMask) == 0) { - if (ObjData & 0x40000000) { - // object is already being invalidated - // possible when no CObj left and object is invalidated and during this all CMObj are also out - return; - } + } else if ((ObjData & nMask) == 0) { + if (ObjData & 0x40000000) { + // object is already being invalidated + // possible when no CObj left and object is invalidated and during this all CMObj are also out + return; + } ObjData |= 0xc0000000; AddRef(); DestroyContents(); - assert((ObjData & nMask) == 0); // otherwise empty constructor is adding CObjs on self - ObjData &= ~0x40000000; + assert((ObjData & nMask) == 0); // otherwise empty constructor is adding CObjs on self + ObjData &= ~0x40000000; ReleaseRef(); } -} -//////////////////////////////////////////////////////////////////////////////////////////////////// +} +//////////////////////////////////////////////////////////////////////////////////////////////////// void IObjectBase::ReleaseRefComplete() { - assert(RefData == 0); - if ((ObjData & 0x3fffffff) == 0) { - assert((ObjData & 0x40000000) == 0); // object not being invalidated - delete this; - } -} + assert(RefData == 0); + if ((ObjData & 0x3fffffff) == 0) { + assert((ObjData & 0x40000000) == 0); // object not being invalidated + delete this; + } +} diff --git a/util/ysafeptr.h b/util/ysafeptr.h index a191f4881d..af7dfd4bed 100644 --- a/util/ysafeptr.h +++ b/util/ysafeptr.h @@ -1,11 +1,11 @@ #pragma once -#include <stddef.h> -#include <util/system/yassert.h> +#include <stddef.h> +#include <util/system/yassert.h> #include <util/system/defaults.h> -#include <util/system/tls.h> - -//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <util/system/tls.h> + +//////////////////////////////////////////////////////////////////////////////////////////////////// // There are different templates of pointers: // 1. Simple pointers. // 2. TPtr with refereces. @@ -18,18 +18,18 @@ // pointer to a forward declared class. // 2. It's prohibited to override the 'new' operator, since the standard 'delete' will be used // for destruction of objects (because of 'delete this'). -//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// #if defined(_MSC_VER) && defined(_DEBUG) #include <util/system/winint.h> #define CHECK_YPTR2 -#endif - -struct IBinSaver; - +#endif + +struct IBinSaver; + class IObjectBase { -private: -#ifdef CHECK_YPTR2 +private: +#ifdef CHECK_YPTR2 static Y_POD_THREAD(bool) DisableThreadCheck; void CheckThreadId() { if (dwThreadId == 0) @@ -45,7 +45,7 @@ private: CheckThreadId(); ObjData += nRef; } -#else +#else void CheckThreadId() { } void AddRef() { @@ -54,7 +54,7 @@ private: void AddObj(int nRef) { ObjData += nRef; } -#endif +#endif void ReleaseRefComplete(); void ReleaseObjComplete(int nMask); void DecRef() { @@ -78,10 +78,10 @@ private: ReleaseObjComplete(nMask); } -protected: -#ifdef CHECK_YPTR2 +protected: +#ifdef CHECK_YPTR2 DWORD dwThreadId; -#endif +#endif ui32 ObjData; ui32 RefData; // function should clear contents of object, easy to implement via consequent calls to @@ -93,43 +93,43 @@ protected: ObjData |= a.ObjData & 0x80000000; } -public: +public: IObjectBase() : ObjData(0) , RefData(0) { -#ifdef CHECK_YPTR2 +#ifdef CHECK_YPTR2 dwThreadId = 0; -#endif +#endif } // do not copy refcount when copy object IObjectBase(const IObjectBase& a) : ObjData(0) , RefData(0) { -#ifdef CHECK_YPTR2 +#ifdef CHECK_YPTR2 dwThreadId = 0; -#endif +#endif CopyValidFlag(a); } IObjectBase& operator=(const IObjectBase& a) { CopyValidFlag(a); return *this; } -#ifdef CHECK_YPTR2 +#ifdef CHECK_YPTR2 static void SetThreadCheckMode(bool val) { DisableThreadCheck = !val; } void ResetThreadId() { Y_ASSERT(RefData == 0 && ObjData == 0); // can reset thread check only for ref free objects - dwThreadId = 0; - } -#else + dwThreadId = 0; + } +#else static void SetThreadCheckMode(bool) { } void ResetThreadId() { } -#endif +#endif // class name of derived class virtual const char* GetClassName() const = 0; @@ -187,10 +187,10 @@ public: friend struct IObjectBase::TRef; friend struct IObjectBase::TRefO; friend struct IObjectBase::TRefM; -}; -//////////////////////////////////////////////////////////////////////////////////////////////////// -// macro that helps to create neccessary members for proper operation of refcount system -// if class needs special destructor, use CFundament +}; +//////////////////////////////////////////////////////////////////////////////////////////////////// +// macro that helps to create neccessary members for proper operation of refcount system +// if class needs special destructor, use CFundament #define OBJECT_METHODS(classname) \ public: \ virtual const char* GetClassName() const override { \ @@ -210,7 +210,7 @@ protected: \ } \ \ private: -#define OBJECT_NOCOPY_METHODS(classname) OBJECT_METHODS(classname) +#define OBJECT_NOCOPY_METHODS(classname) OBJECT_METHODS(classname) #define BASIC_REGISTER_CLASS(classname) \ Y_PRAGMA_DIAGNOSTIC_PUSH \ Y_PRAGMA_NO_UNUSED_FUNCTION \ @@ -224,7 +224,7 @@ private: } \ Y_PRAGMA_DIAGNOSTIC_POP -//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// template <class TUserObj> IObjectBase* CastToObjectBaseImpl(TUserObj* p, void*); template <class TUserObj> @@ -236,7 +236,7 @@ TUserObj* CastToUserObjectImpl(IObjectBase* p, TUserObj*, void*); template <class TUserObj> TUserObj* CastToUserObjectImpl(IObjectBase* _p, TUserObj*, IObjectBase*) { return dynamic_cast<TUserObj*>(_p); -} +} template <class TUserObj> inline IObjectBase* CastToObjectBase(TUserObj* p) { return CastToObjectBaseImpl(p, p); @@ -249,12 +249,12 @@ template <class TUserObj> inline TUserObj* CastToUserObject(IObjectBase* p, TUserObj* pu) { return CastToUserObjectImpl(p, pu, pu); } -//////////////////////////////////////////////////////////////////////////////////////////////////// -// TObject - base object for reference counting, TUserObj - user object name -// TRef - struct with AddRef/DecRef/Release methods for refcounting to use +//////////////////////////////////////////////////////////////////////////////////////////////////// +// TObject - base object for reference counting, TUserObj - user object name +// TRef - struct with AddRef/DecRef/Release methods for refcounting to use template <class TUserObj, class TRef> class TPtrBase { -private: +private: TUserObj* ptr; void AddRef(TUserObj* _ptr) { @@ -273,7 +273,7 @@ private: p.Release(CastToObjectBase(_ptr)); } -protected: +protected: void SetObject(TUserObj* _ptr) { TUserObj* pOld = ptr; ptr = _ptr; @@ -281,7 +281,7 @@ protected: Release(pOld); } -public: +public: TPtrBase() : ptr(nullptr) { @@ -337,8 +337,8 @@ public: return CastToObjectBase(ptr); } int operator&(IBinSaver& f); -}; -//////////////////////////////////////////////////////////////////////////////////////////////////// +}; +//////////////////////////////////////////////////////////////////////////////////////////////////// template <class T> inline bool IsValid(T* p) { return p != nullptr && !CastToObjectBase(p)->IsRefInvalid(); @@ -347,7 +347,7 @@ template <class T, class TRef> inline bool IsValid(const TPtrBase<T, TRef>& p) { return p.Get() && !p.GetBarePtr()->IsRefInvalid(); } -//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// #define BASIC_PTR_DECLARE(TPtrName, TRef) \ template <class T> \ class TPtrName: public TPtrBase<T, TRef> { \ @@ -381,7 +381,7 @@ inline bool IsValid(const TPtrBase<T, TRef>& p) { BASIC_PTR_DECLARE(TPtr, IObjectBase::TRef) BASIC_PTR_DECLARE(TObj, IObjectBase::TRefO) BASIC_PTR_DECLARE(TMObj, IObjectBase::TRefM) -// misuse guard +// misuse guard template <class T> inline bool IsValid(TObj<T>* p) { return p->YouHaveMadeMistake(); @@ -394,13 +394,13 @@ template <class T> inline bool IsValid(TMObj<T>* p) { return p->YouHaveMadeMistake(); } -//////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// // assumes base class is IObjectBase template <class T> class TDynamicCast { T* ptr; -public: +public: template <class TT> TDynamicCast(TT* _ptr) { ptr = dynamic_cast<T*>(CastToObjectBase(_ptr)); @@ -422,8 +422,8 @@ public: T* Get() const { return ptr; } -}; -template <class T> +}; +template <class T> inline bool IsValid(const TDynamicCast<T>& p) { return IsValid(p.Get()); } |