mutex.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Mutex functions
  26. *
  27. * @defgroup mutex Mutex
  28. * @{
  29. */
  30. #include <debug.h>
  31. #include <err.h>
  32. #include <kernel/mutex.h>
  33. #include <kernel/thread.h>
  34. #if DEBUGLEVEL > 1
  35. #define MUTEX_CHECK 1
  36. #endif
  37. /**
  38. * @brief Initialize a mutex_t
  39. */
  40. void mutex_init(mutex_t *m)
  41. {
  42. #if MUTEX_CHECK
  43. // ASSERT(m->magic != MUTEX_MAGIC);
  44. #endif
  45. m->magic = MUTEX_MAGIC;
  46. m->count = 0;
  47. m->holder = 0;
  48. wait_queue_init(&m->wait);
  49. }
  50. /**
  51. * @brief Destroy a mutex_t
  52. *
  53. * This function frees any resources that were allocated
  54. * in mutex_init(). The mutex_t object itself is not freed.
  55. */
  56. void mutex_destroy(mutex_t *m)
  57. {
  58. enter_critical_section();
  59. #if MUTEX_CHECK
  60. ASSERT(m->magic == MUTEX_MAGIC);
  61. #endif
  62. // if (m->holder != 0 && current_thread != m->holder)
  63. // panic("mutex_destroy: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
  64. // current_thread, current_thread->name, m, m->holder, m->holder ? m->holder->name : "none");
  65. m->magic = 0;
  66. m->count = 0;
  67. wait_queue_destroy(&m->wait, true);
  68. exit_critical_section();
  69. }
  70. /**
  71. * @brief Acquire a mutex; wait if needed.
  72. *
  73. * This function waits for a mutex to become available. It
  74. * may wait forever if the mutex never becomes free.
  75. *
  76. * @return NO_ERROR on success, other values on error
  77. */
  78. status_t mutex_acquire(mutex_t *m)
  79. {
  80. status_t ret = NO_ERROR;
  81. if (current_thread == m->holder)
  82. panic("mutex_acquire: thread %p (%s) tried to acquire mutex %p it already owns.\n",
  83. current_thread, current_thread->name, m);
  84. enter_critical_section();
  85. #if MUTEX_CHECK
  86. ASSERT(m->magic == MUTEX_MAGIC);
  87. #endif
  88. // dprintf("mutex_acquire: m %p, count %d, curr %p\n", m, m->count, current_thread);
  89. m->count++;
  90. if (unlikely(m->count > 1)) {
  91. /*
  92. * block on the wait queue. If it returns an error, it was likely destroyed
  93. * out from underneath us, so make sure we dont scribble thread ownership
  94. * on the mutex.
  95. */
  96. ret = wait_queue_block(&m->wait, INFINITE_TIME);
  97. if (ret < 0)
  98. goto err;
  99. }
  100. m->holder = current_thread;
  101. err:
  102. exit_critical_section();
  103. return ret;
  104. }
  105. /**
  106. * @brief Mutex wait with timeout
  107. *
  108. * This function waits up to \a timeout ms for the mutex to become available.
  109. * Timeout may be zero, in which case this function returns immediately if
  110. * the mutex is not free.
  111. *
  112. * @return NO_ERROR on success, ERR_TIMED_OUT on timeout,
  113. * other values on error
  114. */
  115. status_t mutex_acquire_timeout(mutex_t *m, time_t timeout)
  116. {
  117. status_t ret = NO_ERROR;
  118. if (current_thread == m->holder)
  119. panic("mutex_acquire_timeout: thread %p (%s) tried to acquire mutex %p it already owns.\n",
  120. current_thread, current_thread->name, m);
  121. if (timeout == INFINITE_TIME)
  122. return mutex_acquire(m);
  123. enter_critical_section();
  124. #if MUTEX_CHECK
  125. ASSERT(m->magic == MUTEX_MAGIC);
  126. #endif
  127. // dprintf("mutex_acquire_timeout: m %p, count %d, curr %p, timeout %d\n", m, m->count, current_thread, timeout);
  128. m->count++;
  129. if (unlikely(m->count > 1)) {
  130. ret = wait_queue_block(&m->wait, timeout);
  131. if (ret < NO_ERROR) {
  132. /* if the acquisition timed out, back out the acquire and exit */
  133. if (ret == ERR_TIMED_OUT) {
  134. /*
  135. * XXX race: the mutex may have been destroyed after the timeout,
  136. * but before we got scheduled again which makes messing with the
  137. * count variable dangerous.
  138. */
  139. m->count--;
  140. goto err;
  141. }
  142. /* if there was a general error, it may have been destroyed out from
  143. * underneath us, so just exit (which is really an invalid state anyway)
  144. */
  145. }
  146. }
  147. m->holder = current_thread;
  148. err:
  149. exit_critical_section();
  150. return ret;
  151. }
  152. /**
  153. * @brief Release mutex
  154. */
  155. status_t mutex_release(mutex_t *m)
  156. {
  157. if (current_thread != m->holder)
  158. panic("mutex_release: thread %p (%s) tried to release mutex %p it doesn't own. owned by %p (%s)\n",
  159. current_thread, current_thread->name, m, m->holder, m->holder ? m->holder->name : "none");
  160. enter_critical_section();
  161. #if MUTEX_CHECK
  162. ASSERT(m->magic == MUTEX_MAGIC);
  163. #endif
  164. // dprintf("mutex_release: m %p, count %d, holder %p, curr %p\n", m, m->count, m->holder, current_thread);
  165. m->holder = 0;
  166. m->count--;
  167. if (unlikely(m->count >= 1)) {
  168. /* release a thread */
  169. // dprintf("releasing thread\n");
  170. wait_queue_wake_one(&m->wait, true, NO_ERROR);
  171. }
  172. exit_critical_section();
  173. return NO_ERROR;
  174. }