Coverage Report

Created: 2025-10-13 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/osal_semaphore.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
21
/*****************************************************************************/
22
/*                                                                           */
23
/*  File Name         : osal_semaphore.c                                     */
24
/*                                                                           */
25
/*  Description       : This file contains all the necessary function        */
26
/*                      definitions required to operate on semaphore         */
27
/*                                                                           */
28
/*  List of Functions : osal_sem_create                                      */
29
/*                      osal_sem_destroy                                     */
30
/*                      osal_sem_wait                                        */
31
/*                      osal_sem_wait_timed                                  */
32
/*                      osal_sem_post                                        */
33
/*                      osal_sem_count                                       */
34
/*                      query_semaphore                                      */
35
/*                                                                           */
36
/*  Issues / Problems : None                                                 */
37
/*                                                                           */
38
/*  Revision History  :                                                      */
39
/*                                                                           */
40
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
41
/*         07 03 2006   Ittiam          Draft                                */
42
/*                                                                           */
43
/*****************************************************************************/
44
45
/*****************************************************************************/
46
/* File Includes                                                             */
47
/*****************************************************************************/
48
49
/* System include files */
50
#include <stdio.h>
51
52
#ifdef DARWIN
53
#include <string.h>
54
#include <stdlib.h>
55
#endif
56
#include <semaphore.h>
57
#include <errno.h>
58
59
/* User include files */
60
#include "cast_types.h"
61
#include "osal.h"
62
#include "osal_handle.h"
63
#include "osal_semaphore.h"
64
65
/*****************************************************************************/
66
/* Static Function Declarations                                              */
67
/*****************************************************************************/
68
69
/*****************************************************************************/
70
/*                                                                           */
71
/*  Function Name : osal_sem_create                                          */
72
/*                                                                           */
73
/*  Description   : This function creates the semaphore and returns the      */
74
/*                  handle to the user.                                      */
75
/*                                                                           */
76
/*  Inputs        : Memory manager hamdle                                    */
77
/*                  Attributes to sempahore handle                           */
78
/*                                                                           */
79
/*  Globals       : None                                                     */
80
/*                                                                           */
81
/*  Processing    : Allocates memory for handle and creates the semaphore    */
82
/*                  with specified initialized value by calling OS specific  */
83
/*                  API's.                                                   */
84
/*                                                                           */
85
/*  Outputs       : Semaphore handle                                         */
86
/*                                                                           */
87
/*  Returns       : On SUCCESS - Semaphore handle                            */
88
/*                  On FAILURE - NULL                                        */
89
/*                                                                           */
90
/*  Issues        : None                                                     */
91
/*                                                                           */
92
/*  Revision History:                                                        */
93
/*                                                                           */
94
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
95
/*         07 03 2006   Ittiam          Draft                                */
96
/*                                                                           */
97
/*****************************************************************************/
98
99
void *osal_sem_create(IN void *osal_handle, IN osal_sem_attr_t *attr)
100
90.7k
{
101
90.7k
    osal_t *handle = (osal_t *)osal_handle;
102
90.7k
    void *mmr_handle = 0;
103
104
90.7k
    if(0 == handle || 0 == handle->alloc || 0 == handle->free)
105
0
        return 0;
106
107
    /* Initialize MMR handle */
108
90.7k
    mmr_handle = handle->mmr_handle;
109
110
90.7k
    if(0 == attr)
111
0
        return 0;
112
113
    /* Currenlty naming semaphores is not supported */
114
90.7k
    {
115
        /* Allocate memory for the sempahore handle */
116
90.7k
        sem_handle_t *sem_handle = handle->alloc(mmr_handle, sizeof(sem_handle_t));
117
118
90.7k
        if(0 == sem_handle)
119
0
            return 0;
120
121
        /* Initialize Semaphore handle parameters */
122
90.7k
        sem_handle->mmr_handle = mmr_handle;
123
90.7k
        sem_handle->hdl = handle;
124
125
        /* Create a sempahore */
126
#ifdef DARWIN
127
        static int sem_counter = 0;
128
        char sem_name[32];
129
        snprintf(sem_name, sizeof(sem_name), "/osal_sem_%d", sem_counter++);
130
131
        sem_t *sem = sem_open(sem_name, O_CREAT, 0644, attr->value);
132
        if(sem == SEM_FAILED){
133
            handle->free(sem_handle->mmr_handle, sem_handle);
134
            return 0;
135
        }
136
        sem_handle->sem_handle = sem;
137
        sem_handle->sem_name = strdup(sem_name);
138
        if(sem_handle->sem_name == NULL){
139
            sem_close(sem);
140
            sem_unlink(sem_name);
141
            handle->free(sem_handle->mmr_handle, sem_handle);
142
            return 0;
143
        }
144
#else
145
90.7k
        if(-1 == sem_init(
146
90.7k
                     &(sem_handle->sem_handle), /* Semaphore handle     */
147
90.7k
                     0, /* Shared only between threads */
148
90.7k
                     attr->value)) /* Initialize value.           */
149
0
        {
150
0
            handle->free(sem_handle->mmr_handle, sem_handle);
151
0
            return 0;
152
0
        }
153
90.7k
#endif
154
155
90.7k
        return sem_handle;
156
90.7k
    }
157
90.7k
}
158
159
/*****************************************************************************/
160
/*                                                                           */
161
/*  Function Name : osal_sem_destroy                                         */
162
/*                                                                           */
163
/*  Description   : This function closes the opened semaphore                */
164
/*                                                                           */
165
/*  Inputs        : Initialized Semaphore handle.                            */
166
/*                                                                           */
167
/*  Globals       : None                                                     */
168
/*                                                                           */
169
/*  Processing    : Calls OS specific API's to close the semaphore.          */
170
/*                                                                           */
171
/*  Outputs       : Status of Semaphore close                                */
172
/*                                                                           */
173
/*  Returns       : On SUCCESS - 0                                           */
174
/*                  On FAILURE - -1                                          */
175
/*                                                                           */
176
/*  Issues        : None                                                     */
177
/*                                                                           */
178
/*  Revision History:                                                        */
179
/*                                                                           */
180
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
181
/*         07 03 2006   Ittiam          Draft                                */
182
/*                                                                           */
183
/*****************************************************************************/
184
185
WORD32 osal_sem_destroy(IN void *sem_handle)
186
90.7k
{
187
90.7k
    if(0 == sem_handle)
188
0
        return OSAL_ERROR;
189
190
90.7k
    {
191
90.7k
        sem_handle_t *handle = (sem_handle_t *)sem_handle;
192
193
        /* Validate OSAL handle */
194
90.7k
        if(0 == handle->hdl || 0 == handle->hdl->free)
195
0
            return OSAL_ERROR;
196
197
        /* Destroy the semaphore */
198
#ifdef DARWIN
199
        if(0 == sem_close(handle->sem_handle) && 0 == sem_unlink(handle->sem_name))
200
        {
201
            free(handle->sem_name);
202
            handle->hdl->free(handle->mmr_handle, handle);
203
            return OSAL_SUCCESS;
204
        }
205
#else
206
90.7k
        if(0 == sem_destroy(&(handle->sem_handle)))
207
90.7k
        {
208
90.7k
            handle->hdl->free(handle->mmr_handle, handle);
209
90.7k
            return OSAL_SUCCESS;
210
90.7k
        }
211
0
#endif
212
213
0
        return OSAL_ERROR;
214
90.7k
    }
215
90.7k
}
216
217
/*****************************************************************************/
218
/*                                                                           */
219
/*  Function Name : osal_sem_wait                                            */
220
/*                                                                           */
221
/*  Description   : This function waits for semaphore to be unlocked and     */
222
/*                  then locks the semaphore and control returns back.       */
223
/*                                                                           */
224
/*  Inputs        : Initialized Semaphore handle                             */
225
/*                                                                           */
226
/*  Globals       : None                                                     */
227
/*                                                                           */
228
/*  Processing    : This fucntion calls blocking semaphore lock API's which  */
229
/*                  block the caller till semaphore is locked by them or a   */
230
/*                  signal occurs which results in API function failure      */
231
/*                                                                           */
232
/*  Outputs       : Status of Semaphore wait                                 */
233
/*                                                                           */
234
/*  Returns       : On SUCCESS - 0                                           */
235
/*                  On FAILURE - -1                                          */
236
/*                                                                           */
237
/*  Issues        : None                                                     */
238
/*                                                                           */
239
/*  Revision History:                                                        */
240
/*                                                                           */
241
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
242
/*         07 03 2006   Ittiam          Draft                                */
243
/*                                                                           */
244
/*****************************************************************************/
245
246
WORD32 osal_sem_wait(IN void *sem_handle)
247
908k
{
248
908k
    if(0 == sem_handle)
249
0
        return OSAL_ERROR;
250
251
908k
    {
252
908k
        sem_handle_t *handle = (sem_handle_t *)sem_handle;
253
254
        /* Wait on Semaphore object infinitly */
255
#ifdef DARWIN
256
        return sem_wait(handle->sem_handle);
257
#else
258
908k
        return sem_wait(&(handle->sem_handle));
259
908k
#endif
260
908k
    }
261
908k
}
262
263
/*****************************************************************************/
264
/*                                                                           */
265
/*  Function Name : osal_sem_post                                            */
266
/*                                                                           */
267
/*  Description   : This function releases the lock on the semaphore         */
268
/*                                                                           */
269
/*  Inputs        : Initialized Semaphore handle                             */
270
/*                                                                           */
271
/*  Globals       : None                                                     */
272
/*                                                                           */
273
/*  Processing    : Calls OS specific API's to release the lock on Semaphore */
274
/*                                                                           */
275
/*  Outputs       : Status of semaphore lock release                         */
276
/*                                                                           */
277
/*  Returns       : On SUCCESS - 0                                           */
278
/*                  On FAILURE - -1                                          */
279
/*                                                                           */
280
/*  Issues        : None                                                     */
281
/*                                                                           */
282
/*  Revision History:                                                        */
283
/*                                                                           */
284
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
285
/*         07 03 2006   Ittiam          Draft                                */
286
/*                                                                           */
287
/*****************************************************************************/
288
289
WORD32 osal_sem_post(IN void *sem_handle)
290
2.57M
{
291
2.57M
    if(0 == sem_handle)
292
0
        return OSAL_ERROR;
293
294
2.57M
    {
295
2.57M
        sem_handle_t *handle = (sem_handle_t *)sem_handle;
296
297
        /* Semaphore Post */
298
#ifdef DARWIN
299
        return sem_post(handle->sem_handle);
300
#else
301
2.57M
        return sem_post(&(handle->sem_handle));
302
2.57M
#endif
303
2.57M
    }
304
2.57M
}
305
306
/*****************************************************************************/
307
/*                                                                           */
308
/*  Function Name : osal_sem_count                                           */
309
/*                                                                           */
310
/*  Description   : This function returns the count of semaphore             */
311
/*                                                                           */
312
/*  Inputs        : Handle to Semaphore                                      */
313
/*                  Pointer to value holder                                  */
314
/*                                                                           */
315
/*  Globals       : None                                                     */
316
/*                                                                           */
317
/*  Processing    : Calls OS specific API calls to query on semaphore        */
318
/*                                                                           */
319
/*  Outputs       : Status of Query                                          */
320
/*                                                                           */
321
/*  Returns       : On SUCCESS - 0                                           */
322
/*                  On FAILURE - -1                                          */
323
/*                                                                           */
324
/*  Issues        : None                                                     */
325
/*                                                                           */
326
/*  Revision History:                                                        */
327
/*                                                                           */
328
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
329
/*         30 03 2006   Ittiam          Draft                                */
330
/*                                                                           */
331
/*****************************************************************************/
332
333
WORD32 osal_sem_count(IN void *sem_handle, OUT WORD32 *count)
334
0
{
335
0
    if(0 == sem_handle || 0 == count)
336
0
        return OSAL_ERROR;
337
338
0
    {
339
0
        sem_handle_t *handle = (sem_handle_t *)sem_handle;
340
341
#ifdef DARWIN
342
        if(-1 == sem_getvalue(handle->sem_handle, count))
343
            return OSAL_ERROR;
344
#else
345
0
        if(-1 == sem_getvalue(&(handle->sem_handle), count))
346
0
            return OSAL_ERROR;
347
0
#endif
348
349
0
        return OSAL_SUCCESS;
350
0
    }
351
0
}
352
353
/*****************************************************************************/
354
/*                                                                           */
355
/*  Function Name : query_semaphore                                          */
356
/*                                                                           */
357
/*  Description   : This function calls NtQuerySemaphore() API call of       */
358
/*                  ntdll.dll                                                */
359
/*                                                                           */
360
/*  Inputs        : Handle to Semaphore                                      */
361
/*                  Pointer to value holder                                  */
362
/*                                                                           */
363
/*  Globals       : None                                                     */
364
/*                                                                           */
365
/*  Processing    : This function calls NtQuerySemaphore() API call of       */
366
/*                  ntdll.dll                                                */
367
/*                                                                           */
368
/*  Outputs       : Status of Query                                          */
369
/*                                                                           */
370
/*  Returns       : On SUCCESS - 0                                           */
371
/*                  On FAILURE - -1                                          */
372
/*                                                                           */
373
/*  Issues        : None                                                     */
374
/*                                                                           */
375
/*  Revision History:                                                        */
376
/*                                                                           */
377
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
378
/*         30 03 2006   Ittiam          Draft                                */
379
/*                                                                           */
380
/*****************************************************************************/