2018-06-18 01:33:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-07-15 11:16:00 +00:00
|
|
|
#if defined(OS_LINUX) || defined(__FreeBSD__)
|
2018-06-18 01:33:34 +00:00
|
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <future>
|
|
|
|
#include <mutex>
|
|
|
|
#include <map>
|
|
|
|
#include <IO/AIO.h>
|
2019-01-14 19:22:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2018-06-18 01:33:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-08-22 03:24:05 +00:00
|
|
|
class AIOContextPool : private boost::noncopyable
|
2018-06-18 01:33:34 +00:00
|
|
|
{
|
|
|
|
static const auto max_concurrent_events = 128;
|
|
|
|
static const auto timeout_sec = 1;
|
|
|
|
|
|
|
|
AIOContext aio_context{max_concurrent_events};
|
|
|
|
|
|
|
|
using ID = size_t;
|
|
|
|
using BytesRead = ssize_t;
|
|
|
|
|
|
|
|
/// Autoincremental id used to identify completed requests
|
2018-08-26 02:13:41 +00:00
|
|
|
ID next_id{};
|
2018-06-18 01:33:34 +00:00
|
|
|
mutable std::mutex mutex;
|
|
|
|
mutable std::condition_variable have_resources;
|
|
|
|
std::map<ID, std::promise<BytesRead>> promises;
|
|
|
|
|
|
|
|
std::atomic<bool> cancelled{false};
|
2019-01-14 19:22:09 +00:00
|
|
|
ThreadFromGlobalPool io_completion_monitor{&AIOContextPool::doMonitor, this};
|
2018-06-18 01:33:34 +00:00
|
|
|
|
|
|
|
~AIOContextPool();
|
|
|
|
|
|
|
|
void doMonitor();
|
|
|
|
void waitForCompletion();
|
2020-04-22 05:39:31 +00:00
|
|
|
int getCompletionEvents(io_event events[], const int max_events) const;
|
2018-06-18 01:33:34 +00:00
|
|
|
void fulfillPromises(const io_event events[], const int num_events);
|
|
|
|
void notifyProducers(const int num_producers) const;
|
|
|
|
void reportExceptionToAnyProducer();
|
|
|
|
|
|
|
|
public:
|
2019-08-22 03:24:05 +00:00
|
|
|
static AIOContextPool & instance();
|
|
|
|
|
2018-06-18 01:33:34 +00:00
|
|
|
/// Request AIO read operation for iocb, returns a future with number of bytes read
|
|
|
|
std::future<BytesRead> post(struct iocb & iocb);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|