xos_cond.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /** @file */
  2. // xos_cond.h - XOS condition variables 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_COND_H__
  26. #define __XOS_COND_H__
  27. #include "xos_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Function pointer type for condition callbacks (defined in xos_thread.h)
  34. //
  35. // typedef int32_t (XosCondFunc)(void * arg, int32_t sig_value, XosThread * thread);
  36. //
  37. //-----------------------------------------------------------------------------
  38. //-----------------------------------------------------------------------------
  39. ///
  40. /// Condition object.
  41. ///
  42. //-----------------------------------------------------------------------------
  43. typedef struct XosCond {
  44. XosThreadQueue queue; ///< Queue of waiters.
  45. #if XOS_COND_DEBUG
  46. uint32_t sig; // Signature indicates valid object.
  47. #endif
  48. } XosCond;
  49. //-----------------------------------------------------------------------------
  50. ///
  51. /// Initialize a condition object before first use. The object must be
  52. /// allocated by the caller.
  53. ///
  54. /// \param cond Pointer to condition object.
  55. ///
  56. /// \return Returns nothing.
  57. ///
  58. //-----------------------------------------------------------------------------
  59. void
  60. xos_cond_create(XosCond * cond);
  61. //-----------------------------------------------------------------------------
  62. ///
  63. /// Destroy a condition object. Must have been previously created by calling
  64. /// xos_cond_create().
  65. ///
  66. /// \param cond Pointer to condition object.
  67. ///
  68. /// \return Returns nothing.
  69. ///
  70. //-----------------------------------------------------------------------------
  71. void
  72. xos_cond_delete(XosCond * cond);
  73. //-----------------------------------------------------------------------------
  74. ///
  75. /// Wait on a condition: block until the condition is satisfied. The condition
  76. /// is satisfied when xos_cond_signal() is called on this condition *and* the
  77. /// condition callback function returns non-zero. If there is no callback
  78. /// function, then the condition is automatically satisfied.
  79. ///
  80. /// The condition structure must have been initialized before first use by
  81. /// calling xos_cond_create().
  82. ///
  83. /// \param cond Pointer to condition object.
  84. ///
  85. /// \param cond_fn Pointer to a function, called by xos_cond_signal(),
  86. /// that should return non-zero if this thread is to
  87. /// be resumed. The function is invoked as:
  88. /// `(*cond_fn)(cond_arg, sig_value)`.
  89. ///
  90. /// \param cond_arg Argument passed to cond_fn.
  91. ///
  92. /// \return Returns the value passed to xos_cond_signal().
  93. ///
  94. //-----------------------------------------------------------------------------
  95. int32_t
  96. xos_cond_wait(XosCond * cond, XosCondFunc * cond_fn, void * cond_arg);
  97. //-----------------------------------------------------------------------------
  98. ///
  99. /// Trigger the condition: wake all threads waiting on the condition, if their
  100. /// condition function evaluates to true (non-zero). If there is no condition
  101. /// function for a thread then it is automatically awakened.
  102. ///
  103. /// The condition structure must have been initialized before first use by
  104. /// calling xos_cond_create().
  105. ///
  106. /// \param cond Pointer to condition object.
  107. ///
  108. /// \param sig_value Value passed to all waiters, returned by
  109. /// xos_cond_wait().
  110. ///
  111. /// \return Returns the number of waiting threads that were resumed.
  112. ///
  113. /// NOTE: Signaling a condition that has no waiters has no effect on it, and
  114. /// the signal is not remembered. Any thread that waits on it later must be
  115. /// woken by another call to xos_cond_signal().
  116. ///
  117. //-----------------------------------------------------------------------------
  118. int32_t
  119. xos_cond_signal(XosCond * cond, int32_t sig_value);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif // __XOS_COND_H__