X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4271ab433cd55bbd2612292bcf39e4dc3d7274f1..0907aa4037b12b6b88ee24495d4577a064d4f8db:/projects/stargazer/eventloop.h diff --git a/projects/stargazer/eventloop.h b/projects/stargazer/eventloop.h new file mode 100644 index 00000000..0e798142 --- /dev/null +++ b/projects/stargazer/eventloop.h @@ -0,0 +1,63 @@ +#ifndef __EVENT_LOOP_H__ +#define __EVENT_LOOP_H__ + +#include + +#include "stg/noncopyable.h" +#include "actions.h" + +class EVENT_LOOP : private NONCOPYABLE, + private ACTIONS_LIST +{ + public: + bool Start(); + bool Stop(); + bool IsRunning() const { return _running; } + + template + void Enqueue(ACTIVE_CLASS & ac, + typename ACTOR::TYPE a, + DATA_TYPE d); + + 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; +}; + +class EVENT_LOOP_SINGLETON : private NONCOPYABLE +{ + public: + static EVENT_LOOP & GetInstance(); + + private: + static EVENT_LOOP * _instance; + static void CreateInstance(); + + EVENT_LOOP_SINGLETON() {} + ~EVENT_LOOP_SINGLETON() {} +}; + +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