41 #ifndef CRL_MULTISENSE_THREAD_HH 42 #define CRL_MULTISENSE_THREAD_HH 44 #ifndef WIN32_LEAN_AND_MEAN 45 #define WIN32_LEAN_AND_MEAN 1 58 #include "../Exception.hh" 61 namespace multisense {
78 Thread(LPTHREAD_START_ROUTINE functionP,
82 int32_t priority=0) :
m_flags(flags) {
87 if (-1 != scheduler) {
116 if (!(
m_flags & FLAGS_DETACH) &&
118 CRL_DEBUG(
"WaitForSingleObject() failed: %d\n", GetLastError());
136 InitializeCriticalSection(&m_mutex);
140 DeleteCriticalSection(&m_mutex);
167 LeaveCriticalSection(m_lockP);
172 void lock(CRITICAL_SECTION *lockP) {
174 EnterCriticalSection(m_lockP);
194 if (0 == wait_(INFINITE))
208 int32_t ret = wait_((DWORD)(timeout * 1000));
212 else if (ETIMEDOUT == ret)
224 return ReleaseSemaphore(m_handle, 1, NULL) != FALSE;
234 while(WaitForSingleObject (m_handle, 0) == WAIT_OBJECT_0)
247 m_handle = CreateSemaphore (NULL, 0, (max == 0 || max > LONG_MAX) ? LONG_MAX : max, NULL);
248 if (m_handle == NULL)
249 CRL_EXCEPTION (
"CreateSemaphore() failed: %d\n", GetLastError());
254 if (m_handle != NULL)
255 CloseHandle (m_handle);
264 inline int32_t
wait_(DWORD ts=INFINITE) {
265 InterlockedIncrement (&m_waiters);
266 const int32_t ret = WaitForSingleObject (m_handle, ts);
267 InterlockedDecrement (&m_waiters);
269 if (ret == WAIT_OBJECT_0)
271 else if (ret == WAIT_TIMEOUT)
284 template<
class T>
class WaitVar {
305 const double& timeout) {
307 if (
false == m_sem.timedWait(timeout))
346 m_maximum == m_queue.size()) {
356 m_queue.push_back(data);
371 if (0 == m_queue.size())
374 data = m_queue.front();
382 return m_sem.waiters();
387 return m_queue.size();
393 while(
false == m_sem.clear());
401 const std::size_t m_maximum;
402 std::deque<T> m_queue;
#define CRL_EXCEPTION(fmt,...)
bool timedWait(T &data, const double &timeout)
bool timedWait(const double &timeout)
void lock(CRITICAL_SECTION *lockP)
Thread(LPTHREAD_START_ROUTINE functionP, void *contextP=NULL, uint32_t flags=0, int32_t scheduler=-1, int32_t priority=0)
static CRL_CONSTEXPR uint32_t FLAGS_DETACH
WaitQueue(std::size_t max=0)
CRITICAL_SECTION * m_lockP
Semaphore(std::size_t max=0)
#define CRL_DEBUG(fmt,...)
ScopedLock(CRITICAL_SECTION &lock)
int32_t wait_(DWORD ts=INFINITE)
ScopedLock(CRITICAL_SECTION *lockP)