ClickHouse/src/Interpreters/Cache/Guards.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
1.6 KiB
C++
Raw Normal View History

#pragma once
#include <mutex>
#include <Interpreters/Cache/FileCache_fwd.h>
#include <boost/noncopyable.hpp>
#include <map>
namespace DB
{
2023-01-05 21:39:17 +00:00
/**
* Priority of locking:
* Cache priority queue guard > key prefix guard > file segment guard.
*/
/**
2023-01-27 12:48:46 +00:00
* Cache priority queue guard.
2023-01-05 21:39:17 +00:00
*/
2023-01-27 12:48:46 +00:00
struct CacheGuard
{
struct Lock
{
2023-01-27 12:48:46 +00:00
explicit Lock(CacheGuard & guard) : lock(guard.mutex) {}
std::unique_lock<std::mutex> lock;
};
2023-01-27 12:48:46 +00:00
using LockPtr = std::shared_ptr<Lock>;
std::mutex mutex;
2023-01-27 12:48:46 +00:00
LockPtr lock() { return std::make_shared<Lock>(*this); }
2023-01-27 12:48:46 +00:00
CacheGuard() = default;
};
2023-01-20 15:30:25 +00:00
/**
* Guard for cache metadata.
*/
struct CacheMetadataGuard
{
struct Lock
{
explicit Lock(CacheMetadataGuard & guard) : lock(guard.mutex) {}
std::unique_lock<std::mutex> lock;
};
using LockPtr = std::shared_ptr<Lock>;
std::mutex mutex;
Lock lock() { return Lock(*this); }
CacheMetadataGuard() = default;
};
2023-01-05 21:39:17 +00:00
/**
2023-01-27 12:48:46 +00:00
* Guard for a set of keys.
* One guard per key prefix (first three digits of the path hash).
2023-01-05 21:39:17 +00:00
*/
2023-01-27 12:48:46 +00:00
struct KeyGuard
{
struct Lock
{
2023-01-27 12:48:46 +00:00
explicit Lock(KeyGuard & guard) : lock(guard.mutex) {}
std::unique_lock<std::mutex> lock;
};
std::mutex mutex;
2023-01-27 12:48:46 +00:00
Lock lock() { return Lock(*this); }
2023-01-27 12:48:46 +00:00
KeyGuard() = default;
};
2023-01-27 12:48:46 +00:00
using KeyGuardPtr = std::shared_ptr<KeyGuard>;
/**
* Guard for a file segment.
*/
struct FileSegmentGuard
{
struct Lock
{
explicit Lock(FileSegmentGuard & guard) : lock(guard.mutex) {}
std::unique_lock<std::mutex> lock;
};
std::mutex mutex;
Lock lock() { return Lock(*this); }
};
}