mbc.c

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

C
938
字号
/*************************************************************************/
/*                                                                       */
/*               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       */
/*                                                                       */
/*      mbc.c                                          Nucleus PLUS 1.15 */
/*                                                                       */
/* COMPONENT                                                             */
/*                                                                       */
/*      MB - Mailbox Management                                          */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This file contains the core routines for the Mailbox management  */
/*      component.                                                       */
/*                                                                       */
/* DATA STRUCTURES                                                       */
/*                                                                       */
/*      None                                                             */
/*                                                                       */
/* FUNCTIONS                                                             */
/*                                                                       */
/*      MBC_Create_Mailbox                  Create a mailbox             */
/*      MBC_Delete_Mailbox                  Delete a mailbox             */
/*      MBC_Send_To_Mailbox                 Send a mailbox message       */
/*      MBC_Receive_From_Mailbox            Receive a mailbox message    */
/*      MBC_Cleanup                         Cleanup on timeout or a      */
/*                                          terminate condition          */
/*                                                                       */
/* DEPENDENCIES                                                          */
/*                                                                       */
/*      cs_extr.h                           Common Service functions     */
/*      tc_extr.h                           Thread Control functions     */
/*      mb_extr.h                           Mailbox 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/mb_extr.h"        /* Mailbox functions         */
#include        "plus/inc/hi_extr.h"        /* History functions         */

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

extern CS_NODE         *MBD_Created_Mailboxes_List;
extern UNSIGNED         MBD_Total_Mailboxes;
extern TC_PROTECT       MBD_List_Protect;


/* Define internal component function prototypes.  */

VOID    MBC_Cleanup(VOID *information);


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      MBC_Create_Mailbox                                               */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function creates a mailbox and then places it on the list   */
/*      of created mailboxes.                                            */
/*                                                                       */
/* CALLED BY                                                             */
/*                                                                       */
/*      Application                                                      */
/*      MBCE_Create_Mailbox                 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                                                                */
/*                                                                       */
/*      mailbox_ptr                         Mailbox control block pointer*/
/*      name                                Mailbox name                 */
/*      suspend_type                        Suspension type              */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      NU_SUCCESS                                                       */
/*                                                                       */
/*************************************************************************/
STATUS  MBC_Create_Mailbox(NU_MAILBOX *mailbox_ptr, CHAR *name,
                                                OPTION suspend_type)
{

R1 MB_MCB      *mailbox;                    /* Mailbox control block ptr */
INT             i;                          /* Working index variable    */
NU_SUPERV_USER_VARIABLES

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

    /* Move input mailbox pointer into internal pointer.  */
    mailbox =  (MB_MCB *) mailbox_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_MAILBOX_ID, (UNSIGNED) mailbox,
                                (UNSIGNED) name, (UNSIGNED) suspend_type);

#endif

    /* First, clear the mailbox ID just in case it is an old Mailbox
       Control Block.  */
    mailbox -> mb_id =             0;

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

    /* Clear the message present flag- initial it to "no message present." */
    mailbox -> mb_message_present =  NU_FALSE;

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

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

        /* Priority suspension is selected.  */
        mailbox -> mb_fifo_suspend =  NU_FALSE;

    /* Clear the suspension list pointer.  */
    mailbox -> mb_suspension_list =  NU_NULL;

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

    /* Initialize link pointers.  */
    mailbox -> mb_created.cs_previous =    NU_NULL;
    mailbox -> mb_created.cs_next =        NU_NULL;

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

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

    /* Link the mailbox into the list of created mailboxes and increment the
       total number of mailboxes in the system.  */
    CSC_Place_On_List(&MBD_Created_Mailboxes_List, &(mailbox -> mb_created));
    MBD_Total_Mailboxes++;

#ifdef NU_PROFILE_PLUS

    PRF_PLUS_MBC_CREATE_MAILBOX_0

#endif /* NU_PROFILE_PLUS */

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

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

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


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      MBC_Delete_Mailbox                                               */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function deletes a mailbox and removes it from the list of  */
/*      created mailboxes.  All tasks suspended on the mailbox are       */
/*      resumed.  Note that this function does not free the memory       */
/*      associated with the mailbox control block.                       */
/*                                                                       */
/* CALLED BY                                                             */
/*                                                                       */
/*      Application                                                      */
/*      MBCE_Delete_Mailbox                 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             Setup current protection ptr */
/*      TCT_System_Protect                  Protect against system access*/
/*      TCT_System_Unprotect                Release system protection    */
/*      TCT_Unprotect                       Release protection           */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      mailbox_ptr                         Mailbox control block pointer*/
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      NU_SUCCESS                                                       */
/*                                                                       */
/*************************************************************************/
STATUS  MBC_Delete_Mailbox(NU_MAILBOX *mailbox_ptr)
{

R1 MB_MCB      *mailbox;                    /* Mailbox control block ptr */
MB_SUSPEND     *suspend_ptr;                /* Suspend block pointer     */
MB_SUSPEND     *next_ptr;                   /* Next suspend block pointer*/
STATUS          preempt;                    /* Status for resume call    */
NU_SUPERV_USER_VARIABLES

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

    /* Move input mailbox pointer into internal pointer.  */
    mailbox =  (MB_MCB *) mailbox_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_DELETE_MAILBOX_ID, (UNSIGNED) mailbox,
                                        (UNSIGNED) 0, (UNSIGNED) 0);

#endif

    /* Call protection just in case another thread is using the mailbox.  */
    TCT_System_Protect();

#ifdef NU_PROFILE_PLUS

    PRF_PLUS_MBC_DELETE_MAILBOX_0

#endif /* NU_PROFILE_PLUS */

    /* Clear the mailbox ID.  */
    mailbox -> mb_id =  0;

    /* Clear protection.  */
    TCT_Unprotect();

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

    /* Remove the mailbox from the list of created mailboxes.  */
    CSC_Remove_From_List(&MBD_Created_Mailboxes_List,
                                        &(mailbox -> mb_created));

    /* Decrement the total number of created mailboxes.  */
    MBD_Total_Mailboxes--;

    /* Pickup the suspended task pointer list.  */
    suspend_ptr =  mailbox -> mb_suspension_list;

    /* Walk the chain task(s) currently suspended on the mailbox.  */
    preempt =  0;
    while (suspend_ptr)
    {

        /* Protect against system access.  */
        TCT_System_Protect();

        /* Resume the suspended task.  Insure that the status returned is
           NU_MAILBOX_DELETED.  */
        suspend_ptr -> mb_return_status =  NU_MAILBOX_DELETED;

        /* Point to the next suspend structure in the link.  */
        next_ptr =  (MB_SUSPEND *) (suspend_ptr -> mb_suspend_link.cs_next);

        /* Resume the specified task.  */
        preempt =  preempt |
            TCC_Resume_Task((NU_TASK *) suspend_ptr -> mb_suspended_task,
                                                NU_MAILBOX_SUSPEND);

        /* Determine if the next is the same as the head pointer.  */
        if (next_ptr == mailbox -> mb_suspension_list)

⌨️ 快捷键说明

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