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_cond_var.c                                      */
     24 /*                                                                           */
     25 /*  Description       : This file contains all the necessary function        */
     26 /*                      definitions required to operate on Conditional       */
     27 /*                      Variable.                                            */
     28 /*                                                                           */
     29 /*  List of Functions : osal_cond_var_create                                 */
     30 /*                      osal_cond_var_destroy                                */
     31 /*                      osal_cond_var_wait                                   */
     32 /*                      osal_cond_var_wait_timed                             */
     33 /*                      osal_cond_var_signal                                 */
     34 /*                                                                           */
     35 /*  Issues / Problems : None                                                 */
     36 /*                                                                           */
     37 /*  Revision History  :                                                      */
     38 /*                                                                           */
     39 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
     40 /*         05 09 2006   Ittiam          Draft                                */
     41 /*                                                                           */
     42 /*****************************************************************************/
     43 
     44 /*****************************************************************************/
     45 /* File Includes                                                             */
     46 /*****************************************************************************/
     47 
     48 /* System include files */
     49 #include <stdio.h>
     50 
     51 #include <errno.h>
     52 #include <pthread.h>
     53 #include <stdlib.h>
     54 #include <unistd.h>
     55 #include <time.h>
     56 
     57 /* User include files */
     58 #include "cast_types.h"
     59 #include "osal.h"
     60 #include "osal_handle.h"
     61 #include "osal_mutex.h"
     62 #include "osal_cond_var.h"
     63 
     64 /*****************************************************************************/
     65 /*                                                                           */
     66 /*  Function Name : osal_cond_var_create                                     */
     67 /*                                                                           */
     68 /*  Description   : This function initializes the conditional variable and   */
     69 /*                  returns the handle to it.                                */
     70 /*                                                                           */
     71 /*  Inputs        : OSAL handle                                              */
     72 /*                  Memory manager handle                                    */
     73 /*                                                                           */
     74 /*  Globals       : None                                                     */
     75 /*                                                                           */
     76 /*  Processing    : Calls system specific API and returns handle to the      */
     77 /*                  conditional variable.                                    */
     78 /*                                                                           */
     79 /*  Outputs       : Handle to Condtional Variable                            */
     80 /*                                                                           */
     81 /*  Returns       : On SUCCESS - Handle to Conditional Varaible              */
     82 /*                  On FAILURE - NULL                                        */
     83 /*                                                                           */
     84 /*  Issues        : None                                                     */
     85 /*                                                                           */
     86 /*  Revision History:                                                        */
     87 /*                                                                           */
     88 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
     89 /*         05 09 2006   Ittiam          Draft                                */
     90 /*                                                                           */
     91 /*****************************************************************************/
     92 
     93 void *osal_cond_var_create(IN void *osal_handle)
     94 {
     95     if(0 == osal_handle)
     96         return 0;
     97 
     98     {
     99         osal_t *handle = osal_handle;
    100         cond_var_handle_t *cond_var_handle = 0;
    101         void *mmr_handle = 0;
    102 
    103         if(0 == handle || 0 == handle->alloc || 0 == handle->free)
    104             return 0;
    105 
    106         /* Initialize MMR handle */
    107         mmr_handle = handle->mmr_handle;
    108 
    109         /* Allocate memory for the Handle */
    110         cond_var_handle = handle->alloc(mmr_handle, sizeof(cond_var_handle_t));
    111 
    112         /* Error in memory allocation */
    113         if(0 == cond_var_handle)
    114             return 0;
    115 
    116         cond_var_handle->mmr_handle = mmr_handle;
    117         cond_var_handle->hdl = handle;
    118 
    119         /* Create semaphore */
    120         if(0 != pthread_cond_init(&(cond_var_handle->cond_var), 0))
    121         {
    122             handle->free(mmr_handle, cond_var_handle);
    123             return 0;
    124         }
    125 
    126         return cond_var_handle;
    127     }
    128 }
    129 
    130 /*****************************************************************************/
    131 /*                                                                           */
    132 /*  Function Name : osal_cond_var_destroy                                    */
    133 /*                                                                           */
    134 /*  Description   : This function destroys all the OS resources allocated by */
    135 /*                  'osal_cond_var_create' API.                              */
    136 /*                                                                           */
    137 /*  Inputs        : Conditional Variable handle                              */
    138 /*                                                                           */
    139 /*  Globals       : None                                                     */
    140 /*                                                                           */
    141 /*  Processing    : Validates the input and destroys all the OS allocated    */
    142 /*                  resources.                                               */
    143 /*                                                                           */
    144 /*  Outputs       : Status of closure                                        */
    145 /*                                                                           */
    146 /*  Returns       : On SUCCESS - OSAL_SUCCESS                                */
    147 /*                  On FAILURE - OSAL_ERROR                                  */
    148 /*                                                                           */
    149 /*  Issues        : None                                                     */
    150 /*                                                                           */
    151 /*  Revision History:                                                        */
    152 /*                                                                           */
    153 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    154 /*         10 05 2006   Ittiam          Draft                                */
    155 /*                                                                           */
    156 /*****************************************************************************/
    157 
    158 WORD32 osal_cond_var_destroy(IN void *cond_var_handle)
    159 {
    160     if(0 == cond_var_handle)
    161         return OSAL_ERROR;
    162 
    163     {
    164         cond_var_handle_t *handle = (cond_var_handle_t *)cond_var_handle;
    165         WORD32 status = 0;
    166 
    167         if(0 == handle->hdl || 0 == handle->hdl->free)
    168             return OSAL_ERROR;
    169 
    170         /* Destroy the mutex */
    171         status = pthread_cond_destroy(&(handle->cond_var));
    172 
    173         if(0 != status)
    174             return OSAL_ERROR;
    175 
    176         /* Free the handle */
    177         handle->hdl->free(handle->mmr_handle, handle);
    178         return OSAL_SUCCESS;
    179     }
    180 }
    181 
    182 /*****************************************************************************/
    183 /*                                                                           */
    184 /*  Function Name : osal_cond_var_wait                                       */
    185 /*                                                                           */
    186 /*  Description   : This function waits infinitely on conditional varaiable. */
    187 /*                                                                           */
    188 /*  Inputs        : Conditional Variable handle                              */
    189 /*                  Mutex handle for lock                                    */
    190 /*                                                                           */
    191 /*  Globals       : None                                                     */
    192 /*                                                                           */
    193 /*  Processing    : This function waits on Conditional variable signal. Till */
    194 /*                  signal is not, lock on mutex is relinquished.            */
    195 /*                                                                           */
    196 /*  Outputs       : Status of wait on conditional variable                   */
    197 /*                                                                           */
    198 /*  Returns       : On SUCCESS - OSAL_SUCCESS                                */
    199 /*                  On FAILURE - OSAL_ERROR                                  */
    200 /*                                                                           */
    201 /*  Issues        : None                                                     */
    202 /*                                                                           */
    203 /*  Revision History:                                                        */
    204 /*                                                                           */
    205 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    206 /*         10 05 2006   Ittiam          Draft                                */
    207 /*                                                                           */
    208 /*****************************************************************************/
    209 
    210 WORD32 osal_cond_var_wait(IN void *cond_var_handle, IN void *mutex_handle)
    211 {
    212     if(0 == cond_var_handle || 0 == mutex_handle)
    213         return OSAL_ERROR;
    214 
    215     {
    216         mutex_handle_t *mutex = (mutex_handle_t *)mutex_handle;
    217         cond_var_handle_t *cond_var = (cond_var_handle_t *)cond_var_handle;
    218 
    219         return pthread_cond_wait(&(cond_var->cond_var), &(mutex->mutex_handle));
    220     }
    221 }
    222 
    223 /*****************************************************************************/
    224 /*                                                                           */
    225 /*  Function Name : osal_cond_var_signal                                     */
    226 /*                                                                           */
    227 /*  Description   : This function signals on a conditional variable.         */
    228 /*                                                                           */
    229 /*  Inputs        : Conditional Variable handle                              */
    230 /*                                                                           */
    231 /*  Globals       : None                                                     */
    232 /*                                                                           */
    233 /*  Processing    : Calls the underlaying API to signal on a conditional     */
    234 /*                  variable.                                                */
    235 /*                                                                           */
    236 /*  Outputs       : Status of signalling                                     */
    237 /*                                                                           */
    238 /*  Returns       : On SUCCESS - OSAL_SUCCESS                                */
    239 /*                  On FAILURE - OSAL_ERROR                                  */
    240 /*                                                                           */
    241 /*  Issues        : None                                                     */
    242 /*                                                                           */
    243 /*  Revision History:                                                        */
    244 /*                                                                           */
    245 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    246 /*         10 05 2006   Ittiam          Draft                                */
    247 /*                                                                           */
    248 /*****************************************************************************/
    249 
    250 WORD32 osal_cond_var_signal(IN void *cond_var_handle)
    251 {
    252     if(0 == cond_var_handle)
    253         return OSAL_ERROR;
    254 
    255     {
    256         cond_var_handle_t *cond_var = (cond_var_handle_t *)cond_var_handle;
    257         return pthread_cond_signal(&(cond_var->cond_var));
    258     }
    259 }
    260