1/* 2 * Copyright (c) 2010, Texas Instruments Incorporated 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of Texas Instruments Incorporated nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/* 34* @file timm_osal_Mutexs.c 35* This file contains methods that provides the functionality 36* 37* @path \ 38* 39*/ 40/* -------------------------------------------------------------------------- */ 41/* ========================================================================= 42 *! 43 *! Revision History 44 *! =================================== 45 *! 04-Nov-2008 Maiya ShreeHarsha: Linux specific changes 46 *! 0.1: Created the first draft version, ksrini@ti.com 47 * ========================================================================= */ 48 49/****************************************************************************** 50* Includes 51******************************************************************************/ 52 53 54#include "timm_osal_types.h" 55#include "timm_osal_trace.h" 56#include "timm_osal_error.h" 57#include "timm_osal_memory.h" 58#include "timm_osal_semaphores.h" 59 60#include <errno.h> 61 62#include <pthread.h> 63#include <sys/time.h> 64 65 66/* ========================================================================== */ 67/** 68* @fn TIMM_OSAL_MutexCreate function 69* 70* 71*/ 72/* ========================================================================== */ 73TIMM_OSAL_ERRORTYPE TIMM_OSAL_MutexCreate(TIMM_OSAL_PTR * pMutex) 74{ 75 TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN; 76 pthread_mutex_t *plMutex = TIMM_OSAL_NULL; 77 78 plMutex = 79 (pthread_mutex_t *) TIMM_OSAL_Malloc(sizeof(pthread_mutex_t), 0, 80 0, 0); 81 if (TIMM_OSAL_NULL == plMutex) 82 { 83 bReturnStatus = TIMM_OSAL_ERR_ALLOC; 84 goto EXIT; 85 } 86 87 /*if (SUCCESS != pthread_mutex_init(plMutex, pAttr)) */ 88 if (SUCCESS != pthread_mutex_init(plMutex, TIMM_OSAL_NULL)) 89 { 90 /*TIMM_OSAL_Error ("Mutex Create failed !"); */ 91 /*goto EXIT; */ 92 } else 93 { 94 /**pMutex = (TIMM_OSAL_PTR *)plMutex;*/ 95 *pMutex = (TIMM_OSAL_PTR) plMutex; 96 bReturnStatus = TIMM_OSAL_ERR_NONE; 97 } 98 EXIT: 99 /*if((TIMM_OSAL_ERR_NONE != bReturnStatus)) { 100 TIMM_OSAL_Free(plMutex); 101 } */ 102 if ((TIMM_OSAL_ERR_NONE != bReturnStatus) && 103 (TIMM_OSAL_NULL != plMutex)) 104 { 105 TIMM_OSAL_Free(plMutex); 106 } 107 return bReturnStatus; 108 109/**********************************************************/ 110/*return TIMM_OSAL_SemaphoreCreate(pMutex, 1);*/ 111/**********************************************************/ 112} 113 114/* ========================================================================== */ 115/** 116* @fn TIMM_OSAL_MutexDelete function 117* 118* 119*/ 120/* ========================================================================== */ 121TIMM_OSAL_ERRORTYPE TIMM_OSAL_MutexDelete(TIMM_OSAL_PTR pMutex) 122{ 123 TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_NONE; 124 pthread_mutex_t *plMutex = (pthread_mutex_t *) pMutex; 125 126 if (plMutex == TIMM_OSAL_NULL) 127 { 128 bReturnStatus = TIMM_OSAL_ERR_PARAMETER; 129 goto EXIT; 130 } 131 /*can we do away with if or with switch case */ 132 if (SUCCESS != pthread_mutex_destroy(plMutex)) 133 { 134 /*TIMM_OSAL_Error("Delete Mutex failed !"); */ 135 bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;; 136 } 137 138 TIMM_OSAL_Free(plMutex); 139 EXIT: 140 return bReturnStatus; 141/**********************************************************/ 142/*return TIMM_OSAL_SemaphoreDelete(pMutex);*/ 143/**********************************************************/ 144 145} 146 147/* ========================================================================== */ 148/** 149* @fn TIMM_OSAL_MutexObtain function 150* 151* 152*/ 153/* ========================================================================== */ 154 155TIMM_OSAL_ERRORTYPE TIMM_OSAL_MutexObtain(TIMM_OSAL_PTR pMutex, 156 TIMM_OSAL_U32 uTimeOut) 157{ 158 TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN; 159 struct timespec abs_timeout; 160 struct timeval ltime_now; 161 TIMM_OSAL_U32 ltimenow_us; 162 pthread_mutex_t *plMutex = (pthread_mutex_t *) pMutex; 163 164 if (plMutex == TIMM_OSAL_NULL) 165 { 166 bReturnStatus = TIMM_OSAL_ERR_PARAMETER; 167 goto EXIT; 168 } 169 170 if (TIMM_OSAL_SUSPEND == uTimeOut) 171 { 172 if (SUCCESS != pthread_mutex_lock(plMutex)) 173 { 174 /*TIMM_OSAL_Error("Lock Mutex failed !"); */ 175 goto EXIT; 176 } 177 } else if (TIMM_OSAL_NO_SUSPEND == uTimeOut) 178 { 179 if (SUCCESS != pthread_mutex_trylock(plMutex)) 180 { 181 /*TIMM_OSAL_Error("Lock Mutex failed !"); */ 182 goto EXIT; 183 } 184 } else 185 { 186 gettimeofday(<ime_now, NULL); 187 /*uTimeOut is assumed to be in milliseconds */ 188 ltimenow_us = ltime_now.tv_usec + 1000 * uTimeOut; 189 abs_timeout.tv_sec = ltime_now.tv_sec + uTimeOut / 1000; 190 abs_timeout.tv_nsec = (ltimenow_us % 1000000) * 1000; 191 192#ifdef _POSIX_VERSION_1_ 193 if (SUCCESS != pthread_mutex_lock(plMutex)) 194 { //Some Posix versions dont support timeout 195#else 196 if (SUCCESS != pthread_mutex_timedlock(plMutex, &abs_timeout)) 197 { 198#endif 199 /*TIMM_OSAL_Error("Lock Mutex failed !"); */ 200 goto EXIT; 201 } 202 } 203 bReturnStatus = TIMM_OSAL_ERR_NONE; 204 205 EXIT: 206 return bReturnStatus; 207/**********************************************************/ 208/*return TIMM_OSAL_SemaphoreObtain(pMutex, uTimeOut);*/ 209/**********************************************************/ 210} 211 212 213 214/* ========================================================================== */ 215/** 216* @fn TIMM_OSAL_MutexRelease function 217* 218* 219*/ 220/* ========================================================================== */ 221TIMM_OSAL_ERRORTYPE TIMM_OSAL_MutexRelease(TIMM_OSAL_PTR pMutex) 222{ 223 TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN; 224 pthread_mutex_t *plMutex = (pthread_mutex_t *) pMutex; 225 226 if (TIMM_OSAL_NULL == plMutex) 227 { 228 bReturnStatus = TIMM_OSAL_ERR_PARAMETER; 229 goto EXIT; 230 } 231 232 if (SUCCESS != pthread_mutex_unlock(plMutex)) 233 { 234 /*TIMM_OSAL_Error("Unlock Mutex failed !"); */ 235 } else 236 { 237 bReturnStatus = TIMM_OSAL_ERR_NONE; 238 } 239 EXIT: 240 return bReturnStatus; 241/**********************************************************/ 242/*return TIMM_OSAL_SemaphoreRelease(pMutex);*/ 243/**********************************************************/ 244} 245