⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emos_sem.c

📁 emos是一个新的类似于ucos的内核
💻 C
字号:
/****************************************************************************
 *
 * (c) Copyright 2001,2008, EMB system, All Rights Reserved.
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF EMB SYSTEM, INC.
 * The copyright notice above does not evidence any actual or intended
 * publication of such source code. 
 *
 *  Subsystem:    EMOS 
 *  File:         emos_sem.c
 *  Author:       zenf zhao
 *  Description:  EMOS sem management file
 *
 ****************************************************************************/
#include "emos_cfg.h"
#include "emos_core.h"

#if EMOS_SEM_EN

/*********************************************************************************************************
*ACCEPT SEMAPHORE
* Description: This function checks the semaphore to see if a resource is available or, if an event
*              occurred.  Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the
*              resource is not available or the event did not occur.
* Arguments  : pevent     is a pointer to the event control block
* Returns    : >  0       if the resource is available or the event did not occur the semaphore is
*                         decremented to obtain the resource.
*              == 0       if the resource is not available or the event did not occur or,
*                         you didn't pass a pointer to a semaphore
**********************************************************************************************************/

uint16 emosSemAccept (EMOS_EVENT_T *pevent)
{
    uint16 cnt;

    EMOS_ENTER_CRITICAL();
    if (pevent->osEventType != EMOS_EVENT_TYPE_SEM) 
    {   /* Validate event block type */
        EMOS_EXIT_CRITICAL();
        return (0);
    }
    
    cnt = pevent->osEventCnt;
    if (cnt > 0) 
    { 
    	/* See if resource is available */
        pevent->osEventCnt--; /* Yes, decrement semaphore and notify caller */
    }
    EMOS_EXIT_CRITICAL();
    return (cnt);  /* Return semaphore count */
}


/*********************************************************************************************************
* CREATE A SEMAPHORE
* Description: This function creates a semaphore.
* Arguments  : cnt           is the initial value for the semaphore.  If the value is 0, no resource is
*                            available (or no event has occurred).  You initialize the semaphore to a 
*                            non-zero value to specify how many resources are available (e.g. if you have
*                            10 resources, you would initialize the semaphore to 10).
* Returns    : != (void *)0  is a pointer to the event control clock (EMOS_EVENT_T) associated with the
*                            created semaphore
*              == (void *)0  if no event control blocks were available
**********************************************************************************************************/
EMOS_EVENT_T *emosSemCreate (uint16 cnt)
{
    EMOS_EVENT_T *pevent;

    EMOS_ENTER_CRITICAL();
    pevent = gEmosEventFreeList;  /* Get next free event control block */
    if (gEmosEventFreeList != (EMOS_EVENT_T *)0)
    {   
    	/* See if pool of free ECB pool was empty   */
        gEmosEventFreeList = (EMOS_EVENT_T *)gEmosEventFreeList->osEventPtr;
    }
    EMOS_EXIT_CRITICAL();
    
    if (pevent != (EMOS_EVENT_T *)0)
    {   /* Get an event control block */
        pevent->osEventType = EMOS_EVENT_TYPE_SEM;
        pevent->osEventCnt  = cnt; /* Set semaphore value */
        emosEventWaitListInit(pevent);
    }
    return (pevent);
}


/*********************************************************************************************************
* PEND ON SEMAPHORE
* Description: This function waits for a semaphore.
* Arguments  : pevent        is a pointer to the event control block associated with the desired 
*                            semaphore.
*              timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will
*                            wait for the resource up to the amount of time specified by this argument.  
*                            If you specify 0, however, your task will wait forever at the specified 
*                            semaphore or, until the resource becomes available (or the event occurs).
*              err           is a pointer to where an error message will be deposited.  Possible error
*                            messages are:
*                            EMOS_NO_ERR  The call was successful and your task owns the resource 
*                                          or, the event you are waiting for occurred.
*                            EMOS_TIMEOUT  The semaphore was not received within the specified 
*                                          timeout.
*                            EMOS_ERR_EVENT_TYPE  If you didn't pass a pointer to a semaphore.
*                            EMOS_ERR_PEND_ISR    If you called this function from an ISR and the result
*                                               would lead to a suspension.
*              Returns    : none
**********************************************************************************************************/
void emosSemPend (EMOS_EVENT_T *pevent, uint16 timeout, uint8 *err)
{
    EMOS_ENTER_CRITICAL();
    if (pevent->osEventType != EMOS_EVENT_TYPE_SEM)
    {   /* Validate event block type */
        EMOS_EXIT_CRITICAL();
        *err = EMOS_ERR_EVENT_TYPE;
    }
    
    if (pevent->osEventCnt > 0) 
    {   /* If sem. is positive, resource available ... */
        pevent->osEventCnt--; /* ... decrement semaphore only if positive. */
        EMOS_EXIT_CRITICAL();
        *err = EMOS_NO_ERR;
    } 
    else if (gEmosIntNesting > 0)
    {   /* See if called from ISR ...*/
        EMOS_EXIT_CRITICAL(); /* ... can't PEND from an ISR */
        *err = EMOS_ERR_PEND_ISR;
    } 
    else 
    {                                                /* Otherwise, must wait until event occurs       */
        gEmosTCBCur->osTCBStat |= EMOS_STAT_SEM;    /* Resource not available, pend on semaphore     */
        gEmosTCBCur->osTCBDly = timeout;            /* Store pend timeout in TCB                     */
        emosEventTaskWait(pevent);                  /* Suspend task until event or timeout occurs    */
        EMOS_EXIT_CRITICAL();
        emosSched();                                    /* Find next highest priority task ready         */

        EMOS_ENTER_CRITICAL();
        if (gEmosTCBCur->osTCBStat & EMOS_STAT_SEM) 
        {  
        	/* Must have timed out if still waiting for event*/
            emosEventTo(pevent);
            EMOS_EXIT_CRITICAL();
            *err = EMOS_TIMEOUT;  /* Indicate that didn't get event within TO */
        } 
        else 
        {
            gEmosTCBCur->osTCBEventPtr = (EMOS_EVENT_T *)0;
            EMOS_EXIT_CRITICAL();
            *err = EMOS_NO_ERR;
        }
    }
}

