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