2019-06-03 17:09:16 -04:00
|
|
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
2019-05-11 13:06:17 -04:00
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
|
2018-04-13 21:11:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-22 12:36:47 -04:00
|
|
|
#include "spdlog/details/log_msg_buffer.h"
|
2018-05-22 14:59:27 -04:00
|
|
|
#include "spdlog/details/mpmc_blocking_q.h"
|
2018-04-28 18:43:42 -04:00
|
|
|
#include "spdlog/details/os.h"
|
2018-04-13 21:11:03 -04:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2019-06-19 11:30:50 -04:00
|
|
|
#include <functional>
|
2018-04-13 21:11:03 -04:00
|
|
|
|
|
|
|
namespace spdlog {
|
2019-04-27 11:44:48 -04:00
|
|
|
class async_logger;
|
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
namespace details {
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
enum class async_msg_type
|
|
|
|
{
|
|
|
|
log,
|
|
|
|
flush,
|
2019-08-26 12:59:16 -04:00
|
|
|
terminate
|
2018-07-10 16:53:00 -04:00
|
|
|
};
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2019-08-22 12:36:47 -04:00
|
|
|
#include "spdlog/details/log_msg_buffer.h"
|
2018-05-26 11:48:39 -04:00
|
|
|
// Async msg to move to/from the queue
|
|
|
|
// Movable only. should never be copied
|
2019-08-22 12:38:00 -04:00
|
|
|
struct async_msg : log_msg_buffer
|
2018-07-10 16:53:00 -04:00
|
|
|
{
|
2019-08-22 12:38:00 -04:00
|
|
|
async_msg_type msg_type{async_msg_type::log};
|
2018-07-10 16:53:00 -04:00
|
|
|
async_logger_ptr worker_ptr;
|
|
|
|
|
2019-08-19 05:20:13 -04:00
|
|
|
async_msg() = default;
|
2018-07-10 16:53:00 -04:00
|
|
|
~async_msg() = default;
|
|
|
|
|
|
|
|
// should only be moved in or out of the queue..
|
|
|
|
async_msg(const async_msg &) = delete;
|
|
|
|
|
2018-07-21 16:48:07 -04:00
|
|
|
// support for vs2013 move
|
2018-07-10 16:53:00 -04:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER <= 1800
|
2019-08-19 04:31:33 -04:00
|
|
|
async_msg(async_msg &&other)
|
2019-08-22 12:36:47 -04:00
|
|
|
: log_msg_buffer(std::move(other))
|
|
|
|
, msg_type(other.msg_type)
|
2019-08-19 04:31:33 -04:00
|
|
|
, worker_ptr(std::move(other.worker_ptr))
|
2019-05-11 17:22:39 -04:00
|
|
|
{}
|
2018-07-10 16:51:20 -04:00
|
|
|
|
2019-08-19 04:31:33 -04:00
|
|
|
async_msg &operator=(async_msg &&other)
|
2018-07-10 13:20:55 -04:00
|
|
|
{
|
2019-08-22 12:38:00 -04:00
|
|
|
*static_cast<log_msg_buffer *>(this) = std::move(other);
|
2018-07-10 13:20:55 -04:00
|
|
|
msg_type = other.msg_type;
|
|
|
|
worker_ptr = std::move(other.worker_ptr);
|
2018-07-10 16:53:00 -04:00
|
|
|
return *this;
|
2018-05-26 11:48:39 -04:00
|
|
|
}
|
2018-07-10 16:53:00 -04:00
|
|
|
#else // (_MSC_VER) && _MSC_VER <= 1800
|
2019-08-22 17:16:44 -04:00
|
|
|
async_msg(async_msg &&) = default;
|
|
|
|
async_msg &operator=(async_msg &&) = default;
|
2018-07-10 16:51:20 -04:00
|
|
|
#endif
|
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
// construct from log_msg with given type
|
2019-08-22 12:57:59 -04:00
|
|
|
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
|
2019-08-22 12:45:32 -04:00
|
|
|
: log_msg_buffer{m}
|
|
|
|
, msg_type{the_type}
|
|
|
|
, worker_ptr{std::move(worker)}
|
2019-08-22 12:38:00 -04:00
|
|
|
{}
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
|
2019-08-22 12:45:32 -04:00
|
|
|
: log_msg_buffer{}
|
|
|
|
, msg_type{the_type}
|
|
|
|
, worker_ptr{std::move(worker)}
|
2019-05-11 17:22:39 -04:00
|
|
|
{}
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-24 17:33:11 -04:00
|
|
|
explicit async_msg(async_msg_type the_type)
|
2019-08-22 12:45:32 -04:00
|
|
|
: async_msg{nullptr, the_type}
|
2019-05-11 17:22:39 -04:00
|
|
|
{}
|
2018-07-10 16:53:00 -04:00
|
|
|
};
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
class thread_pool
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using item_type = async_msg;
|
2018-07-25 16:33:03 -04:00
|
|
|
using q_type = details::mpmc_blocking_queue<item_type>;
|
2018-07-10 16:53:00 -04:00
|
|
|
|
2019-06-19 11:30:50 -04:00
|
|
|
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
|
2019-05-08 10:16:56 -04:00
|
|
|
thread_pool(size_t q_max_items, size_t threads_n);
|
2019-06-19 11:30:50 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
// message all threads to terminate gracefully join them
|
2019-05-08 10:16:56 -04:00
|
|
|
~thread_pool();
|
|
|
|
|
2018-09-26 19:03:12 -04:00
|
|
|
thread_pool(const thread_pool &) = delete;
|
|
|
|
thread_pool &operator=(thread_pool &&) = delete;
|
2018-09-26 18:58:39 -04:00
|
|
|
|
2019-08-22 12:57:59 -04:00
|
|
|
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
|
2019-05-08 10:16:56 -04:00
|
|
|
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
|
|
|
size_t overrun_counter();
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
private:
|
|
|
|
q_type q_;
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
std::vector<std::thread> threads_;
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2019-05-08 10:16:56 -04:00
|
|
|
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
|
|
|
|
void worker_loop_();
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
// process next message in the queue
|
2018-07-21 16:48:07 -04:00
|
|
|
// return true if this thread should still be active (while no terminate msg
|
|
|
|
// was received)
|
2019-05-08 10:16:56 -04:00
|
|
|
bool process_next_msg_();
|
2018-07-10 16:53:00 -04:00
|
|
|
};
|
2018-05-26 11:48:39 -04:00
|
|
|
|
2018-07-10 16:53:00 -04:00
|
|
|
} // namespace details
|
2018-08-08 10:59:57 -04:00
|
|
|
} // namespace spdlog
|
2019-05-08 10:16:56 -04:00
|
|
|
|
2019-05-11 17:22:39 -04:00
|
|
|
#ifdef SPDLOG_HEADER_ONLY
|
2019-05-11 06:19:53 -04:00
|
|
|
#include "thread_pool-inl.h"
|
2019-08-22 20:30:56 -04:00
|
|
|
#endif
|