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