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