diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-05-29 11:53:16 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-05-29 12:02:41 +0300 |
commit | 0fb82c7e38da7a8c39d5f1bbe95631ef39492bae (patch) | |
tree | c938ee5618c725b5aa99b3837e9a53ae10cc2897 /contrib/libs/poco/Foundation | |
parent | 9314042511cd9d2598ed16eb0a19c84909895938 (diff) | |
download | ydb-0fb82c7e38da7a8c39d5f1bbe95631ef39492bae.tar.gz |
Backport AtomicCounter from poco upstream to make it compilable under modern macOS SDK
551605377eff8967caeb593938df14b9f0d0172c
Diffstat (limited to 'contrib/libs/poco/Foundation')
-rw-r--r-- | contrib/libs/poco/Foundation/include/Poco/AtomicCounter.h | 320 | ||||
-rw-r--r-- | contrib/libs/poco/Foundation/src/AtomicCounter.cpp | 177 |
2 files changed, 20 insertions, 477 deletions
diff --git a/contrib/libs/poco/Foundation/include/Poco/AtomicCounter.h b/contrib/libs/poco/Foundation/include/Poco/AtomicCounter.h index 4c7c7884f0..1377fd4c96 100644 --- a/contrib/libs/poco/Foundation/include/Poco/AtomicCounter.h +++ b/contrib/libs/poco/Foundation/include/Poco/AtomicCounter.h @@ -19,33 +19,7 @@ #include "Poco/Foundation.h" -#if POCO_OS == POCO_OS_WINDOWS_NT - #include "Poco/UnWindows.h" -#elif POCO_OS == POCO_OS_MAC_OS_X - #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 || __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000 || __TV_OS_VERSION_MAX_ALLOWED >= 100000 || __WATCH_OS_VERSION_MAX_ALLOWED >= 30000 - #if __cplusplus >= 201103L - #ifndef POCO_HAVE_STD_ATOMICS - #define POCO_HAVE_STD_ATOMICS - #endif - #else - #include <libkern/OSAtomic.h> - #endif - #else - #include <libkern/OSAtomic.h> - #endif -#elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) || __GNUC__ > 4) && (defined(__x86_64__) || defined(__i386__)) - #if !defined(POCO_HAVE_GCC_ATOMICS) && !defined(POCO_NO_GCC_ATOMICS) - #define POCO_HAVE_GCC_ATOMICS - #endif -#elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3) || __GNUC__ > 4) - #if !defined(POCO_HAVE_GCC_ATOMICS) && !defined(POCO_NO_GCC_ATOMICS) - #define POCO_HAVE_GCC_ATOMICS - #endif -#endif // POCO_OS -#include "Poco/Mutex.h" -#ifdef POCO_HAVE_STD_ATOMICS #include <atomic> -#endif namespace Poco { @@ -57,81 +31,53 @@ class Foundation_API AtomicCounter /// use in a multithreaded environment. /// /// Typical usage of AtomicCounter is for implementing - /// reference counting and similar things. - /// - /// On some platforms, the implementation of AtomicCounter - /// is based on atomic primitives specific to the platform - /// (such as InterlockedIncrement, etc. on Windows), and - /// thus very efficient. On platforms that do not support - /// atomic primitives, operations are guarded by a FastMutex. - /// - /// The following platforms currently have atomic - /// primitives: - /// - Windows - /// - Mac OS X - /// - GCC 4.1+ (Intel platforms only) + /// reference counting and similar functionality. { public: typedef int ValueType; /// The underlying integer type. - + AtomicCounter(); /// Creates a new AtomicCounter and initializes it to zero. - + explicit AtomicCounter(ValueType initialValue); /// Creates a new AtomicCounter and initializes it with /// the given value. - + AtomicCounter(const AtomicCounter& counter); /// Creates the counter by copying another one. - + ~AtomicCounter(); /// Destroys the AtomicCounter. AtomicCounter& operator = (const AtomicCounter& counter); /// Assigns the value of another AtomicCounter. - + AtomicCounter& operator = (ValueType value); /// Assigns a value to the counter. operator ValueType () const; - /// Returns the value of the counter. - + /// Converts the AtomicCounter to ValueType. + ValueType value() const; /// Returns the value of the counter. - + ValueType operator ++ (); // prefix /// Increments the counter and returns the result. - + ValueType operator ++ (int); // postfix /// Increments the counter and returns the previous value. - + ValueType operator -- (); // prefix /// Decrements the counter and returns the result. - + ValueType operator -- (int); // postfix /// Decrements the counter and returns the previous value. - + bool operator ! () const; /// Returns true if the counter is zero, false otherwise. private: -#if defined(POCO_HAVE_STD_ATOMICS) - typedef std::atomic<int> ImplType; -#elif POCO_OS == POCO_OS_WINDOWS_NT - typedef volatile LONG ImplType; -#elif POCO_OS == POCO_OS_MAC_OS_X - typedef int32_t ImplType; -#elif defined(POCO_HAVE_GCC_ATOMICS) - typedef int ImplType; -#else // generic implementation based on FastMutex - struct ImplType - { - mutable FastMutex mutex; - volatile int value; - }; -#endif // POCO_OS - - ImplType _counter; + std::atomic<int> _counter; }; @@ -139,17 +85,12 @@ private: // inlines // - -#if defined(POCO_HAVE_STD_ATOMICS) -// -// C++11 atomics -// inline AtomicCounter::operator AtomicCounter::ValueType () const { return _counter.load(); } - + inline AtomicCounter::ValueType AtomicCounter::value() const { return _counter.load(); @@ -161,7 +102,7 @@ inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix return ++_counter; } - + inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix { return _counter++; @@ -173,244 +114,19 @@ inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix return --_counter; } - -inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix -{ - return _counter--; -} - - -inline bool AtomicCounter::operator ! () const -{ - return _counter.load() == 0; -} - - -#elif POCO_OS == POCO_OS_WINDOWS_NT -// -// Windows -// -inline AtomicCounter::operator AtomicCounter::ValueType () const -{ - return _counter; -} - - -inline AtomicCounter::ValueType AtomicCounter::value() const -{ - return _counter; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix -{ - return InterlockedIncrement(&_counter); -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix -{ - ValueType result = InterlockedIncrement(&_counter); - return --result; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix -{ - return InterlockedDecrement(&_counter); -} - - -inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix -{ - ValueType result = InterlockedDecrement(&_counter); - return ++result; -} - - -inline bool AtomicCounter::operator ! () const -{ - return _counter == 0; -} - - -#elif POCO_OS == POCO_OS_MAC_OS_X -// -// Mac OS X -// -inline AtomicCounter::operator AtomicCounter::ValueType () const -{ - return _counter; -} - - -inline AtomicCounter::ValueType AtomicCounter::value() const -{ - return _counter; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix -{ - return OSAtomicIncrement32(&_counter); -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix -{ - ValueType result = OSAtomicIncrement32(&_counter); - return --result; -} - -inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix -{ - return OSAtomicDecrement32(&_counter); -} - - inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix { - ValueType result = OSAtomicDecrement32(&_counter); - return ++result; -} - - -inline bool AtomicCounter::operator ! () const -{ - return _counter == 0; -} - -#elif defined(POCO_HAVE_GCC_ATOMICS) -// -// GCC 4.1+ atomic builtins. -// -inline AtomicCounter::operator AtomicCounter::ValueType () const -{ - return _counter; -} - - -inline AtomicCounter::ValueType AtomicCounter::value() const -{ - return _counter; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix -{ - return __sync_add_and_fetch(&_counter, 1); -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix -{ - return __sync_fetch_and_add(&_counter, 1); -} - - -inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix -{ - return __sync_sub_and_fetch(&_counter, 1); -} - - -inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix -{ - return __sync_fetch_and_sub(&_counter, 1); -} - - -inline bool AtomicCounter::operator ! () const -{ - return _counter == 0; -} - - -#else -// -// Generic implementation based on FastMutex -// -inline AtomicCounter::operator AtomicCounter::ValueType () const -{ - ValueType result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = _counter.value; - } - return result; -} - - -inline AtomicCounter::ValueType AtomicCounter::value() const -{ - ValueType result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = _counter.value; - } - return result; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ () // prefix -{ - ValueType result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = ++_counter.value; - } - return result; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator ++ (int) // postfix -{ - ValueType result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = _counter.value++; - } - return result; -} - - -inline AtomicCounter::ValueType AtomicCounter::operator -- () // prefix -{ - ValueType result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = --_counter.value; - } - return result; + return _counter--; } - -inline AtomicCounter::ValueType AtomicCounter::operator -- (int) // postfix -{ - ValueType result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = _counter.value--; - } - return result; -} - inline bool AtomicCounter::operator ! () const { - bool result; - { - FastMutex::ScopedLock lock(_counter.mutex); - result = _counter.value == 0; - } - return result; + return _counter.load() == 0; } -#endif // POCO_OS - - } // namespace Poco diff --git a/contrib/libs/poco/Foundation/src/AtomicCounter.cpp b/contrib/libs/poco/Foundation/src/AtomicCounter.cpp index 72d503caca..32a4ef6a00 100644 --- a/contrib/libs/poco/Foundation/src/AtomicCounter.cpp +++ b/contrib/libs/poco/Foundation/src/AtomicCounter.cpp @@ -18,57 +18,12 @@ namespace Poco { -#if defined(POCO_HAVE_STD_ATOMICS) -// -// C++11 atomics -// AtomicCounter::AtomicCounter(): _counter(0) { } - -AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue): - _counter(initialValue) -{ -} - - -AtomicCounter::AtomicCounter(const AtomicCounter& counter): - _counter(counter.value()) -{ -} - - -AtomicCounter::~AtomicCounter() -{ -} - - -AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) -{ - _counter.store(counter._counter.load()); - return *this; -} - - -AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) -{ - _counter.store(value); - return *this; -} - - -#elif POCO_OS == POCO_OS_WINDOWS_NT -// -// Windows -// -AtomicCounter::AtomicCounter(): - _counter(0) -{ -} - AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue): _counter(initialValue) { @@ -88,144 +43,16 @@ AtomicCounter::~AtomicCounter() AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) { - InterlockedExchange(&_counter, counter.value()); - return *this; -} - - -AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) -{ - InterlockedExchange(&_counter, value); - return *this; -} - - -#elif POCO_OS == POCO_OS_MAC_OS_X -// -// Mac OS X -// -AtomicCounter::AtomicCounter(): - _counter(0) -{ -} - - -AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue): - _counter(initialValue) -{ -} - - -AtomicCounter::AtomicCounter(const AtomicCounter& counter): - _counter(counter.value()) -{ -} - - -AtomicCounter::~AtomicCounter() -{ -} - - -AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) -{ - _counter = counter.value(); - return *this; -} - - -AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) -{ - _counter = value; - return *this; -} - - -#elif defined(POCO_HAVE_GCC_ATOMICS) -// -// GCC 4.1+ atomic builtins. -// -AtomicCounter::AtomicCounter(): - _counter(0) -{ -} - - -AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue): - _counter(initialValue) -{ -} - - -AtomicCounter::AtomicCounter(const AtomicCounter& counter): - _counter(counter.value()) -{ -} - - -AtomicCounter::~AtomicCounter() -{ -} - - -AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) -{ - __sync_lock_test_and_set(&_counter, counter.value()); - return *this; -} - - -AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType valueType) -{ - __sync_lock_test_and_set(&_counter, valueType); + _counter.store(counter._counter.load()); return *this; } -#else -// -// Generic implementation based on FastMutex -// -AtomicCounter::AtomicCounter() -{ - _counter.value = 0; -} - - -AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue) -{ - _counter.value = initialValue; -} - - -AtomicCounter::AtomicCounter(const AtomicCounter& counter) -{ - _counter.value = counter.value(); -} - - -AtomicCounter::~AtomicCounter() -{ -} - - -AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) -{ - FastMutex::ScopedLock lock(_counter.mutex); - _counter.value = counter.value(); - return *this; -} - - AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) { - FastMutex::ScopedLock lock(_counter.mutex); - _counter.value = value; + _counter.store(value); return *this; } -#endif // POCO_OS - - } // namespace Poco |