summaryrefslogtreecommitdiff
path: root/source/api_wrappers/linux/CSemaphore.cpp
blob: b7e422417d9c141ef7166d7694b11136b09940a5 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/

#include <sys/errno.h>
#include <semaphore.h>
#include <sys/time.h>
#include <fcntl.h>
#include "Types.h"
#include "CSemaphore.h"
#include "OS.h"


CSemaphore::CSemaphore(unsigned int initial_count)
{
#if defined(__APPLE__)
    char sem_name[SEM_NAME_MAX_LENGTH];
    int sem_nr = 1;

    while (sem_nr <= SEM_MAX_NR) {
        snprintf(sem_name, SEM_NAME_MAX_LENGTH, "lcdriversem_%d", sem_nr);

        /* open semaphore with "rw" permissions for everyone - 0666 */
        m_semaphore = sem_open(sem_name, O_CREAT | O_EXCL, 0666 , 0);

        if (m_semaphore != SEM_FAILED) {
            break;
        }

        sem_nr++;
    }
#elif defined(__linux__)
    sem_init(&m_semaphore, 0, initial_count);
#endif
}

CSemaphore::~CSemaphore()
{
#if defined(__APPLE__)
    sem_close(m_semaphore);
#elif defined(__linux__)
    sem_destroy(&m_semaphore);
#endif
}

bool CSemaphore::Release(unsigned int count)
{
    if (!count) {
        return false;
    }

    for (unsigned int i = 0; i < count; ++i) {
#if defined(__APPLE__)
        if (sem_post(m_semaphore)) {
#elif defined(__linux__)
        if (sem_post(&m_semaphore)) {
#endif
            return false;
        }
    }

    return true;
}

DWORD CSemaphore::Wait(DWORD timeout)
{
#if defined(__APPLE__)
    if (INFINITE == timeout) {
        sem_wait(m_semaphore);
    } else {
        struct timeval curr_time, start_time;
        DWORD dwTimePassed = 0;
        int ret;

        gettimeofday(&start_time, NULL);

        /* Try to lock the semaphore */
        ret = sem_trywait(m_semaphore);

        if (ret != 0) {
            while (dwTimePassed < timeout) {
                /* Sleep 1ms */
                OS::Sleep(1);

                /* Try to lock the semaphore again*/
                ret = sem_trywait(m_semaphore);

                if (ret == 0) {
                    return WAIT_OBJECT_0;
                }

                gettimeofday(&curr_time, NULL);

                dwTimePassed = 1000 * (curr_time.tv_sec - start_time.tv_sec) + \
                               (curr_time.tv_usec - start_time.tv_usec) / 1000;
            }

            return WAIT_TIMEOUT;
        }
    }

    return WAIT_OBJECT_0;
#elif defined(__linux__)
    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;
#endif
}