mutex.cpp

来自「这是DVD中伺服部分的核心代码」· C++ 代码 · 共 144 行

CPP
144
字号
/******************************************************************************
*******************************************************************************
**                                                                           **
**  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 mutex.cpp
 *
 * $Revision: 1.7 $ 
 *
 * Implements a Mutual Exlusion (Mutex) object class
 *
 */

#include <stdlib.h>
#include "mutex.h"






/**
 *******************************************************************************
 *  Mutex::Mutex    initialize members to known state
 *
 *******************************************************************************/
Mutex::Mutex ( void )
{
#if _MSC_VER
    InitializeCriticalSection(&criticalSection);
#else
	int result;
	pthread_mutexattr_t		criticalSectionAttributes;

	pthread_mutexattr_init( &criticalSectionAttributes );
	result = pthread_mutexattr_settype( &criticalSectionAttributes, PTHREAD_MUTEX_RECURSIVE );
	VDVD_ASSERT_REPORT_ERROR( (result!=0), VDVD_ERROR_PLATFORM_FAILED );

	pthread_mutex_init( &criticalSection, &criticalSectionAttributes );
#endif
}




/**
 *******************************************************************************
 *  Mutex::Lock locks the mutex by entering a critical section.
 *
 *  @return     If the function succeeds, the return value is SUCCESS.
 *******************************************************************************/
VDVD_ERROR  Mutex::Lock( void )
{
#if _MSC_VER
    EnterCriticalSection(&criticalSection);
#else

	int result;

    result = pthread_mutex_lock(&criticalSection);
	VDVD_ASSERT_ERROR( (result!=0), VDVD_ERROR_PLATFORM_FAILED );

#endif

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  Mutex::Unlock unlocks the mutex by leaving a critical section.
 *
 *  @return     If the function succeeds, the return value is SUCCESS.
 *******************************************************************************/
VDVD_ERROR  Mutex::Unlock( void )
{
#if _MSC_VER
    LeaveCriticalSection(&criticalSection);
#else

	int result;

	result = pthread_mutex_unlock(&criticalSection);
	VDVD_ASSERT_ERROR( (result!=0), VDVD_ERROR_PLATFORM_FAILED );

#endif

    return ( VDVD_SUCCESS );
}






/**
 *******************************************************************************
 *  AutoMutex::AutoMutex    Locks the mutex in the constructor
 *  This class provides an automatic mechanism to take the mutex in a routine
 *  and release it when leaving the subroutine (to avoid writing the same error
 *  handling code to release the mutex before exiting over and over again)
 *
 *******************************************************************************/
AutoMutex::AutoMutex ( Mutex* pMutex )
{
    VDVD_ERROR   error = VDVD_SUCCESS;

    VDVD_ASSERT_REPORT_ERROR( (pMutex==NULL), VDVD_ERROR_INVALID_PARAMETER );
    m_pMutex = pMutex;
    
    VDVD_REPORT_ERROR( m_pMutex->Lock() );
}






/**
 *******************************************************************************
 *  AutoMutex::~AutoMutex    Frees the mutex in the destructor
 *
 *******************************************************************************/
AutoMutex::~AutoMutex ( void )
{
    VDVD_ERROR   error = VDVD_SUCCESS;

    VDVD_REPORT_ERROR( m_pMutex->Unlock() );
}

⌨️ 快捷键说明

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