blob: 0246091f329cd3a82ef1637e666e4f8b2032e9f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/*******************************************************************************
* Copyright (C) ST-Ericsson SA 2011
* License terms: 3-clause BSD license
******************************************************************************/
#include <errno.h>
#include <semaphore.h>
#include <sys/time.h>
#include "Types.h"
#include "CSemaphore.h"
#include "OS.h"
CSemaphore::CSemaphore(unsigned int initial_count)
{
sem_init(&m_semaphore, 0, initial_count);
}
CSemaphore::~CSemaphore()
{
sem_destroy(&m_semaphore);
}
bool CSemaphore::Release(unsigned int count)
{
if (!count) {
return false;
}
for (unsigned int i = 0; i < count; ++i) {
if (sem_post(&m_semaphore)) {
return false;
}
}
return true;
}
DWORD CSemaphore::Wait(DWORD timeout)
{
if (INFINITE == timeout) {
sem_wait(&m_semaphore);
} else {
timespec absoulute_time = OS::GetAbsoluteTime(timeout);
int ret;
/* coverity[returned_null] */
while (-1 == (ret = sem_timedwait(&m_semaphore, &absoulute_time)) && errno == EINTR);
if (0 != ret) {
return WAIT_TIMEOUT;
}
}
return WAIT_OBJECT_0;
}
|