]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_Semaphore.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_A / rt_Semaphore.c
1 /*----------------------------------------------------------------------------
2 * RL-ARM - RTX
3 *----------------------------------------------------------------------------
4 * Name: RT_SEMAPHORE.C
5 * Purpose: Implements binary and counting semaphores
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
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
37 #include "rt_System.h"
38 #include "rt_List.h"
39 #include "rt_Task.h"
40 #include "rt_Semaphore.h"
41 #ifdef __CORTEX_A9
42 #include "rt_HAL_CA.h"
43 #else
44 #include "rt_HAL_CM.h"
45 #endif
46
47
48 /*----------------------------------------------------------------------------
49 * Functions
50 *---------------------------------------------------------------------------*/
51
52
53 /*--------------------------- rt_sem_init -----------------------------------*/
54
55 void rt_sem_init (OS_ID semaphore, U16 token_count) {
56 /* Initialize a semaphore */
57 P_SCB p_SCB = semaphore;
58
59 p_SCB->cb_type = SCB;
60 p_SCB->p_lnk = NULL;
61 p_SCB->tokens = token_count;
62 }
63
64
65 /*--------------------------- rt_sem_delete ---------------------------------*/
66
67 #ifdef __CMSIS_RTOS
68 OS_RESULT rt_sem_delete (OS_ID semaphore) {
69 /* Delete semaphore */
70 P_SCB p_SCB = semaphore;
71 P_TCB p_TCB;
72
73 __DMB();
74 while (p_SCB->p_lnk != NULL) {
75 /* A task is waiting for token */
76 p_TCB = rt_get_first ((P_XCB)p_SCB);
77 rt_ret_val(p_TCB, 0);
78 rt_rmv_dly(p_TCB);
79 p_TCB->state = READY;
80 rt_put_prio (&os_rdy, p_TCB);
81 }
82
83 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
84 /* preempt running task */
85 rt_put_prio (&os_rdy, os_tsk.run);
86 os_tsk.run->state = READY;
87 rt_dispatch (NULL);
88 }
89
90 p_SCB->cb_type = 0;
91
92 return (OS_R_OK);
93 }
94 #endif
95
96
97 /*--------------------------- rt_sem_send -----------------------------------*/
98
99 OS_RESULT rt_sem_send (OS_ID semaphore) {
100 /* Return a token to semaphore */
101 P_SCB p_SCB = semaphore;
102 P_TCB p_TCB;
103
104 __DMB();
105 if (p_SCB->p_lnk != NULL) {
106 /* A task is waiting for token */
107 p_TCB = rt_get_first ((P_XCB)p_SCB);
108 #ifdef __CMSIS_RTOS
109 rt_ret_val(p_TCB, 1);
110 #else
111 rt_ret_val(p_TCB, OS_R_SEM);
112 #endif
113 rt_rmv_dly (p_TCB);
114 rt_dispatch (p_TCB);
115 }
116 else {
117 /* Store token. */
118 p_SCB->tokens++;
119 }
120 return (OS_R_OK);
121 }
122
123
124 /*--------------------------- rt_sem_wait -----------------------------------*/
125
126 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
127 /* Obtain a token; possibly wait for it */
128 P_SCB p_SCB = semaphore;
129
130 if (p_SCB->tokens) {
131 p_SCB->tokens--;
132 __DMB();
133 return (OS_R_OK);
134 }
135 /* No token available: wait for one */
136 if (timeout == 0) {
137 return (OS_R_TMO);
138 }
139 if (p_SCB->p_lnk != NULL) {
140 rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
141 }
142 else {
143 p_SCB->p_lnk = os_tsk.run;
144 os_tsk.run->p_lnk = NULL;
145 os_tsk.run->p_rlnk = (P_TCB)p_SCB;
146 }
147 rt_block(timeout, WAIT_SEM);
148 return (OS_R_TMO);
149 }
150
151
152 /*--------------------------- isr_sem_send ----------------------------------*/
153
154 void isr_sem_send (OS_ID semaphore) {
155 /* Same function as "os_sem"send", but to be called by ISRs */
156 P_SCB p_SCB = semaphore;
157
158 rt_psq_enq (p_SCB, 0);
159 rt_psh_req ();
160 }
161
162
163 /*--------------------------- rt_sem_psh ------------------------------------*/
164
165 void rt_sem_psh (P_SCB p_CB) {
166 /* Check if task has to be waken up */
167 P_TCB p_TCB;
168
169 __DMB();
170 if (p_CB->p_lnk != NULL) {
171 /* A task is waiting for token */
172 p_TCB = rt_get_first ((P_XCB)p_CB);
173 rt_rmv_dly (p_TCB);
174 p_TCB->state = READY;
175 #ifdef __CMSIS_RTOS
176 rt_ret_val(p_TCB, 1);
177 #else
178 rt_ret_val(p_TCB, OS_R_SEM);
179 #endif
180 rt_put_prio (&os_rdy, p_TCB);
181 }
182 else {
183 /* Store token */
184 p_CB->tokens++;
185 }
186 }
187
188 /*----------------------------------------------------------------------------
189 * end of file
190 *---------------------------------------------------------------------------*/
191
Imprint / Impressum