Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_Mutex.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_MUTEX.C
  5. * Purpose: Implements mutex synchronization objects
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 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. #ifdef __CORTEX_A9
  40. #include "rt_HAL_CA.h"
  41. #else
  42. #include "rt_HAL_CM.h"
  43. #endif
  44. /*----------------------------------------------------------------------------
  45. * Functions
  46. *---------------------------------------------------------------------------*/
  47. /*--------------------------- rt_mut_init -----------------------------------*/
  48. void rt_mut_init (OS_ID mutex) {
  49. /* Initialize a mutex object */
  50. P_MUCB p_MCB = mutex;
  51. p_MCB->cb_type = MUCB;
  52. p_MCB->prio = 0;
  53. p_MCB->level = 0;
  54. p_MCB->p_lnk = NULL;
  55. p_MCB->owner = NULL;
  56. }
  57. /*--------------------------- rt_mut_delete ---------------------------------*/
  58. #ifdef __CMSIS_RTOS
  59. OS_RESULT rt_mut_delete (OS_ID mutex) {
  60. /* Delete a mutex object */
  61. P_MUCB p_MCB = mutex;
  62. P_TCB p_TCB;
  63. __DMB();
  64. /* Restore owner task's priority. */
  65. if (p_MCB->level != 0) {
  66. p_MCB->owner->prio = p_MCB->prio;
  67. if (p_MCB->owner != os_tsk.run) {
  68. rt_resort_prio (p_MCB->owner);
  69. }
  70. }
  71. while (p_MCB->p_lnk != NULL) {
  72. /* A task is waiting for mutex. */
  73. p_TCB = rt_get_first ((P_XCB)p_MCB);
  74. rt_ret_val(p_TCB, 0/*osOK*/);
  75. rt_rmv_dly(p_TCB);
  76. p_TCB->state = READY;
  77. rt_put_prio (&os_rdy, p_TCB);
  78. }
  79. if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
  80. /* preempt running task */
  81. rt_put_prio (&os_rdy, os_tsk.run);
  82. os_tsk.run->state = READY;
  83. rt_dispatch (NULL);
  84. }
  85. p_MCB->cb_type = 0;
  86. return (OS_R_OK);
  87. }
  88. #endif
  89. /*--------------------------- rt_mut_release --------------------------------*/
  90. OS_RESULT rt_mut_release (OS_ID mutex) {
  91. /* Release a mutex object */
  92. P_MUCB p_MCB = mutex;
  93. P_TCB p_TCB;
  94. if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
  95. /* Unbalanced mutex release or task is not the owner */
  96. return (OS_R_NOK);
  97. }
  98. __DMB();
  99. if (--p_MCB->level != 0) {
  100. return (OS_R_OK);
  101. }
  102. /* Restore owner task's priority. */
  103. os_tsk.run->prio = p_MCB->prio;
  104. if (p_MCB->p_lnk != NULL) {
  105. /* A task is waiting for mutex. */
  106. p_TCB = rt_get_first ((P_XCB)p_MCB);
  107. #ifdef __CMSIS_RTOS
  108. rt_ret_val(p_TCB, 0/*osOK*/);
  109. #else
  110. rt_ret_val(p_TCB, OS_R_MUT);
  111. #endif
  112. rt_rmv_dly (p_TCB);
  113. /* A waiting task becomes the owner of this mutex. */
  114. p_MCB->level = 1;
  115. p_MCB->owner = p_TCB;
  116. p_MCB->prio = p_TCB->prio;
  117. /* Priority inversion, check which task continues. */
  118. if (os_tsk.run->prio >= rt_rdy_prio()) {
  119. rt_dispatch (p_TCB);
  120. }
  121. else {
  122. /* Ready task has higher priority than running task. */
  123. rt_put_prio (&os_rdy, os_tsk.run);
  124. rt_put_prio (&os_rdy, p_TCB);
  125. os_tsk.run->state = READY;
  126. p_TCB->state = READY;
  127. rt_dispatch (NULL);
  128. }
  129. }
  130. else {
  131. /* Check if own priority raised by priority inversion. */
  132. if (rt_rdy_prio() > os_tsk.run->prio) {
  133. rt_put_prio (&os_rdy, os_tsk.run);
  134. os_tsk.run->state = READY;
  135. rt_dispatch (NULL);
  136. }
  137. }
  138. return (OS_R_OK);
  139. }
  140. /*--------------------------- rt_mut_wait -----------------------------------*/
  141. OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
  142. /* Wait for a mutex, continue when mutex is free. */
  143. P_MUCB p_MCB = mutex;
  144. if (p_MCB->level == 0) {
  145. p_MCB->owner = os_tsk.run;
  146. p_MCB->prio = os_tsk.run->prio;
  147. goto inc;
  148. }
  149. if (p_MCB->owner == os_tsk.run) {
  150. /* OK, running task is the owner of this mutex. */
  151. inc:p_MCB->level++;
  152. __DMB();
  153. return (OS_R_OK);
  154. }
  155. /* Mutex owned by another task, wait until released. */
  156. if (timeout == 0) {
  157. return (OS_R_TMO);
  158. }
  159. /* Raise the owner task priority if lower than current priority. */
  160. /* This priority inversion is called priority inheritance. */
  161. if (p_MCB->prio < os_tsk.run->prio) {
  162. p_MCB->owner->prio = os_tsk.run->prio;
  163. rt_resort_prio (p_MCB->owner);
  164. }
  165. if (p_MCB->p_lnk != NULL) {
  166. rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
  167. }
  168. else {
  169. p_MCB->p_lnk = os_tsk.run;
  170. os_tsk.run->p_lnk = NULL;
  171. os_tsk.run->p_rlnk = (P_TCB)p_MCB;
  172. }
  173. rt_block(timeout, WAIT_MUT);
  174. return (OS_R_TMO);
  175. }
  176. /*----------------------------------------------------------------------------
  177. * end of file
  178. *---------------------------------------------------------------------------*/