cmppmutex.c

来自「用c/c++实现的一个CMPP API」· C语言 代码 · 共 181 行

C
181
字号
/***************************************************************************  Copyright    : 2001-2002, ASPIRE TECHNOLOGIES (SHENZHEN) LTD.  Program      : cmppmutex.c  Description  : 线程锁  Version      : 1.5***************************************************************************/#include <stdio.h>#include <stdlib.h>#include "cmppmutex.h"#include "log.h"#define ErrMsg Trace/************************************************************ *  desc:   mutex_init with recursive. *  rett:   int. *  retv:   0:  Success. *          <0: Failed. ************************************************************/#ifdef WIN32int nMutexInit(   RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {        return -1;    }	precMutex->rec_mutex = CreateMutex( NULL, FALSE, NULL );    if( precMutex->rec_mutex == NULL )    {        ErrMsg( "error when nMutexInit, nRet =%d\n" , 0 );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nMutexInit(   RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {        return -1;    }    nRet = pthread_mutex_init( &(precMutex->rec_mutex), NULL );    if( nRet != 0 )    {        ErrMsg( "error when nMutexInit, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif/************************************************************ *  desc:   mutex_destroy with recursive. *  rett:   int. *  retv:   0:  Success. *          <0: Failed. ************************************************************/#ifdef WIN32int nMutexDestroy( RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {        return -1;    }	nRet = CloseHandle(precMutex->rec_mutex);    if( nRet == 0 )    {        ErrMsg( "error when nMutexDestroy, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nMutexDestroy( RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {        return -1;    }    nRet = pthread_mutex_destroy( &(precMutex->rec_mutex) );    if( nRet != 0 )    {        ErrMsg( "error when nMutexDestroy, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif/************************************************************ *  desc: mutex_lock with recursive. *  rett: int. *  retv: 0:    Success. *        <0:   Failed. ************************************************************/#ifdef WIN32int nMutexLock(   RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {		return -1;    }	nRet = WaitForSingleObject(precMutex->rec_mutex , INFINITE );    if( nRet != WAIT_OBJECT_0)    {        ErrMsg( "error when nMutexLock, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nMutexLock(   RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {		return -1;    }    nRet = pthread_mutex_lock( &(precMutex->rec_mutex) );    if( nRet != 0 )    {        ErrMsg( "error when nMutexLock, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif/************************************************************ *  desc:   mutex_unlock with recursive. *  rett:   int. *  retv:   0:  Success. *          <0: Failed. ************************************************************/#ifdef WIN32int nMutexUnlock( RecMutex * precMutex ){    DWORD nRet = 0;    if( NULL == precMutex )    {		return -1;    }	nRet = ReleaseMutex(precMutex->rec_mutex);    if( nRet == 0 )    {        ErrMsg( "error when nMutexUnlock, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif#if defined(_HPUX_SOURCE) || defined(_LINUX_SOURCE)int nMutexUnlock( RecMutex * precMutex ){    int nRet = 0;    if( NULL == precMutex )    {		return -1;    }    nRet = pthread_mutex_unlock( &(precMutex->rec_mutex) );    if( nRet != 0 )    {        ErrMsg( "error when nMutexUnlock, nRet =%d\n" , nRet );        return MUTEX_FAIL;    }    return MUTEX_OK;}#endif

⌨️ 快捷键说明

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