aboutsummaryrefslogtreecommitdiff
path: root/encoder/osal_cond_var.c
blob: 434763cc3b3f10ce8b1dc1ed1bed4c304d33a3f1 (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
/******************************************************************************
 *
 * 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_cond_var.c                                      */
/*                                                                           */
/*  Description       : This file contains all the necessary function        */
/*                      definitions required to operate on Conditional       */
/*                      Variable.                                            */
/*                                                                           */
/*  List of Functions : osal_cond_var_create                                 */
/*                      osal_cond_var_destroy                                */
/*                      osal_cond_var_wait                                   */
/*                      osal_cond_var_wait_timed                             */
/*                      osal_cond_var_signal                                 */
/*                                                                           */
/*  Issues / Problems : None                                                 */
/*                                                                           */
/*  Revision History  :                                                      */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         05 09 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

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

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

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

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

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_cond_var_create                                     */
/*                                                                           */
/*  Description   : This function initializes the conditional variable and   */
/*                  returns the handle to it.                                */
/*                                                                           */
/*  Inputs        : OSAL handle                                              */
/*                  Memory manager handle                                    */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Calls system specific API and returns handle to the      */
/*                  conditional variable.                                    */
/*                                                                           */
/*  Outputs       : Handle to Condtional Variable                            */
/*                                                                           */
/*  Returns       : On SUCCESS - Handle to Conditional Varaible              */
/*                  On FAILURE - NULL                                        */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         05 09 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

void *osal_cond_var_create(IN void *osal_handle)
{
    if(0 == osal_handle)
        return 0;

    {
        osal_t *handle = osal_handle;
        cond_var_handle_t *cond_var_handle = 0;
        void *mmr_handle = 0;

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

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

        /* Allocate memory for the Handle */
        cond_var_handle = handle->alloc(mmr_handle, sizeof(cond_var_handle_t));

        /* Error in memory allocation */
        if(0 == cond_var_handle)
            return 0;

        cond_var_handle->mmr_handle = mmr_handle;
        cond_var_handle->hdl = handle;

        /* Create semaphore */
        if(0 != pthread_cond_init(&(cond_var_handle->cond_var), 0))
        {
            handle->free(mmr_handle, cond_var_handle);
            return 0;
        }

        return cond_var_handle;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_cond_var_destroy                                    */
/*                                                                           */
/*  Description   : This function destroys all the OS resources allocated by */
/*                  'osal_cond_var_create' API.                              */
/*                                                                           */
/*  Inputs        : Conditional Variable handle                              */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Validates the input and destroys all the OS allocated    */
/*                  resources.                                               */
/*                                                                           */
/*  Outputs       : Status of closure                                        */
/*                                                                           */
/*  Returns       : On SUCCESS - OSAL_SUCCESS                                */
/*                  On FAILURE - OSAL_ERROR                                  */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         10 05 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_cond_var_destroy(IN void *cond_var_handle)
{
    if(0 == cond_var_handle)
        return OSAL_ERROR;

    {
        cond_var_handle_t *handle = (cond_var_handle_t *)cond_var_handle;
        WORD32 status = 0;

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

        /* Destroy the mutex */
        status = pthread_cond_destroy(&(handle->cond_var));

        if(0 != status)
            return OSAL_ERROR;

        /* Free the handle */
        handle->hdl->free(handle->mmr_handle, handle);
        return OSAL_SUCCESS;
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_cond_var_wait                                       */
/*                                                                           */
/*  Description   : This function waits infinitely on conditional varaiable. */
/*                                                                           */
/*  Inputs        : Conditional Variable handle                              */
/*                  Mutex handle for lock                                    */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : This function waits on Conditional variable signal. Till */
/*                  signal is not, lock on mutex is relinquished.            */
/*                                                                           */
/*  Outputs       : Status of wait on conditional variable                   */
/*                                                                           */
/*  Returns       : On SUCCESS - OSAL_SUCCESS                                */
/*                  On FAILURE - OSAL_ERROR                                  */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         10 05 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_cond_var_wait(IN void *cond_var_handle, IN void *mutex_handle)
{
    if(0 == cond_var_handle || 0 == mutex_handle)
        return OSAL_ERROR;

    {
        mutex_handle_t *mutex = (mutex_handle_t *)mutex_handle;
        cond_var_handle_t *cond_var = (cond_var_handle_t *)cond_var_handle;

        return pthread_cond_wait(&(cond_var->cond_var), &(mutex->mutex_handle));
    }
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : osal_cond_var_signal                                     */
/*                                                                           */
/*  Description   : This function signals on a conditional variable.         */
/*                                                                           */
/*  Inputs        : Conditional Variable handle                              */
/*                                                                           */
/*  Globals       : None                                                     */
/*                                                                           */
/*  Processing    : Calls the underlaying API to signal on a conditional     */
/*                  variable.                                                */
/*                                                                           */
/*  Outputs       : Status of signalling                                     */
/*                                                                           */
/*  Returns       : On SUCCESS - OSAL_SUCCESS                                */
/*                  On FAILURE - OSAL_ERROR                                  */
/*                                                                           */
/*  Issues        : None                                                     */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         10 05 2006   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 osal_cond_var_signal(IN void *cond_var_handle)
{
    if(0 == cond_var_handle)
        return OSAL_ERROR;

    {
        cond_var_handle_t *cond_var = (cond_var_handle_t *)cond_var_handle;
        return pthread_cond_signal(&(cond_var->cond_var));
    }
}