IxOsalOsSemaphore.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * SPDX-License-Identifier: BSD-3-Clause
  18. * @par
  19. * -- End of Copyright Notice --
  20. */
  21. #include "IxOsal.h"
  22. #include "IxNpeMhReceive_p.h"
  23. /* Define a large number */
  24. #define IX_OSAL_MAX_LONG (0x7FFFFFFF)
  25. /* Max timeout in MS, used to guard against possible overflow */
  26. #define IX_OSAL_MAX_TIMEOUT_MS (IX_OSAL_MAX_LONG/HZ)
  27. PUBLIC IX_STATUS
  28. ixOsalSemaphoreInit (IxOsalSemaphore * sid, UINT32 start_value)
  29. {
  30. diag_printf("%s called\n", __FUNCTION__);
  31. return IX_SUCCESS;
  32. }
  33. /**
  34. * DESCRIPTION: If the semaphore is 'empty', the calling thread is blocked.
  35. * If the semaphore is 'full', it is taken and control is returned
  36. * to the caller. If the time indicated in 'timeout' is reached,
  37. * the thread will unblock and return an error indication. If the
  38. * timeout is set to 'IX_OSAL_WAIT_NONE', the thread will never block;
  39. * if it is set to 'IX_OSAL_WAIT_FOREVER', the thread will block until
  40. * the semaphore is available.
  41. *
  42. *
  43. */
  44. PUBLIC IX_STATUS
  45. ixOsalSemaphoreWait (IxOsalOsSemaphore * sid, INT32 timeout)
  46. {
  47. diag_printf("%s called\n", __FUNCTION__);
  48. return IX_SUCCESS;
  49. }
  50. /*
  51. * Attempt to get semaphore, return immediately,
  52. * no error info because users expect some failures
  53. * when using this API.
  54. */
  55. PUBLIC IX_STATUS
  56. ixOsalSemaphoreTryWait (IxOsalSemaphore * sid)
  57. {
  58. diag_printf("%s called\n", __FUNCTION__);
  59. return IX_FAIL;
  60. }
  61. /**
  62. *
  63. * DESCRIPTION: This function causes the next available thread in the pend queue
  64. * to be unblocked. If no thread is pending on this semaphore, the
  65. * semaphore becomes 'full'.
  66. */
  67. PUBLIC IX_STATUS
  68. ixOsalSemaphorePost (IxOsalSemaphore * sid)
  69. {
  70. diag_printf("%s called\n", __FUNCTION__);
  71. return IX_SUCCESS;
  72. }
  73. PUBLIC IX_STATUS
  74. ixOsalSemaphoreGetValue (IxOsalSemaphore * sid, UINT32 * value)
  75. {
  76. diag_printf("%s called\n", __FUNCTION__);
  77. return IX_FAIL;
  78. }
  79. PUBLIC IX_STATUS
  80. ixOsalSemaphoreDestroy (IxOsalSemaphore * sid)
  81. {
  82. diag_printf("%s called\n", __FUNCTION__);
  83. return IX_FAIL;
  84. }
  85. /****************************
  86. * Mutex
  87. ****************************/
  88. static void drv_mutex_init(IxOsalMutex *mutex)
  89. {
  90. *mutex = 0;
  91. }
  92. static void drv_mutex_destroy(IxOsalMutex *mutex)
  93. {
  94. *mutex = -1;
  95. }
  96. static int drv_mutex_trylock(IxOsalMutex *mutex)
  97. {
  98. int result = true;
  99. if (*mutex == 1)
  100. result = false;
  101. return result;
  102. }
  103. static void drv_mutex_unlock(IxOsalMutex *mutex)
  104. {
  105. if (*mutex == 1)
  106. printf("Trying to unlock unlocked mutex!");
  107. *mutex = 0;
  108. }
  109. PUBLIC IX_STATUS
  110. ixOsalMutexInit (IxOsalMutex * mutex)
  111. {
  112. drv_mutex_init(mutex);
  113. return IX_SUCCESS;
  114. }
  115. PUBLIC IX_STATUS
  116. ixOsalMutexLock (IxOsalMutex * mutex, INT32 timeout)
  117. {
  118. int tries;
  119. if (timeout == IX_OSAL_WAIT_NONE) {
  120. if (drv_mutex_trylock(mutex))
  121. return IX_SUCCESS;
  122. else
  123. return IX_FAIL;
  124. }
  125. tries = (timeout * 1000) / 50;
  126. while (1) {
  127. if (drv_mutex_trylock(mutex))
  128. return IX_SUCCESS;
  129. if (timeout != IX_OSAL_WAIT_FOREVER && tries-- <= 0)
  130. break;
  131. udelay(50);
  132. }
  133. return IX_FAIL;
  134. }
  135. PUBLIC IX_STATUS
  136. ixOsalMutexUnlock (IxOsalMutex * mutex)
  137. {
  138. drv_mutex_unlock(mutex);
  139. return IX_SUCCESS;
  140. }
  141. /*
  142. * Attempt to get mutex, return immediately,
  143. * no error info because users expect some failures
  144. * when using this API.
  145. */
  146. PUBLIC IX_STATUS
  147. ixOsalMutexTryLock (IxOsalMutex * mutex)
  148. {
  149. if (drv_mutex_trylock(mutex))
  150. return IX_SUCCESS;
  151. return IX_FAIL;
  152. }
  153. PUBLIC IX_STATUS
  154. ixOsalMutexDestroy (IxOsalMutex * mutex)
  155. {
  156. drv_mutex_destroy(mutex);
  157. return IX_SUCCESS;
  158. }
  159. PUBLIC IX_STATUS
  160. ixOsalFastMutexInit (IxOsalFastMutex * mutex)
  161. {
  162. return ixOsalMutexInit(mutex);
  163. }
  164. PUBLIC IX_STATUS ixOsalFastMutexTryLock(IxOsalFastMutex *mutex)
  165. {
  166. return ixOsalMutexTryLock(mutex);
  167. }
  168. PUBLIC IX_STATUS
  169. ixOsalFastMutexUnlock (IxOsalFastMutex * mutex)
  170. {
  171. return ixOsalMutexUnlock(mutex);
  172. }
  173. PUBLIC IX_STATUS
  174. ixOsalFastMutexDestroy (IxOsalFastMutex * mutex)
  175. {
  176. return ixOsalMutexDestroy(mutex);
  177. }