X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8e80bb9cec2c90dd61f810fb1525932a434288eb..5dfab5bea026e33031d041b233c73e196469b4ac:/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