X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1347f3d1e04bedd1508589173f577673ee2c5554..f4333737c7e347efe9258ccf85c1237e03be619c:/projects/stargazer/eventloop.h diff --git a/projects/stargazer/eventloop.h b/projects/stargazer/eventloop.h index 35de3675..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, __FILE__, __LINE__); -// Add new action -ACTIONS_LIST::Enqueue(ac, a, d); -// Signal about new action -pthread_cond_signal(&_condition); -} - #endif