xos_event.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /** @file */
  2. // xos_event.h - XOS Event 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_EVENT_H__
  26. #define __XOS_EVENT_H__
  27. #include "xos_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. // Defines.
  33. //-----------------------------------------------------------------------------
  34. #define XOS_EVENT_BITS_ALL 0xFFFFFFFF
  35. #define XOS_EVENT_BITS_NONE 0
  36. //-----------------------------------------------------------------------------
  37. // Event flags.
  38. //-----------------------------------------------------------------------------
  39. //-----------------------------------------------------------------------------
  40. ///
  41. /// Event object.
  42. ///
  43. //-----------------------------------------------------------------------------
  44. typedef struct XosEvent {
  45. XosThreadQueue waitq; ///< Queue of waiters.
  46. uint32_t bits; ///< Event bits
  47. uint32_t mask; ///< Specifies which bits are valid
  48. uint16_t flags; ///< Properties.
  49. uint16_t pad; ///< Padding
  50. #if XOS_EVENT_DEBUG
  51. uint32_t sig; // Valid signature indicates inited.
  52. #endif
  53. } XosEvent;
  54. //-----------------------------------------------------------------------------
  55. ///
  56. /// Initialize an event object before first use.
  57. ///
  58. /// \param event Pointer to event object.
  59. ///
  60. /// \param mask Mask of active bits. Only these bits can be signaled.
  61. ///
  62. /// \param flags Creation flags (currently ignored, should be zero).
  63. ///
  64. /// \return Returns nothing.
  65. ///
  66. //-----------------------------------------------------------------------------
  67. void
  68. xos_event_create(XosEvent * event, uint32_t mask, uint32_t flags);
  69. //-----------------------------------------------------------------------------
  70. ///
  71. /// Destroy an event object. Must have been previously created by calling
  72. /// xos_event_create().
  73. ///
  74. /// \param event Pointer to event object.
  75. ///
  76. /// \return Returns nothing.
  77. ///
  78. //-----------------------------------------------------------------------------
  79. void
  80. xos_event_delete(XosEvent * event);
  81. //-----------------------------------------------------------------------------
  82. ///
  83. /// Set the specified bits in the specified event. Propagates the bit states
  84. /// to all waiting threads and wakes them if needed.
  85. ///
  86. /// \param event Pointer to event object.
  87. ///
  88. /// \param bits Mask of bits to set. Bits not set in the mask
  89. /// will not be modified by this call. To set all
  90. /// the bits in the event, use the constant
  91. /// XOS_EVENT_BITS_ALL.
  92. ///
  93. /// \return Returns XOS_OK on success, else error code.
  94. ///
  95. //-----------------------------------------------------------------------------
  96. int32_t
  97. xos_event_set(XosEvent * event, uint32_t bits);
  98. //-----------------------------------------------------------------------------
  99. ///
  100. /// Clear the specified bits in the specified event. Propagates the bit states
  101. /// to all waiting threads and wakes them if needed.
  102. ///
  103. /// \param event Pointer to event object.
  104. ///
  105. /// \param bits Mask of bits to clear. Every bit that is set in
  106. /// the mask will be cleared from the event. Bits
  107. /// not set in the mask will not be modified by this
  108. /// call. To clear all the bits in an event use the
  109. /// constant XOS_EVENT_BITS_ALL.
  110. ///
  111. /// \return Returns XOS_OK on success, else error code.
  112. ///
  113. //-----------------------------------------------------------------------------
  114. int32_t
  115. xos_event_clear(XosEvent * event, uint32_t bits);
  116. //-----------------------------------------------------------------------------
  117. ///
  118. /// Clear and set the specified bits in the specified event. The two steps are
  119. /// combined into one update, so this is faster than calling xos_event_clear()
  120. /// and xos_event_set() separately. Only one update is sent out to waiting
  121. /// threads.
  122. ///
  123. /// \param event Pointer to event object.
  124. ///
  125. /// \param clr_bits Mask of bits to clear. The clear operation
  126. /// happens before the set operation.
  127. ///
  128. /// \param set_bits Mask of bits to set.
  129. ///
  130. /// \return Returns XOS_OK on success, else error code.
  131. ///
  132. //-----------------------------------------------------------------------------
  133. int32_t
  134. xos_event_clear_and_set(XosEvent * event, uint32_t clr_bits, uint32_t set_bits);
  135. //-----------------------------------------------------------------------------
  136. ///
  137. /// Get the current state of the event object. This is a snapshot of the state
  138. /// of the event at this time.
  139. ///
  140. /// \param event Pointer to event object.
  141. ///
  142. /// \param pstate Pointer to a uint32_t variable where the state
  143. /// will be returned.
  144. ///
  145. /// \return Returns XOS_OK on success, else error code.
  146. ///
  147. //-----------------------------------------------------------------------------
  148. int32_t
  149. xos_event_get(XosEvent * event, uint32_t * pstate);
  150. //-----------------------------------------------------------------------------
  151. ///
  152. /// Wait until all the specified bits in the wait mask become set in the given
  153. /// event object.
  154. ///
  155. /// \param event Pointer to event object.
  156. ///
  157. /// \param bits Mask of bits to test.
  158. ///
  159. /// \return Returns XOS_OK on success, else error code.
  160. ///
  161. //-----------------------------------------------------------------------------
  162. int32_t
  163. xos_event_wait_all(XosEvent * event, uint32_t bits);
  164. //-----------------------------------------------------------------------------
  165. ///
  166. /// Wait until all the specified bits in the wait mask become set in the given
  167. /// event object, or the timeout expires.
  168. ///
  169. /// \param event Pointer to event object.
  170. ///
  171. /// \param bits Mask of bits to test.
  172. ///
  173. /// \param to_cycles Timeout in cycles. Convert from time to cycles
  174. /// using the helper functions provided in xos_timer.
  175. /// A value of zero indicates no timeout.
  176. ///
  177. /// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else
  178. /// error code.
  179. ///
  180. /// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
  181. /// ignored, and no timeout will occur.
  182. ///
  183. //-----------------------------------------------------------------------------
  184. int32_t
  185. xos_event_wait_all_timeout(XosEvent * event, uint32_t bits, uint64_t to_cycles);
  186. //-----------------------------------------------------------------------------
  187. ///
  188. /// Wait until any of the specified bits in the wait mask become set in the
  189. /// given event object.
  190. ///
  191. /// \param event Pointer to event object.
  192. ///
  193. /// \param bits Mask of bits to test.
  194. ///
  195. /// \return Returns XOS_OK on success, else error code.
  196. ///
  197. //-----------------------------------------------------------------------------
  198. int32_t
  199. xos_event_wait_any(XosEvent * event, uint32_t bits);
  200. //-----------------------------------------------------------------------------
  201. ///
  202. /// Wait until any of the specified bits in the wait mask become set in the
  203. /// event object, or the timeout expires.
  204. ///
  205. /// \param event Pointer to event object.
  206. ///
  207. /// \param bits Mask of bits to test.
  208. ///
  209. /// \param to_cycles Timeout in cycles. Convert from time to cycles
  210. /// using the helper functions provided in xos_timer.
  211. /// A value of zero indicates no timeout.
  212. ///
  213. /// \return Returns XOS_OK on success, XOS_ERR_TIMEOUT on timeout, else
  214. /// error code.
  215. ///
  216. /// NOTE: If XOS_OPT_WAIT_TIMEOUT is not enabled, then the timeout value is
  217. /// ignored, and no timeout will occur.
  218. ///
  219. //-----------------------------------------------------------------------------
  220. int32_t
  221. xos_event_wait_any_timeout(XosEvent * event, uint32_t bits, uint64_t to_cycles);
  222. //-----------------------------------------------------------------------------
  223. ///
  224. /// Atomically set a specified group of bits, then wait for another specified
  225. /// group of bits to become set.
  226. ///
  227. /// \param event Pointer to event object.
  228. ///
  229. /// \param set_bits Group of bits to set.
  230. ///
  231. /// \param wait_bits Group of bits to wait on. All the bits in the
  232. /// group will have to get set before the wait is
  233. /// satisfied.
  234. ///
  235. /// \return Returns XOS_OK on success, else error code.
  236. ///
  237. //-----------------------------------------------------------------------------
  238. int32_t
  239. xos_event_set_and_wait(XosEvent * event, uint32_t set_bits, uint32_t wait_bits);
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif // __XOS_EVENT_H__