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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_Conf.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->prio = 0;
  49. p_MCB->level = 0;
  50. p_MCB->p_lnk = NULL;
  51. p_MCB->owner = 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. /* Restore owner task's priority. */
  60. if (p_MCB->level != 0) {
  61. p_MCB->owner->prio = p_MCB->prio;
  62. if (p_MCB->owner != os_tsk.run) {
  63. rt_resort_prio (p_MCB->owner);
  64. }
  65. }
  66. while (p_MCB->p_lnk != NULL) {
  67. /* A task is waiting for mutex. */
  68. p_TCB = rt_get_first ((P_XCB)p_MCB);
  69. rt_ret_val(p_TCB, 0/*osOK*/);
  70. rt_rmv_dly(p_TCB);
  71. p_TCB->state = READY;
  72. rt_put_prio (&os_rdy, p_TCB);
  73. }
  74. if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
  75. /* preempt running task */
  76. rt_put_prio (&os_rdy, os_tsk.run);
  77. os_tsk.run->state = READY;
  78. rt_dispatch (NULL);
  79. }
  80. p_MCB->cb_type = 0;
  81. return (OS_R_OK);
  82. }
  83. #endif
  84. /*--------------------------- rt_mut_release --------------------------------*/
  85. OS_RESULT rt_mut_release (OS_ID mutex) {
  86. /* Release a mutex object */
  87. P_MUCB p_MCB = mutex;
  88. P_TCB p_TCB;
  89. if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
  90. /* Unbalanced mutex release or task is not the owner */
  91. return (OS_R_NOK);
  92. }
  93. if (--p_MCB->level != 0) {
  94. return (OS_R_OK);
  95. }
  96. /* Restore owner task's priority. */
  97. os_tsk.run->prio = p_MCB->prio;
  98. if (p_MCB->p_lnk != NULL) {
  99. /* A task is waiting for mutex. */
  100. p_TCB = rt_get_first ((P_XCB)p_MCB);
  101. #ifdef __CMSIS_RTOS
  102. rt_ret_val(p_TCB, 0/*osOK*/);
  103. #else
  104. rt_ret_val(p_TCB, OS_R_MUT);
  105. #endif
  106. rt_rmv_dly (p_TCB);
  107. /* A waiting task becomes the owner of this mutex. */
  108. p_MCB->level = 1;
  109. p_MCB->owner = p_TCB;
  110. p_MCB->prio = p_TCB->prio;
  111. /* Priority inversion, check which task continues. */
  112. if (os_tsk.run->prio >= rt_rdy_prio()) {
  113. rt_dispatch (p_TCB);
  114. }
  115. else {
  116. /* Ready task has higher priority than running task. */
  117. rt_put_prio (&os_rdy, os_tsk.run);
  118. rt_put_prio (&os_rdy, p_TCB);
  119. os_tsk.run->state = READY;
  120. p_TCB->state = READY;
  121. rt_dispatch (NULL);
  122. }
  123. }
  124. else {
  125. /* Check if own priority raised by priority inversion. */
  126. if (rt_rdy_prio() > os_tsk.run->prio) {
  127. rt_put_prio (&os_rdy, os_tsk.run);
  128. os_tsk.run->state = READY;
  129. rt_dispatch (NULL);
  130. }
  131. }
  132. return (OS_R_OK);
  133. }
  134. /*--------------------------- rt_mut_wait -----------------------------------*/
  135. OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
  136. /* Wait for a mutex, continue when mutex is free. */
  137. P_MUCB p_MCB = mutex;
  138. if (p_MCB->level == 0) {
  139. p_MCB->owner = os_tsk.run;
  140. p_MCB->prio = os_tsk.run->prio;
  141. goto inc;
  142. }
  143. if (p_MCB->owner == os_tsk.run) {
  144. /* OK, running task is the owner of this mutex. */
  145. inc:p_MCB->level++;
  146. return (OS_R_OK);
  147. }
  148. /* Mutex owned by another task, wait until released. */
  149. if (timeout == 0) {
  150. return (OS_R_TMO);
  151. }
  152. /* Raise the owner task priority if lower than current priority. */
  153. /* This priority inversion is called priority inheritance. */
  154. if (p_MCB->prio < os_tsk.run->prio) {
  155. p_MCB->owner->prio = os_tsk.run->prio;
  156. rt_resort_prio (p_MCB->owner);
  157. }
  158. if (p_MCB->p_lnk != NULL) {
  159. rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
  160. }
  161. else {
  162. p_MCB->p_lnk = os_tsk.run;
  163. os_tsk.run->p_lnk = NULL;
  164. os_tsk.run->p_rlnk = (P_TCB)p_MCB;
  165. }
  166. rt_block(timeout, WAIT_MUT);
  167. return (OS_R_TMO);
  168. }
  169. /*----------------------------------------------------------------------------
  170. * end of file
  171. *---------------------------------------------------------------------------*/