1 #include "nta/CallbackManager.h"
2 #include "nta/Logger.h"
10 std::mutex CallbackManager::m_mutex;
11 std::condition_variable_any CallbackManager::m_cv;
16 std::lock_guard<std::mutex> lg(m_mutex);
37 std::lock_guard<std::mutex> lg(m_mutex);
59 m_cv.wait(m_mutex, [&](){
return !
m_working ||
71 auto& es =
m_queue.begin()->second;
77 e->period > 0 ? (void)
delay(e->id, e->period,
false) :
clear(e->id);
86 std::lock_guard<std::mutex> lg(m_mutex);
90 for (
size_t i = 0; i < es.size(); i++) {
92 es.erase(es.begin() + i);
96 if (es.empty())
m_queue.erase(e.when);
99 std::lock_guard<std::mutex> lg(m_mutex);
124 std::lock_guard<std::mutex> lg(m_mutex);
static void writeToLog(crstring entry)
writes an entry in the log
static void increment_frame()
Informs CallbackManager that a frame has passed.
static uint64_t setTimeout(const Thunk &thunk, uint64_t when)
Schedules a function to be called once after when frames and then repeatedly after.
static std::atomic< uint64_t > m_next
id given to the next created event
static std::map< uint64_t, event > m_active
The events waiting to happen (key is event.id)
void wait()
Waits for all scheduled functions to finish execution.
static std::map< uint64_t, std::vector< event * > > m_queue
"Priority queue" of events ordered by when they should occur
static void clear(uint64_t id)
Removes the event with the given id.
static bool delay(uint64_t id, uint64_t when, bool absolute=true)
Changes when an event is next scheduled to run.
static void dequeue(uint64_t id)
Removes event from m_queue but not m_active (assumes id is valid)
static utils::ThreadPool m_pool
ThreadPool used for scheduling events to happen.
static void init()
Initializes the CallbackManager.
void schedule(const Thunk &thunk)
Schedules a function to be executed.
static uint64_t m_curr_frame
The current frame.
static std::thread m_dispatcher
Thread used to dispatch events.
static void destroy()
Destroys the CallbackManager.
static uint64_t setInterval(const Thunk &thunk, uint64_t when, uint64_t period)
static bool m_working
Global flag saying whether or not the CallbackManager has been destroyed.