Files
spdlog/include/spdlog/details/thread_pool.h

154 lines
4.3 KiB
C
Raw Normal View History

2019-06-04 00:09:16 +03:00
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2019-05-11 20:06:17 +03:00
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2018-04-14 04:11:03 +03:00
#pragma once
2018-04-29 01:43:42 +03:00
#include "spdlog/details/log_msg.h"
2018-05-22 21:59:27 +03:00
#include "spdlog/details/mpmc_blocking_q.h"
2018-04-29 01:43:42 +03:00
#include "spdlog/details/os.h"
2018-04-14 04:11:03 +03:00
#include <chrono>
#include <memory>
#include <thread>
#include <vector>
2019-06-19 18:30:50 +03:00
#include <functional>
2018-04-14 04:11:03 +03:00
namespace spdlog {
2019-04-27 18:44:48 +03:00
class async_logger;
2018-07-10 23:53:00 +03:00
namespace details {
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
enum class async_msg_type
{
log,
flush,
terminate
};
2018-05-26 18:48:39 +03:00
// Async msg to move to/from the queue
// Movable only. should never be copied
2018-07-10 23:53:00 +03: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;
source_loc source;
2018-07-10 23:53:00 +03: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-21 23:48:07 +03:00
// support for vs2013 move
2018-07-10 23:53:00 +03:00
#if defined(_MSC_VER) && _MSC_VER <= 1800
async_msg(async_msg &&other) SPDLOG_NOEXCEPT : msg_type(other.msg_type),
2018-07-10 20:20:55 +03:00
level(other.level),
time(other.time),
thread_id(other.thread_id),
raw(move(other.raw)),
2018-07-10 20:20:55 +03:00
msg_id(other.msg_id),
source(other.source),
2018-07-10 20:20:55 +03:00
worker_ptr(std::move(other.worker_ptr))
{}
2018-07-10 20:20:55 +03: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-10 20:20:55 +03:00
msg_id = other.msg_id;
source = other.source;
2018-07-10 20:20:55 +03:00
worker_ptr = std::move(other.worker_ptr);
2018-07-10 23:53:00 +03:00
return *this;
2018-05-26 18:48:39 +03:00
}
2018-07-10 23:53:00 +03:00
#else // (_MSC_VER) && _MSC_VER <= 1800
2018-09-03 19:08:57 +03:00
async_msg(async_msg &&) = default;
async_msg &operator=(async_msg &&) = default;
#endif
2018-07-10 23:53:00 +03: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-10 23:53:00 +03:00
: msg_type(the_type)
, level(m.level)
, time(m.time)
, thread_id(m.thread_id)
, source(m.source)
, worker_ptr(std::move(worker))
2018-07-10 23:53:00 +03:00
{
raw.append(m.payload.data(), m.payload.data() + m.payload.size());
2018-07-10 23:53:00 +03:00
}
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
2018-10-30 00:54:22 +02:00
: msg_type(the_type)
, level(level::off)
, time()
, thread_id(0)
, source()
2018-10-30 00:54:22 +02:00
, worker_ptr(std::move(worker))
{}
2018-05-26 18:48:39 +03:00
2018-07-25 00:33:11 +03:00
explicit async_msg(async_msg_type the_type)
2018-10-30 00:54:22 +02:00
: async_msg(nullptr, the_type)
{}
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
// copy into log_msg
log_msg to_log_msg()
2018-07-10 23:53:00 +03:00
{
log_msg msg(string_view_t(worker_ptr->name()), level, string_view_t(raw.data(), raw.size()));
2018-07-10 23:53:00 +03:00
msg.time = time;
msg.thread_id = thread_id;
msg.source = source;
2018-07-10 23:53:00 +03:00
msg.color_range_start = 0;
msg.color_range_end = 0;
return msg;
2018-07-10 23:53:00 +03:00
}
};
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
class thread_pool
{
public:
using item_type = async_msg;
2018-07-25 23:33:03 +03:00
using q_type = details::mpmc_blocking_queue<item_type>;
2018-07-10 23:53:00 +03:00
2019-06-19 18:30:50 +03:00
thread_pool(size_t q_max_items, size_t threads_n, std::function<void()> on_thread_start);
2019-05-08 17:16:56 +03:00
thread_pool(size_t q_max_items, size_t threads_n);
2019-06-19 18:30:50 +03:00
2018-07-10 23:53:00 +03:00
// message all threads to terminate gracefully join them
2019-05-08 17:16:56 +03:00
~thread_pool();
2018-09-27 02:03:12 +03:00
thread_pool(const thread_pool &) = delete;
thread_pool &operator=(thread_pool &&) = delete;
2018-09-27 01:58:39 +03:00
2019-05-08 17:16:56 +03: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 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
private:
q_type q_;
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
std::vector<std::thread> threads_;
2018-05-26 18:48:39 +03:00
2019-05-08 17:16:56 +03:00
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy);
void worker_loop_();
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
// process next message in the queue
2018-07-21 23:48:07 +03:00
// return true if this thread should still be active (while no terminate msg
// was received)
2019-05-08 17:16:56 +03:00
bool process_next_msg_();
2018-07-10 23:53:00 +03:00
};
2018-05-26 18:48:39 +03:00
2018-07-10 23:53:00 +03:00
} // namespace details
} // namespace spdlog
2019-05-08 17:16:56 +03:00
#ifdef SPDLOG_HEADER_ONLY
2019-05-11 13:19:53 +03:00
#include "thread_pool-inl.h"
2019-05-08 17:16:56 +03:00
#endif