4 #include "stg/locker.h"
5 #include "stg/common.h"
8 EVENT_LOOP::EVENT_LOOP()
16 pthread_mutex_init(&_mutex, NULL);
17 pthread_cond_init(&_condition, NULL);
20 EVENT_LOOP::~EVENT_LOOP()
22 pthread_cond_destroy(&_condition);
23 pthread_mutex_destroy(&_mutex);
26 bool EVENT_LOOP::Start()
29 if (pthread_create(&_tid, NULL, Run, this))
31 printfd(__FILE__, "EVENT_LOOP::Start - Failed to create thread: '%s'\n", strerror(errno));
37 bool EVENT_LOOP::Stop()
41 pthread_cond_signal(&_condition);
42 // Wait until thread exit
43 pthread_join(_tid, NULL);
47 void * EVENT_LOOP::Run(void * self)
49 EVENT_LOOP * ev = static_cast<EVENT_LOOP *>(self);
54 void EVENT_LOOP::Runner()
57 printfd(__FILE__, "EVENT_LOOP::Runner - Before start\n");
61 STG_LOCKER lock(&_mutex, __FILE__, __LINE__);
62 // Check for any actions...
65 // ... and sleep until new actions added
66 printfd(__FILE__, "EVENT_LOOP::Runner - Sleeping until new actions arrived\n");
67 pthread_cond_wait(&_condition, &_mutex);
69 // Check for running after wake up
72 // Don't process any actions if stopping
76 // Create new empty actions list
78 // Fast swap with current
80 // Invoke all current actions
81 printfd(__FILE__, "EVENT_LOOP::Runner - Invoke %d actions\n", local.size());
84 printfd(__FILE__, "EVENT_LOOP::Runner - Before stop\n");
90 pthread_mutex_t singletonMutex;
94 EVENT_LOOP & EVENT_LOOP_SINGLETON::GetInstance()
96 // Double-checking technique
99 STG_LOCKER lock(&singletonMutex, __FILE__, __LINE__);
108 void EVENT_LOOP_SINGLETON::CreateInstance()
110 static EVENT_LOOP loop;
114 EVENT_LOOP * EVENT_LOOP_SINGLETON::_instance = NULL;