Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rt_Semaphore.c Source File

rt_Semaphore.c

Go to the documentation of this file.
00001 /**
00002  * @file    rt_Semaphore.c
00003  * @brief   
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include "rt_TypeDef.h"
00023 #include "RTX_Config.h"
00024 #include "rt_System.h"
00025 #include "rt_List.h"
00026 #include "rt_Task.h"
00027 #include "rt_Semaphore.h"
00028 
00029 
00030 /*----------------------------------------------------------------------------
00031  *      Functions
00032  *---------------------------------------------------------------------------*/
00033 
00034 
00035 /*--------------------------- rt_sem_init -----------------------------------*/
00036 
00037 void rt_sem_init (OS_ID semaphore, U16 token_count) {
00038   /* Initialize a semaphore */
00039   P_SCB p_SCB = semaphore;
00040 
00041   p_SCB->cb_type = SCB;
00042   p_SCB->p_lnk  = NULL;
00043   p_SCB->tokens = token_count;
00044 }
00045 
00046 
00047 /*--------------------------- rt_sem_send -----------------------------------*/
00048 
00049 OS_RESULT rt_sem_send (OS_ID semaphore) {
00050   /* Return a token to semaphore */
00051   P_SCB p_SCB = semaphore;
00052   P_TCB p_TCB;
00053 
00054   if (p_SCB->p_lnk != NULL) {
00055     /* A task is waiting for token */
00056     p_TCB = rt_get_first ((P_XCB)p_SCB);
00057     p_TCB->ret_val = OS_R_SEM;
00058     rt_rmv_dly (p_TCB);
00059     rt_dispatch (p_TCB);
00060     os_tsk.run->ret_val = OS_R_OK;
00061   }
00062   else {
00063     /* Store token. */
00064     p_SCB->tokens++;
00065   }
00066   return (OS_R_OK);
00067 }
00068 
00069 
00070 /*--------------------------- rt_sem_wait -----------------------------------*/
00071 
00072 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
00073   /* Obtain a token; possibly wait for it */
00074   P_SCB p_SCB = semaphore;
00075 
00076   if (p_SCB->tokens) {
00077     p_SCB->tokens--;
00078     return (OS_R_OK);
00079   }
00080   /* No token available: wait for one */
00081   if (timeout == 0) {
00082     return (OS_R_TMO);
00083   }
00084   if (p_SCB->p_lnk != NULL) {
00085     rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
00086   }
00087   else {
00088     p_SCB->p_lnk = os_tsk.run;
00089     os_tsk.run->p_lnk = NULL;
00090     os_tsk.run->p_rlnk = (P_TCB)p_SCB;
00091   }
00092   rt_block(timeout, WAIT_SEM);
00093   return (OS_R_TMO);
00094 }
00095 
00096 
00097 /*--------------------------- isr_sem_send ----------------------------------*/
00098 
00099 void isr_sem_send (OS_ID semaphore) {
00100   /* Same function as "os_sem"send", but to be called by ISRs */
00101   P_SCB p_SCB = semaphore;
00102 
00103   rt_psq_enq (p_SCB, 0);
00104   rt_psh_req ();
00105 }
00106 
00107 
00108 /*--------------------------- rt_sem_psh ------------------------------------*/
00109 
00110 void rt_sem_psh (P_SCB p_CB) {
00111   /* Check if task has to be waken up */
00112   P_TCB p_TCB;
00113 
00114   if (p_CB->p_lnk != NULL) {
00115     /* A task is waiting for token */
00116     p_TCB = rt_get_first ((P_XCB)p_CB);
00117     rt_rmv_dly (p_TCB);
00118     p_TCB->state   = READY;
00119     p_TCB->ret_val = OS_R_SEM;
00120     rt_put_prio (&os_rdy, p_TCB);
00121   }
00122   else {
00123     /* Store token */
00124     p_CB->tokens++;
00125   }
00126 }
00127 
00128 /*----------------------------------------------------------------------------
00129  * end of file
00130  *---------------------------------------------------------------------------*/
00131