summaryrefslogtreecommitdiffstats
path: root/domx/mm_osal/src/timm_osal_semaphores.c
blob: b69ee3c4d6fdd886d22e85b9d70109710c719c69 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (c) 2010, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
*   @file  timm_osal_semaphores.c
*   This file contains methods that provides the functionality
*
*  @path \
*
*/
/* -------------------------------------------------------------------------- */
/* =========================================================================
 *!
 *! Revision History
 *! ===================================
 *! 30-Oct-2008 Maiya ShreeHarsha: Linux specific changes
 *! 0.1: Created the first draft version, ksrini@ti.com
 * ========================================================================= */

/******************************************************************************
* Includes
******************************************************************************/

#include <stdio.h>

#include <semaphore.h>
#include <sys/time.h>


#include "timm_osal_types.h"
#include "timm_osal_trace.h"
#include "timm_osal_error.h"
#include "timm_osal_memory.h"


#define SEMNAME_MAX           7

/*
typedef struct TIMM_OSAL_SEMAPHORE {
    sem_t  sem;
    CHAR name[SEMNAME_MAX];
} TIMM_OSAL_SEMAPHORE;
*/

/* ========================================================================== */
/**
* @fn TIMM_OSAL_SemaphoreCreate function
*
*
*/
/* ========================================================================== */
TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreCreate(TIMM_OSAL_PTR * pSemaphore,
    TIMM_OSAL_U32 uInitCount)
{
	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
	*pSemaphore = TIMM_OSAL_NULL;

	sem_t *psem = (sem_t *) TIMM_OSAL_Malloc(sizeof(sem_t), 0, 0, 0);

	if (TIMM_OSAL_NULL == psem)
	{
		bReturnStatus = TIMM_OSAL_ERR_ALLOC;
		goto EXIT;
	}

	/*Unnamed semaphore */
	if (SUCCESS != sem_init(psem, 0, uInitCount))
	{
		/*TIMM_OSAL_Error("Semaphore Create failed !"); */
		/*goto EXIT; */
	} else
	{
		*pSemaphore = (TIMM_OSAL_PTR) psem;
		bReturnStatus = TIMM_OSAL_ERR_NONE;
	}
      EXIT:
	if ((TIMM_OSAL_ERR_NONE != bReturnStatus) && (TIMM_OSAL_NULL != psem))
	{
		TIMM_OSAL_Free(psem);
	}
	return bReturnStatus;
}

/* ========================================================================== */
/**
* @fn TIMM_OSAL_SemaphoreDelete function
*
*
*/
/* ========================================================================== */
TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreDelete(TIMM_OSAL_PTR pSemaphore)
{
	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_NONE;
	sem_t *psem = (sem_t *) pSemaphore;

	if (psem == TIMM_OSAL_NULL)
	{
		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
		goto EXIT;
	}
	/* Release the semaphore.  */
	if (SUCCESS != sem_destroy(psem))
	{
		/*TIMM_OSAL_Error("Semaphore Delete failed !"); */
		bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
	}

	TIMM_OSAL_Free(psem);
      EXIT:
	return bReturnStatus;
}

/* ========================================================================== */
/**
* @fn TIMM_OSAL_SemaphoreObtain function
*
*
*/
/* ========================================================================== */

TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreObtain(TIMM_OSAL_PTR pSemaphore,
    TIMM_OSAL_U32 uTimeOut)
{
	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
	struct timeval ltime_now;
	struct timespec abs_timeout;
	sem_t *psem = (sem_t *) pSemaphore;

	if (psem == TIMM_OSAL_NULL)
	{
		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
		goto EXIT;
	}

	if (TIMM_OSAL_SUSPEND == uTimeOut)
	{
		if (SUCCESS != sem_wait(psem))
		{
			/*TIMM_OSAL_Error("Semaphore Wait failed !"); */
			goto EXIT;
		}

	} else if (TIMM_OSAL_NO_SUSPEND == uTimeOut)
	{
		if (SUCCESS != sem_trywait(psem))
		{
			/*TIMM_OSAL_Error("Semaphore blocked !"); */
			goto EXIT;
		}
	} else
	{
		/*Some delay in calling gettimeofday and sem_timedwait - cant
		   be avoided. Possibility of thread switch after gettimeofday
		   in which case time out will be less than expected */
		gettimeofday(&ltime_now, NULL);
		/*uTimeOut is assumed to be in milliseconds */
		abs_timeout.tv_sec = ltime_now.tv_sec + (uTimeOut / 1000);
		abs_timeout.tv_nsec =
		    1000 * (ltime_now.tv_usec + ((uTimeOut % 1000) * 1000));

		if (SUCCESS != sem_timedwait(psem, &abs_timeout))
		{
			/*TIMM_OSAL_Error("Semaphore Timed Wait failed !"); */
			goto EXIT;
		}
	}
	bReturnStatus = TIMM_OSAL_ERR_NONE;

      EXIT:
	return bReturnStatus;
}

/* ========================================================================== */
/**
* @fn TIMM_OSAL_SemaphoreRelease function
*
*
*/
/* ========================================================================== */
TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreRelease(TIMM_OSAL_PTR pSemaphore)
{
	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
	sem_t *psem = (sem_t *) pSemaphore;

	if (TIMM_OSAL_NULL == psem)
	{
		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
		goto EXIT;
	}
	/* Release the semaphore.  */
	if (SUCCESS != sem_post(psem))
	{
		/*TIMM_OSAL_Error("Release failed !"); */
	} else
	{
		bReturnStatus = TIMM_OSAL_ERR_NONE;
	}

      EXIT:
	return bReturnStatus;
}

/* ========================================================================== */
/**
* @fn TIMM_OSAL_SemaphoreReset function
*
*
*/
/* ========================================================================== */
TIMM_OSAL_ERRORTYPE TIMM_OSAL_SemaphoreReset(TIMM_OSAL_PTR pSemaphore,
    TIMM_OSAL_U32 uInitCount)
{
	/*  TIMM_OSAL_SEMAPHORE *pHandle = (TIMM_OSAL_SEMAPHORE *)pSemaphore;
	   STATUS  status;
	   TIMM_OSAL_ERRORTYPE bReturnStatus; */


	/* Release the semaphore.  */
	/*status = NU_Reset_Semaphore(&(pHandle->sem),
	   uInitCount);
	 */
	/*   switch(status)
	   {
	   case NU_SUCCESS:
	   bReturnStatus = TIMM_OSAL_ERR_NONE;
	   break;
	   default:
	   bReturnStatus = TIMM_OSAL_ERR_CREATE(TIMM_OSAL_ERR, TIMM_OSAL_COMP_SEMAPHORES, status);
	   break;
	   }
	 */
	/* return bReturnStatus; */
	return TIMM_OSAL_ERR_UNKNOWN;
}

/* ========================================================================== */
/**
* @fn TIMM_OSAL_GetSemaphoreCount function
*
*
*/
/* ========================================================================== */
TIMM_OSAL_ERRORTYPE TIMM_OSAL_GetSemaphoreCount(TIMM_OSAL_PTR pSemaphore,
    TIMM_OSAL_U32 * count)
{
	TIMM_OSAL_ERRORTYPE bReturnStatus = TIMM_OSAL_ERR_UNKNOWN;
	int sval = -2;		/*value that is not possible */
	sem_t *psem = (sem_t *) pSemaphore;

	if (TIMM_OSAL_NULL == psem)
	{
		bReturnStatus = TIMM_OSAL_ERR_PARAMETER;
		goto EXIT;
	}

	/* Release the semaphore.  */
	if (SUCCESS != sem_getvalue(psem, &sval))
	{
		/*TIMM_OSAL_Error("Get Semaphore Count failed !"); */
	} else
	{
		*count = sval;
		bReturnStatus = TIMM_OSAL_ERR_NONE;
	}

      EXIT:
	return bReturnStatus;
}