4 #include "stg/stg_locker.h"
5 #include "stg/common.h"
8 EVENT_LOOP::EVENT_LOOP()
11 pthread_mutex_init(&_mutex, NULL);
12 pthread_cond_init(&_condition, NULL);
15 EVENT_LOOP::~EVENT_LOOP()
17 pthread_cond_destroy(&_condition);
18 pthread_mutex_destroy(&_mutex);
21 bool EVENT_LOOP::Start()
24 if (pthread_create(&_tid, NULL, Run, this))
26 printfd(__FILE__, "EVENT_LOOP::Start - Failed to create thread: '%s'\n", strerror(errno));
32 bool EVENT_LOOP::Stop()
36 pthread_cond_signal(&_condition);
37 // Wait until thread exit
38 pthread_join(_tid, NULL);
42 void * EVENT_LOOP::Run(void * self)
44 EVENT_LOOP * ev = static_cast<EVENT_LOOP *>(self);
49 void EVENT_LOOP::Runner()
52 printfd(__FILE__, "EVENT_LOOP::Runner - Before start\n");
56 STG_LOCKER lock(&_mutex, __FILE__, __LINE__);
57 // Check for any actions...
60 // ... and sleep until new actions added
61 printfd(__FILE__, "EVENT_LOOP::Runner - Sleeping until new actions arrived\n");
62 pthread_cond_wait(&_condition, &_mutex);
64 // Check for running after wake up
67 // Don't process any actions if stopping
71 // Create new empty actions list
73 // Fast swap with current
75 // Invoke all current actions
76 printfd(__FILE__, "EVENT_LOOP::Runner - Invoke %d actions\n", local.size());
79 printfd(__FILE__, "EVENT_LOOP::Runner - Before stop\n");
85 pthread_mutex_t singletonMutex;
89 EVENT_LOOP & EVENT_LOOP_SINGLETON::GetInstance()
91 // Double-checking technique
94 STG_LOCKER lock(&singletonMutex, __FILE__, __LINE__);
103 void EVENT_LOOP_SINGLETON::CreateInstance()
105 static EVENT_LOOP loop;
109 EVENT_LOOP * EVENT_LOOP_SINGLETON::_instance = NULL;