Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context added TSA #55278

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/base/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
# define TSA_ACQUIRE_SHARED(...) __attribute__((acquire_shared_capability(__VA_ARGS__))) /// function acquires a shared capability, but does not release it
# define TSA_TRY_ACQUIRE_SHARED(...) __attribute__((try_acquire_shared_capability(__VA_ARGS__))) /// function tries to acquire a shared capability and returns a boolean value indicating success or failure
# define TSA_RELEASE_SHARED(...) __attribute__((release_shared_capability(__VA_ARGS__))) /// function releases the given shared capability
# define TSA_SCOPED_LOCKABLE __attribute__((scoped_lockable)) /// object of a class has scoped lockable capability
kitaisreal marked this conversation as resolved.
Show resolved Hide resolved

/// Macros for suppressing TSA warnings for specific reads/writes (instead of suppressing it for the whole function)
/// They use a lambda function to apply function attribute to a single statement. This enable us to suppress warnings locally instead of
Expand Down Expand Up @@ -177,6 +178,7 @@
# define TSA_ACQUIRE_SHARED(...)
# define TSA_TRY_ACQUIRE_SHARED(...)
# define TSA_RELEASE_SHARED(...)
# define TSA_SCOPED_LOCKABLE

# define TSA_SUPPRESS_WARNING_FOR_READ(x) (x)
# define TSA_SUPPRESS_WARNING_FOR_WRITE(x) (x)
Expand Down
25 changes: 25 additions & 0 deletions src/Common/SharedLockGuard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <base/defines.h>

namespace DB
{

/** SharedLockGuard provide RAII-style locking mechanism for acquiring shared ownership of the implementation
* of the SharedLockable concept (for example std::shared_mutex) supplied as the constructor argument.
* On construction it acquires shared ownership using `lock_shared` method.
* On desruction shared ownership is released using `unlock_shared` method.
*/
template <typename Mutex>
kitaisreal marked this conversation as resolved.
Show resolved Hide resolved
class TSA_SCOPED_LOCKABLE SharedLockGuard
kitaisreal marked this conversation as resolved.
Show resolved Hide resolved
{
public:
explicit SharedLockGuard(Mutex & mutex_) TSA_ACQUIRE_SHARED(mutex_) : mutex(mutex_) { mutex_.lock_shared(); }

~SharedLockGuard() TSA_RELEASE() { mutex.unlock_shared(); }

private:
Mutex & mutex;
};

}
112 changes: 112 additions & 0 deletions src/Common/SharedMutexHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#pragma once

#include <base/types.h>
#include <base/defines.h>
#include <Common/SharedMutex.h>

namespace DB
{

/** SharedMutexHelper class allows to inject specific logic when underlying shared mutex is acquired
* and released.
*
* Example:
*
* class ProfileSharedMutex : public SharedMutexHelper<ProfileSharedMutex>
* {
* public:
* size_t getLockCount() const { return lock_count; }
*
* size_t getSharedLockCount() const { return shared_lock_count; }
*
* private:
* using Base = SharedMutexHelper<ProfileSharedMutex, SharedMutex>;
* friend class SharedMutexHelper<ProfileSharedMutex, SharedMutex>;
*
* void lockImpl()
* {
* ++lock_count;
* Base::lockImpl();
* }
*
* void lockSharedImpl()
* {
* ++shared_lock_count;
* Base::lockSharedImpl();
* }
*
* std::atomic<size_t> lock_count = 0;
* std::atomic<size_t> shared_lock_count = 0;
* };
*/
template <typename Derived, typename MutexType = SharedMutex>
class TSA_CAPABILITY("SharedMutexHelper") SharedMutexHelper
{
public:
// Exclusive ownership
void lock() TSA_ACQUIRE() /// NOLINT
{
static_cast<Derived *>(this)->lockImpl();
}

bool try_lock() TSA_TRY_ACQUIRE(true) /// NOLINT
{
static_cast<Derived *>(this)->tryLockImpl();
}

void unlock() TSA_RELEASE() /// NOLINT
{
static_cast<Derived *>(this)->unlockImpl();
}

// Shared ownership
void lock_shared() TSA_ACQUIRE_SHARED() /// NOLINT
{
static_cast<Derived *>(this)->lockSharedImpl();
}

bool try_lock_shared() TSA_TRY_ACQUIRE_SHARED(true) /// NOLINT
{
static_cast<Derived *>(this)->tryLockSharedImpl();
}

void unlock_shared() TSA_RELEASE_SHARED() /// NOLINT
{
static_cast<Derived *>(this)->unlockSharedImpl();
}

protected:
void lockImpl() TSA_NO_THREAD_SAFETY_ANALYSIS
{
mutex.lock();
}

void tryLockImpl() TSA_NO_THREAD_SAFETY_ANALYSIS
{
mutex.try_lock();
}

void unlockImpl() TSA_NO_THREAD_SAFETY_ANALYSIS
{
mutex.unlock();
}

void lockSharedImpl() TSA_NO_THREAD_SAFETY_ANALYSIS
{
mutex.lock_shared();
}

void tryLockSharedImpl() TSA_NO_THREAD_SAFETY_ANALYSIS
{
mutex.try_lock_shared();
}

void unlockSharedImpl() TSA_NO_THREAD_SAFETY_ANALYSIS
{
mutex.unlock_shared();
}

MutexType mutex;
};

}