2017-01-25 18:40:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-07-28 17:34:02 +00:00
|
|
|
#include <shared_mutex>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/Stopwatch.h>
|
2017-01-25 18:40:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-28 12:34:39 +00:00
|
|
|
|
|
|
|
class ProfilingScopedWriteUnlocker;
|
|
|
|
|
2017-01-25 18:40:44 +00:00
|
|
|
class ProfilingScopedWriteRWLock
|
|
|
|
{
|
|
|
|
public:
|
2020-02-28 12:34:39 +00:00
|
|
|
friend class ProfilingScopedWriteUnlocker;
|
|
|
|
|
|
|
|
ProfilingScopedWriteRWLock(std::shared_mutex & rwl_, ProfileEvents::Event event_) :
|
2017-04-01 07:20:54 +00:00
|
|
|
watch(),
|
2020-02-28 12:34:39 +00:00
|
|
|
event(event_),
|
|
|
|
scoped_write_lock(rwl_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
ProfileEvents::increment(event, watch.elapsed());
|
|
|
|
}
|
2017-01-25 18:40:44 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Stopwatch watch;
|
2020-02-28 12:34:39 +00:00
|
|
|
ProfileEvents::Event event;
|
2017-07-28 17:34:02 +00:00
|
|
|
std::unique_lock<std::shared_mutex> scoped_write_lock;
|
2017-01-25 18:40:44 +00:00
|
|
|
};
|
|
|
|
|
2020-02-28 12:34:39 +00:00
|
|
|
/// Inversed RAII
|
|
|
|
/// Used to unlock current writelock for various purposes.
|
|
|
|
class ProfilingScopedWriteUnlocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProfilingScopedWriteUnlocker() = delete;
|
|
|
|
|
|
|
|
ProfilingScopedWriteUnlocker(ProfilingScopedWriteRWLock & parent_lock_) : parent_lock(parent_lock_)
|
|
|
|
{
|
|
|
|
parent_lock.scoped_write_lock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProfilingScopedWriteUnlocker()
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
parent_lock.scoped_write_lock.lock();
|
|
|
|
ProfileEvents::increment(parent_lock.event, watch.elapsed());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProfilingScopedWriteRWLock & parent_lock;
|
|
|
|
};
|
|
|
|
|
2017-01-25 18:40:44 +00:00
|
|
|
class ProfilingScopedReadRWLock
|
|
|
|
{
|
|
|
|
public:
|
2017-07-28 17:34:02 +00:00
|
|
|
ProfilingScopedReadRWLock(std::shared_mutex & rwl, ProfileEvents::Event event) :
|
2017-04-01 07:20:54 +00:00
|
|
|
watch(),
|
|
|
|
scoped_read_lock(rwl)
|
|
|
|
{
|
|
|
|
ProfileEvents::increment(event, watch.elapsed());
|
|
|
|
}
|
2017-01-25 18:40:44 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Stopwatch watch;
|
2017-07-28 17:34:02 +00:00
|
|
|
std::shared_lock<std::shared_mutex> scoped_read_lock;
|
2017-01-25 18:40:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|