xos_mutex.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /** @file */
  2. // xos_mutex.h - XOS Mutex API interface and data structures.
  3. // Copyright (c) 2003-2015 Cadence Design Systems, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included
  14. // in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. // NOTE: Do not include this file directly in your application. Including
  24. // xos.h will automatically include this file.
  25. #ifndef __XOS_MUTEX_H__
  26. #define __XOS_MUTEX_H__
  27. #include "xos_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. // Mutex flags.
  33. //-----------------------------------------------------------------------------
  34. #define XOS_MUTEX_WAIT_PRIORITY 0x0000 ///< Wake waiters in priority order (default)
  35. #define XOS_MUTEX_WAIT_FIFO 0x0001 ///< Wake waiters in FIFO order
  36. #define XOS_MUTEX_PRIORITY_CLG 0x0004 // Use priority ceiling
  37. #define XOS_MUTEX_PRIORITY_INV 0x0008 // Protect against priority inversion
  38. //-----------------------------------------------------------------------------
  39. ///
  40. /// XosMutex object.
  41. ///
  42. //-----------------------------------------------------------------------------
  43. typedef struct XosMutex {
  44. XosThread * owner; ///< Owning thread (null if unlocked).
  45. XosThreadQueue waitq; ///< Queue of waiters.
  46. uint32_t flags; ///< Properties.
  47. uint32_t priority;
  48. int32_t lock_count; ///< For recursive locking.
  49. #if XOS_MUTEX_DEBUG
  50. uint32_t sig; // Valid signature indicates inited.
  51. #endif
  52. } XosMutex;
  53. //-----------------------------------------------------------------------------
  54. ///
  55. /// Initialize a mutex object before first use.
  56. ///
  57. /// \param mutex Pointer to mutex object.
  58. ///
  59. /// \param flags Creation flags:
  60. /// - XOS_MUTEX_WAIT_FIFO -- Queue waiting threads
  61. /// in fifo order.
  62. /// - XOS_MUTEX_WAIT_PRIORITY -- Queue waiting threads
  63. /// by priority. This is the default.
  64. /// - XOS_MUTEX_PRIORITY_CLG -- Use specified priority
  65. /// value as the mutex's priority ceiling. If the
  66. /// owning thread has a priority lower than the mutex's
  67. /// priority, then the thread will have its priority
  68. /// raised to the higher value as long as it owns the
  69. /// mutex.
  70. /// - XOS_MUTEX_PRIORITY_INV -- Protect against priority
  71. /// inversion. If there is a waiting thread with a
  72. /// higher priority than the current owner thread,
  73. /// then the owner thread's priority is raised to the
  74. /// higher value for as long as it owns the mutex.
  75. ///
  76. /// \param priority Mutex's priority ceiling. This is used only if the
  77. /// XOS_MUTEX_PRIORITY_CLG flag is set.
  78. ///
  79. /// \return Returns XOS_OK on success, else error code.
  80. ///
  81. /// NOTE: XOS_MUTEX_PRIORITY_CLG and XOS_MUTEX_PRIORITY_INV are NOT supported
  82. /// in the current release. They will be supported in a future release.
  83. ///
  84. //-----------------------------------------------------------------------------
  85. int32_t
  86. xos_mutex_create(XosMutex * mutex, uint32_t flags, uint8_t priority);
  87. //-----------------------------------------------------------------------------
  88. ///
  89. /// Destroy a mutex object. Must have been previously initialized by calling
  90. /// xos_mutex_create().
  91. ///
  92. /// \param mutex Pointer to mutex object.
  93. ///
  94. /// \return Returns XOS_OK on success, else error code.
  95. ///
  96. //-----------------------------------------------------------------------------
  97. int32_t
  98. xos_mutex_delete(XosMutex * mutex);
  99. //-----------------------------------------------------------------------------
  100. ///
  101. /// Take ownership of the mutex: block until the mutex is owned.
  102. /// The mutex must have been initialized.
  103. ///
  104. /// \param mutex Pointer to mutex object.
  105. ///
  106. /// \return Returns XOS_OK on success, else error code.
  107. ///
  108. //-----------------------------------------------------------------------------
  109. int32_t
  110. xos_mutex_lock(XosMutex * mutex);
  111. //-----------------------------------------------------------------------------
  112. ///
  113. /// Take ownership of the mutex: block until the mutex is owned or the timeout
  114. /// expires. The mutex must have been initialized.
  115. ///
  116. /// \param mutex Pointer to mutex object.
  117. ///
  118. /// \param to_cycles Timeout in cycles. Convert from time to cycles
  119. /// using the helper functions provided in xos_timer.
  120. /// A value of zero indicates no timeout.
  121. ///
  122. /// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else error code.
  123. ///
  124. /// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
  125. /// ignored, and no timeout will occur.
  126. ///
  127. //-----------------------------------------------------------------------------
  128. int32_t
  129. xos_mutex_lock_timeout(XosMutex * mutex, uint64_t to_cycles);
  130. //-----------------------------------------------------------------------------
  131. ///
  132. /// Release ownership of the mutex. The mutex must have been initialized and
  133. /// must be owned by the calling thread.
  134. ///
  135. /// \param mutex Pointer to mutex object.
  136. ///
  137. /// \return Returns XOS_OK on success, else error code.
  138. ///
  139. //-----------------------------------------------------------------------------
  140. int32_t
  141. xos_mutex_unlock(XosMutex * mutex);
  142. //-----------------------------------------------------------------------------
  143. ///
  144. /// Try to take ownership of the mutex, but do not block if the mutex is taken.
  145. /// Return immediately. The mutex must have been initialized.
  146. ///
  147. /// \param mutex Pointer to mutex object.
  148. ///
  149. /// \return Returns XOS_OK on success (mutex owned), else error code.
  150. ///
  151. //-----------------------------------------------------------------------------
  152. int32_t
  153. xos_mutex_trylock(XosMutex * mutex);
  154. //-----------------------------------------------------------------------------
  155. ///
  156. /// Return the state of the mutex (locked or unlocked) but do not attempt to
  157. /// take ownership. The mutex must have been initialized.
  158. ///
  159. /// \param mutex Pointer to mutex object.
  160. ///
  161. /// \return Returns 0 if the mutex is unlocked, 1 if it is locked, -1 on error.
  162. ///
  163. //-----------------------------------------------------------------------------
  164. static inline int32_t
  165. xos_mutex_test(XosMutex * mutex)
  166. {
  167. XOS_ASSERT(mutex);
  168. if (mutex != XOS_NULL) {
  169. return (mutex->owner != XOS_NULL) ? 1 : 0;
  170. }
  171. return -1;
  172. }
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif // __XOS_MUTEX_H__