spdlog/include/spdlog/details/thread_pool.h

122 lines
3.3 KiB
C
Raw Normal View History

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