rt_Semaphore.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_SEMAPHORE.C
  5. * Purpose: Implements binary and counting semaphores
  6. * Rev.: V4.79
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. #include "rt_TypeDef.h"
  26. #include "RTX_Config.h"
  27. #include "rt_System.h"
  28. #include "rt_List.h"
  29. #include "rt_Task.h"
  30. #include "rt_Semaphore.h"
  31. #include "rt_HAL_CM.h"
  32. /*----------------------------------------------------------------------------
  33. * Functions
  34. *---------------------------------------------------------------------------*/
  35. /*--------------------------- rt_sem_init -----------------------------------*/
  36. void rt_sem_init (OS_ID semaphore, U16 token_count) {
  37. /* Initialize a semaphore */
  38. P_SCB p_SCB = semaphore;
  39. p_SCB->cb_type = SCB;
  40. p_SCB->p_lnk = NULL;
  41. p_SCB->tokens = token_count;
  42. }
  43. /*--------------------------- rt_sem_delete ---------------------------------*/
  44. #ifdef __CMSIS_RTOS
  45. OS_RESULT rt_sem_delete (OS_ID semaphore) {
  46. /* Delete semaphore */
  47. P_SCB p_SCB = semaphore;
  48. P_TCB p_TCB;
  49. while (p_SCB->p_lnk != NULL) {
  50. /* A task is waiting for token */
  51. p_TCB = rt_get_first ((P_XCB)p_SCB);
  52. rt_ret_val(p_TCB, 0U);
  53. rt_rmv_dly(p_TCB);
  54. p_TCB->state = READY;
  55. rt_put_prio (&os_rdy, p_TCB);
  56. }
  57. if ((os_rdy.p_lnk != NULL) && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
  58. /* preempt running task */
  59. rt_put_prio (&os_rdy, os_tsk.run);
  60. os_tsk.run->state = READY;
  61. rt_dispatch (NULL);
  62. }
  63. p_SCB->cb_type = 0U;
  64. return (OS_R_OK);
  65. }
  66. #endif
  67. /*--------------------------- rt_sem_send -----------------------------------*/
  68. OS_RESULT rt_sem_send (OS_ID semaphore) {
  69. /* Return a token to semaphore */
  70. P_SCB p_SCB = semaphore;
  71. P_TCB p_TCB;
  72. if (p_SCB->p_lnk != NULL) {
  73. /* A task is waiting for token */
  74. p_TCB = rt_get_first ((P_XCB)p_SCB);
  75. #ifdef __CMSIS_RTOS
  76. rt_ret_val(p_TCB, 1U);
  77. #else
  78. rt_ret_val(p_TCB, OS_R_SEM);
  79. #endif
  80. rt_rmv_dly (p_TCB);
  81. rt_dispatch (p_TCB);
  82. }
  83. else {
  84. /* Store token. */
  85. p_SCB->tokens++;
  86. }
  87. return (OS_R_OK);
  88. }
  89. /*--------------------------- rt_sem_wait -----------------------------------*/
  90. OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
  91. /* Obtain a token; possibly wait for it */
  92. P_SCB p_SCB = semaphore;
  93. if (p_SCB->tokens) {
  94. p_SCB->tokens--;
  95. return (OS_R_OK);
  96. }
  97. /* No token available: wait for one */
  98. if (timeout == 0U) {
  99. return (OS_R_TMO);
  100. }
  101. if (p_SCB->p_lnk != NULL) {
  102. rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
  103. }
  104. else {
  105. p_SCB->p_lnk = os_tsk.run;
  106. os_tsk.run->p_lnk = NULL;
  107. os_tsk.run->p_rlnk = (P_TCB)p_SCB;
  108. }
  109. rt_block(timeout, WAIT_SEM);
  110. return (OS_R_TMO);
  111. }
  112. /*--------------------------- isr_sem_send ----------------------------------*/
  113. void isr_sem_send (OS_ID semaphore) {
  114. /* Same function as "os_sem_send", but to be called by ISRs */
  115. P_SCB p_SCB = semaphore;
  116. rt_psq_enq (p_SCB, 0U);
  117. rt_psh_req ();
  118. }
  119. /*--------------------------- rt_sem_psh ------------------------------------*/
  120. void rt_sem_psh (P_SCB p_CB) {
  121. /* Check if task has to be waken up */
  122. P_TCB p_TCB;
  123. if (p_CB->p_lnk != NULL) {
  124. /* A task is waiting for token */
  125. p_TCB = rt_get_first ((P_XCB)p_CB);
  126. rt_rmv_dly (p_TCB);
  127. p_TCB->state = READY;
  128. #ifdef __CMSIS_RTOS
  129. rt_ret_val(p_TCB, 1U);
  130. #else
  131. rt_ret_val(p_TCB, OS_R_SEM);
  132. #endif
  133. rt_put_prio (&os_rdy, p_TCB);
  134. }
  135. else {
  136. /* Store token */
  137. p_CB->tokens++;
  138. }
  139. }
  140. /*----------------------------------------------------------------------------
  141. * end of file
  142. *---------------------------------------------------------------------------*/