X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/0907aa4037b12b6b88ee24495d4577a064d4f8db..8bfc0bdce2bf4f53ac2c072e7a44e9a7d3b3cca8:/projects/stargazer/eventloop.h?ds=sidebyside 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