in OS_TSK, rename run as tsk_run and new as tsk_new.

Committer:
jonathonfletcher
Date:
Sun Sep 02 03:24:20 2012 +0000
Revision:
0:5f46ebd8588e
in OS_TSK, rename run as tsk_run and new as tsk_new.

Who changed what in which revision?

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