This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
YimMenu/BigBaseV2/src/fiber_pool.cpp
2019-06-23 22:00:18 +02:00

54 lines
862 B
C++

#include "common.hpp"
#include "fiber_pool.hpp"
#include "script.hpp"
#include "script_mgr.hpp"
namespace big
{
fiber_pool::fiber_pool(std::size_t num_fibers)
{
for (std::size_t i = 0; i < num_fibers; ++i)
{
g_script_mgr.add_script(std::make_unique<script>(&fiber_func));
}
g_fiber_pool = this;
}
fiber_pool::~fiber_pool()
{
g_fiber_pool = nullptr;
}
void fiber_pool::queue_job(std::function<void()> func)
{
if (func)
{
std::lock_guard lock(m_mutex);
m_jobs.push(std::move(func));
}
}
void fiber_pool::fiber_tick()
{
std::unique_lock lock(m_mutex);
if (!m_jobs.empty())
{
auto job = std::move(m_jobs.top());
m_jobs.pop();
lock.unlock();
std::invoke(std::move(job));
}
}
void fiber_pool::fiber_func()
{
while (true)
{
g_fiber_pool->fiber_tick();
script::get_current()->yield();
}
}
}