Fork of the mbed-rtos library by mbed.

Dependents:   internet_clock 4180_Final_Project 4180_Final_Project_WaveProblems 4180_Final_Project ... more

Fork of mbed-rtos by mbed official

Committer:
mbed_official
Date:
Thu Nov 06 13:00:11 2014 +0000
Revision:
49:77c8e4604045
Synchronized with git revision 7b90c2ba137baaf9769219e0e8a7b8e8d1299c4f

Full URL: https://github.com/mbedmicro/mbed/commit/7b90c2ba137baaf9769219e0e8a7b8e8d1299c4f/

This target is not yet tested, so it can't be released as part of the official
SDK build for now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 49:77c8e4604045 1 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 2 * RL-ARM - 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 49:77c8e4604045 6 * Rev.: V4.60
mbed_official 49:77c8e4604045 7 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 8 *
mbed_official 49:77c8e4604045 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 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 49:77c8e4604045 36 #include "RTX_Conf.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 49:77c8e4604045 55 p_MCB->prio = 0;
mbed_official 49:77c8e4604045 56 p_MCB->level = 0;
mbed_official 49:77c8e4604045 57 p_MCB->p_lnk = NULL;
mbed_official 49:77c8e4604045 58 p_MCB->owner = 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 49:77c8e4604045 69
mbed_official 49:77c8e4604045 70 /* Restore owner task's priority. */
mbed_official 49:77c8e4604045 71 if (p_MCB->level != 0) {
mbed_official 49:77c8e4604045 72 p_MCB->owner->prio = p_MCB->prio;
mbed_official 49:77c8e4604045 73 if (p_MCB->owner != os_tsk.run) {
mbed_official 49:77c8e4604045 74 rt_resort_prio (p_MCB->owner);
mbed_official 49:77c8e4604045 75 }
mbed_official 49:77c8e4604045 76 }
mbed_official 49:77c8e4604045 77
mbed_official 49:77c8e4604045 78 while (p_MCB->p_lnk != NULL) {
mbed_official 49:77c8e4604045 79 /* A task is waiting for mutex. */
mbed_official 49:77c8e4604045 80 p_TCB = rt_get_first ((P_XCB)p_MCB);
mbed_official 49:77c8e4604045 81 rt_ret_val(p_TCB, 0/*osOK*/);
mbed_official 49:77c8e4604045 82 rt_rmv_dly(p_TCB);
mbed_official 49:77c8e4604045 83 p_TCB->state = READY;
mbed_official 49:77c8e4604045 84 rt_put_prio (&os_rdy, p_TCB);
mbed_official 49:77c8e4604045 85 }
mbed_official 49:77c8e4604045 86
mbed_official 49:77c8e4604045 87 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
mbed_official 49:77c8e4604045 88 /* preempt running task */
mbed_official 49:77c8e4604045 89 rt_put_prio (&os_rdy, os_tsk.run);
mbed_official 49:77c8e4604045 90 os_tsk.run->state = READY;
mbed_official 49:77c8e4604045 91 rt_dispatch (NULL);
mbed_official 49:77c8e4604045 92 }
mbed_official 49:77c8e4604045 93
mbed_official 49:77c8e4604045 94 p_MCB->cb_type = 0;
mbed_official 49:77c8e4604045 95
mbed_official 49:77c8e4604045 96 return (OS_R_OK);
mbed_official 49:77c8e4604045 97 }
mbed_official 49:77c8e4604045 98 #endif
mbed_official 49:77c8e4604045 99
mbed_official 49:77c8e4604045 100
mbed_official 49:77c8e4604045 101 /*--------------------------- rt_mut_release --------------------------------*/
mbed_official 49:77c8e4604045 102
mbed_official 49:77c8e4604045 103 OS_RESULT rt_mut_release (OS_ID mutex) {
mbed_official 49:77c8e4604045 104 /* Release a mutex object */
mbed_official 49:77c8e4604045 105 P_MUCB p_MCB = mutex;
mbed_official 49:77c8e4604045 106 P_TCB p_TCB;
mbed_official 49:77c8e4604045 107
mbed_official 49:77c8e4604045 108 if (p_MCB->level == 0 || p_MCB->owner != os_tsk.run) {
mbed_official 49:77c8e4604045 109 /* Unbalanced mutex release or task is not the owner */
mbed_official 49:77c8e4604045 110 return (OS_R_NOK);
mbed_official 49:77c8e4604045 111 }
mbed_official 49:77c8e4604045 112 if (--p_MCB->level != 0) {
mbed_official 49:77c8e4604045 113 return (OS_R_OK);
mbed_official 49:77c8e4604045 114 }
mbed_official 49:77c8e4604045 115 /* Restore owner task's priority. */
mbed_official 49:77c8e4604045 116 os_tsk.run->prio = p_MCB->prio;
mbed_official 49:77c8e4604045 117 if (p_MCB->p_lnk != NULL) {
mbed_official 49:77c8e4604045 118 /* A task is waiting for mutex. */
mbed_official 49:77c8e4604045 119 p_TCB = rt_get_first ((P_XCB)p_MCB);
mbed_official 49:77c8e4604045 120 #ifdef __CMSIS_RTOS
mbed_official 49:77c8e4604045 121 rt_ret_val(p_TCB, 0/*osOK*/);
mbed_official 49:77c8e4604045 122 #else
mbed_official 49:77c8e4604045 123 rt_ret_val(p_TCB, OS_R_MUT);
mbed_official 49:77c8e4604045 124 #endif
mbed_official 49:77c8e4604045 125 rt_rmv_dly (p_TCB);
mbed_official 49:77c8e4604045 126 /* A waiting task becomes the owner of this mutex. */
mbed_official 49:77c8e4604045 127 p_MCB->level = 1;
mbed_official 49:77c8e4604045 128 p_MCB->owner = p_TCB;
mbed_official 49:77c8e4604045 129 p_MCB->prio = p_TCB->prio;
mbed_official 49:77c8e4604045 130 /* Priority inversion, check which task continues. */
mbed_official 49:77c8e4604045 131 if (os_tsk.run->prio >= rt_rdy_prio()) {
mbed_official 49:77c8e4604045 132 rt_dispatch (p_TCB);
mbed_official 49:77c8e4604045 133 }
mbed_official 49:77c8e4604045 134 else {
mbed_official 49:77c8e4604045 135 /* Ready task has higher priority than running task. */
mbed_official 49:77c8e4604045 136 rt_put_prio (&os_rdy, os_tsk.run);
mbed_official 49:77c8e4604045 137 rt_put_prio (&os_rdy, p_TCB);
mbed_official 49:77c8e4604045 138 os_tsk.run->state = READY;
mbed_official 49:77c8e4604045 139 p_TCB->state = READY;
mbed_official 49:77c8e4604045 140 rt_dispatch (NULL);
mbed_official 49:77c8e4604045 141 }
mbed_official 49:77c8e4604045 142 }
mbed_official 49:77c8e4604045 143 else {
mbed_official 49:77c8e4604045 144 /* Check if own priority raised by priority inversion. */
mbed_official 49:77c8e4604045 145 if (rt_rdy_prio() > os_tsk.run->prio) {
mbed_official 49:77c8e4604045 146 rt_put_prio (&os_rdy, os_tsk.run);
mbed_official 49:77c8e4604045 147 os_tsk.run->state = READY;
mbed_official 49:77c8e4604045 148 rt_dispatch (NULL);
mbed_official 49:77c8e4604045 149 }
mbed_official 49:77c8e4604045 150 }
mbed_official 49:77c8e4604045 151 return (OS_R_OK);
mbed_official 49:77c8e4604045 152 }
mbed_official 49:77c8e4604045 153
mbed_official 49:77c8e4604045 154
mbed_official 49:77c8e4604045 155 /*--------------------------- rt_mut_wait -----------------------------------*/
mbed_official 49:77c8e4604045 156
mbed_official 49:77c8e4604045 157 OS_RESULT rt_mut_wait (OS_ID mutex, U16 timeout) {
mbed_official 49:77c8e4604045 158 /* Wait for a mutex, continue when mutex is free. */
mbed_official 49:77c8e4604045 159 P_MUCB p_MCB = mutex;
mbed_official 49:77c8e4604045 160
mbed_official 49:77c8e4604045 161 if (p_MCB->level == 0) {
mbed_official 49:77c8e4604045 162 p_MCB->owner = os_tsk.run;
mbed_official 49:77c8e4604045 163 p_MCB->prio = os_tsk.run->prio;
mbed_official 49:77c8e4604045 164 goto inc;
mbed_official 49:77c8e4604045 165 }
mbed_official 49:77c8e4604045 166 if (p_MCB->owner == os_tsk.run) {
mbed_official 49:77c8e4604045 167 /* OK, running task is the owner of this mutex. */
mbed_official 49:77c8e4604045 168 inc:p_MCB->level++;
mbed_official 49:77c8e4604045 169 return (OS_R_OK);
mbed_official 49:77c8e4604045 170 }
mbed_official 49:77c8e4604045 171 /* Mutex owned by another task, wait until released. */
mbed_official 49:77c8e4604045 172 if (timeout == 0) {
mbed_official 49:77c8e4604045 173 return (OS_R_TMO);
mbed_official 49:77c8e4604045 174 }
mbed_official 49:77c8e4604045 175 /* Raise the owner task priority if lower than current priority. */
mbed_official 49:77c8e4604045 176 /* This priority inversion is called priority inheritance. */
mbed_official 49:77c8e4604045 177 if (p_MCB->prio < os_tsk.run->prio) {
mbed_official 49:77c8e4604045 178 p_MCB->owner->prio = os_tsk.run->prio;
mbed_official 49:77c8e4604045 179 rt_resort_prio (p_MCB->owner);
mbed_official 49:77c8e4604045 180 }
mbed_official 49:77c8e4604045 181 if (p_MCB->p_lnk != NULL) {
mbed_official 49:77c8e4604045 182 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
mbed_official 49:77c8e4604045 183 }
mbed_official 49:77c8e4604045 184 else {
mbed_official 49:77c8e4604045 185 p_MCB->p_lnk = os_tsk.run;
mbed_official 49:77c8e4604045 186 os_tsk.run->p_lnk = NULL;
mbed_official 49:77c8e4604045 187 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
mbed_official 49:77c8e4604045 188 }
mbed_official 49:77c8e4604045 189 rt_block(timeout, WAIT_MUT);
mbed_official 49:77c8e4604045 190 return (OS_R_TMO);
mbed_official 49:77c8e4604045 191 }
mbed_official 49:77c8e4604045 192
mbed_official 49:77c8e4604045 193
mbed_official 49:77c8e4604045 194 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 195 * end of file
mbed_official 49:77c8e4604045 196 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 197