Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rt_Semaphore.c Source File

rt_Semaphore.c

00001 /*----------------------------------------------------------------------------
00002  *      RL-ARM - RTX
00003  *----------------------------------------------------------------------------
00004  *      Name:    RT_SEMAPHORE.C
00005  *      Purpose: Implements binary and counting semaphores
00006  *      Rev.:    V4.50
00007  *----------------------------------------------------------------------------
00008  *
00009  * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
00010  * All rights reserved.
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are met:
00013  *  - Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  *  - Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  *  - Neither the name of ARM  nor the names of its contributors may be used 
00019  *    to endorse or promote products derived from this software without 
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  *---------------------------------------------------------------------------*/
00034 
00035 #include "rt_TypeDef.h"
00036 #include "RTX_Config.h"
00037 #include "rt_System.h"
00038 #include "rt_List.h"
00039 #include "rt_Task.h"
00040 #include "rt_Semaphore.h"
00041 #include "rt_HAL_CM.h"
00042 
00043 
00044 /*----------------------------------------------------------------------------
00045  *      Functions
00046  *---------------------------------------------------------------------------*/
00047 
00048 
00049 /*--------------------------- rt_sem_init -----------------------------------*/
00050 
00051 void rt_sem_init (OS_ID semaphore, U16 token_count) {
00052   /* Initialize a semaphore */
00053   P_SCB p_SCB = semaphore;
00054 
00055   p_SCB->cb_type = SCB;
00056   p_SCB->p_lnk  = NULL;
00057   p_SCB->tokens = token_count;
00058 }
00059 
00060 
00061 /*--------------------------- rt_sem_delete ---------------------------------*/
00062 OS_RESULT rt_sem_delete (OS_ID semaphore) {
00063   /* Delete semaphore */
00064   P_SCB p_SCB = semaphore;
00065   P_TCB p_TCB;
00066 
00067   while (p_SCB->p_lnk != NULL) {
00068     /* A task is waiting for token */
00069     p_TCB = rt_get_first ((P_XCB)p_SCB);
00070     rt_ret_val(p_TCB, 0);
00071     rt_rmv_dly(p_TCB);
00072     p_TCB->state = READY;
00073     rt_put_prio (&os_rdy, p_TCB);
00074   }
00075 
00076   if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
00077     /* preempt running task */
00078     rt_put_prio (&os_rdy, os_tsk.run);
00079     os_tsk.run->state = READY;
00080     rt_dispatch (NULL);
00081   }
00082 
00083   p_SCB->cb_type = 0;
00084 
00085   return (OS_R_OK);
00086 }
00087 
00088 /*--------------------------- rt_sem_send -----------------------------------*/
00089 
00090 OS_RESULT rt_sem_send (OS_ID semaphore) {
00091   /* Return a token to semaphore */
00092   P_SCB p_SCB = semaphore;
00093   P_TCB p_TCB;
00094 
00095   if (p_SCB->p_lnk != NULL) {
00096     /* A task is waiting for token */
00097     p_TCB = rt_get_first ((P_XCB)p_SCB);
00098     rt_ret_val(p_TCB, 1);
00099     rt_rmv_dly (p_TCB);
00100     rt_dispatch (p_TCB);
00101   }
00102   else {
00103     /* Store token. */
00104     p_SCB->tokens++;
00105   }
00106   return (OS_R_OK);
00107 }
00108 
00109 
00110 /*--------------------------- rt_sem_wait -----------------------------------*/
00111 
00112 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
00113   /* Obtain a token; possibly wait for it */
00114   P_SCB p_SCB = semaphore;
00115 
00116   if (p_SCB->tokens) {
00117     p_SCB->tokens--;
00118     return (OS_R_OK);
00119   }
00120   /* No token available: wait for one */
00121   if (timeout == 0) {
00122     return (OS_R_TMO);
00123   }
00124   if (p_SCB->p_lnk != NULL) {
00125     rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
00126   }
00127   else {
00128     p_SCB->p_lnk = os_tsk.run;
00129     os_tsk.run->p_lnk = NULL;
00130     os_tsk.run->p_rlnk = (P_TCB)p_SCB;
00131   }
00132   rt_block(timeout, WAIT_SEM);
00133   return (OS_R_TMO);
00134 }
00135 
00136 
00137 /*--------------------------- isr_sem_send ----------------------------------*/
00138 
00139 void isr_sem_send (OS_ID semaphore) {
00140   /* Same function as "os_sem"send", but to be called by ISRs */
00141   P_SCB p_SCB = semaphore;
00142 
00143   rt_psq_enq (p_SCB, 0);
00144   rt_psh_req ();
00145 }
00146 
00147 
00148 /*--------------------------- rt_sem_psh ------------------------------------*/
00149 
00150 void rt_sem_psh (P_SCB p_CB) {
00151   /* Check if task has to be waken up */
00152   P_TCB p_TCB;
00153 
00154   if (p_CB->p_lnk != NULL) {
00155     /* A task is waiting for token */
00156     p_TCB = rt_get_first ((P_XCB)p_CB);
00157     rt_rmv_dly (p_TCB);
00158     p_TCB->state   = READY;
00159     rt_ret_val(p_TCB, 1);
00160     rt_put_prio (&os_rdy, p_TCB);
00161   }
00162   else {
00163     /* Store token */
00164     p_CB->tokens++;
00165   }
00166 }
00167 
00168 /*----------------------------------------------------------------------------
00169  * end of file
00170  *---------------------------------------------------------------------------*/
00171