]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_Semaphore.c
1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Implements binary and counting semaphores
7 *----------------------------------------------------------------------------
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.
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 *---------------------------------------------------------------------------*/
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
37 #include "rt_System.h"
40 #include "rt_Semaphore.h"
42 #include "rt_HAL_CA.h"
44 #include "rt_HAL_CM.h"
48 /*----------------------------------------------------------------------------
50 *---------------------------------------------------------------------------*/
53 /*--------------------------- rt_sem_init -----------------------------------*/
55 void rt_sem_init (OS_ID semaphore
, U16 token_count
) {
56 /* Initialize a semaphore */
57 P_SCB p_SCB
= semaphore
;
61 p_SCB
->tokens
= token_count
;
65 /*--------------------------- rt_sem_delete ---------------------------------*/
68 OS_RESULT
rt_sem_delete (OS_ID semaphore
) {
69 /* Delete semaphore */
70 P_SCB p_SCB
= semaphore
;
74 while (p_SCB
->p_lnk
!= NULL
) {
75 /* A task is waiting for token */
76 p_TCB
= rt_get_first ((P_XCB
)p_SCB
);
80 rt_put_prio (&os_rdy
, p_TCB
);
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
;
97 /*--------------------------- rt_sem_send -----------------------------------*/
99 OS_RESULT
rt_sem_send (OS_ID semaphore
) {
100 /* Return a token to semaphore */
101 P_SCB p_SCB
= semaphore
;
105 if (p_SCB
->p_lnk
!= NULL
) {
106 /* A task is waiting for token */
107 p_TCB
= rt_get_first ((P_XCB
)p_SCB
);
109 rt_ret_val(p_TCB
, 1);
111 rt_ret_val(p_TCB
, OS_R_SEM
);
124 /*--------------------------- rt_sem_wait -----------------------------------*/
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
;
135 /* No token available: wait for one */
139 if (p_SCB
->p_lnk
!= NULL
) {
140 rt_put_prio ((P_XCB
)p_SCB
, os_tsk
.run
);
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
;
147 rt_block(timeout
, WAIT_SEM
);
152 /*--------------------------- isr_sem_send ----------------------------------*/
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
;
158 rt_psq_enq (p_SCB
, 0);
163 /*--------------------------- rt_sem_psh ------------------------------------*/
165 void rt_sem_psh (P_SCB p_CB
) {
166 /* Check if task has to be waken up */
170 if (p_CB
->p_lnk
!= NULL
) {
171 /* A task is waiting for token */
172 p_TCB
= rt_get_first ((P_XCB
)p_CB
);
174 p_TCB
->state
= READY
;
176 rt_ret_val(p_TCB
, 1);
178 rt_ret_val(p_TCB
, OS_R_SEM
);
180 rt_put_prio (&os_rdy
, p_TCB
);
188 /*----------------------------------------------------------------------------
190 *---------------------------------------------------------------------------*/