Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Deprecated

This is the mbed 2 rtos library. mbed OS 5 integrates the mbed library with mbed-rtos. With this, we have provided thread safety for all mbed APIs. If you'd like to learn about using mbed OS 5, please see the docs.

Committer:
Kojto
Date:
Tue Jul 04 13:32:20 2017 +0100
Revision:
125:5713cbbdb706
Parent:
112:53ace74b190c
replace mbed_rtx by mbed_rtx4

Not causing a conflict with mbed_rtx that is for newer rtos

Who changed what in which revision?

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