24 #include "condition.h" 36 : m_finalizeOnStop(
false)
37 , m_handle(
new Handle()) { }
46 this->m_handle =
new Handle();
47 this->m_finalizeOnStop = _thread.m_finalizeOnStop;
54 delete this->m_handle;
55 this->m_handle =
new Handle();
56 this->m_finalizeOnStop = _thread.m_finalizeOnStop;
63 return &(m_handle->nativeHandle);
66 bool StartThread(
bool wait =
true)
69 if (!m_handle->running)
71 m_handle->notifiedStop =
false;
72 if (thread_create(&(m_handle->nativeHandle), CThread::ThreadHandler, ((
void*)static_cast<CThread*>(
this))))
75 m_handle->condition.Wait(m_handle->mutex, m_handle->running);
82 void StopThread(
bool wait =
true)
87 m_handle->notifiedStop =
true;
88 m_handle->condition.Broadcast();
94 m_handle->condition.Wait(m_handle->mutex, m_handle->stopped);
98 bool WaitThread(
unsigned timeout)
101 return m_handle->stopped ? true : m_handle->condition.Wait(m_handle->mutex, m_handle->stopped, timeout);
107 return m_handle->running;
113 return m_handle->notifiedStop || m_handle->stopped;
116 void Sleep(
unsigned timeout)
120 while (!m_handle->notifiedStop && !m_handle->notifiedWake && m_handle->condition.Wait(m_handle->mutex, _timeout));
121 m_handle->notifiedWake =
false;
127 m_handle->notifiedWake =
true;
128 m_handle->condition.Broadcast();
132 virtual void* Process(
void) = 0;
133 virtual void Finalize(
void) { };
134 bool m_finalizeOnStop;
140 volatile bool running;
141 volatile bool stopped;
142 volatile bool notifiedStop;
143 volatile bool notifiedWake;
151 , notifiedStop(
false)
152 , notifiedWake(
false)
159 static void* ThreadHandler(
void* _thread)
166 bool finalize = thread->m_finalizeOnStop;
169 thread->m_handle->running =
true;
170 thread->m_handle->stopped =
false;
171 thread->m_handle->condition.Broadcast();
173 ret = thread->Process();
175 thread->m_handle->running =
false;
176 thread->m_handle->stopped =
true;
177 thread->m_handle->condition.Broadcast();