mirror of
https://github.com/Mr-X-GTA/YimMenu.git
synced 2025-01-09 18:58:58 +08:00
54 lines
862 B
C++
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();
|
||
|
}
|
||
|
}
|
||
|
}
|