event.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2008-2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * @file
  25. * @brief Event wait and signal functions for threads.
  26. * @defgroup event Events
  27. *
  28. * An event is a subclass of a wait queue.
  29. *
  30. * Threads wait for events, with optional timeouts.
  31. *
  32. * Events are "signaled", releasing waiting threads to continue.
  33. * Signals may be one-shot signals (EVENT_FLAG_AUTOUNSIGNAL), in which
  34. * case one signal releases only one thread, at which point it is
  35. * automatically cleared. Otherwise, signals release all waiting threads
  36. * to continue immediately until the signal is manually cleared with
  37. * event_unsignal().
  38. *
  39. * @{
  40. */
  41. #include <debug.h>
  42. #include <err.h>
  43. #include <kernel/event.h>
  44. #if DEBUGLEVEL > 1
  45. #define EVENT_CHECK 1
  46. #endif
  47. /**
  48. * @brief Initialize an event object
  49. *
  50. * @param e Event object to initialize
  51. * @param initial Initial value for "signaled" state
  52. * @param flags 0 or EVENT_FLAG_AUTOUNSIGNAL
  53. */
  54. void event_init(event_t *e, bool initial, uint flags)
  55. {
  56. #if EVENT_CHECK
  57. // ASSERT(e->magic != EVENT_MAGIC);
  58. #endif
  59. e->magic = EVENT_MAGIC;
  60. e->signalled = initial;
  61. e->flags = flags;
  62. wait_queue_init(&e->wait);
  63. }
  64. /**
  65. * @brief Destroy an event object.
  66. *
  67. * Event's resources are freed and it may no longer be
  68. * used until event_init() is called again. Any threads
  69. * still waiting on the event will be resumed.
  70. *
  71. * @param e Event object to initialize
  72. */
  73. void event_destroy(event_t *e)
  74. {
  75. enter_critical_section();
  76. #if EVENT_CHECK
  77. ASSERT(e->magic == EVENT_MAGIC);
  78. #endif
  79. e->magic = 0;
  80. e->signalled = false;
  81. e->flags = 0;
  82. wait_queue_destroy(&e->wait, true);
  83. exit_critical_section();
  84. }
  85. /**
  86. * @brief Wait for event to be signaled
  87. *
  88. * If the event has already been signaled, this function
  89. * returns immediately. Otherwise, the current thread
  90. * goes to sleep until the event object is signaled,
  91. * the timeout is reached, or the event object is destroyed
  92. * by another thread.
  93. *
  94. * @param e Event object
  95. * @param timeout Timeout value, in ms
  96. *
  97. * @return 0 on success, ERR_TIMED_OUT on timeout,
  98. * other values on other errors.
  99. */
  100. status_t event_wait_timeout(event_t *e, time_t timeout)
  101. {
  102. status_t ret = NO_ERROR;
  103. enter_critical_section();
  104. #if EVENT_CHECK
  105. ASSERT(e->magic == EVENT_MAGIC);
  106. #endif
  107. if (e->signalled) {
  108. /* signalled, we're going to fall through */
  109. if (e->flags & EVENT_FLAG_AUTOUNSIGNAL) {
  110. /* autounsignal flag lets one thread fall through before unsignalling */
  111. e->signalled = false;
  112. }
  113. } else {
  114. /* unsignalled, block here */
  115. ret = wait_queue_block(&e->wait, timeout);
  116. if (ret < 0)
  117. goto err;
  118. }
  119. err:
  120. exit_critical_section();
  121. return ret;
  122. }
  123. /**
  124. * @brief Same as event_wait_timeout(), but without a timeout.
  125. */
  126. status_t event_wait(event_t *e)
  127. {
  128. return event_wait_timeout(e, INFINITE_TIME);
  129. }
  130. /**
  131. * @brief Signal an event
  132. *
  133. * Signals an event. If EVENT_FLAG_AUTOUNSIGNAL is set in the event
  134. * object's flags, only one waiting thread is allowed to proceed. Otherwise,
  135. * all waiting threads are allowed to proceed until such time as
  136. * event_unsignal() is called.
  137. *
  138. * @param e Event object
  139. * @param reschedule If true, waiting thread(s) are executed immediately,
  140. * and the current thread resumes only after the
  141. * waiting threads have been satisfied. If false,
  142. * waiting threads are placed at the end of the run
  143. * queue.
  144. *
  145. * @return Returns NO_ERROR on success.
  146. */
  147. status_t event_signal(event_t *e, bool reschedule)
  148. {
  149. enter_critical_section();
  150. #if EVENT_CHECK
  151. ASSERT(e->magic == EVENT_MAGIC);
  152. #endif
  153. if (!e->signalled) {
  154. if (e->flags & EVENT_FLAG_AUTOUNSIGNAL) {
  155. /* try to release one thread and leave unsignalled if successful */
  156. if (wait_queue_wake_one(&e->wait, reschedule, NO_ERROR) <= 0) {
  157. /*
  158. * if we didn't actually find a thread to wake up, go to
  159. * signalled state and let the next call to event_wait
  160. * unsignal the event.
  161. */
  162. e->signalled = true;
  163. }
  164. } else {
  165. /* release all threads and remain signalled */
  166. e->signalled = true;
  167. wait_queue_wake_all(&e->wait, reschedule, NO_ERROR);
  168. }
  169. }
  170. exit_critical_section();
  171. return NO_ERROR;
  172. }
  173. /**
  174. * @brief Clear the "signaled" property of an event
  175. *
  176. * Used mainly for event objects without the EVENT_FLAG_AUTOUNSIGNAL
  177. * flag. Once this function is called, threads that call event_wait()
  178. * functions will once again need to wait until the event object
  179. * is signaled.
  180. *
  181. * @param e Event object
  182. *
  183. * @return Returns NO_ERROR on success.
  184. */
  185. status_t event_unsignal(event_t *e)
  186. {
  187. enter_critical_section();
  188. #if EVENT_CHECK
  189. ASSERT(e->magic == EVENT_MAGIC);
  190. #endif
  191. e->signalled = false;
  192. exit_critical_section();
  193. return NO_ERROR;
  194. }