]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_Mutex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_A / rt_Mutex.c
1 /*----------------------------------------------------------------------------
2  *      RL-ARM - RTX
3  *----------------------------------------------------------------------------
4  *      Name:    RT_MUTEX.C
5  *      Purpose: Implements mutex synchronization objects
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_List.h"
38 #include "rt_Task.h"
39 #include "rt_Mutex.h"
40 #ifdef __CORTEX_A9
41 #include "rt_HAL_CA.h"
42 #else
43 #include "rt_HAL_CM.h"
44 #endif
45
46
47 /*----------------------------------------------------------------------------
48  *      Functions
49  *---------------------------------------------------------------------------*/
50
51
52 /*--------------------------- rt_mut_init -----------------------------------*/
53
54 void rt_mut_init (OS_ID mutex) {
55   /* Initialize a mutex object */
56   P_MUCB p_MCB = mutex;
57
58   p_MCB->cb_type = MUCB;
59   p_MCB->prio    = 0;
60   p_MCB->level   = 0;
61   p_MCB->p_lnk   = NULL;
62   p_MCB->owner   = NULL;
63 }
64
65
66 /*--------------------------- rt_mut_delete ---------------------------------*/
67
68 #ifdef __CMSIS_RTOS
69 OS_RESULT rt_mut_delete (OS_ID mutex) {
70   /* Delete a mutex object */
71   P_MUCB p_MCB = mutex;
72   P_TCB  p_TCB;
73
74   __DMB();
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);
80     }
81   }
82
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*/);
87     rt_rmv_dly(p_TCB);
88     p_TCB->state = READY;
89     rt_put_prio (&os_rdy, p_TCB);
90   }
91
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;
96     rt_dispatch (NULL);
97   }
98
99   p_MCB->cb_type = 0;
100
101   return (OS_R_OK);
102 }
103 #endif
104
105
106 /*--------------------------- rt_mut_release --------------------------------*/
107
108 OS_RESULT rt_mut_release (OS_ID mutex) {
109   /* Release a mutex object */
110   P_MUCB p_MCB = mutex;
111   P_TCB  p_TCB;
112
113   if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
114     /* Unbalanced mutex release or task is not the owner */
115     return (OS_R_NOK);
116   }
117   __DMB();
118   if (--p_MCB->level != 0) {
119     return (OS_R_OK);
120   }
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);
126 #ifdef __CMSIS_RTOS
127     rt_ret_val(p_TCB, 0/*osOK*/);
128 #else
129     rt_ret_val(p_TCB, OS_R_MUT);
130 #endif
131     rt_rmv_dly (p_TCB);
132     /* A waiting task becomes the owner of this mutex. */
133     p_MCB->level     = 1;
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()) {
138       rt_dispatch (p_TCB);
139     }
140     else {
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;
146       rt_dispatch (NULL);
147     }
148   }
149   else {
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;
154       rt_dispatch (NULL);
155     }
156   }
157   return (OS_R_OK);
158 }
159
160
161 /*--------------------------- rt_mut_wait -----------------------------------*/
162
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;
166
167   if (p_MCB->level == 0) {
168     p_MCB->owner = os_tsk.run;
169     p_MCB->prio  = os_tsk.run->prio;
170     goto inc;
171   }
172   if (p_MCB->owner == os_tsk.run) {
173     /* OK, running task is the owner of this mutex. */
174 inc:p_MCB->level++;
175     __DMB();
176     return (OS_R_OK);
177   }
178   /* Mutex owned by another task, wait until released. */
179   if (timeout == 0) {
180     return (OS_R_TMO);
181   }
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);
187   }
188   if (p_MCB->p_lnk != NULL) {
189     rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
190   }
191   else {
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;
195   }
196   rt_block(timeout, WAIT_MUT);
197   return (OS_R_TMO);
198 }
199
200
201 /*----------------------------------------------------------------------------
202  * end of file
203  *---------------------------------------------------------------------------*/
204