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_mutex.c */ 24 /* */ 25 /* Description : This file contains all the necessary function */ 26 /* definitions required to operate on mutex */ 27 /* */ 28 /* List of Functions : osal_get_mutex_handle_size */ 29 /* osal_mutex_create */ 30 /* osal_mutex_destroy */ 31 /* osal_mutex_lock */ 32 /* osal_mutex_lock_timed */ 33 /* osal_mutex_unlock */ 34 /* */ 35 /* Issues / Problems : None */ 36 /* */ 37 /* Revision History : */ 38 /* */ 39 /* DD MM YYYY Author(s) Changes (Describe the changes made) */ 40 /* 20 03 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 <semaphore.h> 53 #include <pthread.h> 54 #include <time.h> 55 56 /* User include files */ 57 #include "cast_types.h" 58 #include "osal.h" 59 #include "osal_handle.h" 60 #include "osal_mutex.h" 61 62 /*****************************************************************************/ 63 /* */ 64 /* Function Name : osal_mutex_create */ 65 /* */ 66 /* Description : This function creates the mutex and returns the handle */ 67 /* to the user. */ 68 /* */ 69 /* Inputs : OSAL handle */ 70 /* Pointer to Memory manager handle */ 71 /* */ 72 /* Globals : None */ 73 /* */ 74 /* Processing : Allocates memory for Mutex handle and calls OS specific */ 75 /* mutex create API call. */ 76 /* */ 77 /* Outputs : Mutex handle */ 78 /* */ 79 /* Returns : On SUCCESS - Mutex handle */ 80 /* On FAILURE - NULL */ 81 /* */ 82 /* Issues : None */ 83 /* */ 84 /* Revision History: */ 85 /* */ 86 /* DD MM YYYY Author(s) Changes (Describe the changes made) */ 87 /* 20 03 2006 Ittiam Draft */ 88 /* */ 89 /*****************************************************************************/ 90 91 void *osal_mutex_create(IN void *osal_handle) 92 { 93 void *mmr_handle = 0; 94 95 /* Currenlty naming semaphores is not supported */ 96 { 97 osal_t *handle = osal_handle; 98 mutex_handle_t *mutex_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 /* Allocate memory for the Handle */ 107 mutex_handle = handle->alloc(mmr_handle, sizeof(mutex_handle_t)); 108 109 /* Error in memory allocation */ 110 if(0 == mutex_handle) 111 return 0; 112 113 mutex_handle->mmr_handle = mmr_handle; 114 mutex_handle->hdl = handle; 115 116 /* Create semaphore */ 117 if(0 != pthread_mutex_init(&(mutex_handle->mutex_handle), NULL)) 118 { 119 handle->free(mmr_handle, mutex_handle); 120 return 0; 121 } 122 123 return mutex_handle; 124 } 125 } 126 127 /*****************************************************************************/ 128 /* */ 129 /* Function Name : osal_mutex_destroy */ 130 /* */ 131 /* Description : This function destroys the mutex. */ 132 /* */ 133 /* Inputs : Mutex Handle */ 134 /* */ 135 /* Globals : None */ 136 /* */ 137 /* Processing : This function destroys the mutex refernced by the handle */ 138 /* and frees the memory held by the handle. */ 139 /* */ 140 /* Outputs : Status of mutex destroy */ 141 /* */ 142 /* Returns : On SUCCESS - 0 */ 143 /* On FAILURE - -1 */ 144 /* */ 145 /* Issues : None */ 146 /* */ 147 /* Revision History: */ 148 /* */ 149 /* DD MM YYYY Author(s) Changes (Describe the changes made) */ 150 /* 22 03 2006 Ittiam Draft */ 151 /* */ 152 /*****************************************************************************/ 153 154 WORD32 osal_mutex_destroy(IN void *mutex_handle) 155 { 156 if(0 == mutex_handle) 157 return OSAL_ERROR; 158 159 { 160 mutex_handle_t *handle = (mutex_handle_t *)mutex_handle; 161 WORD32 status = 0; 162 163 if(0 == handle->hdl || 0 == handle->hdl->free) 164 return OSAL_ERROR; 165 166 /* Destroy the mutex */ 167 status = pthread_mutex_destroy(&(handle->mutex_handle)); 168 169 if(0 != status) 170 return OSAL_ERROR; 171 172 /* Free the handle */ 173 handle->hdl->free(handle->mmr_handle, handle); 174 return OSAL_SUCCESS; 175 } 176 } 177 178 /*****************************************************************************/ 179 /* */ 180 /* Function Name : osal_mutex_lock */ 181 /* */ 182 /* Description : This function locks the mutex. */ 183 /* */ 184 /* Inputs : Mutex handle */ 185 /* */ 186 /* Globals : None */ 187 /* */ 188 /* Processing : Calls OS specific mutex lock API. */ 189 /* */ 190 /* Outputs : Status of mutex lock */ 191 /* */ 192 /* Returns : On SUCCESS - 0 */ 193 /* On FAILURE - -1 */ 194 /* */ 195 /* Issues : None */ 196 /* */ 197 /* Revision History: */ 198 /* */ 199 /* DD MM YYYY Author(s) Changes (Describe the changes made) */ 200 /* 22 03 2006 Ittiam Draft */ 201 /* */ 202 /*****************************************************************************/ 203 204 WORD32 osal_mutex_lock(IN void *mutex_handle) 205 { 206 if(0 == mutex_handle) 207 return OSAL_ERROR; 208 209 { 210 mutex_handle_t *handle = (mutex_handle_t *)mutex_handle; 211 212 /* Wait on mutex lock */ 213 return pthread_mutex_lock(&(handle->mutex_handle)); 214 } 215 } 216 217 /*****************************************************************************/ 218 /* */ 219 /* Function Name : osal_mutex_unlock */ 220 /* */ 221 /* Description : This function unlocks the mutex */ 222 /* */ 223 /* Inputs : Mutex handle */ 224 /* */ 225 /* Globals : None */ 226 /* */ 227 /* Processing : Calls OS specific unlock mutex API. */ 228 /* */ 229 /* Outputs : Status of mutex unlock */ 230 /* */ 231 /* Returns : On SUCCESS - 0 */ 232 /* On FAILURE - -1 */ 233 /* */ 234 /* Issues : None */ 235 /* */ 236 /* Revision History: */ 237 /* */ 238 /* DD MM YYYY Author(s) Changes (Describe the changes made) */ 239 /* 22 03 2006 Ittiam Draft */ 240 /* */ 241 /*****************************************************************************/ 242 243 WORD32 osal_mutex_unlock(IN void *mutex_handle) 244 { 245 if(0 == mutex_handle) 246 return OSAL_ERROR; 247 248 { 249 mutex_handle_t *handle = (mutex_handle_t *)mutex_handle; 250 251 /* Release the lock */ 252 if(0 == pthread_mutex_unlock(&(handle->mutex_handle))) 253 return OSAL_SUCCESS; 254 255 return OSAL_ERROR; 256 } 257 } 258