blob: 33de7f6e42008d66816d2f9575fd25de51e47cf5 [file] [log] [blame]
Wolfgang Denk4646d2a2006-05-30 15:56:48 +02001/**
2 * @file IxOsalOsSemaphore.c (eCos)
3 *
4 * @brief Implementation for semaphore and mutex.
5 *
6 *
7 * @par
8 * IXP400 SW Release version 1.5
9 *
10 * -- Copyright Notice --
11 *
12 * @par
13 * Copyright 2001-2005, Intel Corporation.
14 * All rights reserved.
15 *
16 * @par
Wolfgang Denkc57eadc2013-07-28 22:12:47 +020017 * SPDX-License-Identifier: BSD-3-Clause
Wolfgang Denk4646d2a2006-05-30 15:56:48 +020018 * @par
19 * -- End of Copyright Notice --
20 */
21
22#include "IxOsal.h"
23#include "IxNpeMhReceive_p.h"
24
25/* Define a large number */
26#define IX_OSAL_MAX_LONG (0x7FFFFFFF)
27
28/* Max timeout in MS, used to guard against possible overflow */
29#define IX_OSAL_MAX_TIMEOUT_MS (IX_OSAL_MAX_LONG/HZ)
30
31
32PUBLIC IX_STATUS
33ixOsalSemaphoreInit (IxOsalSemaphore * sid, UINT32 start_value)
34{
35 diag_printf("%s called\n", __FUNCTION__);
36 return IX_SUCCESS;
37}
38
39/**
40 * DESCRIPTION: If the semaphore is 'empty', the calling thread is blocked.
41 * If the semaphore is 'full', it is taken and control is returned
42 * to the caller. If the time indicated in 'timeout' is reached,
43 * the thread will unblock and return an error indication. If the
44 * timeout is set to 'IX_OSAL_WAIT_NONE', the thread will never block;
45 * if it is set to 'IX_OSAL_WAIT_FOREVER', the thread will block until
46 * the semaphore is available.
47 *
48 *
49 */
50
51
52PUBLIC IX_STATUS
53ixOsalSemaphoreWait (IxOsalOsSemaphore * sid, INT32 timeout)
54{
55 diag_printf("%s called\n", __FUNCTION__);
56 return IX_SUCCESS;
57}
58
59/*
60 * Attempt to get semaphore, return immediately,
61 * no error info because users expect some failures
62 * when using this API.
63 */
64PUBLIC IX_STATUS
65ixOsalSemaphoreTryWait (IxOsalSemaphore * sid)
66{
67 diag_printf("%s called\n", __FUNCTION__);
68 return IX_FAIL;
69}
70
71/**
72 *
73 * DESCRIPTION: This function causes the next available thread in the pend queue
74 * to be unblocked. If no thread is pending on this semaphore, the
75 * semaphore becomes 'full'.
76 */
77PUBLIC IX_STATUS
78ixOsalSemaphorePost (IxOsalSemaphore * sid)
79{
80 diag_printf("%s called\n", __FUNCTION__);
81 return IX_SUCCESS;
82}
83
84PUBLIC IX_STATUS
85ixOsalSemaphoreGetValue (IxOsalSemaphore * sid, UINT32 * value)
86{
87 diag_printf("%s called\n", __FUNCTION__);
88 return IX_FAIL;
89}
90
91PUBLIC IX_STATUS
92ixOsalSemaphoreDestroy (IxOsalSemaphore * sid)
93{
94 diag_printf("%s called\n", __FUNCTION__);
95 return IX_FAIL;
96}
97
98/****************************
99 * Mutex
100 ****************************/
101
102static void drv_mutex_init(IxOsalMutex *mutex)
103{
104 *mutex = 0;
105}
106
107static void drv_mutex_destroy(IxOsalMutex *mutex)
108{
109 *mutex = -1;
110}
111
112static int drv_mutex_trylock(IxOsalMutex *mutex)
113{
York Sun4a598092013-04-01 11:29:11 -0700114 int result = true;
Wolfgang Denk4646d2a2006-05-30 15:56:48 +0200115
116 if (*mutex == 1)
York Sun4a598092013-04-01 11:29:11 -0700117 result = false;
Wolfgang Denk4646d2a2006-05-30 15:56:48 +0200118
119 return result;
120}
121
122static void drv_mutex_unlock(IxOsalMutex *mutex)
123{
124 if (*mutex == 1)
125 printf("Trying to unlock unlocked mutex!");
126
127 *mutex = 0;
128}
129
130PUBLIC IX_STATUS
131ixOsalMutexInit (IxOsalMutex * mutex)
132{
133 drv_mutex_init(mutex);
134 return IX_SUCCESS;
135}
136
137PUBLIC IX_STATUS
138ixOsalMutexLock (IxOsalMutex * mutex, INT32 timeout)
139{
140 int tries;
141
142 if (timeout == IX_OSAL_WAIT_NONE) {
143 if (drv_mutex_trylock(mutex))
144 return IX_SUCCESS;
145 else
146 return IX_FAIL;
147 }
148
149 tries = (timeout * 1000) / 50;
150 while (1) {
151 if (drv_mutex_trylock(mutex))
152 return IX_SUCCESS;
153 if (timeout != IX_OSAL_WAIT_FOREVER && tries-- <= 0)
154 break;
155 udelay(50);
156 }
157 return IX_FAIL;
158}
159
160PUBLIC IX_STATUS
161ixOsalMutexUnlock (IxOsalMutex * mutex)
162{
163 drv_mutex_unlock(mutex);
164 return IX_SUCCESS;
165}
166
167/*
168 * Attempt to get mutex, return immediately,
169 * no error info because users expect some failures
170 * when using this API.
171 */
172PUBLIC IX_STATUS
173ixOsalMutexTryLock (IxOsalMutex * mutex)
174{
175 if (drv_mutex_trylock(mutex))
176 return IX_SUCCESS;
177 return IX_FAIL;
178}
179
180PUBLIC IX_STATUS
181ixOsalMutexDestroy (IxOsalMutex * mutex)
182{
183 drv_mutex_destroy(mutex);
184 return IX_SUCCESS;
185}
186
187PUBLIC IX_STATUS
188ixOsalFastMutexInit (IxOsalFastMutex * mutex)
189{
190 return ixOsalMutexInit(mutex);
191}
192
193PUBLIC IX_STATUS ixOsalFastMutexTryLock(IxOsalFastMutex *mutex)
194{
195 return ixOsalMutexTryLock(mutex);
196}
197
198
199PUBLIC IX_STATUS
200ixOsalFastMutexUnlock (IxOsalFastMutex * mutex)
201{
202 return ixOsalMutexUnlock(mutex);
203}
204
205PUBLIC IX_STATUS
206ixOsalFastMutexDestroy (IxOsalFastMutex * mutex)
207{
208 return ixOsalMutexDestroy(mutex);
209}