Home | History | Annotate | Download | only in encoder
      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 #include <semaphore.h>
     53 #include <errno.h>
     54 
     55 /* User include files */
     56 #include "cast_types.h"
     57 #include "osal.h"
     58 #include "osal_handle.h"
     59 #include "osal_semaphore.h"
     60 
     61 /*****************************************************************************/
     62 /* Static Function Declarations                                              */
     63 /*****************************************************************************/
     64 
     65 /*****************************************************************************/
     66 /*                                                                           */
     67 /*  Function Name : osal_sem_create                                          */
     68 /*                                                                           */
     69 /*  Description   : This function creates the semaphore and returns the      */
     70 /*                  handle to the user.                                      */
     71 /*                                                                           */
     72 /*  Inputs        : Memory manager hamdle                                    */
     73 /*                  Attributes to sempahore handle                           */
     74 /*                                                                           */
     75 /*  Globals       : None                                                     */
     76 /*                                                                           */
     77 /*  Processing    : Allocates memory for handle and creates the semaphore    */
     78 /*                  with specified initialized value by calling OS specific  */
     79 /*                  API's.                                                   */
     80 /*                                                                           */
     81 /*  Outputs       : Semaphore handle                                         */
     82 /*                                                                           */
     83 /*  Returns       : On SUCCESS - Semaphore handle                            */
     84 /*                  On FAILURE - NULL                                        */
     85 /*                                                                           */
     86 /*  Issues        : None                                                     */
     87 /*                                                                           */
     88 /*  Revision History:                                                        */
     89 /*                                                                           */
     90 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
     91 /*         07 03 2006   Ittiam          Draft                                */
     92 /*                                                                           */
     93 /*****************************************************************************/
     94 
     95 void *osal_sem_create(IN void *osal_handle, IN osal_sem_attr_t *attr)
     96 {
     97     osal_t *handle = (osal_t *)osal_handle;
     98     void *mmr_handle = 0;
     99 
    100     if(0 == handle || 0 == handle->alloc || 0 == handle->free)
    101         return 0;
    102 
    103     /* Initialize MMR handle */
    104     mmr_handle = handle->mmr_handle;
    105 
    106     if(0 == attr)
    107         return 0;
    108 
    109     /* Currenlty naming semaphores is not supported */
    110     {
    111         /* Allocate memory for the sempahore handle */
    112         sem_handle_t *sem_handle = handle->alloc(mmr_handle, sizeof(sem_handle_t));
    113 
    114         if(0 == sem_handle)
    115             return 0;
    116 
    117         /* Initialize Semaphore handle parameters */
    118         sem_handle->mmr_handle = mmr_handle;
    119         sem_handle->hdl = handle;
    120 
    121         /* Create a sempahore */
    122         if(-1 == sem_init(
    123                      &(sem_handle->sem_handle), /* Semaphore handle     */
    124                      0, /* Shared only between threads */
    125                      attr->value)) /* Initialize value.           */
    126         {
    127             handle->free(sem_handle->mmr_handle, sem_handle);
    128             return 0;
    129         }
    130 
    131         return sem_handle;
    132     }
    133 }
    134 
    135 /*****************************************************************************/
    136 /*                                                                           */
    137 /*  Function Name : osal_sem_destroy                                         */
    138 /*                                                                           */
    139 /*  Description   : This function closes the opened semaphore                */
    140 /*                                                                           */
    141 /*  Inputs        : Initialized Semaphore handle.                            */
    142 /*                                                                           */
    143 /*  Globals       : None                                                     */
    144 /*                                                                           */
    145 /*  Processing    : Calls OS specific API's to close the semaphore.          */
    146 /*                                                                           */
    147 /*  Outputs       : Status of Semaphore close                                */
    148 /*                                                                           */
    149 /*  Returns       : On SUCCESS - 0                                           */
    150 /*                  On FAILURE - -1                                          */
    151 /*                                                                           */
    152 /*  Issues        : None                                                     */
    153 /*                                                                           */
    154 /*  Revision History:                                                        */
    155 /*                                                                           */
    156 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    157 /*         07 03 2006   Ittiam          Draft                                */
    158 /*                                                                           */
    159 /*****************************************************************************/
    160 
    161 WORD32 osal_sem_destroy(IN void *sem_handle)
    162 {
    163     if(0 == sem_handle)
    164         return OSAL_ERROR;
    165 
    166     {
    167         sem_handle_t *handle = (sem_handle_t *)sem_handle;
    168 
    169         /* Validate OSAL handle */
    170         if(0 == handle->hdl || 0 == handle->hdl->free)
    171             return OSAL_ERROR;
    172 
    173         /* Destroy the semaphore */
    174         if(0 == sem_destroy(&(handle->sem_handle)))
    175         {
    176             handle->hdl->free(handle->mmr_handle, handle);
    177             return OSAL_SUCCESS;
    178         }
    179 
    180         return OSAL_ERROR;
    181     }
    182 }
    183 
    184 /*****************************************************************************/
    185 /*                                                                           */
    186 /*  Function Name : osal_sem_wait                                            */
    187 /*                                                                           */
    188 /*  Description   : This function waits for semaphore to be unlocked and     */
    189 /*                  then locks the semaphore and control returns back.       */
    190 /*                                                                           */
    191 /*  Inputs        : Initialized Semaphore handle                             */
    192 /*                                                                           */
    193 /*  Globals       : None                                                     */
    194 /*                                                                           */
    195 /*  Processing    : This fucntion calls blocking semaphore lock API's which  */
    196 /*                  block the caller till semaphore is locked by them or a   */
    197 /*                  signal occurs which results in API function failure      */
    198 /*                                                                           */
    199 /*  Outputs       : Status of Semaphore wait                                 */
    200 /*                                                                           */
    201 /*  Returns       : On SUCCESS - 0                                           */
    202 /*                  On FAILURE - -1                                          */
    203 /*                                                                           */
    204 /*  Issues        : None                                                     */
    205 /*                                                                           */
    206 /*  Revision History:                                                        */
    207 /*                                                                           */
    208 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    209 /*         07 03 2006   Ittiam          Draft                                */
    210 /*                                                                           */
    211 /*****************************************************************************/
    212 
    213 WORD32 osal_sem_wait(IN void *sem_handle)
    214 {
    215     if(0 == sem_handle)
    216         return OSAL_ERROR;
    217 
    218     {
    219         sem_handle_t *handle = (sem_handle_t *)sem_handle;
    220 
    221         /* Wait on Semaphore object infinitly */
    222         return sem_wait(&(handle->sem_handle));
    223     }
    224 }
    225 
    226 /*****************************************************************************/
    227 /*                                                                           */
    228 /*  Function Name : osal_sem_post                                            */
    229 /*                                                                           */
    230 /*  Description   : This function releases the lock on the semaphore         */
    231 /*                                                                           */
    232 /*  Inputs        : Initialized Semaphore handle                             */
    233 /*                                                                           */
    234 /*  Globals       : None                                                     */
    235 /*                                                                           */
    236 /*  Processing    : Calls OS specific API's to release the lock on Semaphore */
    237 /*                                                                           */
    238 /*  Outputs       : Status of semaphore lock release                         */
    239 /*                                                                           */
    240 /*  Returns       : On SUCCESS - 0                                           */
    241 /*                  On FAILURE - -1                                          */
    242 /*                                                                           */
    243 /*  Issues        : None                                                     */
    244 /*                                                                           */
    245 /*  Revision History:                                                        */
    246 /*                                                                           */
    247 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    248 /*         07 03 2006   Ittiam          Draft                                */
    249 /*                                                                           */
    250 /*****************************************************************************/
    251 
    252 WORD32 osal_sem_post(IN void *sem_handle)
    253 {
    254     if(0 == sem_handle)
    255         return OSAL_ERROR;
    256 
    257     {
    258         sem_handle_t *handle = (sem_handle_t *)sem_handle;
    259 
    260         /* Semaphore Post */
    261         return sem_post(&(handle->sem_handle));
    262     }
    263 }
    264 
    265 /*****************************************************************************/
    266 /*                                                                           */
    267 /*  Function Name : osal_sem_count                                           */
    268 /*                                                                           */
    269 /*  Description   : This function returns the count of semaphore             */
    270 /*                                                                           */
    271 /*  Inputs        : Handle to Semaphore                                      */
    272 /*                  Pointer to value holder                                  */
    273 /*                                                                           */
    274 /*  Globals       : None                                                     */
    275 /*                                                                           */
    276 /*  Processing    : Calls OS specific API calls to query on semaphore        */
    277 /*                                                                           */
    278 /*  Outputs       : Status of Query                                          */
    279 /*                                                                           */
    280 /*  Returns       : On SUCCESS - 0                                           */
    281 /*                  On FAILURE - -1                                          */
    282 /*                                                                           */
    283 /*  Issues        : None                                                     */
    284 /*                                                                           */
    285 /*  Revision History:                                                        */
    286 /*                                                                           */
    287 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    288 /*         30 03 2006   Ittiam          Draft                                */
    289 /*                                                                           */
    290 /*****************************************************************************/
    291 
    292 WORD32 osal_sem_count(IN void *sem_handle, OUT WORD32 *count)
    293 {
    294     if(0 == sem_handle || 0 == count)
    295         return OSAL_ERROR;
    296 
    297     {
    298         sem_handle_t *handle = (sem_handle_t *)sem_handle;
    299 
    300         if(-1 == sem_getvalue(&(handle->sem_handle), count))
    301             return OSAL_ERROR;
    302 
    303         return OSAL_SUCCESS;
    304     }
    305 }
    306 
    307 /*****************************************************************************/
    308 /*                                                                           */
    309 /*  Function Name : query_semaphore                                          */
    310 /*                                                                           */
    311 /*  Description   : This function calls NtQuerySemaphore() API call of       */
    312 /*                  ntdll.dll                                                */
    313 /*                                                                           */
    314 /*  Inputs        : Handle to Semaphore                                      */
    315 /*                  Pointer to value holder                                  */
    316 /*                                                                           */
    317 /*  Globals       : None                                                     */
    318 /*                                                                           */
    319 /*  Processing    : This function calls NtQuerySemaphore() API call of       */
    320 /*                  ntdll.dll                                                */
    321 /*                                                                           */
    322 /*  Outputs       : Status of Query                                          */
    323 /*                                                                           */
    324 /*  Returns       : On SUCCESS - 0                                           */
    325 /*                  On FAILURE - -1                                          */
    326 /*                                                                           */
    327 /*  Issues        : None                                                     */
    328 /*                                                                           */
    329 /*  Revision History:                                                        */
    330 /*                                                                           */
    331 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    332 /*         30 03 2006   Ittiam          Draft                                */
    333 /*                                                                           */
    334 /*****************************************************************************/
    335