aboutsummaryrefslogtreecommitdiff
path: root/encoder/osal_semaphore.c
blob: 987515938ed802d1f9a09ce6e47c29a73bf9d45e (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/******************************************************************************
 *
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/

/*****************************************************************************/
/*                                                                           */
/*  File Name         : osal_semaphore.c                                     */
/*                                                                           */
/*  Description       : This file contains all the necessary function        */
/*                      definitions required to operate on semaphore         */
/*                                                                           */
/*  List of Functions : osal_sem_create                                      */
/*                      osal_sem_destroy                                     */
/*                      osal_sem_wait                                        */
/*                      osal_sem_wait_timed                                  */
/*                      osal_sem_post                                        */
/*                      osal_sem_count                                       */
/*                      query_semaphore                                      */
/*                                                                           */
/*  Issues / Problems : None                                                 */
/*                                                                           */
/*  Revision History  :                                                      */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/

/* System include files */
#include <stdio.h>

#include <semaphore.h>
#include <errno.h>

/* User include files */
#include "cast_types.h"
#include "osal.h"
#include "osal_handle.h"
#include "osal_semaphore.h"

/*****************************************************************************/
/* Static Function Declarations                                              */
/*****************************************************************************/

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_sem_create                                          */
/*                                                                           */
/*  Description   : This function creates the semaphore and returns the      */
/*                  handle to the user.                                      */
/*                                                                           */
/*  Inputs        : Memory manager hamdle                                    */
/*                  Attributes to sempahore handle                           */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Allocates memory for handle and creates the semaphore    */
/*                  with specified initialized value by calling OS specific  */
/*                  API's.                                                   */
/*                                                                           */
/*  Outputs       : Semaphore handle                                         */
/*                                                                           */
/*  Returns       : On SUCCESS - Semaphore handle                            */
/*                  On FAILURE - NULL                                        */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

void *osal_sem_create(IN void *osal_handle, IN osal_sem_attr_t *attr)
{
    osal_t *handle = (osal_t *)osal_handle;
    void *mmr_handle = 0;

    if(0 == handle || 0 == handle->alloc || 0 == handle->free)
        return 0;

    /* Initialize MMR handle */
    mmr_handle = handle->mmr_handle;

    if(0 == attr)
        return 0;

    /* Currenlty naming semaphores is not supported */
    {
        /* Allocate memory for the sempahore handle */
        sem_handle_t *sem_handle = handle->alloc(mmr_handle, sizeof(sem_handle_t));

        if(0 == sem_handle)
            return 0;

        /* Initialize Semaphore handle parameters */
        sem_handle->mmr_handle = mmr_handle;
        sem_handle->hdl = handle;

        /* Create a sempahore */
        if(-1 == sem_init(
                     &(sem_handle->sem_handle), /* Semaphore handle     */
                     0, /* Shared only between threads */
                     attr->value)) /* Initialize value.           */
        {
            handle->free(sem_handle->mmr_handle, sem_handle);
            return 0;
        }

        return sem_handle;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_sem_destroy                                         */
/*                                                                           */
/*  Description   : This function closes the opened semaphore                */
/*                                                                           */
/*  Inputs        : Initialized Semaphore handle.                            */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Calls OS specific API's to close the semaphore.          */
/*                                                                           */
/*  Outputs       : Status of Semaphore close                                */
/*                                                                           */
/*  Returns       : On SUCCESS - 0                                           */
/*                  On FAILURE - -1                                          */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_sem_destroy(IN void *sem_handle)
{
    if(0 == sem_handle)
        return OSAL_ERROR;

    {
        sem_handle_t *handle = (sem_handle_t *)sem_handle;

        /* Validate OSAL handle */
        if(0 == handle->hdl || 0 == handle->hdl->free)
            return OSAL_ERROR;

        /* Destroy the semaphore */
        if(0 == sem_destroy(&(handle->sem_handle)))
        {
            handle->hdl->free(handle->mmr_handle, handle);
            return OSAL_SUCCESS;
        }

        return OSAL_ERROR;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_sem_wait                                            */
/*                                                                           */
/*  Description   : This function waits for semaphore to be unlocked and     */
/*                  then locks the semaphore and control returns back.       */
/*                                                                           */
/*  Inputs        : Initialized Semaphore handle                             */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : This fucntion calls blocking semaphore lock API's which  */
/*                  block the caller till semaphore is locked by them or a   */
/*                  signal occurs which results in API function failure      */
/*                                                                           */
/*  Outputs       : Status of Semaphore wait                                 */
/*                                                                           */
/*  Returns       : On SUCCESS - 0                                           */
/*                  On FAILURE - -1                                          */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_sem_wait(IN void *sem_handle)
{
    if(0 == sem_handle)
        return OSAL_ERROR;

    {
        sem_handle_t *handle = (sem_handle_t *)sem_handle;

        /* Wait on Semaphore object infinitly */
        return sem_wait(&(handle->sem_handle));
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_sem_post                                            */
/*                                                                           */
/*  Description   : This function releases the lock on the semaphore         */
/*                                                                           */
/*  Inputs        : Initialized Semaphore handle                             */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Calls OS specific API's to release the lock on Semaphore */
/*                                                                           */
/*  Outputs       : Status of semaphore lock release                         */
/*                                                                           */
/*  Returns       : On SUCCESS - 0                                           */
/*                  On FAILURE - -1                                          */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         07 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_sem_post(IN void *sem_handle)
{
    if(0 == sem_handle)
        return OSAL_ERROR;

    {
        sem_handle_t *handle = (sem_handle_t *)sem_handle;

        /* Semaphore Post */
        return sem_post(&(handle->sem_handle));
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_sem_count                                           */
/*                                                                           */
/*  Description   : This function returns the count of semaphore             */
/*                                                                           */
/*  Inputs        : Handle to Semaphore                                      */
/*                  Pointer to value holder                                  */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Calls OS specific API calls to query on semaphore        */
/*                                                                           */
/*  Outputs       : Status of Query                                          */
/*                                                                           */
/*  Returns       : On SUCCESS - 0                                           */
/*                  On FAILURE - -1                                          */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         30 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_sem_count(IN void *sem_handle, OUT WORD32 *count)
{
    if(0 == sem_handle || 0 == count)
        return OSAL_ERROR;

    {
        sem_handle_t *handle = (sem_handle_t *)sem_handle;

        if(-1 == sem_getvalue(&(handle->sem_handle), count))
            return OSAL_ERROR;

        return OSAL_SUCCESS;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : query_semaphore                                          */
/*                                                                           */
/*  Description   : This function calls NtQuerySemaphore() API call of       */
/*                  ntdll.dll                                                */
/*                                                                           */
/*  Inputs        : Handle to Semaphore                                      */
/*                  Pointer to value holder                                  */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : This function calls NtQuerySemaphore() API call of       */
/*                  ntdll.dll                                                */
/*                                                                           */
/*  Outputs       : Status of Query                                          */
/*                                                                           */
/*  Returns       : On SUCCESS - 0                                           */
/*                  On FAILURE - -1                                          */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         30 03 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/