smc.c

来自「nucleus 2006 source code」· C语言 代码 · 共 737 行 · 第 1/3 页

C
737
字号
/*************************************************************************/
/*                                                                       */
/*               Copyright Mentor Graphics Corporation 2004              */
/*                         All Rights Reserved.                          */
/*                                                                       */
/* THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS  */
/* THE PROPERTY OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS   */
/* SUBJECT TO LICENSE TERMS.                                             */
/*                                                                       */
/*************************************************************************/

/*************************************************************************/
/*                                                                       */
/* FILE NAME                                               VERSION       */
/*                                                                       */
/*      smc.c                                          Nucleus PLUS 1.15 */
/*                                                                       */
/* COMPONENT                                                             */
/*                                                                       */
/*      SM - Semaphore Management                                        */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This file contains the core routines for the Semaphore Management*/
/*      component.                                                       */
/*                                                                       */
/* DATA STRUCTURES                                                       */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* FUNCTIONS                                                             */
/*                                                                       */
/*      SMC_Create_Semaphore                Create a semaphore           */
/*      SMC_Delete_Semaphore                Delete a semaphore           */
/*      SMC_Obtain_Semaphore                Obtain instance of semaphore */
/*      SMC_Release_Semaphore               Release instance of semaphore*/
/*      SMC_Cleanup                         Cleanup on timeout or a      */
/*                                            terminate condition        */
/*                                                                       */
/* DEPENDENCIES                                                          */
/*                                                                       */
/*      cs_extr.h                           Common Service functions     */
/*      tc_extr.h                           Thread Control functions     */
/*      sm_extr.h                           Semaphore functions          */
/*      hi_extr.h                           History functions            */
/*                                                                       */
/*************************************************************************/
#define         NU_SOURCE_FILE


#include        "plus/inc/cs_extr.h"        /* Common service functions  */
#include        "plus/inc/tc_extr.h"        /* Thread control functions  */
#include        "plus/inc/sm_extr.h"        /* Semaphore functions       */
#include        "plus/inc/hi_extr.h"        /* History functions         */


/* Define external inner-component global data references.  */

extern CS_NODE         *SMD_Created_Semaphores_List;
extern UNSIGNED         SMD_Total_Semaphores;
extern TC_PROTECT       SMD_List_Protect;


/* Define internal component function prototypes.  */

