X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/dda964a76b486001f0debf38deb594ad7c13f416..6fd1ed115d0f47b3e88f54c89fa3295446f34851:/projects/stargazer/eventloop.h diff --git a/projects/stargazer/eventloop.h b/projects/stargazer/eventloop.h index 0e798142..0b2b8397 100644 --- a/projects/stargazer/eventloop.h +++ b/projects/stargazer/eventloop.h @@ -1,63 +1,45 @@ #ifndef __EVENT_LOOP_H__ #define __EVENT_LOOP_H__ -#include - -#include "stg/noncopyable.h" #include "actions.h" -class EVENT_LOOP : private NONCOPYABLE, - private ACTIONS_LIST +#include +#include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop + +class EVENT_LOOP { public: + static EVENT_LOOP& instance(); + bool Start(); bool Stop(); - bool IsRunning() const { return _running; } template void Enqueue(ACTIVE_CLASS & ac, typename ACTOR::TYPE a, - DATA_TYPE d); + DATA_TYPE d) + { + std::lock_guard lock(m_mutex); + // Add new action + m_list.Enqueue(ac, a, d); + // Signal about new action + m_cond.notify_all(); + } private: - bool _running; - bool _stopped; - pthread_t _tid; - pthread_mutex_t _mutex; - pthread_cond_t _condition; - - EVENT_LOOP(); - virtual ~EVENT_LOOP(); - - static void * Run(void *); - void Runner(); - - friend class EVENT_LOOP_SINGLETON; -}; + std::jthread m_thread; + std::mutex m_mutex; + std::condition_variable m_cond; -class EVENT_LOOP_SINGLETON : private NONCOPYABLE -{ - public: - static EVENT_LOOP & GetInstance(); + ACTIONS_LIST m_list; - private: - static EVENT_LOOP * _instance; - static void CreateInstance(); + EVENT_LOOP() = default; - EVENT_LOOP_SINGLETON() {} - ~EVENT_LOOP_SINGLETON() {} + void Run(std::stop_token token); }; -template -void EVENT_LOOP::Enqueue(ACTIVE_CLASS & ac, - typename ACTOR::TYPE a, - DATA_TYPE d) -{ -STG_LOCKER lock(&_mutex); -// Add new action -ACTIONS_LIST::Enqueue(ac, a, d); -// Signal about new action -pthread_cond_signal(&_condition); -} - #endif