5 #include "stg/locker.h"
6 #include "stg/common.h"
9 EVENT_LOOP::EVENT_LOOP()
17 pthread_mutex_init(&_mutex, NULL);
18 pthread_cond_init(&_condition, NULL);
21 EVENT_LOOP::~EVENT_LOOP()
23 pthread_cond_destroy(&_condition);
24 pthread_mutex_destroy(&_mutex);
27 bool EVENT_LOOP::Start()
30 if (pthread_create(&_tid, NULL, Run, this))
32 printfd(__FILE__, "EVENT_LOOP::Start - Failed to create thread: '%s'\n", strerror(errno));
38 bool EVENT_LOOP::Stop()
42 pthread_cond_signal(&_condition);
43 // Wait until thread exit
44 pthread_join(_tid, NULL);
48 void * EVENT_LOOP::Run(void * self)
50 EVENT_LOOP * ev = static_cast<EVENT_LOOP *>(self);
55 void EVENT_LOOP::Runner()
58 sigfillset(&signalSet);
59 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
62 printfd(__FILE__, "EVENT_LOOP::Runner - Before start\n");
66 STG_LOCKER lock(&_mutex);
67 // Check for any actions...
70 // ... and sleep until new actions added
71 printfd(__FILE__, "EVENT_LOOP::Runner - Sleeping until new actions arrived\n");
72 pthread_cond_wait(&_condition, &_mutex);
74 // Check for running after wake up
77 // Don't process any actions if stopping
81 // Create new empty actions list
83 // Fast swap with current
85 // Invoke all current actions
86 printfd(__FILE__, "EVENT_LOOP::Runner - Invoke %d actions\n", local.size());
89 printfd(__FILE__, "EVENT_LOOP::Runner - Before stop\n");
95 pthread_mutex_t singletonMutex;
99 EVENT_LOOP & EVENT_LOOP_SINGLETON::GetInstance()
101 // Double-checking technique
104 STG_LOCKER lock(&singletonMutex);
113 void EVENT_LOOP_SINGLETON::CreateInstance()
115 static EVENT_LOOP loop;
119 EVENT_LOOP * EVENT_LOOP_SINGLETON::_instance = NULL;