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

📄 semaphore.cpp

📁 这是DVD中伺服部分的核心代码
💻 CPP
字号:
/******************************************************************************
*******************************************************************************
**                                                                           **
**  Copyright (c) 2006 Videon Central, Inc.                                  **
**  All rights reserved.                                                     **
**                                                                           **
**  The computer program contained herein contains proprietary information   **
**  which is the property of Videon Central, Inc.  The program may be used   **
**  and/or copied only with the written permission of Videon Central, Inc.   **
**  or in accordance with the terms and conditions stipulated in the         **
**  agreement/contract under which the programs have been supplied.          **
**                                                                           **
*******************************************************************************
******************************************************************************/
/**
 * @file semaphore.cpp
 *
 * $Revision: 1.4 $ 
 *
 * Wraps the osapi binary and counting semaphores as a referenced object
 * Binary Semaphores are slightly different in functionally from the base
 * implementation in osapi - it does not consider signaling an already
 * signaled semaphore an error.
 */

#include "semaphore.h"






/**
 *******************************************************************************
 *  Semaphore::Semaphore initializes members to known state
 *
 *******************************************************************************/
Semaphore::Semaphore( void )
{
    semaphoreHandle = 0;
}






/**
 *******************************************************************************
 *  Semaphore::Destruct destroys all resources used. This method is called 
 *  by the reference counter object when the reference count = 0
 *
 *  @return     If the function succeeds, the return value is VDVD_SUCCESS.
 *  @return     ERROR_INVALID_HANDLE = semaphore handle is invalid
 *  @return     O/S failure deleting semaphore
 *******************************************************************************/
VDVD_ERROR  Semaphore::Destruct( void )
{
    VDVD_ASSERT_ERROR( (semaphoreHandle==0), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    VDVD_RAISE_ERROR( (VDVD_ERROR)(OS_SemDelete(semaphoreHandle)) );

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Semaphore::Create    Creates the semaphore
 *
 *  Creates semaphore via Construct method automatically called from method
 *  ReferenceCounter::AddReference. Use this interface(instead of AddReference)
 *  if it is desired to limit the object to one reference(user).
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *              VDVD_ERROR_OBJECT_HAS_INVALID_STATE if the semaphore has already been referenced(created)
 *******************************************************************************/
VDVD_ERROR  Semaphore::Create( void )
{
    VDVD_ASSERT_ERROR( (IsReferenced()==TRUE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    VDVD_RAISE_ERROR( AddReference() );

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Semaphore::Destroy    Destroys the semaphore
 *
 *  Destroys semaphore via Destruct method automatically called from method
 *  ReferenceCounter::DeleteReference. Use this interface(instead of DeleteReference)
 *  if it is desired to limit the object to one reference(user).
 *
 *  @return     VDVD_SUCCESS if the function is successful
 *              VDVD_ERROR_OBJECT_HAS_INVALID_STATE if the semaphore has already been referenced(created)
 *******************************************************************************/
VDVD_ERROR  Semaphore::Destroy( void )
{
    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    VDVD_RAISE_ERROR( DeleteReference() );

    VDVD_ASSERT_ERROR( (IsReferenced()==TRUE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    return ( VDVD_SUCCESS );
}





/**
 *******************************************************************************
 *  Semaphore::Wait waits for the semaphore to be signaled 
 *
 *  @return     If the function succeeds, the return value is VDVD_SUCCESS.
 *  @return     O/S failure taking semaphore
 *******************************************************************************/
VDVD_ERROR  Semaphore::Wait( void )
{
    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    VDVD_RAISE_ERROR( (VDVD_ERROR)(OS_SemTake(semaphoreHandle, OS_WAIT_FOREVER)) );

    return ( VDVD_SUCCESS );
}





/**
 *******************************************************************************
 *  Semaphore::Wait waits for the semaphore to be signaled 
 *
 *  @return     If the function succeeds, the return value is VDVD_SUCCESS.
 *  @return     O/S failure taking semaphore
 *******************************************************************************/
VDVD_ERROR  Semaphore::Wait( uint32 timeoutInMilliseconds )
{
    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    VDVD_RAISE_ERROR( (VDVD_ERROR)(OS_SemTakeMsec(semaphoreHandle, timeoutInMilliseconds)) );

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Semaphore::Signal signals the semaphore
 *
 *  @return     If the function succeeds, the return value is VDVD_SUCCESS.
 *  @return     O/S failure taking semaphore
 *******************************************************************************/
VDVD_ERROR  Semaphore::Signal( void )
{
    OS_STATUS   osStatus;


    VDVD_ASSERT_ERROR( (IsReferenced()==FALSE), VDVD_ERROR_OBJECT_HAS_INVALID_STATE );

    osStatus = OS_SemGive(semaphoreHandle);
    if (( osStatus == OS_SEM_FULL ) || ( osStatus == OS_OK ))
    {
        return ( VDVD_SUCCESS );
    }

    return ( (VDVD_ERROR)osStatus );
}






/**
 *******************************************************************************
 *  BinarySemaphore::Construct allocates all resources needed. This method is called 
 *  by the reference counter object when the reference count > 0
 *
 *  @return     If the function succeeds, the return value is VDVD_SUCCESS.
 *  @return     O/S failure allocating semaphore
 *******************************************************************************/
VDVD_ERROR  BinarySemaphore::Construct( void )
{
    semaphoreHandle = OS_SemBCreate(OS_SEM_Q_FIFO, OS_SEM_EMPTY);
    VDVD_ASSERT_ERROR( (semaphoreHandle==0), VDVD_ERROR_UNABLE_TO_CREATE_OBJECT );

    return ( VDVD_SUCCESS );
}





/**
 *******************************************************************************
 *  CountingSemaphore::Construct allocates all resources needed. This method is called 
 *  by the reference counter object when the reference count > 0.
 *  The initial count of the semaphore is 0.
 *
 *  @return     If the function succeeds, the return value is VDVD_SUCCESS.
 *  @return     O/S failure allocating semaphore
 *******************************************************************************/
VDVD_ERROR  CountingSemaphore::Construct( void )
{
    semaphoreHandle = OS_SemCCreate(0, 0);
    VDVD_ASSERT_ERROR( (semaphoreHandle==0), VDVD_ERROR_UNABLE_TO_CREATE_OBJECT );

    return ( VDVD_SUCCESS );
}

⌨️ 快捷键说明

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