]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_Mutex.c
1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Implements mutex synchronization objects
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"
41 #include "rt_HAL_CA.h"
43 #include "rt_HAL_CM.h"
47 /*----------------------------------------------------------------------------
49 *---------------------------------------------------------------------------*/
52 /*--------------------------- rt_mut_init -----------------------------------*/
54 void rt_mut_init (OS_ID mutex
) {
55 /* Initialize a mutex object */
58 p_MCB
->cb_type
= MUCB
;
66 /*--------------------------- rt_mut_delete ---------------------------------*/
69 OS_RESULT
rt_mut_delete (OS_ID mutex
) {
70 /* Delete a mutex object */
75 /* Restore owner task's priority. */
76 if (p_MCB
->level
!= 0) {
77 p_MCB
->owner
->prio
= p_MCB
->prio
;
78 if (p_MCB
->owner
!= os_tsk
.run
) {
79 rt_resort_prio (p_MCB
->owner
);
83 while (p_MCB
->p_lnk
!= NULL
) {
84 /* A task is waiting for mutex. */
85 p_TCB
= rt_get_first ((P_XCB
)p_MCB
);
86 rt_ret_val(p_TCB
, 0/*osOK*/);
89 rt_put_prio (&os_rdy
, p_TCB
);
92 if (os_rdy
.p_lnk
&& (os_rdy
.p_lnk
->prio
> os_tsk
.run
->prio
)) {
93 /* preempt running task */
94 rt_put_prio (&os_rdy
, os_tsk
.run
);
95 os_tsk
.run
->state
= READY
;
106 /*--------------------------- rt_mut_release --------------------------------*/
108 OS_RESULT
rt_mut_release (OS_ID mutex
) {
109 /* Release a mutex object */
110 P_MUCB p_MCB
= mutex
;
113 if (p_MCB
->level
== 0 || p_MCB
->owner
!= os_tsk
.run
) {
114 /* Unbalanced mutex release or task is not the owner */
118 if (--p_MCB
->level
!= 0) {
121 /* Restore owner task's priority. */
122 os_tsk
.run
->prio
= p_MCB
->prio
;
123 if (p_MCB
->p_lnk
!= NULL
) {
124 /* A task is waiting for mutex. */
125 p_TCB
= rt_get_first ((P_XCB
)p_MCB
);
127 rt_ret_val(p_TCB
, 0/*osOK*/);
129 rt_ret_val(p_TCB
, OS_R_MUT
);
132 /* A waiting task becomes the owner of this mutex. */
134 p_MCB
->owner
= p_TCB
;
135 p_MCB
->prio
= p_TCB
->prio
;
136 /* Priority inversion, check which task continues. */
137 if (os_tsk
.run
->prio
>= rt_rdy_prio()) {
141 /* Ready task has higher priority than running task. */
142 rt_put_prio (&os_rdy
, os_tsk
.run
);
143 rt_put_prio (&os_rdy
, p_TCB
);
144 os_tsk
.run
->state
= READY
;
145 p_TCB
->state
= READY
;
150 /* Check if own priority raised by priority inversion. */
151 if (rt_rdy_prio() > os_tsk
.run
->prio
) {
152 rt_put_prio (&os_rdy
, os_tsk
.run
);
153 os_tsk
.run
->state
= READY
;
161 /*--------------------------- rt_mut_wait -----------------------------------*/
163 OS_RESULT
rt_mut_wait (OS_ID mutex
, U16 timeout
) {
164 /* Wait for a mutex, continue when mutex is free. */
165 P_MUCB p_MCB
= mutex
;
167 if (p_MCB
->level
== 0) {
168 p_MCB
->owner
= os_tsk
.run
;
169 p_MCB
->prio
= os_tsk
.run
->prio
;
172 if (p_MCB
->owner
== os_tsk
.run
) {
173 /* OK, running task is the owner of this mutex. */
178 /* Mutex owned by another task, wait until released. */
182 /* Raise the owner task priority if lower than current priority. */
183 /* This priority inversion is called priority inheritance. */
184 if (p_MCB
->prio
< os_tsk
.run
->prio
) {
185 p_MCB
->owner
->prio
= os_tsk
.run
->prio
;
186 rt_resort_prio (p_MCB
->owner
);
188 if (p_MCB
->p_lnk
!= NULL
) {
189 rt_put_prio ((P_XCB
)p_MCB
, os_tsk
.run
);
192 p_MCB
->p_lnk
= os_tsk
.run
;
193 os_tsk
.run
->p_lnk
= NULL
;
194 os_tsk
.run
->p_rlnk
= (P_TCB
)p_MCB
;
196 rt_block(timeout
, WAIT_MUT
);
201 /*----------------------------------------------------------------------------
203 *---------------------------------------------------------------------------*/