/********************************************************************************************************
* POST TO A SEMAPHORE
* Description: This function signals a semaphore
* Arguments  : pevent        is a pointer to the event control block associated with the desired 
*                            semaphore.
* Returns    : EMOS_NO_ERR   The call was successful and the semaphore was signaled.
*              EMOS_SEM_OVF  If the semaphore count exceeded its limit.  In other words, you have 
*                            signalled the semaphore more often than you waited on it with either
*                            OSSemAccept() or OSSemPend().
*              EMOS_ERR_EVENT_TYPE  If you didn't pass a pointer to a semaphore
**********************************************************************************************************/

uint8 emosSemPost (EMOS_EVENT_T *pevent)
{
    EMOS_ENTER_CRITICAL();
    if (pevent->osEventType != EMOS_EVENT_TYPE_SEM)
    { 
    	/* Validate event block type */
        EMOS_EXIT_CRITICAL();
        return (EMOS_ERR_EVENT_TYPE);
    }
    if (pevent->osEventGrp) 
    {   /* See if any task waiting for semaphore    */
        emosEventTaskRdy(pevent, (void *)0, EMOS_STAT_SEM);  /* Ready highest prio task waiting on event */
        EMOS_EXIT_CRITICAL();
        emosSched();  /* Find highest priority task ready to run*/
        return (EMOS_NO_ERR);
    } 
    else 
    {
        if (pevent->osEventCnt < 65535) 
        {   /* Make sure semaphore will not overflow*/
            pevent->osEventCnt++; /* Increment semaphore count to register event   */
            EMOS_EXIT_CRITICAL();
            return (EMOS_NO_ERR);
        } 
        else 
        {                                      /* Semaphore value has reached its maximum       */
            EMOS_EXIT_CRITICAL();
            return (EMOS_SEM_OVF);
        }
    }
}

/*********************************************************************************************************
* QUERY A SEMAPHORE
* Description: This function obtains information about a semaphore
* Arguments  : pevent        is a pointer to the event control block associated with the desired 
*                            semaphore
*              pdata         is a pointer to a structure that will contain information about the 
*                            semaphore.
* Returns    : EMOS_NO_ERR          The call was successful and the message was sent
*              EMOS_ERR_EVENT_TYPE  If you are attempting to obtain data from a non semaphore.
**********************************************************************************************************/
uint8 emosSemQuery (EMOS_EVENT_T *pevent, EMOS_SEM_DATA_T *pdata)
{
    uint8  i;
    uint8 *psrc;
    uint8 *pdest;
    
    EMOS_ENTER_CRITICAL();
    if (pevent->osEventType != EMOS_EVENT_TYPE_SEM)
    {   /* Validate event block type */
        EMOS_EXIT_CRITICAL();
        return (EMOS_ERR_EVENT_TYPE);
    }
    
    pdata->osEventGrp = pevent->osEventGrp; /* Copy message mailbox wait list*/
    psrc              = &pevent->osEventTbl[0];
    pdest             = &pdata->osEventTbl[0];
    for (i = 0; i < EMOS_EVENT_TBL_SIZE; i++) 
    {
        *pdest++ = *psrc++;   
    }
    pdata->osCnt      = pevent->osEventCnt; /* Get semaphore count */
    EMOS_EXIT_CRITICAL();
    return (EMOS_NO_ERR);
}


#if 0
EMOS_EVENT_T* gTestSem = NULL;
void emos_sem_test()
{
  if(NULL == gTestSem)	
  {
  	gTestSem = emosSemCreate (1);
  }
  emosSemPend(gTestSem,0,&gEmosError);
  //emosSemPend(tSem,-1,&gEmosError);
}

void emos_sem_test2()
{
  if(NULL != gTestSem)
  {	
  	emosSemPost(gTestSem);
  }
}
#endif

#endif


 /*
 * Please add "$" around "Id" and "Log".
 * $Id$
 * $Log$
 *
 */

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -