mutex++

Dependents:   header_main_publish

Fork of mbed-rtos by mbed official

Committer:
mbed_official
Date:
Tue Jun 03 11:30:14 2014 +0100
Revision:
31:015df9e602b6
Parent:
11:db1fc233faa9
Synchronized with git revision bcacbb9fbf3432829227430830cca4315b57c1b9

Full URL: https://github.com/mbedmicro/mbed/commit/bcacbb9fbf3432829227430830cca4315b57c1b9/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 6:350b53afb889 1 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 2 * RL-ARM - RTX
emilmont 6:350b53afb889 3 *----------------------------------------------------------------------------
emilmont 6:350b53afb889 4 * Name: RT_SEMAPHORE.C
emilmont 6:350b53afb889 5 * Purpose: Implements binary and counting semaphores
emilmont 6:350b53afb889 6 * Rev.: V4.60
emilmont 6:350b53afb889 7 *----------------------------------------------------------------------------
emilmont 6:350b53afb889 8 *
emilmont 6:350b53afb889 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
emilmont 6:350b53afb889 10 * All rights reserved.
emilmont 6:350b53afb889 11 * Redistribution and use in source and binary forms, with or without
emilmont 6:350b53afb889 12 * modification, are permitted provided that the following conditions are met:
emilmont 6:350b53afb889 13 * - Redistributions of source code must retain the above copyright
emilmont 6:350b53afb889 14 * notice, this list of conditions and the following disclaimer.
emilmont 6:350b53afb889 15 * - Redistributions in binary form must reproduce the above copyright
emilmont 6:350b53afb889 16 * notice, this list of conditions and the following disclaimer in the
emilmont 6:350b53afb889 17 * documentation and/or other materials provided with the distribution.
mbed_official 31:015df9e602b6 18 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 31:015df9e602b6 19 * to endorse or promote products derived from this software without
emilmont 6:350b53afb889 20 * specific prior written permission.
emilmont 6:350b53afb889 21 *
mbed_official 31:015df9e602b6 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 31:015df9e602b6 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
emilmont 6:350b53afb889 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
emilmont 6:350b53afb889 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
emilmont 6:350b53afb889 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 31:015df9e602b6 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 31:015df9e602b6 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 31:015df9e602b6 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 31:015df9e602b6 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
emilmont 6:350b53afb889 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
emilmont 6:350b53afb889 32 * POSSIBILITY OF SUCH DAMAGE.
emilmont 6:350b53afb889 33 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 34
emilmont 6:350b53afb889 35 #include "rt_TypeDef.h"
emilmont 11:db1fc233faa9 36 #include "RTX_Conf.h"
emilmont 6:350b53afb889 37 #include "rt_System.h"
emilmont 6:350b53afb889 38 #include "rt_List.h"
emilmont 6:350b53afb889 39 #include "rt_Task.h"
emilmont 6:350b53afb889 40 #include "rt_Semaphore.h"
emilmont 6:350b53afb889 41 #include "rt_HAL_CM.h"
emilmont 6:350b53afb889 42
emilmont 6:350b53afb889 43
emilmont 6:350b53afb889 44 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 45 * Functions
emilmont 6:350b53afb889 46 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 47
emilmont 6:350b53afb889 48
emilmont 6:350b53afb889 49 /*--------------------------- rt_sem_init -----------------------------------*/
emilmont 6:350b53afb889 50
emilmont 6:350b53afb889 51 void rt_sem_init (OS_ID semaphore, U16 token_count) {
emilmont 6:350b53afb889 52 /* Initialize a semaphore */
emilmont 6:350b53afb889 53 P_SCB p_SCB = semaphore;
emilmont 6:350b53afb889 54
emilmont 6:350b53afb889 55 p_SCB->cb_type = SCB;
emilmont 6:350b53afb889 56 p_SCB->p_lnk = NULL;
emilmont 6:350b53afb889 57 p_SCB->tokens = token_count;
emilmont 6:350b53afb889 58 }
emilmont 6:350b53afb889 59
emilmont 6:350b53afb889 60
emilmont 6:350b53afb889 61 /*--------------------------- rt_sem_delete ---------------------------------*/
emilmont 6:350b53afb889 62
emilmont 6:350b53afb889 63 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 64 OS_RESULT rt_sem_delete (OS_ID semaphore) {
emilmont 6:350b53afb889 65 /* Delete semaphore */
emilmont 6:350b53afb889 66 P_SCB p_SCB = semaphore;
emilmont 6:350b53afb889 67 P_TCB p_TCB;
emilmont 6:350b53afb889 68
emilmont 6:350b53afb889 69 while (p_SCB->p_lnk != NULL) {
emilmont 6:350b53afb889 70 /* A task is waiting for token */
emilmont 6:350b53afb889 71 p_TCB = rt_get_first ((P_XCB)p_SCB);
emilmont 6:350b53afb889 72 rt_ret_val(p_TCB, 0);
emilmont 6:350b53afb889 73 rt_rmv_dly(p_TCB);
emilmont 6:350b53afb889 74 p_TCB->state = READY;
emilmont 6:350b53afb889 75 rt_put_prio (&os_rdy, p_TCB);
emilmont 6:350b53afb889 76 }
emilmont 6:350b53afb889 77
emilmont 6:350b53afb889 78 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
emilmont 6:350b53afb889 79 /* preempt running task */
emilmont 6:350b53afb889 80 rt_put_prio (&os_rdy, os_tsk.run);
emilmont 6:350b53afb889 81 os_tsk.run->state = READY;
emilmont 6:350b53afb889 82 rt_dispatch (NULL);
emilmont 6:350b53afb889 83 }
emilmont 6:350b53afb889 84
emilmont 6:350b53afb889 85 p_SCB->cb_type = 0;
emilmont 6:350b53afb889 86
emilmont 6:350b53afb889 87 return (OS_R_OK);
emilmont 6:350b53afb889 88 }
emilmont 6:350b53afb889 89 #endif
emilmont 6:350b53afb889 90
emilmont 6:350b53afb889 91
emilmont 6:350b53afb889 92 /*--------------------------- rt_sem_send -----------------------------------*/
emilmont 6:350b53afb889 93
emilmont 6:350b53afb889 94 OS_RESULT rt_sem_send (OS_ID semaphore) {
emilmont 6:350b53afb889 95 /* Return a token to semaphore */
emilmont 6:350b53afb889 96 P_SCB p_SCB = semaphore;
emilmont 6:350b53afb889 97 P_TCB p_TCB;
emilmont 6:350b53afb889 98
emilmont 6:350b53afb889 99 if (p_SCB->p_lnk != NULL) {
emilmont 6:350b53afb889 100 /* A task is waiting for token */
emilmont 6:350b53afb889 101 p_TCB = rt_get_first ((P_XCB)p_SCB);
emilmont 6:350b53afb889 102 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 103 rt_ret_val(p_TCB, 1);
emilmont 6:350b53afb889 104 #else
emilmont 6:350b53afb889 105 rt_ret_val(p_TCB, OS_R_SEM);
emilmont 6:350b53afb889 106 #endif
emilmont 6:350b53afb889 107 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 108 rt_dispatch (p_TCB);
emilmont 6:350b53afb889 109 }
emilmont 6:350b53afb889 110 else {
emilmont 6:350b53afb889 111 /* Store token. */
emilmont 6:350b53afb889 112 p_SCB->tokens++;
emilmont 6:350b53afb889 113 }
emilmont 6:350b53afb889 114 return (OS_R_OK);
emilmont 6:350b53afb889 115 }
emilmont 6:350b53afb889 116
emilmont 6:350b53afb889 117
emilmont 6:350b53afb889 118 /*--------------------------- rt_sem_wait -----------------------------------*/
emilmont 6:350b53afb889 119
emilmont 6:350b53afb889 120 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
emilmont 6:350b53afb889 121 /* Obtain a token; possibly wait for it */
emilmont 6:350b53afb889 122 P_SCB p_SCB = semaphore;
emilmont 6:350b53afb889 123
emilmont 6:350b53afb889 124 if (p_SCB->tokens) {
emilmont 6:350b53afb889 125 p_SCB->tokens--;
emilmont 6:350b53afb889 126 return (OS_R_OK);
emilmont 6:350b53afb889 127 }
emilmont 6:350b53afb889 128 /* No token available: wait for one */
emilmont 6:350b53afb889 129 if (timeout == 0) {
emilmont 6:350b53afb889 130 return (OS_R_TMO);
emilmont 6:350b53afb889 131 }
emilmont 6:350b53afb889 132 if (p_SCB->p_lnk != NULL) {
emilmont 6:350b53afb889 133 rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
emilmont 6:350b53afb889 134 }
emilmont 6:350b53afb889 135 else {
emilmont 6:350b53afb889 136 p_SCB->p_lnk = os_tsk.run;
emilmont 6:350b53afb889 137 os_tsk.run->p_lnk = NULL;
emilmont 6:350b53afb889 138 os_tsk.run->p_rlnk = (P_TCB)p_SCB;
emilmont 6:350b53afb889 139 }
emilmont 6:350b53afb889 140 rt_block(timeout, WAIT_SEM);
emilmont 6:350b53afb889 141 return (OS_R_TMO);
emilmont 6:350b53afb889 142 }
emilmont 6:350b53afb889 143
emilmont 6:350b53afb889 144
emilmont 6:350b53afb889 145 /*--------------------------- isr_sem_send ----------------------------------*/
emilmont 6:350b53afb889 146
emilmont 6:350b53afb889 147 void isr_sem_send (OS_ID semaphore) {
emilmont 6:350b53afb889 148 /* Same function as "os_sem"send", but to be called by ISRs */
emilmont 6:350b53afb889 149 P_SCB p_SCB = semaphore;
emilmont 6:350b53afb889 150
emilmont 6:350b53afb889 151 rt_psq_enq (p_SCB, 0);
emilmont 6:350b53afb889 152 rt_psh_req ();
emilmont 6:350b53afb889 153 }
emilmont 6:350b53afb889 154
emilmont 6:350b53afb889 155
emilmont 6:350b53afb889 156 /*--------------------------- rt_sem_psh ------------------------------------*/
emilmont 6:350b53afb889 157
emilmont 6:350b53afb889 158 void rt_sem_psh (P_SCB p_CB) {
emilmont 6:350b53afb889 159 /* Check if task has to be waken up */
emilmont 6:350b53afb889 160 P_TCB p_TCB;
emilmont 6:350b53afb889 161
emilmont 6:350b53afb889 162 if (p_CB->p_lnk != NULL) {
emilmont 6:350b53afb889 163 /* A task is waiting for token */
emilmont 6:350b53afb889 164 p_TCB = rt_get_first ((P_XCB)p_CB);
emilmont 6:350b53afb889 165 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 166 p_TCB->state = READY;
emilmont 6:350b53afb889 167 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 168 rt_ret_val(p_TCB, 1);
emilmont 6:350b53afb889 169 #else
emilmont 6:350b53afb889 170 rt_ret_val(p_TCB, OS_R_SEM);
emilmont 6:350b53afb889 171 #endif
emilmont 6:350b53afb889 172 rt_put_prio (&os_rdy, p_TCB);
emilmont 6:350b53afb889 173 }
emilmont 6:350b53afb889 174 else {
emilmont 6:350b53afb889 175 /* Store token */
emilmont 6:350b53afb889 176 p_CB->tokens++;
emilmont 6:350b53afb889 177 }
emilmont 6:350b53afb889 178 }
emilmont 6:350b53afb889 179
emilmont 6:350b53afb889 180 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 181 * end of file
emilmont 6:350b53afb889 182 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 183