2018-12-27 11:11:52 -05:00
|
|
|
#include <std_include.hpp>
|
|
|
|
#include "scheduler.hpp"
|
|
|
|
|
|
|
|
std::mutex scheduler::mutex_;
|
|
|
|
std::vector<std::function<void()>> scheduler::callbacks_;
|
2018-12-28 06:50:34 -05:00
|
|
|
std::vector<std::function<void()>> scheduler::single_callbacks_;
|
2018-12-27 11:11:52 -05:00
|
|
|
|
|
|
|
void scheduler::on_frame(const std::function<void()>& callback)
|
|
|
|
{
|
|
|
|
std::lock_guard _(mutex_);
|
|
|
|
callbacks_.push_back(callback);
|
|
|
|
}
|
|
|
|
|
2018-12-28 06:50:34 -05:00
|
|
|
void scheduler::once(const std::function<void()>& callback)
|
|
|
|
{
|
|
|
|
std::lock_guard _(mutex_);
|
|
|
|
single_callbacks_.push_back(callback);
|
|
|
|
}
|
|
|
|
|
2018-12-27 11:11:52 -05:00
|
|
|
void scheduler::execute()
|
|
|
|
{
|
|
|
|
std::vector<std::function<void()>> callbacks_copy;
|
2018-12-28 06:50:34 -05:00
|
|
|
std::vector<std::function<void()>> single_callbacks_copy;
|
2018-12-27 11:11:52 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard _(mutex_);
|
|
|
|
callbacks_copy = callbacks_;
|
2018-12-28 06:50:34 -05:00
|
|
|
single_callbacks_copy = single_callbacks_;
|
|
|
|
single_callbacks_.clear();
|
2018-12-27 11:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& callback : callbacks_copy)
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
2018-12-28 06:50:34 -05:00
|
|
|
|
|
|
|
for (const auto& callback : single_callbacks_copy)
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
}
|
2018-12-27 11:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void scheduler::pre_destroy()
|
|
|
|
{
|
|
|
|
std::lock_guard _(mutex_);
|
|
|
|
callbacks_.clear();
|
2018-12-28 06:50:34 -05:00
|
|
|
single_callbacks_.clear();
|
2018-12-27 11:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_MODULE(scheduler);
|