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