blob: 443aefd4fcee368d469b492d2632c4e89576670c [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
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the Intel Corporation nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * @par
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @par
43 * -- End of Copyright Notice --
44 */
45
46#include "IxOsal.h"
47#include "IxNpeMhReceive_p.h"
48
49/* Define a large number */
50#define IX_OSAL_MAX_LONG (0x7FFFFFFF)
51
52/* Max timeout in MS, used to guard against possible overflow */
53#define IX_OSAL_MAX_TIMEOUT_MS (IX_OSAL_MAX_LONG/HZ)
54
55
56PUBLIC IX_STATUS
57ixOsalSemaphoreInit (IxOsalSemaphore * sid, UINT32 start_value)
58{
59 diag_printf("%s called\n", __FUNCTION__);
60 return IX_SUCCESS;
61}
62
63/**
64 * DESCRIPTION: If the semaphore is 'empty', the calling thread is blocked.
65 * If the semaphore is 'full', it is taken and control is returned
66 * to the caller. If the time indicated in 'timeout' is reached,
67 * the thread will unblock and return an error indication. If the
68 * timeout is set to 'IX_OSAL_WAIT_NONE', the thread will never block;
69 * if it is set to 'IX_OSAL_WAIT_FOREVER', the thread will block until
70 * the semaphore is available.
71 *
72 *
73 */
74
75
76PUBLIC IX_STATUS
77ixOsalSemaphoreWait (IxOsalOsSemaphore * sid, INT32 timeout)
78{
79 diag_printf("%s called\n", __FUNCTION__);
80 return IX_SUCCESS;
81}
82
83/*
84 * Attempt to get semaphore, return immediately,
85 * no error info because users expect some failures
86 * when using this API.
87 */
88PUBLIC IX_STATUS
89ixOsalSemaphoreTryWait (IxOsalSemaphore * sid)
90{
91 diag_printf("%s called\n", __FUNCTION__);
92 return IX_FAIL;
93}
94
95/**
96 *
97 * DESCRIPTION: This function causes the next available thread in the pend queue
98 * to be unblocked. If no thread is pending on this semaphore, the
99 * semaphore becomes 'full'.
100 */
101PUBLIC IX_STATUS
102ixOsalSemaphorePost (IxOsalSemaphore * sid)
103{
104 diag_printf("%s called\n", __FUNCTION__);
105 return IX_SUCCESS;
106}
107
108PUBLIC IX_STATUS
109ixOsalSemaphoreGetValue (IxOsalSemaphore * sid, UINT32 * value)
110{
111 diag_printf("%s called\n", __FUNCTION__);
112 return IX_FAIL;
113}
114
115PUBLIC IX_STATUS
116ixOsalSemaphoreDestroy (IxOsalSemaphore * sid)
117{
118 diag_printf("%s called\n", __FUNCTION__);
119 return IX_FAIL;
120}
121
122/****************************
123 * Mutex
124 ****************************/
125
126static void drv_mutex_init(IxOsalMutex *mutex)
127{
128 *mutex = 0;
129}
130
131static void drv_mutex_destroy(IxOsalMutex *mutex)
132{
133 *mutex = -1;
134}
135
136static int drv_mutex_trylock(IxOsalMutex *mutex)
137{
138 int result = TRUE;
139
140 if (*mutex == 1)
141 result = FALSE;
142
143 return result;
144}
145
146static void drv_mutex_unlock(IxOsalMutex *mutex)
147{
148 if (*mutex == 1)
149 printf("Trying to unlock unlocked mutex!");
150
151 *mutex = 0;
152}
153
154PUBLIC IX_STATUS
155ixOsalMutexInit (IxOsalMutex * mutex)
156{
157 drv_mutex_init(mutex);
158 return IX_SUCCESS;
159}
160
161PUBLIC IX_STATUS
162ixOsalMutexLock (IxOsalMutex * mutex, INT32 timeout)
163{
164 int tries;
165
166 if (timeout == IX_OSAL_WAIT_NONE) {
167 if (drv_mutex_trylock(mutex))
168 return IX_SUCCESS;
169 else
170 return IX_FAIL;
171 }
172
173 tries = (timeout * 1000) / 50;
174 while (1) {
175 if (drv_mutex_trylock(mutex))
176 return IX_SUCCESS;
177 if (timeout != IX_OSAL_WAIT_FOREVER && tries-- <= 0)
178 break;
179 udelay(50);
180 }
181 return IX_FAIL;
182}
183
184PUBLIC IX_STATUS
185ixOsalMutexUnlock (IxOsalMutex * mutex)
186{
187 drv_mutex_unlock(mutex);
188 return IX_SUCCESS;
189}
190
191/*
192 * Attempt to get mutex, return immediately,
193 * no error info because users expect some failures
194 * when using this API.
195 */
196PUBLIC IX_STATUS
197ixOsalMutexTryLock (IxOsalMutex * mutex)
198{
199 if (drv_mutex_trylock(mutex))
200 return IX_SUCCESS;
201 return IX_FAIL;
202}
203
204PUBLIC IX_STATUS
205ixOsalMutexDestroy (IxOsalMutex * mutex)
206{
207 drv_mutex_destroy(mutex);
208 return IX_SUCCESS;
209}
210
211PUBLIC IX_STATUS
212ixOsalFastMutexInit (IxOsalFastMutex * mutex)
213{
214 return ixOsalMutexInit(mutex);
215}
216
217PUBLIC IX_STATUS ixOsalFastMutexTryLock(IxOsalFastMutex *mutex)
218{
219 return ixOsalMutexTryLock(mutex);
220}
221
222
223PUBLIC IX_STATUS
224ixOsalFastMutexUnlock (IxOsalFastMutex * mutex)
225{
226 return ixOsalMutexUnlock(mutex);
227}
228
229PUBLIC IX_STATUS
230ixOsalFastMutexDestroy (IxOsalFastMutex * mutex)
231{
232 return ixOsalMutexDestroy(mutex);
233}