ClickHouse/src/Common/SharedLockGuard.h

28 lines
804 B
C++
Raw Normal View History

2023-10-06 17:10:22 +00:00
#pragma once
#include <base/defines.h>
namespace DB
{
2023-10-24 16:39:25 +00:00
/** SharedLockGuard provide RAII-style locking mechanism for acquiring shared ownership of the implementation
* of the SharedLockable concept (for example std::shared_mutex or ContextSharedMutex) supplied as the
* constructor argument. Think of it as std::lock_guard which locks shared.
*
2023-10-24 16:39:25 +00:00
* On construction it acquires shared ownership using `lock_shared` method.
* On destruction shared ownership is released using `unlock_shared` method.
2023-10-24 16:39:25 +00:00
*/
2023-10-06 17:10:22 +00:00
template <typename Mutex>
2023-10-18 10:44:02 +00:00
class TSA_SCOPED_LOCKABLE SharedLockGuard
2023-10-06 17:10:22 +00:00
{
public:
2023-10-24 16:39:25 +00:00
explicit SharedLockGuard(Mutex & mutex_) TSA_ACQUIRE_SHARED(mutex_) : mutex(mutex_) { mutex_.lock_shared(); }
2023-10-06 17:10:22 +00:00
2023-10-24 16:39:25 +00:00
~SharedLockGuard() TSA_RELEASE() { mutex.unlock_shared(); }
2023-10-06 17:10:22 +00:00
private:
Mutex & mutex;
};
}