spdlog/include/spdlog/details/thread_pool.h

156 lines
4.2 KiB
C
Raw Normal View History

2018-04-14 09:11:03 +08:00
#pragma once
#include "spdlog/details/fmt_helper.h"
2018-04-29 06:43:42 +08:00
#include "spdlog/details/log_msg.h"
2018-05-23 02:59:27 +08:00
#include "spdlog/details/mpmc_blocking_q.h"
2018-04-29 06:43:42 +08:00
#include "spdlog/details/os.h"
2018-04-14 09:11:03 +08:00
#include <chrono>
#include <memory>
#include <thread>
#include <vector>
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,
terminate
};
2018-05-26 23:48:39 +08:00
// Async msg to move to/from the queue
// Movable only. should never be copied
2018-07-11 04:53:00 +08:00
struct async_msg
{
async_msg_type msg_type;
level::level_enum level;
log_clock::time_point time;
size_t thread_id;
fmt::basic_memory_buffer<char, 176> raw;
size_t msg_id;
source_loc source;
2018-07-11 04:53:00 +08:00
async_logger_ptr worker_ptr;
async_msg() = default;
~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) SPDLOG_NOEXCEPT : msg_type(other.msg_type),
2018-07-11 01:20:55 +08:00
level(other.level),
time(other.time),
thread_id(other.thread_id),
raw(move(other.raw)),
2018-07-11 01:20:55 +08:00
msg_id(other.msg_id),
source(other.source),
2018-07-11 01:20:55 +08:00
worker_ptr(std::move(other.worker_ptr))
{
}
2018-07-11 01:20:55 +08:00
async_msg &operator=(async_msg &&other) SPDLOG_NOEXCEPT
{
msg_type = other.msg_type;
level = other.level;
time = other.time;
thread_id = other.thread_id;
raw = std::move(other.raw);
2018-07-11 01:20:55 +08:00
msg_id = other.msg_id;
source = other.source;
2018-07-11 01:20:55 +08:00
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
2018-09-04 00:08:57 +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
2018-10-31 11:01:28 +08:00
async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &m)
2018-07-11 04:53:00 +08:00
: msg_type(the_type)
, level(m.level)
, time(m.time)
, thread_id(m.thread_id)
, msg_id(m.msg_id)
, source(m.source)
, worker_ptr(std::move(worker))
2018-07-11 04:53:00 +08:00
{
fmt_helper::append_string_view(m.payload, raw);
2018-07-11 04:53: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)
2018-10-30 06:54:22 +08:00
: msg_type(the_type)
, level(level::off)
, time()
, thread_id(0)
, msg_id(0)
, source()
2018-10-30 06:54:22 +08:00
, worker_ptr(std::move(worker))
2018-07-11 04:53:00 +08:00
{
}
2018-05-26 23:48:39 +08:00
2018-07-25 05:33:11 +08:00
explicit async_msg(async_msg_type the_type)
2018-10-30 06:54:22 +08:00
: async_msg(nullptr, the_type)
2018-07-11 04:53:00 +08:00
{
}
2018-05-26 23:48:39 +08:00
2018-07-11 04:53:00 +08:00
// copy into log_msg
log_msg to_log_msg()
2018-07-11 04:53:00 +08:00
{
log_msg msg(&worker_ptr->name(), level, string_view_t(raw.data(), raw.size()));
2018-07-11 04:53:00 +08:00
msg.time = time;
msg.thread_id = thread_id;
msg.msg_id = msg_id;
msg.source = source;
2018-07-11 04:53:00 +08:00
msg.color_range_start = 0;
msg.color_range_end = 0;
return 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
class thread_pool
{
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
2019-05-08 22:16:56 +08:00
thread_pool(size_t q_max_items, size_t threads_n);
2018-07-11 04:53:00 +08:00
// message all threads to terminate gracefully 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
2019-05-08 22:16:56 +08:00
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &msg, async_overflow_policy overflow_policy);
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
size_t overrun_counter();
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
#ifndef SPDLOG_STATIC_LIB
2019-05-11 18:19:53 +08:00
#include "thread_pool-inl.h"
2019-05-08 22:16:56 +08:00
#endif