Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.
Dependents: Assignment_3__Embedded_Software
Fork of mbed-rtos by
rtx/TARGET_ARM7/rt_Mutex.c@124:b63b65f13991, 2018-03-16 (annotated)
- Committer:
- f_legge
- Date:
- Fri Mar 16 13:44:31 2018 +0000
- Revision:
- 124:b63b65f13991
- Parent:
- 80:2dab120a94c2
WattBob_Compat;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 80:2dab120a94c2 | 1 | /*---------------------------------------------------------------------------- |
mbed_official | 80:2dab120a94c2 | 2 | * RL-ARM - RTX |
mbed_official | 80:2dab120a94c2 | 3 | *---------------------------------------------------------------------------- |
mbed_official | 80:2dab120a94c2 | 4 | * Name: RT_MUTEX.C |
mbed_official | 80:2dab120a94c2 | 5 | * Purpose: Implements mutex synchronization objects |
mbed_official | 80:2dab120a94c2 | 6 | * Rev.: V4.60 |
mbed_official | 80:2dab120a94c2 | 7 | *---------------------------------------------------------------------------- |
mbed_official | 80:2dab120a94c2 | 8 | * |
mbed_official | 80:2dab120a94c2 | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH |
mbed_official | 80:2dab120a94c2 | 10 | * All rights reserved. |
mbed_official | 80:2dab120a94c2 | 11 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 80:2dab120a94c2 | 12 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 80:2dab120a94c2 | 13 | * - Redistributions of source code must retain the above copyright |
mbed_official | 80:2dab120a94c2 | 14 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 80:2dab120a94c2 | 15 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 80:2dab120a94c2 | 16 | * notice, this list of conditions and the following disclaimer in the |
mbed_official | 80:2dab120a94c2 | 17 | * documentation and/or other materials provided with the distribution. |
mbed_official | 80:2dab120a94c2 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
mbed_official | 80:2dab120a94c2 | 19 | * to endorse or promote products derived from this software without |
mbed_official | 80:2dab120a94c2 | 20 | * specific prior written permission. |
mbed_official | 80:2dab120a94c2 | 21 | * |
mbed_official | 80:2dab120a94c2 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 80:2dab120a94c2 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 80:2dab120a94c2 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
mbed_official | 80:2dab120a94c2 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
mbed_official | 80:2dab120a94c2 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
mbed_official | 80:2dab120a94c2 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
mbed_official | 80:2dab120a94c2 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
mbed_official | 80:2dab120a94c2 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
mbed_official | 80:2dab120a94c2 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
mbed_official | 80:2dab120a94c2 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 80:2dab120a94c2 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 80:2dab120a94c2 | 33 | *---------------------------------------------------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 34 | |
mbed_official | 80:2dab120a94c2 | 35 | #include "rt_TypeDef.h" |
mbed_official | 80:2dab120a94c2 | 36 | #include "RTX_Conf.h" |
mbed_official | 80:2dab120a94c2 | 37 | #include "rt_List.h" |
mbed_official | 80:2dab120a94c2 | 38 | #include "rt_Task.h" |
mbed_official | 80:2dab120a94c2 | 39 | #include "rt_Mutex.h" |
mbed_official | 80:2dab120a94c2 | 40 | #include "rt_HAL_CM.h" |
mbed_official | 80:2dab120a94c2 | 41 | |
mbed_official | 80:2dab120a94c2 | 42 | |
mbed_official | 80:2dab120a94c2 | 43 | /*---------------------------------------------------------------------------- |
mbed_official | 80:2dab120a94c2 | 44 | * Functions |
mbed_official | 80:2dab120a94c2 | 45 | *---------------------------------------------------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 46 | |
mbed_official | 80:2dab120a94c2 | 47 | |
mbed_official | 80:2dab120a94c2 | 48 | /*--------------------------- rt_mut_init -----------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 49 | |
mbed_official | 80:2dab120a94c2 | 50 | void rt_mut_init (OS_ID mutex) { |
mbed_official | 80:2dab120a94c2 | 51 | /* Initialize a mutex object */ |
mbed_official | 80:2dab120a94c2 | 52 | P_MUCB p_MCB = mutex; |
mbed_official | 80:2dab120a94c2 | 53 | |
mbed_official | 80:2dab120a94c2 | 54 | p_MCB->cb_type = MUCB; |
mbed_official | 80:2dab120a94c2 | 55 | p_MCB->prio = 0; |
mbed_official | 80:2dab120a94c2 | 56 | p_MCB->level = 0; |
mbed_official | 80:2dab120a94c2 | 57 | p_MCB->p_lnk = NULL; |
mbed_official | 80:2dab120a94c2 | 58 | p_MCB->owner = NULL; |
mbed_official | 80:2dab120a94c2 | 59 | } |
mbed_official | 80:2dab120a94c2 | 60 | |
mbed_official | 80:2dab120a94c2 | 61 | |
mbed_official | 80:2dab120a94c2 | 62 | /*--------------------------- rt_mut_delete ---------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 63 | |
mbed_official | 80:2dab120a94c2 | 64 | #ifdef __CMSIS_RTOS |
mbed_official | 80:2dab120a94c2 | 65 | OS_RESULT rt_mut_delete (OS_ID mutex) { |
mbed_official | 80:2dab120a94c2 | 66 | /* Delete a mutex object */ |
mbed_official | 80:2dab120a94c2 | 67 | P_MUCB p_MCB = mutex; |
mbed_official | 80:2dab120a94c2 | 68 | P_TCB p_TCB; |
mbed_official | 80:2dab120a94c2 | 69 | |
mbed_official | 80:2dab120a94c2 | 70 | /* Restore owner task's priority. */ |
mbed_official | 80:2dab120a94c2 | 71 | if (p_MCB->level != 0) { |
mbed_official | 80:2dab120a94c2 | 72 | p_MCB->owner->prio = p_MCB->prio; |
mbed_official | 80:2dab120a94c2 | 73 | if (p_MCB->owner != os_tsk.run) { |
mbed_official | 80:2dab120a94c2 | 74 | rt_resort_prio (p_MCB->owner); |
mbed_official | 80:2dab120a94c2 | 75 | } |
mbed_official | 80:2dab120a94c2 | 76 | } |
mbed_official | 80:2dab120a94c2 | 77 | |
mbed_official | 80:2dab120a94c2 | 78 | while (p_MCB->p_lnk != NULL) { |
mbed_official | 80:2dab120a94c2 | 79 | /* A task is waiting for mutex. */ |
mbed_official | 80:2dab120a94c2 | 80 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
mbed_official | 80:2dab120a94c2 | 81 | rt_ret_val(p_TCB, 0/*osOK*/); |
mbed_official | 80:2dab120a94c2 | 82 | rt_rmv_dly(p_TCB); |
mbed_official | 80:2dab120a94c2 | 83 | p_TCB->state = READY; |
mbed_official | 80:2dab120a94c2 | 84 | rt_put_prio (&os_rdy, p_TCB); |
mbed_official | 80:2dab120a94c2 | 85 | } |
mbed_official | 80:2dab120a94c2 | 86 | |
mbed_official | 80:2dab120a94c2 | 87 | if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) { |
mbed_official | 80:2dab120a94c2 | 88 | /* preempt running task */ |
mbed_official | 80:2dab120a94c2 | 89 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 80:2dab120a94c2 | 90 | os_tsk.run->state = READY; |
mbed_official | 80:2dab120a94c2 | 91 | rt_dispatch (NULL); |
mbed_official | 80:2dab120a94c2 | 92 | } |
mbed_official | 80:2dab120a94c2 | 93 | |
mbed_official | 80:2dab120a94c2 | 94 | p_MCB->cb_type = 0; |
mbed_official | 80:2dab120a94c2 | 95 | |
mbed_official | 80:2dab120a94c2 | 96 | return (OS_R_OK); |
mbed_official | 80:2dab120a94c2 | 97 | } |
mbed_official | 80:2dab120a94c2 | 98 | #endif |
mbed_official | 80:2dab120a94c2 | 99 | |
mbed_official | 80:2dab120a94c2 | 100 | |
mbed_official | 80:2dab120a94c2 | 101 | /*--------------------------- rt_mut_release --------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 102 | |
mbed_official | 80:2dab120a94c2 | 103 | OS_RESULT rt_mut_release (OS_ID mutex) { |
mbed_official | 80:2dab120a94c2 | 104 | /* Release a mutex object */ |
mbed_official | 80:2dab120a94c2 | 105 | P_MUCB p_MCB = mutex; |
mbed_official | 80:2dab120a94c2 | 106 | P_TCB p_TCB; |
mbed_official | 80:2dab120a94c2 | 107 | |
mbed_official | 80:2dab120a94c2 | 108 | if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) { |
mbed_official | 80:2dab120a94c2 | 109 | /* Unbalanced mutex release or task is not the owner */ |
mbed_official | 80:2dab120a94c2 | 110 | return (OS_R_NOK); |
mbed_official | 80:2dab120a94c2 | 111 | } |
mbed_official | 80:2dab120a94c2 | 112 | if (--p_MCB->level != 0) { |
mbed_official | 80:2dab120a94c2 | 113 | return (OS_R_OK); |
mbed_official | 80:2dab120a94c2 | 114 | } |
mbed_official | 80:2dab120a94c2 | 115 | /* Restore owner task's priority. */ |
mbed_official | 80:2dab120a94c2 | 116 | os_tsk.run->prio = p_MCB->prio; |
mbed_official | 80:2dab120a94c2 | 117 | if (p_MCB->p_lnk != NULL) { |
mbed_official | 80:2dab120a94c2 | 118 | /* A task is waiting for mutex. */ |
mbed_official | 80:2dab120a94c2 | 119 | p_TCB = rt_get_first ((P_XCB)p_MCB); |
mbed_official | 80:2dab120a94c2 | 120 | #ifdef __CMSIS_RTOS |
mbed_official | 80:2dab120a94c2 | 121 | rt_ret_val(p_TCB, 0/*osOK*/); |
mbed_official | 80:2dab120a94c2 | 122 | #else |
mbed_official | 80:2dab120a94c2 | 123 | rt_ret_val(p_TCB, OS_R_MUT); |
mbed_official | 80:2dab120a94c2 | 124 | #endif |
mbed_official | 80:2dab120a94c2 | 125 | rt_rmv_dly (p_TCB); |
mbed_official | 80:2dab120a94c2 | 126 | /* A waiting task becomes the owner of this mutex. */ |
mbed_official | 80:2dab120a94c2 | 127 | p_MCB->level = 1; |
mbed_official | 80:2dab120a94c2 | 128 | p_MCB->owner = p_TCB; |
mbed_official | 80:2dab120a94c2 | 129 | p_MCB->prio = p_TCB->prio; |
mbed_official | 80:2dab120a94c2 | 130 | /* Priority inversion, check which task continues. */ |
mbed_official | 80:2dab120a94c2 | 131 | if (os_tsk.run->prio >= rt_rdy_prio()) { |
mbed_official | 80:2dab120a94c2 | 132 | rt_dispatch (p_TCB); |
mbed_official | 80:2dab120a94c2 | 133 | } |
mbed_official | 80:2dab120a94c2 | 134 | else { |
mbed_official | 80:2dab120a94c2 | 135 | /* Ready task has higher priority than running task. */ |
mbed_official | 80:2dab120a94c2 | 136 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 80:2dab120a94c2 | 137 | rt_put_prio (&os_rdy, p_TCB); |
mbed_official | 80:2dab120a94c2 | 138 | os_tsk.run->state = READY; |
mbed_official | 80:2dab120a94c2 | 139 | p_TCB->state = READY; |
mbed_official | 80:2dab120a94c2 | 140 | rt_dispatch (NULL); |
mbed_official | 80:2dab120a94c2 | 141 | } |
mbed_official | 80:2dab120a94c2 | 142 | } |
mbed_official | 80:2dab120a94c2 | 143 | else { |
mbed_official | 80:2dab120a94c2 | 144 | /* Check if own priority raised by priority inversion. */ |
mbed_official | 80:2dab120a94c2 | 145 | if (rt_rdy_prio() > os_tsk.run->prio) { |
mbed_official | 80:2dab120a94c2 | 146 | rt_put_prio (&os_rdy, os_tsk.run); |
mbed_official | 80:2dab120a94c2 | 147 | os_tsk.run->state = READY; |
mbed_official | 80:2dab120a94c2 | 148 | rt_dispatch (NULL); |
mbed_official | 80:2dab120a94c2 | 149 | } |
mbed_official | 80:2dab120a94c2 | 150 | } |
mbed_official | 80:2dab120a94c2 | 151 | return (OS_R_OK); |
mbed_official | 80:2dab120a94c2 | 152 | } |
mbed_official | 80:2dab120a94c2 | 153 | |
mbed_official | 80:2dab120a94c2 | 154 | |
mbed_official | 80:2dab120a94c2 | 155 | /*--------------------------- rt_mut_wait -----------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 156 | |
mbed_official | 80:2dab120a94c2 | 157 | OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) { |
mbed_official | 80:2dab120a94c2 | 158 | /* Wait for a mutex, continue when mutex is free. */ |
mbed_official | 80:2dab120a94c2 | 159 | P_MUCB p_MCB = mutex; |
mbed_official | 80:2dab120a94c2 | 160 | |
mbed_official | 80:2dab120a94c2 | 161 | if (p_MCB->level == 0) { |
mbed_official | 80:2dab120a94c2 | 162 | p_MCB->owner = os_tsk.run; |
mbed_official | 80:2dab120a94c2 | 163 | p_MCB->prio = os_tsk.run->prio; |
mbed_official | 80:2dab120a94c2 | 164 | goto inc; |
mbed_official | 80:2dab120a94c2 | 165 | } |
mbed_official | 80:2dab120a94c2 | 166 | if (p_MCB->owner == os_tsk.run) { |
mbed_official | 80:2dab120a94c2 | 167 | /* OK, running task is the owner of this mutex. */ |
mbed_official | 80:2dab120a94c2 | 168 | inc:p_MCB->level++; |
mbed_official | 80:2dab120a94c2 | 169 | return (OS_R_OK); |
mbed_official | 80:2dab120a94c2 | 170 | } |
mbed_official | 80:2dab120a94c2 | 171 | /* Mutex owned by another task, wait until released. */ |
mbed_official | 80:2dab120a94c2 | 172 | if (timeout == 0) { |
mbed_official | 80:2dab120a94c2 | 173 | return (OS_R_TMO); |
mbed_official | 80:2dab120a94c2 | 174 | } |
mbed_official | 80:2dab120a94c2 | 175 | /* Raise the owner task priority if lower than current priority. */ |
mbed_official | 80:2dab120a94c2 | 176 | /* This priority inversion is called priority inheritance. */ |
mbed_official | 80:2dab120a94c2 | 177 | if (p_MCB->prio < os_tsk.run->prio) { |
mbed_official | 80:2dab120a94c2 | 178 | p_MCB->owner->prio = os_tsk.run->prio; |
mbed_official | 80:2dab120a94c2 | 179 | rt_resort_prio (p_MCB->owner); |
mbed_official | 80:2dab120a94c2 | 180 | } |
mbed_official | 80:2dab120a94c2 | 181 | if (p_MCB->p_lnk != NULL) { |
mbed_official | 80:2dab120a94c2 | 182 | rt_put_prio ((P_XCB)p_MCB, os_tsk.run); |
mbed_official | 80:2dab120a94c2 | 183 | } |
mbed_official | 80:2dab120a94c2 | 184 | else { |
mbed_official | 80:2dab120a94c2 | 185 | p_MCB->p_lnk = os_tsk.run; |
mbed_official | 80:2dab120a94c2 | 186 | os_tsk.run->p_lnk = NULL; |
mbed_official | 80:2dab120a94c2 | 187 | os_tsk.run->p_rlnk = (P_TCB)p_MCB; |
mbed_official | 80:2dab120a94c2 | 188 | } |
mbed_official | 80:2dab120a94c2 | 189 | rt_block(timeout, WAIT_MUT); |
mbed_official | 80:2dab120a94c2 | 190 | return (OS_R_TMO); |
mbed_official | 80:2dab120a94c2 | 191 | } |
mbed_official | 80:2dab120a94c2 | 192 | |
mbed_official | 80:2dab120a94c2 | 193 | |
mbed_official | 80:2dab120a94c2 | 194 | /*---------------------------------------------------------------------------- |
mbed_official | 80:2dab120a94c2 | 195 | * end of file |
mbed_official | 80:2dab120a94c2 | 196 | *---------------------------------------------------------------------------*/ |
mbed_official | 80:2dab120a94c2 | 197 |