Philippe Bazot / Mbed 2 deprecated DU4SmartCities

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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