rt_Mutex.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_MUTEX.C
  5. * Purpose: Implements mutex synchronization objects
  6. * Rev.: V4.79
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Config.h"
  36. #include "rt_List.h"
  37. #include "rt_Task.h"
  38. #include "rt_Mutex.h"
  39. #include "rt_HAL_CM.h"
  40. /*----------------------------------------------------------------------------
  41. * Functions
  42. *---------------------------------------------------------------------------*/
  43. /*--------------------------- rt_mut_init -----------------------------------*/
  44. void rt_mut_init (OS_ID mutex) {
  45. /* Initialize a mutex object */
  46. P_MUCB p_MCB = mutex;
  47. p_MCB->cb_type = MUCB;
  48. p_MCB->level = 0U;
  49. p_MCB->p_lnk = NULL;
  50. p_MCB->owner = NULL;
  51. p_MCB->p_mlnk = NULL;
  52. }
  53. /*--------------------------- rt_mut_delete ---------------------------------*/
  54. #ifdef __CMSIS_RTOS
  55. OS_RESULT rt_mut_delete (OS_ID mutex) {
  56. /* Delete a mutex object */
  57. P_MUCB p_MCB = mutex;
  58. P_TCB p_TCB;
  59. P_MUCB p_mlnk;
  60. U8 prio;
  61. if (p_MCB->level != 0U) {
  62. p_TCB = p_MCB->owner;
  63. /* Remove mutex from task mutex owner list. */
  64. p_mlnk = p_TCB->p_mlnk;
  65. if (p_mlnk == p_MCB) {
  66. p_TCB->p_mlnk = p_MCB->p_mlnk;
  67. }
  68. else {
  69. while (p_mlnk) {
  70. if (p_mlnk->p_mlnk == p_MCB) {
  71. p_mlnk->p_mlnk = p_MCB->p_mlnk;
  72. break;
  73. }
  74. p_mlnk = p_mlnk->p_mlnk;
  75. }
  76. }
  77. /* Restore owner task's priority. */
  78. prio = p_TCB->prio_base;
  79. p_mlnk = p_TCB->p_mlnk;
  80. while (p_mlnk) {
  81. if ((p_mlnk->p_lnk != NULL) && (p_mlnk->p_lnk->prio > prio)) {
  82. /* A task with higher priority is waiting for mutex. */
  83. prio = p_mlnk->p_lnk->prio;
  84. }
  85. p_mlnk = p_mlnk->p_mlnk;
  86. }
  87. if (p_TCB->prio != prio) {
  88. p_TCB->prio = prio;
  89. if (p_TCB != os_tsk.run) {
  90. rt_resort_prio (p_TCB);
  91. }
  92. }
  93. }
  94. while (p_MCB->p_lnk != NULL) {
  95. /* A task is waiting for mutex. */
  96. p_TCB = rt_get_first ((P_XCB)p_MCB);
  97. rt_ret_val(p_TCB, 0U/*osOK*/);
  98. rt_rmv_dly(p_TCB);
  99. p_TCB->state = READY;
  100. rt_put_prio (&os_rdy, p_TCB);
  101. }
  102. if ((os_rdy.p_lnk != NULL) && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
  103. /* preempt running task */
  104. rt_put_prio (&os_rdy, os_tsk.run);
  105. os_tsk.run->state = READY;
  106. rt_dispatch (NULL);
  107. }
  108. p_MCB->cb_type = 0U;
  109. return (OS_R_OK);
  110. }
  111. #endif
  112. /*--------------------------- rt_mut_release --------------------------------*/
  113. OS_RESULT rt_mut_release (OS_ID mutex) {
  114. /* Release a mutex object */
  115. P_MUCB p_MCB = mutex;
  116. P_TCB p_TCB;
  117. P_MUCB p_mlnk;
  118. U8 prio;
  119. if ((p_MCB->level == 0U) || (p_MCB->owner != os_tsk.run)) {
  120. /* Unbalanced mutex release or task is not the owner */
  121. return (OS_R_NOK);
  122. }
  123. if (--p_MCB->level != 0U) {
  124. return (OS_R_OK);
  125. }
  126. /* Remove mutex from task mutex owner list. */
  127. p_mlnk = os_tsk.run->p_mlnk;
  128. if (p_mlnk == p_MCB) {
  129. os_tsk.run->p_mlnk = p_MCB->p_mlnk;
  130. }
  131. else {
  132. while (p_mlnk) {
  133. if (p_mlnk->p_mlnk == p_MCB) {
  134. p_mlnk->p_mlnk = p_MCB->p_mlnk;
  135. break;
  136. }
  137. p_mlnk = p_mlnk->p_mlnk;
  138. }
  139. }
  140. /* Restore owner task's priority. */
  141. prio = os_tsk.run->prio_base;
  142. p_mlnk = os_tsk.run->p_mlnk;
  143. while (p_mlnk) {
  144. if ((p_mlnk->p_lnk != NULL) && (p_mlnk->p_lnk->prio > prio)) {
  145. /* A task with higher priority is waiting for mutex. */
  146. prio = p_mlnk->p_lnk->prio;
  147. }
  148. p_mlnk = p_mlnk->p_mlnk;
  149. }
  150. os_tsk.run->prio = prio;
  151. if (p_MCB->p_lnk != NULL) {
  152. /* A task is waiting for mutex. */
  153. p_TCB = rt_get_first ((P_XCB)p_MCB);
  154. #ifdef __CMSIS_RTOS
  155. rt_ret_val(p_TCB, 0U/*osOK*/);
  156. #else
  157. rt_ret_val(p_TCB, OS_R_MUT);
  158. #endif
  159. rt_rmv_dly (p_TCB);
  160. /* A waiting task becomes the owner of this mutex. */
  161. p_MCB->level = 1U;
  162. p_MCB->owner = p_TCB;
  163. p_MCB->p_mlnk = p_TCB->p_mlnk;
  164. p_TCB->p_mlnk = p_MCB;
  165. /* Priority inversion, check which task continues. */
  166. if (os_tsk.run->prio >= rt_rdy_prio()) {
  167. rt_dispatch (p_TCB);
  168. }
  169. else {
  170. /* Ready task has higher priority than running task. */
  171. rt_put_prio (&os_rdy, os_tsk.run);
  172. rt_put_prio (&os_rdy, p_TCB);
  173. os_tsk.run->state = READY;
  174. p_TCB->state = READY;
  175. rt_dispatch (NULL);
  176. }
  177. }
  178. else {
  179. /* Check if own priority lowered by priority inversion. */
  180. if (rt_rdy_prio() > os_tsk.run->prio) {
  181. rt_put_prio (&os_rdy, os_tsk.run);
  182. os_tsk.run->state = READY;
  183. rt_dispatch (NULL);
  184. }
  185. }
  186. return (OS_R_OK);
  187. }
  188. /*--------------------------- rt_mut_wait -----------------------------------*/
  189. OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
  190. /* Wait for a mutex, continue when mutex is free. */
  191. P_MUCB p_MCB = mutex;
  192. if (p_MCB->level == 0U) {
  193. p_MCB->owner = os_tsk.run;
  194. p_MCB->p_mlnk = os_tsk.run->p_mlnk;
  195. os_tsk.run->p_mlnk = p_MCB;
  196. goto inc;
  197. }
  198. if (p_MCB->owner == os_tsk.run) {
  199. /* OK, running task is the owner of this mutex. */
  200. inc:p_MCB->level++;
  201. return (OS_R_OK);
  202. }
  203. /* Mutex owned by another task, wait until released. */
  204. if (timeout == 0U) {
  205. return (OS_R_TMO);
  206. }
  207. /* Raise the owner task priority if lower than current priority. */
  208. /* This priority inversion is called priority inheritance. */
  209. if (p_MCB->owner->prio < os_tsk.run->prio) {
  210. p_MCB->owner->prio = os_tsk.run->prio;
  211. rt_resort_prio (p_MCB->owner);
  212. }
  213. if (p_MCB->p_lnk != NULL) {
  214. rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
  215. }
  216. else {
  217. p_MCB->p_lnk = os_tsk.run;
  218. os_tsk.run->p_lnk = NULL;
  219. os_tsk.run->p_rlnk = (P_TCB)p_MCB;
  220. }
  221. rt_block(timeout, WAIT_MUT);
  222. return (OS_R_TMO);
  223. }
  224. /*----------------------------------------------------------------------------
  225. * end of file
  226. *---------------------------------------------------------------------------*/