IxOsalOsSemaphore.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  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. #include "IxOsal.h"
  46. #include "IxNpeMhReceive_p.h"
  47. /* Define a large number */
  48. #define IX_OSAL_MAX_LONG (0x7FFFFFFF)
  49. /* Max timeout in MS, used to guard against possible overflow */
  50. #define IX_OSAL_MAX_TIMEOUT_MS (IX_OSAL_MAX_LONG/HZ)
  51. PUBLIC IX_STATUS
  52. ixOsalSemaphoreInit (IxOsalSemaphore * sid, UINT32 start_value)
  53. {
  54. diag_printf("%s called\n", __FUNCTION__);
  55. return IX_SUCCESS;
  56. }
  57. /**
  58. * DESCRIPTION: If the semaphore is 'empty', the calling thread is blocked.
  59. * If the semaphore is 'full', it is taken and control is returned
  60. * to the caller. If the time indicated in 'timeout' is reached,
  61. * the thread will unblock and return an error indication. If the
  62. * timeout is set to 'IX_OSAL_WAIT_NONE', the thread will never block;
  63. * if it is set to 'IX_OSAL_WAIT_FOREVER', the thread will block until
  64. * the semaphore is available.
  65. *
  66. *
  67. */
  68. PUBLIC IX_STATUS
  69. ixOsalSemaphoreWait (IxOsalOsSemaphore * sid, INT32 timeout)
  70. {
  71. diag_printf("%s called\n", __FUNCTION__);
  72. return IX_SUCCESS;
  73. }
  74. /*
  75. * Attempt to get semaphore, return immediately,
  76. * no error info because users expect some failures
  77. * when using this API.
  78. */
  79. PUBLIC IX_STATUS
  80. ixOsalSemaphoreTryWait (IxOsalSemaphore * sid)
  81. {
  82. diag_printf("%s called\n", __FUNCTION__);
  83. return IX_FAIL;
  84. }
  85. /**
  86. *
  87. * DESCRIPTION: This function causes the next available thread in the pend queue
  88. * to be unblocked. If no thread is pending on this semaphore, the
  89. * semaphore becomes 'full'.
  90. */
  91. PUBLIC IX_STATUS
  92. ixOsalSemaphorePost (IxOsalSemaphore * sid)
  93. {
  94. diag_printf("%s called\n", __FUNCTION__);
  95. return IX_SUCCESS;
  96. }
  97. PUBLIC IX_STATUS
  98. ixOsalSemaphoreGetValue (IxOsalSemaphore * sid, UINT32 * value)
  99. {
  100. diag_printf("%s called\n", __FUNCTION__);
  101. return IX_FAIL;
  102. }
  103. PUBLIC IX_STATUS
  104. ixOsalSemaphoreDestroy (IxOsalSemaphore * sid)
  105. {
  106. diag_printf("%s called\n", __FUNCTION__);
  107. return IX_FAIL;
  108. }
  109. /****************************
  110. * Mutex
  111. ****************************/
  112. static void drv_mutex_init(IxOsalMutex *mutex)
  113. {
  114. *mutex = 0;
  115. }
  116. static void drv_mutex_destroy(IxOsalMutex *mutex)
  117. {
  118. *mutex = -1;
  119. }
  120. static int drv_mutex_trylock(IxOsalMutex *mutex)
  121. {
  122. int result = TRUE;
  123. if (*mutex == 1)
  124. result = FALSE;
  125. return result;
  126. }
  127. static void drv_mutex_unlock(IxOsalMutex *mutex)
  128. {
  129. if (*mutex == 1)
  130. printf("Trying to unlock unlocked mutex!");
  131. *mutex = 0;
  132. }
  133. PUBLIC IX_STATUS
  134. ixOsalMutexInit (IxOsalMutex * mutex)
  135. {
  136. drv_mutex_init(mutex);
  137. return IX_SUCCESS;
  138. }
  139. PUBLIC IX_STATUS
  140. ixOsalMutexLock (IxOsalMutex * mutex, INT32 timeout)
  141. {
  142. int tries;
  143. if (timeout == IX_OSAL_WAIT_NONE) {
  144. if (drv_mutex_trylock(mutex))
  145. return IX_SUCCESS;
  146. else
  147. return IX_FAIL;
  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. PUBLIC IX_STATUS
  160. ixOsalMutexUnlock (IxOsalMutex * mutex)
  161. {
  162. drv_mutex_unlock(mutex);
  163. return IX_SUCCESS;
  164. }
  165. /*
  166. * Attempt to get mutex, return immediately,
  167. * no error info because users expect some failures
  168. * when using this API.
  169. */
  170. PUBLIC IX_STATUS
  171. ixOsalMutexTryLock (IxOsalMutex * mutex)
  172. {
  173. if (drv_mutex_trylock(mutex))
  174. return IX_SUCCESS;
  175. return IX_FAIL;
  176. }
  177. PUBLIC IX_STATUS
  178. ixOsalMutexDestroy (IxOsalMutex * mutex)
  179. {
  180. drv_mutex_destroy(mutex);
  181. return IX_SUCCESS;
  182. }
  183. PUBLIC IX_STATUS
  184. ixOsalFastMutexInit (IxOsalFastMutex * mutex)
  185. {
  186. return ixOsalMutexInit(mutex);
  187. }
  188. PUBLIC IX_STATUS ixOsalFastMutexTryLock(IxOsalFastMutex *mutex)
  189. {
  190. return ixOsalMutexTryLock(mutex);
  191. }
  192. PUBLIC IX_STATUS
  193. ixOsalFastMutexUnlock (IxOsalFastMutex * mutex)
  194. {
  195. return ixOsalMutexUnlock(mutex);
  196. }
  197. PUBLIC IX_STATUS
  198. ixOsalFastMutexDestroy (IxOsalFastMutex * mutex)
  199. {
  200. return ixOsalMutexDestroy(mutex);
  201. }