add TSA support

This commit is contained in:
serxa 2023-01-09 16:46:07 +00:00
parent 534db794c1
commit ae56ac1b56
2 changed files with 16 additions and 14 deletions

View File

@ -4,6 +4,7 @@
#include <Common/CancelToken.h> #include <Common/CancelToken.h>
#include <base/types.h> #include <base/types.h>
#include <base/defines.h>
#include <atomic> #include <atomic>
#include <shared_mutex> // for std::unique_lock and std::shared_lock #include <shared_mutex> // for std::unique_lock and std::shared_lock
@ -13,7 +14,7 @@ namespace DB
// Reimplementation of `std::shared_mutex` that can interoperate with thread cancelation via `CancelToken::signal()`. // Reimplementation of `std::shared_mutex` that can interoperate with thread cancelation via `CancelToken::signal()`.
// It has cancelation point on waiting during `lock()` and `shared_lock()`. // It has cancelation point on waiting during `lock()` and `shared_lock()`.
// NOTE: It has NO cancelation points on fast code path, when locking does not require waiting. // NOTE: It has NO cancelation points on fast code path, when locking does not require waiting.
class CancelableSharedMutex class TSA_CAPABILITY("CancelableSharedMutex") CancelableSharedMutex
{ {
public: public:
CancelableSharedMutex(); CancelableSharedMutex();
@ -22,14 +23,14 @@ public:
CancelableSharedMutex & operator=(const CancelableSharedMutex &) = delete; CancelableSharedMutex & operator=(const CancelableSharedMutex &) = delete;
// Exclusive ownership // Exclusive ownership
void lock(); void lock() TSA_ACQUIRE();
bool try_lock(); bool try_lock() TSA_TRY_ACQUIRE(true);
void unlock(); void unlock() TSA_RELEASE();
// Shared ownership // Shared ownership
void lock_shared(); void lock_shared() TSA_ACQUIRE_SHARED();
bool try_lock_shared(); bool try_lock_shared() TSA_TRY_ACQUIRE_SHARED(true);
void unlock_shared(); void unlock_shared() TSA_RELEASE_SHARED();
private: private:
// State 64-bits layout: // State 64-bits layout:

View File

@ -3,6 +3,7 @@
#ifdef OS_LINUX /// Because of futex #ifdef OS_LINUX /// Because of futex
#include <base/types.h> #include <base/types.h>
#include <base/defines.h>
#include <atomic> #include <atomic>
#include <shared_mutex> // for std::unique_lock and std::shared_lock #include <shared_mutex> // for std::unique_lock and std::shared_lock
@ -10,7 +11,7 @@ namespace DB
{ {
// Faster implementation of `std::shared_mutex` based on a pair of futexes // Faster implementation of `std::shared_mutex` based on a pair of futexes
class SharedMutex class TSA_CAPABILITY("SharedMutex") SharedMutex
{ {
public: public:
SharedMutex(); SharedMutex();
@ -19,14 +20,14 @@ public:
SharedMutex & operator=(const SharedMutex &) = delete; SharedMutex & operator=(const SharedMutex &) = delete;
// Exclusive ownership // Exclusive ownership
void lock(); void lock() TSA_ACQUIRE();
bool try_lock(); bool try_lock() TSA_TRY_ACQUIRE(true);
void unlock(); void unlock() TSA_RELEASE();
// Shared ownership // Shared ownership
void lock_shared(); void lock_shared() TSA_ACQUIRE_SHARED();
bool try_lock_shared(); bool try_lock_shared() TSA_TRY_ACQUIRE_SHARED(true);
void unlock_shared(); void unlock_shared() TSA_RELEASE_SHARED();
private: private:
static constexpr UInt64 readers = (1ull << 32ull) - 1ull; // Lower 32 bits of state static constexpr UInt64 readers = (1ull << 32ull) - 1ull; // Lower 32 bits of state