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