📄 rzkcreatesemaphore.c
字号:
/*
* File : RZKCreateSemaphore.c
*
* Description : This file contains the RZKCreateSemaphore function which
* creates a semaphore with given parameters.
*
* Copyright 2004 ZiLOG Inc. ALL RIGHTS RESERVED.
*
* This file contains unpublished confidential and proprietary information
* of ZiLOG, Inc.
* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED
* IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
* This is not a license and no use of any kind of this work is authorized
* in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's
* sole discretion
*/
#include <string.h>
#include "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include "ZSemaphore.h"
#include "externvar.h"
#include "ZInterrupt.h"
#define pCurrentThread ((RZK_TCB_t *) hCurrentThread)
/** extern functions */
extern RZK_HANDLE_t AcquireControlBlock (RZK_TCB_t *, UINT, UINT);
//extern void RZKScheduler();
extern void* RZKMemcpy(void *, const void *, RZK_LENGTH_t); // IAR port, UINT changed to RZK_LENGTH_t
/** extern variables */
extern RZK_THREADHANDLE_t hCurrentThread;
extern RZK_SEMAPHORE_CB_t nSemaphore[];
/*
* Function : RZKCreateSemaphore
*
* Description : The function creates a counting semaphore with the initial
* count and the mode of waiting for the threads on this semaphore.
* The initial count decides the number of threads that can
* hold this shared resource simultaneously. The notion of
* exclusive ownership of a resource will be special case of
* binary semaphore.
*
* Inputs : szName - Specifies the name of the semaphore to be created.
* uInitialCount - Specifies the initial count of the semaphore
* etAttrib This specifies the receiving attributes for the threads
* waiting on the semaphore. It can be one of the two values
* RZK_RECV_ATTRIB_et - enumerated data type - FIFO
* or PRIORITY
*
* Outputs : RZKERR_INVALID_ARGUMENTS - If any of the argument is
* Invalid
* RZKERR_CB_UNAVAILABLE - If the control block is not
* Available.
*
* Dependencies : All externs
*/
RZK_SEMAPHOREHANDLE_t RZKCreateSemaphore
(
RZK_NAME_t szName[MAX_OBJECT_NAME_LEN],
COUNT_t uInitialCount,
RZK_RECV_ATTRIB_et etAttrib
)
{
/*Declare a handle to semaphore*/
RZK_SEMAPHORE_t *pHandleToSemaphore;
RZK_STATE_t uState;
#if 0 /* CR#6559 */
#ifdef RZK_DEBUG
/*If the initial count specifed for the semaphore is zero return RZKERR_INVALID_ARGUMENTS*/
if(uInitialCount == 0)
{
pCurrentThread -> errNum = RZKERR_INVALID_ARGUMENTS;
return NULL;
}
#endif
#endif
/* Acquire Semaphore Control Block */
pHandleToSemaphore = (RZK_SEMAPHORE_t *)AcquireControlBlock(((RZK_TCB_t*) &nSemaphore[0]),
MAX_SEMAPHORES, sizeof(RZK_SEMAPHORE_t));
#ifndef RZK_PERFORMANCE
if(pHandleToSemaphore == NULL)
{
pCurrentThread -> errNum = RZKERR_CB_UNAVAILABLE;
return NULL;
}
#endif
/* Set the values in the control block */
#ifdef RZK_KERNEL_AWARE
RZKMemcpy(pHandleToSemaphore -> szName, szName, MAX_OBJECT_NAME_LEN);
#endif /* KERNEL_AWARE */
/* Set the uDynamicCount to uInitialCount */
pHandleToSemaphore -> uDynamicCount =
pHandleToSemaphore -> uInitialCount = uInitialCount;
/*Set the next object in owned resource queue to null and Set the owner thread to NULL */
#ifdef RZK_PRIORITYINHERITANCE
pHandleToSemaphore->pOwnerThread = pHandleToSemaphore-> pOwnedResNext = ( RZK_SEMAPHORE_t *) NULL;
#endif
/* Initialize the pLastPosition and pResNext pointers to point to NULL */
pHandleToSemaphore->pLastPosition = pHandleToSemaphore->pResNext = ( RZK_TCB_t * ) NULL;
/* Set the magic number */
#ifdef RZK_DEBUG
pHandleToSemaphore->magic_num = MAGIC_NUM_SEMAPHORE;
#endif
uState = RZKDisablePreemption();/*DISABLE Preemption*/
/* Mark the state of Semaphore as CREATED */
pHandleToSemaphore->uState = OBJECT_CREATED | OBJECT_BUSY;
/* Set the OBJECT_PRIORITY_INHERITANCE bit of uState of Semaphore */
#ifdef RZK_PRIORITYINHERITANCE
if((etAttrib == RECV_ORDER_PRIORITY) && (uInitialCount == 1))
pHandleToSemaphore -> uState |= OBJECT_PRIORITY_INHERITANCE;
#endif /* End of RZK_PRIORITYINHERITANCE */
if (etAttrib & RECV_ORDER_PRIORITY)
{
pHandleToSemaphore->uState |=OBJECT_RECV_PRIORITY;
}
pCurrentThread->errNum = RZKERR_SUCCESS;
RZKRestorePreemption(uState); /* Restore Preemption */
/* CR# 6559 - zerocount semaphore implementation */
if(uInitialCount == 0)
{
RZKAcquireSemaphore(pHandleToSemaphore, 1);
}
return pHandleToSemaphore;
} /* End of RZKCreateSemaphore */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -