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