aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Posix/Semaphore.c
blob: 4c64786386e7ac6e15917cf4adf7bce33436c400 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "Thread.h"
#include "Timespec.h"

#include <stdlib.h>
#include <errno.h>
#include <time.h>


#if (_POSIX_SEMAPHORES >= 200112L)
BH_Semaphore *BH_SemaphoreNew(int value)
{
    BH_Semaphore *semaphore;

    /* Allocate space for mutex and initialize it */
    semaphore = malloc(sizeof(BH_Semaphore));
    if (semaphore && sem_init(&semaphore->handle, 0, value))
    {
        free(semaphore);
        return NULL;
    }

    return semaphore;
}


void BH_SemaphoreFree(BH_Semaphore *semaphore)
{
    sem_destroy(&semaphore->handle);
    free(semaphore);
}


int BH_SemaphorePost(BH_Semaphore *semaphore)
{
    if (sem_post(&semaphore->handle))
        return BH_ERROR;

    return BH_OK;
}


int BH_SemaphoreWait(BH_Semaphore *semaphore)
{
    if (sem_wait(&semaphore->handle))
        return BH_ERROR;

    return BH_OK;
}


int BH_SemaphoreTryWait(BH_Semaphore *semaphore)
{
    if (sem_trywait(&semaphore->handle))
        return BH_ERROR;

    return BH_OK;
}


int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
                        uint32_t timeout)
{
    struct timespec ts;
    if (convertToTimespec(&ts, timeout))
        return BH_ERROR;

    switch (sem_timedwait(&semaphore->handle, &ts))
    {
        case 0: return BH_OK;
        case ETIMEDOUT: return BH_TIMEOUT;
        default: return BH_ERROR;
    }
}
#else
BH_Semaphore *BH_SemaphoreNew(int value)
{
    BH_Semaphore *semaphore;

    /* Allocate space for mutex and initialize it */
    semaphore = malloc(sizeof(BH_Semaphore));
    if (semaphore)
    {
        semaphore->count = value;
        semaphore->waiters = 0;

        if (pthread_mutex_init(&semaphore->mutex, NULL))
        {
            free(semaphore);
            return NULL;
        }

        if (pthread_cond_init(&semaphore->condition, NULL))
        {
            pthread_mutex_destroy(&semaphore->mutex);
            free(semaphore);
            return NULL;
        }
    }

    return semaphore;
}


void BH_SemaphoreFree(BH_Semaphore *semaphore)
{
    /* Destroy condition variable and mutex */
    pthread_cond_destroy(&semaphore->condition);
    pthread_mutex_destroy(&semaphore->mutex);
    free(semaphore);
}


int BH_SemaphorePost(BH_Semaphore *semaphore)
{
    int result;

    /* Increase semaphore value */
    if (pthread_mutex_lock(&semaphore->mutex))
        return BH_ERROR;
    
    result = BH_OK;
    semaphore->count++;
    if (semaphore->waiters && pthread_cond_signal(&semaphore->condition))
        result = BH_ERROR;

    pthread_mutex_unlock(&semaphore->mutex);
    return result;
}


int BH_SemaphoreWait(BH_Semaphore *semaphore)
{
    int result;

    /* Wait until semaphore count is not zero */
    if (pthread_mutex_lock(&semaphore->mutex))
        return BH_ERROR;

    result = BH_OK;
    semaphore->waiters++;
    while (!semaphore->count) {
        result = pthread_cond_wait(&semaphore->condition, &semaphore->mutex);
        if (result && result != ETIMEDOUT) {
            result = BH_ERROR;
            break; 
        }
        result = BH_OK;
    }
    semaphore->waiters--;
    semaphore->count--;
    pthread_mutex_unlock(&semaphore->mutex);

    return result;
}


int BH_SemaphoreTryWait(BH_Semaphore *semaphore)
{
    int result;

    result = BH_OK;

    /* Check if semaphore count is not zero and decrement it */
    pthread_mutex_lock(&semaphore->mutex);
    if (!semaphore->count)
        result = BH_ERROR;
    else
        semaphore->count--;
    pthread_mutex_unlock(&semaphore->mutex);

    return result;
}


int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
                        uint32_t timeout)
{
    int result;
    struct timespec ts;

    /* Calculate absoulute time for timed wait */
    if (convertToTimespec(&ts, timeout))
        return BH_ERROR;
    result = BH_OK;

    /* Wait until semaphore count is not zero or timeout */
    pthread_mutex_lock(&semaphore->mutex);
    semaphore->waiters++;
    while (!semaphore->count && result == BH_OK)
    {
        switch (pthread_cond_timedwait(&semaphore->condition, &semaphore->mutex, &ts))
        {
        case 0: result = BH_OK; break;
        case ETIMEDOUT: result = BH_TIMEOUT; break;
        default: result = BH_ERROR; break;
        }
    }
    semaphore->waiters--;

    /* If everything went fine - decrement semaphore counter */
    if (!result)
        semaphore->count--;

    pthread_mutex_unlock(&semaphore->mutex);

    return result;
}
#endif