2017-08-31 21:11:25 +00:00
|
|
|
#pragma once
|
2017-09-01 15:05:23 +00:00
|
|
|
#include <boost/core/noncopyable.hpp>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <list>
|
2017-09-01 15:05:23 +00:00
|
|
|
#include <vector>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2017-09-04 12:49:49 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <map>
|
2017-09-04 15:14:05 +00:00
|
|
|
#include <string>
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-09-01 18:22:03 +00:00
|
|
|
class RWLockFIFO;
|
2017-08-31 21:11:25 +00:00
|
|
|
using RWLockFIFOPtr = std::shared_ptr<RWLockFIFO>;
|
|
|
|
|
|
|
|
|
|
|
|
/// Implements shared lock with FIFO service
|
2017-09-04 12:49:49 +00:00
|
|
|
/// You could call it recursively (several calls from the same thread) in Read mode
|
2017-08-31 21:11:25 +00:00
|
|
|
class RWLockFIFO : public std::enable_shared_from_this<RWLockFIFO>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
Read,
|
|
|
|
Write
|
|
|
|
};
|
|
|
|
|
2018-06-04 15:44:29 +00:00
|
|
|
private:
|
2017-08-31 21:11:25 +00:00
|
|
|
/// Client is that who wants to acquire the lock.
|
|
|
|
struct Client
|
|
|
|
{
|
2017-09-01 15:05:23 +00:00
|
|
|
explicit Client(const std::string & info = {}) : info{info} {}
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
bool isStarted() { return start_time != 0; }
|
|
|
|
|
2017-09-26 17:19:16 +00:00
|
|
|
/// TODO: delete extra info below if there is no need fot it already.
|
2017-08-31 21:11:25 +00:00
|
|
|
std::string info;
|
2017-09-01 15:05:23 +00:00
|
|
|
int thread_number = 0;
|
|
|
|
std::time_t enqueue_time = 0;
|
|
|
|
std::time_t start_time = 0;
|
2018-09-01 20:28:46 +00:00
|
|
|
Type type = Read;
|
2017-08-31 21:11:25 +00:00
|
|
|
};
|
|
|
|
|
2018-06-04 15:44:29 +00:00
|
|
|
public:
|
|
|
|
static RWLockFIFOPtr create()
|
|
|
|
{
|
|
|
|
return RWLockFIFOPtr(new RWLockFIFO);
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
|
|
|
|
/// Just use LockHandler::reset() to release the lock
|
2017-08-31 21:11:25 +00:00
|
|
|
class LockHandlerImpl;
|
2017-09-26 17:19:16 +00:00
|
|
|
friend class LockHandlerImpl;
|
2017-09-04 12:49:49 +00:00
|
|
|
using LockHandler = std::shared_ptr<LockHandlerImpl>;
|
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
/// Waits in the queue and returns appropriate lock
|
2017-09-01 15:05:23 +00:00
|
|
|
LockHandler getLock(Type type, Client client = Client{});
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
LockHandler getLock(Type type, const std::string & who)
|
|
|
|
{
|
|
|
|
return getLock(type, Client(who));
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:05:23 +00:00
|
|
|
using Clients = std::vector<Client>;
|
|
|
|
|
|
|
|
/// Returns list of executing and waiting clients
|
|
|
|
Clients getClientsInTheQueue() const;
|
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
private:
|
|
|
|
RWLockFIFO() = default;
|
|
|
|
|
|
|
|
struct Group;
|
|
|
|
using GroupsContainer = std::list<Group>;
|
|
|
|
using ClientsContainer = std::list<Client>;
|
2017-09-04 12:49:49 +00:00
|
|
|
using ThreadToHandler = std::map<std::thread::id, std::weak_ptr<LockHandlerImpl>>;
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
/// Group of clients that should be executed concurrently
|
|
|
|
/// i.e. a group could contain several readers, but only one writer
|
|
|
|
struct Group
|
|
|
|
{
|
|
|
|
const Type type;
|
|
|
|
ClientsContainer clients;
|
|
|
|
|
|
|
|
std::condition_variable cv; /// all clients of the group wait group condvar
|
|
|
|
|
|
|
|
explicit Group(Type type) : type{type} {}
|
|
|
|
};
|
|
|
|
|
2017-09-01 15:05:23 +00:00
|
|
|
mutable std::mutex mutex;
|
2017-08-31 21:11:25 +00:00
|
|
|
GroupsContainer queue;
|
2017-09-04 12:49:49 +00:00
|
|
|
ThreadToHandler thread_to_handler;
|
2017-08-31 21:11:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|