xos_semaphore.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /** @file */
  2. // xos_semaphore.h - XOS Semaphore 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_SEMAPHORE_H__
  26. #define __XOS_SEMAPHORE_H__
  27. #include "xos_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. // Semaphore flags.
  33. //-----------------------------------------------------------------------------
  34. #define XOS_SEM_WAIT_PRIORITY 0x0000 ///< Wake waiters in priority order (default)
  35. #define XOS_SEM_WAIT_FIFO 0x0001 ///< Wake waiters in FIFO order
  36. #define XOS_SEM_PRIORITY_INV 0x0004 // Protect against priority inversion
  37. //-----------------------------------------------------------------------------
  38. ///
  39. /// XosSem object.
  40. ///
  41. //-----------------------------------------------------------------------------
  42. typedef struct XosSem {
  43. uint32_t count; ///< Current count
  44. XosThreadQueue waitq; ///< Queue of waiters.
  45. uint32_t flags; ///< Properties.
  46. #if XOS_SEM_DEBUG
  47. uint32_t sig; // Valid signature indicates inited.
  48. #endif
  49. } XosSem;
  50. //-----------------------------------------------------------------------------
  51. ///
  52. /// Initialize a semaphore object before first use.
  53. ///
  54. /// \param sem Pointer to semaphore object.
  55. ///
  56. /// \param flags Creation flags:
  57. /// - XOS_SEM_WAIT_FIFO -- queue waiting threads in
  58. /// fifo order.
  59. /// - XOS_SEM_WAIT_PRIORITY -- queue waiting threads
  60. /// by priority. This is the default.
  61. /// - XOS_SEM_PRIORITY_INV -- protect against priority
  62. /// inversion.
  63. ///
  64. /// \param initial_count Initial count for semaphore on creation.
  65. ///
  66. /// \return Returns XOS_OK on success, else error code.
  67. ///
  68. /// NOTE: XOS_SEM_PRIORITY_INV is NOT supported in the current release. It will
  69. /// be supported in a future release.
  70. ///
  71. //-----------------------------------------------------------------------------
  72. int32_t
  73. xos_sem_create(XosSem * sem, uint32_t flags, uint32_t initial_count);
  74. //-----------------------------------------------------------------------------
  75. ///
  76. /// Destroy a semaphore object. Must have been previously created by calling
  77. /// xos_sem_create().
  78. ///
  79. /// \param sem Pointer to semaphore object.
  80. ///
  81. /// \return Returns XOS_OK on success, else error code.
  82. ///
  83. //-----------------------------------------------------------------------------
  84. int32_t
  85. xos_sem_delete(XosSem * sem);
  86. //-----------------------------------------------------------------------------
  87. ///
  88. /// Decrement the semaphore count: block until the decrement is possible.
  89. /// The semaphore must have been initialized.
  90. ///
  91. /// \param sem Pointer to semaphore object.
  92. ///
  93. /// \return Returns XOS_OK on success, else error code.
  94. ///
  95. //-----------------------------------------------------------------------------
  96. int32_t
  97. xos_sem_get(XosSem * sem);
  98. //-----------------------------------------------------------------------------
  99. ///
  100. /// Decrement the semaphore count: block until the decrement is possible or
  101. /// the timeout expires. The semaphore must have been initialized.
  102. ///
  103. /// \param sem Pointer to semaphore object.
  104. ///
  105. /// \param to_cycles Timeout in cycles. Convert from time to cycles
  106. /// using the helper functions provided in xos_timer.
  107. /// A value of zero indicates no timeout.
  108. ///
  109. /// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else error code.
  110. ///
  111. /// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
  112. /// ignored, and no timeout will occur.
  113. ///
  114. //-----------------------------------------------------------------------------
  115. int32_t
  116. xos_sem_get_timeout(XosSem * sem, uint64_t to_cycles);
  117. //-----------------------------------------------------------------------------
  118. ///
  119. /// Increment the semaphore count. The semaphore must have been initialized.
  120. /// Remember that this action may wake up a waiting thread, and if that thread
  121. /// is higher priority then there will be an immediate context switch.
  122. ///
  123. /// \param sem Pointer to semaphore object.
  124. ///
  125. /// \return Returns XOS_OK on success, else error code.
  126. ///
  127. //-----------------------------------------------------------------------------
  128. int32_t
  129. xos_sem_put(XosSem * sem);
  130. //-----------------------------------------------------------------------------
  131. ///
  132. /// Try to decrement the semaphore, but do not block if the semaphore count is
  133. /// zero. Return immediately. The semaphore must have been initialized.
  134. ///
  135. /// \param sem Pointer to semaphore object.
  136. ///
  137. /// \return Returns XOS_OK on success (semaphore decremented), else error code.
  138. ///
  139. //-----------------------------------------------------------------------------
  140. int32_t
  141. xos_sem_tryget(XosSem * sem);
  142. //-----------------------------------------------------------------------------
  143. ///
  144. /// Return the count of the semaphore but do not attempt to decrement it.
  145. /// The semaphore must have been initialized.
  146. ///
  147. /// \param sem Pointer to semaphore object.
  148. ///
  149. /// \return Returns semaphore count, -1 on error.
  150. ///
  151. //-----------------------------------------------------------------------------
  152. static inline int32_t
  153. xos_sem_test(XosSem * sem)
  154. {
  155. XOS_ASSERT(sem);
  156. return sem ? sem->count : -1;
  157. }
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #endif // __XOS_SEMAPHORE_H__