• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_semaphores.c
35 *   This file contains methods that provides the functionality
36 *
37 *  @path \
38 *
39 */
40 /* -------------------------------------------------------------------------- */
41 /* =========================================================================
42  *!
43  *! Revision History
44  *! ===================================
45  *! 30-Oct-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 #include <stdio.h>
54 
55 #include <semaphore.h>
56 #include <sys/time.h>
57 
58 
59 #include "timm_osal_types.h"
60 #include "timm_osal_trace.h"
61 #include "timm_osal_error.h"
62 #include "timm_osal_memory.h"
63 
64 
65 #define SEMNAME_MAX           7
66 
67 /*
68 typedef struct TIMM_OSAL_SEMAPHORE {
69     sem_t  sem;
70     CHAR name[SEMNAME_MAX];
71 } TIMM_OSAL_SEMAPHORE;
72 */
73 
74 /* ========================================================================== */
75 /**
76 * @fn TIMM_OSAL_SemaphoreCreate function
77 *
78 *
79 */
80 /* ========================================================================== */
TIMM_OSAL_SemaphoreCreate(TIMM_OSAL_PTR * pSemaphore,TIMM_OSAL_U32 uInitCount)81 TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreCreate(TIMM_OSAL_PTR * pSemaphore,
82     TIMM_OSAL_U32 uInitCount)
83 {
84 	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
85 	*pSemaphore = TIMM_OSAL_NULL;
86 
87 	sem_t *psem = (sem_t *) TIMM_OSAL_Malloc(sizeof(sem_t), 0, 0, 0);
88 
89 	if (TIMM_OSAL_NULL == psem)
90 	{
91 		bReturnStatus = TIMM_OSAL_ERR_ALLOC;
92 		goto EXIT;
93 	}
94 
95 	/*Unnamed semaphore */
96 	if (SUCCESS != sem_init(psem, 0, uInitCount))
97 	{
98 		/*TIMM_OSAL_Error("Semaphore Create failed !"); */
99 		/*goto EXIT; */
100 	} else
101 	{
102 		*pSemaphore = (TIMM_OSAL_PTR) psem;
103 		bReturnStatus = TIMM_OSAL_ERR_NONE;
104 	}
105       EXIT:
106 	if ((TIMM_OSAL_ERR_NONE != bReturnStatus) && (TIMM_OSAL_NULL != psem))
107 	{
108 		TIMM_OSAL_Free(psem);
109 	}
110 	return bReturnStatus;
111 }
112 
113 /* ========================================================================== */
114 /**
115 * @fn TIMM_OSAL_SemaphoreDelete function
116 *
117 *
118 */
119 /* ========================================================================== */
TIMM_OSAL_SemaphoreDelete(TIMM_OSAL_PTR pSemaphore)120 TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreDelete(TIMM_OSAL_PTR pSemaphore)
121 {
122 	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_NONE;
123 	sem_t *psem = (sem_t *) pSemaphore;
124 
125 	if (psem == TIMM_OSAL_NULL)
126 	{
127 		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
128 		goto EXIT;
129 	}
130 	/* Release the semaphore.  */
131 	if (SUCCESS != sem_destroy(psem))
132 	{
133 		/*TIMM_OSAL_Error("Semaphore Delete failed !"); */
134 		bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
135 	}
136 
137 	TIMM_OSAL_Free(psem);
138       EXIT:
139 	return bReturnStatus;
140 }
141 
142 /* ========================================================================== */
143 /**
144 * @fn TIMM_OSAL_SemaphoreObtain function
145 *
146 *
147 */
148 /* ========================================================================== */
149 
TIMM_OSAL_SemaphoreObtain(TIMM_OSAL_PTR pSemaphore,TIMM_OSAL_U32 uTimeOut)150 TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreObtain(TIMM_OSAL_PTR pSemaphore,
151     TIMM_OSAL_U32 uTimeOut)
152 {
153 	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
154 	struct timeval ltime_now;
155 	struct timespec abs_timeout;
156 	sem_t *psem = (sem_t *) pSemaphore;
157 
158 	if (psem == TIMM_OSAL_NULL)
159 	{
160 		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
161 		goto EXIT;
162 	}
163 
164 	if (TIMM_OSAL_SUSPEND == uTimeOut)
165 	{
166 		if (SUCCESS != sem_wait(psem))
167 		{
168 			/*TIMM_OSAL_Error("Semaphore Wait failed !"); */
169 			goto EXIT;
170 		}
171 
172 	} else if (TIMM_OSAL_NO_SUSPEND == uTimeOut)
173 	{
174 		if (SUCCESS != sem_trywait(psem))
175 		{
176 			/*TIMM_OSAL_Error("Semaphore blocked !"); */
177 			goto EXIT;
178 		}
179 	} else
180 	{
181 		/*Some delay in calling gettimeofday and sem_timedwait - cant
182 		   be avoided. Possibility of thread switch after gettimeofday
183 		   in which case time out will be less than expected */
184 		gettimeofday(&ltime_now, NULL);
185 		/*uTimeOut is assumed to be in milliseconds */
186 		abs_timeout.tv_sec = ltime_now.tv_sec + (uTimeOut / 1000);
187 		abs_timeout.tv_nsec =
188 		    1000 * (ltime_now.tv_usec + ((uTimeOut % 1000) * 1000));
189 
190 		if (SUCCESS != sem_timedwait(psem, &abs_timeout))
191 		{
192 			/*TIMM_OSAL_Error("Semaphore Timed Wait failed !"); */
193 			goto EXIT;
194 		}
195 	}
196 	bReturnStatus = TIMM_OSAL_ERR_NONE;
197 
198       EXIT:
199 	return bReturnStatus;
200 }
201 
202 /* ========================================================================== */
203 /**
204 * @fn TIMM_OSAL_SemaphoreRelease function
205 *
206 *
207 */
208 /* ========================================================================== */
TIMM_OSAL_SemaphoreRelease(TIMM_OSAL_PTR pSemaphore)209 TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreRelease(TIMM_OSAL_PTR pSemaphore)
210 {
211 	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
212 	sem_t *psem = (sem_t *) pSemaphore;
213 
214 	if (TIMM_OSAL_NULL == psem)
215 	{
216 		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
217 		goto EXIT;
218 	}
219 	/* Release the semaphore.  */
220 	if (SUCCESS != sem_post(psem))
221 	{
222 		/*TIMM_OSAL_Error("Release failed !"); */
223 	} else
224 	{
225 		bReturnStatus = TIMM_OSAL_ERR_NONE;
226 	}
227 
228       EXIT:
229 	return bReturnStatus;
230 }
231 
232 /* ========================================================================== */
233 /**
234 * @fn TIMM_OSAL_SemaphoreReset function
235 *
236 *
237 */
238 /* ========================================================================== */
TIMM_OSAL_SemaphoreReset(TIMM_OSAL_PTR pSemaphore,TIMM_OSAL_U32 uInitCount)239 TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreReset(TIMM_OSAL_PTR pSemaphore,
240     TIMM_OSAL_U32 uInitCount)
241 {
242 	/*  TIMM_OSAL_SEMAPHORE *pHandle = (TIMM_OSAL_SEMAPHORE *)pSemaphore;
243 	   STATUS  status;
244 	   TIMM_OSAL_ERRORTYPE bReturnStatus; */
245 
246 
247 	/* Release the semaphore.  */
248 	/*status = NU_Reset_Semaphore(&(pHandle->sem),
249 	   uInitCount);
250 	 */
251 	/*   switch(status)
252 	   {
253 	   case NU_SUCCESS:
254 	   bReturnStatus = TIMM_OSAL_ERR_NONE;
255 	   break;
256 	   default:
257 	   bReturnStatus = TIMM_OSAL_ERR_CREATE(TIMM_OSAL_ERR, TIMM_OSAL_COMP_SEMAPHORES, status);
258 	   break;
259 	   }
260 	 */
261 	/* return bReturnStatus; */
262 	return TIMM_OSAL_ERR_UNKNOWN;
263 }
264 
265 /* ========================================================================== */
266 /**
267 * @fn TIMM_OSAL_GetSemaphoreCount function
268 *
269 *
270 */
271 /* ========================================================================== */
TIMM_OSAL_GetSemaphoreCount(TIMM_OSAL_PTR pSemaphore,TIMM_OSAL_U32 * count)272 TIMM_OSAL_ERRORTYPE TIMM_OSAL_GetSemaphoreCount(TIMM_OSAL_PTR pSemaphore,
273     TIMM_OSAL_U32 * count)
274 {
275 	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
276 	int sval = -2;		/*value that is not possible */
277 	sem_t *psem = (sem_t *) pSemaphore;
278 
279 	if (TIMM_OSAL_NULL == psem)
280 	{
281 		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
282 		goto EXIT;
283 	}
284 
285 	/* Release the semaphore.  */
286 	if (SUCCESS != sem_getvalue(psem, &sval))
287 	{
288 		/*TIMM_OSAL_Error("Get Semaphore Count failed !"); */
289 	} else
290 	{
291 		*count = sval;
292 		bReturnStatus = TIMM_OSAL_ERR_NONE;
293 	}
294 
295       EXIT:
296 	return bReturnStatus;
297 }
298