VOID    SMC_Cleanup(VOID *information);


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      SMC_Create_Semaphore                                             */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function creates a semaphore and then places it on the list */
/*      of created semaphores.                                           */
/*                                                                       */
/* CALLED BY                                                             */
/*                                                                       */
/*      Application                                                      */
/*      SMCE_Create_Semaphore               Error checking shell         */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      CSC_Place_On_List                   Add node to linked-list      */
/*      [HIC_Make_History_Entry]            Make entry in history log    */
/*      [TCT_Check_Stack]                   Stack checking function      */
/*      TCT_Protect                         Data structure protect       */
/*      TCT_Unprotect                       Un-protect data structure    */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      semaphore_ptr                       Semaphore control block ptr  */
/*      name                                Semaphore name               */
/*      initial_count                       Initial semaphore instance   */
/*                                            count                      */
/*      suspend_type                        Suspension type              */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      NU_SUCCESS                                                       */
/*                                                                       */
/*************************************************************************/
STATUS  SMC_Create_Semaphore(NU_SEMAPHORE *semaphore_ptr, CHAR *name,
                           UNSIGNED initial_count, OPTION suspend_type)
{

R1 SM_SCB      *semaphore;                  /* Semaphore control blk ptr */
INT             i;                          /* Working index variable    */
NU_SUPERV_USER_VARIABLES

    /* Switch to supervisor mode */
    NU_SUPERVISOR_MODE();

    /* Move input semaphore pointer into internal pointer.  */
    semaphore =  (SM_SCB *) semaphore_ptr;


#ifdef  NU_ENABLE_STACK_CHECK

    /* Call stack checking function to check for an overflow condition.  */
    TCT_Check_Stack();

#endif

#ifdef  NU_ENABLE_HISTORY

    /* Make an entry that corresponds to this function in the system history
       log.  */
    HIC_Make_History_Entry(NU_CREATE_SEMAPHORE_ID, (UNSIGNED) semaphore,
                                (UNSIGNED) name, (UNSIGNED) initial_count);

#endif

    /* First, clear the semaphore ID just in case it is an old Semaphore
       Control Block.  */
    semaphore -> sm_id =             0;

    /* Fill in the semaphore name.  */
    for (i = 0; i < NU_MAX_NAME; i++)
        semaphore -> sm_name[i] =  name[i];

    /* Setup the initial semaphore instance count.  */
    semaphore -> sm_semaphore_count =  initial_count;

    /* Setup the semaphore suspension type.  */
    if (suspend_type == NU_FIFO)

        /* FIFO suspension is selected, setup the flag accordingly.  */
        semaphore -> sm_fifo_suspend =  NU_TRUE;
    else

        /* Priority suspension is selected.  */
        semaphore -> sm_fifo_suspend =  NU_FALSE;

    /* Clear the suspension list pointer.  */
    semaphore -> sm_suspension_list =  NU_NULL;

    /* Clear the number of tasks waiting on the semaphore counter.  */
    semaphore -> sm_tasks_waiting =  0;

    /* Initialize link pointers.  */
    semaphore -> sm_created.cs_previous =    NU_NULL;
    semaphore -> sm_created.cs_next =        NU_NULL;

    /* Protect against access to the list of created semaphores.  */
    TCT_Protect(&SMD_List_Protect);

    /* At this point the semaphore is completely built.  The ID can now be
       set and it can be linked into the created semaphore list.  */
    semaphore -> sm_id =  SM_SEMAPHORE_ID;

    /* Link the semaphore into the list of created semaphores and increment the
       total number of semaphores in the system.  */
    CSC_Place_On_List(&SMD_Created_Semaphores_List,&(semaphore -> sm_created));
    SMD_Total_Semaphores++;

#ifdef NU_PROFILE_PLUS

    PRF_PLUS_SMC_CREATE_SEMAPHORE_0

#endif /* NU_PROFILE_PLUS */

    /* Release protection against access to the list of created semaphores.  */
    TCT_Unprotect();

    /* Return to user mode */
    NU_USER_MODE();

    /* Return successful completion.  */
    return(NU_SUCCESS);
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      SMC_Delete_Semaphore                                             */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function deletes a semaphore and removes it from the list   */
/*      of created semaphores.  All tasks suspended on the semaphore are */
/*      resumed.  Note that this function does not free the memory       */
/*      associated with the semaphore control block.                     */
/*                                                                       */
/* CALLED BY                                                             */
/*                                                                       */
/*      Application                                                      */
/*      SMCE_Delete_Semaphore               Error checking shell         */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*      CSC_Remove_From_List                Remove node from list        */
/*      [HIC_Make_History_Entry]            Make entry in history log    */
/*      TCC_Resume_Task                     Resume a suspended task      */
/*      [TCT_Check_Stack]                   Stack checking function      */
/*      TCT_Control_To_System               Transfer control to system   */
/*      TCT_Protect                         Protect created list         */
/*      TCT_Set_Current_Protect             Modify current protection    */
/*      TCT_System_Protect                  Setup system protection      */
/*      TCT_System_Unprotect                Release system protection    */
/*      TCT_Unprotect                       Release protection           */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      semaphore_ptr                       Semaphore control block ptr  */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      NU_SUCCESS                                                       */
/*                                                                       */
/*************************************************************************/
STATUS  SMC_Delete_Semaphore(NU_SEMAPHORE *semaphore_ptr)
{

R1 SM_SCB      *semaphore;                  /* Semaphore control blk ptr */
SM_SUSPEND     *suspend_ptr;                /* Suspend block pointer     */
SM_SUSPEND     *next_ptr;                   /* Next suspend block        */
STATUS          preempt;                    /* Status for resume call    */
NU_SUPERV_USER_VARIABLES

    /* Switch to supervisor mode */

⌨️ 快捷键说明

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