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"
37 #include "rt_System.h"
40 #include "rt_Semaphore.h"
41 #include "rt_HAL_CM.h"
44 /*----------------------------------------------------------------------------
46 *---------------------------------------------------------------------------*/
49 /*--------------------------- rt_sem_init -----------------------------------*/
51 void rt_sem_init (OS_ID semaphore, U16 token_count) {
52 /* Initialize a semaphore */
53 P_SCB p_SCB = semaphore;
57 p_SCB->tokens = token_count;
61 /*--------------------------- rt_sem_delete ---------------------------------*/
64 OS_RESULT rt_sem_delete (OS_ID semaphore) {
65 /* Delete semaphore */
66 P_SCB p_SCB = semaphore;
69 while (p_SCB->p_lnk != NULL) {
70 /* A task is waiting for token */
71 p_TCB = rt_get_first ((P_XCB)p_SCB);
75 rt_put_prio (&os_rdy, p_TCB);
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;
92 /*--------------------------- rt_sem_send -----------------------------------*/
94 OS_RESULT rt_sem_send (OS_ID semaphore) {
95 /* Return a token to semaphore */
96 P_SCB p_SCB = semaphore;
99 if (p_SCB->p_lnk != NULL) {
100 /* A task is waiting for token */
101 p_TCB = rt_get_first ((P_XCB)p_SCB);
103 rt_ret_val(p_TCB, 1);
105 rt_ret_val(p_TCB, OS_R_SEM);
118 /*--------------------------- rt_sem_wait -----------------------------------*/
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;
128 /* No token available: wait for one */
132 if (p_SCB->p_lnk != NULL) {
133 rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
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;
140 rt_block(timeout, WAIT_SEM);
145 /*--------------------------- isr_sem_send ----------------------------------*/
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;
151 rt_psq_enq (p_SCB, 0);
156 /*--------------------------- rt_sem_psh ------------------------------------*/
158 void rt_sem_psh (P_SCB p_CB) {
159 /* Check if task has to be waken up */
162 if (p_CB->p_lnk != NULL) {
163 /* A task is waiting for token */
164 p_TCB = rt_get_first ((P_XCB)p_CB);
166 p_TCB->state = READY;
168 rt_ret_val(p_TCB, 1);
170 rt_ret_val(p_TCB, OS_R_SEM);
172 rt_put_prio (&os_rdy, p_TCB);
180 /*----------------------------------------------------------------------------
182 *---------------------------------------------------------------------------*/