spdlog/include/spdlog/details/thread_pool.h

229 lines
6.1 KiB
C
Raw Normal View History

2018-04-13 21:11:03 -04:00
#pragma once
2018-04-28 18:43:42 -04:00
#include "spdlog/details/log_msg.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>
namespace spdlog {
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,
terminate
};
2018-05-26 11:48:39 -04:00
// Async msg to move to/from the queue
// Movable only. should never be copied
2018-07-10 16:53:00 -04: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;
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 16:48:07 -04:00
// support for vs2013 move
2018-07-10 16:53:00 -04:00
#if defined(_MSC_VER) && _MSC_VER <= 1800
async_msg(async_msg &&other) SPDLOG_NOEXCEPT : msg_type(other.msg_type),
2018-07-10 13:20:55 -04:00
level(other.level),
time(other.time),
thread_id(other.thread_id),
raw(move(other.raw)),
2018-07-10 13:20:55 -04:00
msg_id(other.msg_id),
worker_ptr(std::move(other.worker_ptr))
{
}
2018-07-10 13:20:55 -04: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 13:20:55 -04:00
msg_id = other.msg_id;
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
2018-09-03 12:08:57 -04:00
async_msg(async_msg &&) = default;
async_msg &operator=(async_msg &&) = default;
#endif
2018-07-10 16:53:00 -04:00
// construct from log_msg with given type
2018-09-26 18:14:35 -04:00
async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m)
2018-07-10 16:53:00 -04:00
: msg_type(the_type)
, level(m.level)
, time(m.time)
, thread_id(m.thread_id)
, msg_id(m.msg_id)
, worker_ptr(std::forward<async_logger_ptr>(worker))
{
fmt_helper::append_buf(m.raw, raw);
}
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)
: async_msg(std::forward<async_logger_ptr>(worker), the_type, details::log_msg())
{
}
2018-05-26 11:48:39 -04:00
2018-07-24 17:33:11 -04:00
explicit async_msg(async_msg_type the_type)
2018-07-10 16:53:00 -04:00
: async_msg(nullptr, the_type, details::log_msg())
{
}
2018-05-26 11:48:39 -04:00
2018-07-10 16:53:00 -04:00
// copy into log_msg
void to_log_msg(log_msg &msg)
{
msg.logger_name = &worker_ptr->name();
msg.level = level;
msg.time = time;
msg.thread_id = thread_id;
fmt_helper::append_buf(raw, msg.raw);
msg.msg_id = msg_id;
msg.color_range_start = 0;
msg.color_range_end = 0;
}
};
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
thread_pool(size_t q_max_items, size_t threads_n)
: q_(q_max_items)
{
2018-07-21 16:48:07 -04:00
// std::cout << "thread_pool() q_size_bytes: " << q_size_bytes <<
// "\tthreads_n: " << threads_n << std::endl;
2018-07-10 16:53:00 -04:00
if (threads_n == 0 || threads_n > 1000)
{
2018-07-21 16:48:07 -04:00
throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
"range is 1-1000)");
2018-07-10 16:53:00 -04:00
}
for (size_t i = 0; i < threads_n; i++)
2018-05-26 11:48:39 -04:00
{
threads_.emplace_back(&thread_pool::worker_loop_, this);
2018-07-10 16:53:00 -04:00
}
}
2018-05-26 11:48:39 -04:00
2018-07-10 16:53:00 -04:00
// message all threads to terminate gracefully join them
~thread_pool()
{
try
{
for (size_t i = 0; i < threads_.size(); i++)
2018-05-26 11:48:39 -04:00
{
2018-07-10 16:53:00 -04:00
post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);
2018-05-26 11:48:39 -04:00
}
2018-07-10 16:53:00 -04:00
for (auto &t : threads_)
2018-05-26 11:48:39 -04:00
{
2018-07-10 16:53:00 -04:00
t.join();
2018-05-26 11:48:39 -04:00
}
2018-07-10 16:53:00 -04:00
}
catch (...)
{
}
}
2018-05-26 11:48:39 -04:00
2018-09-26 18:58:39 -04:00
thread_pool(const thread_pool&) = delete;
thread_pool &operator=(thread_pool &&) = delete ;
2018-09-26 18:14:35 -04:00
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
2018-07-10 16:53:00 -04:00
{
2018-09-26 18:14:35 -04:00
async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, msg);
2018-07-10 16:53:00 -04:00
post_async_msg_(std::move(async_m), overflow_policy);
}
2018-05-26 11:48:39 -04:00
2018-07-10 16:53:00 -04:00
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
{
post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
}
2018-05-26 11:48:39 -04:00
2018-08-14 09:38:35 -04:00
size_t overrun_counter()
{
2018-08-14 09:38:35 -04:00
return q_.overrun_counter();
}
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
2018-07-10 16:53:00 -04:00
void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
{
if (overflow_policy == async_overflow_policy::block)
{
q_.enqueue(std::move(new_msg));
}
else
{
q_.enqueue_nowait(std::move(new_msg));
}
}
2018-05-26 11:48:39 -04:00
2018-07-10 16:53:00 -04:00
void worker_loop_()
{
2018-07-21 16:48:07 -04:00
while (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
// 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)
2018-07-10 16:53:00 -04:00
bool process_next_msg_()
{
async_msg incoming_async_msg;
bool dequeued = q_.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
if (!dequeued)
{
return true;
}
switch (incoming_async_msg.msg_type)
{
2018-08-31 07:17:11 -04:00
case async_msg_type::log:
{
log_msg msg;
incoming_async_msg.to_log_msg(msg);
incoming_async_msg.worker_ptr->backend_log_(msg);
return true;
}
2018-07-21 16:48:07 -04:00
case async_msg_type::flush:
{
incoming_async_msg.worker_ptr->backend_flush_();
return true;
}
case async_msg_type::terminate:
{
return false;
}
2018-07-10 16:53:00 -04:00
}
2018-08-31 07:17:11 -04:00
assert(false && "Unexpected async_msg_type");
return true;
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
} // namespace spdlog