CPLMutexHolder::CPLMutexHolder

CPLMutex *CPLCreateMutex()
{
    unsigned char *pabyMutex = static_cast<unsigned char *>(malloc(4));
    if( pabyMutex == nullptr )
        return nullptr;

    pabyMutex[0] = 1;
    pabyMutex[1] = 'r';
    pabyMutex[2] = 'e';
    pabyMutex[3] = 'd';

    return (CPLMutex *) pabyMutex;
}

int CPLCreateOrAcquireMutexEx( CPLMutex **phMutex, double dfWaitInSeconds,
                               int nOptions )
{
    bool bSuccess = false;

    // Ironically, creation of this initial mutex is not threadsafe
    // even though we use it to ensure that creation of other mutexes
    // is threadsafe.
    if( hCOAMutex == nullptr )
    {
        hCOAMutex = CPLCreateMutex();
        if( hCOAMutex == nullptr )
        {
            *phMutex = nullptr;
            return FALSE;
        }
    }
    else
    {
        CPLAcquireMutex( hCOAMutex, dfWaitInSeconds );
    }

    if( *phMutex == nullptr )
    {
        *phMutex = CPLCreateMutexEx( nOptions );
        bSuccess = *phMutex != nullptr;
        CPLReleaseMutex( hCOAMutex );
    }
    else
    {
        CPLReleaseMutex( hCOAMutex );

        bSuccess = CPL_TO_BOOL(CPLAcquireMutex( *phMutex, dfWaitInSeconds ));
    }

    return bSuccess;
}

 

CPLMutexHolder::CPLMutexHolder( CPLMutex **phMutex,
                                double dfWaitInSeconds,
                                const char *pszFileIn,
                                int nLineIn,
                                int nOptions ) :
    hMutex(nullptr),
    pszFile(pszFileIn),
    nLine(nLineIn)
{
    if( phMutex == nullptr )
    {
        fprintf( stderr, "CPLMutexHolder: phMutex )) NULL !\n" );
        hMutex = nullptr;
        return;
    }

#ifdef DEBUG_MUTEX
    // There is no way to use CPLDebug() here because it works with
    // mutexes itself so we will fall in infinite recursion.
    // fprintf() will do the job right.
    fprintf( stderr,
             "CPLMutexHolder: Request %p for pid %ld at %d/%s.\n",
             *phMutex, static_cast<long>(CPLGetPID()), nLine, pszFile );
#else
    // TODO(schwehr): Find a better way to do handle this.
    (void)pszFile;
    (void)nLine;
#endif

    if( !CPLCreateOrAcquireMutexEx( phMutex, dfWaitInSeconds, nOptions ) )
    {
        fprintf( stderr, "CPLMutexHolder: Failed to acquire mutex!\n" );
        hMutex = nullptr;
    }
    else
    {
#ifdef DEBUG_MUTEX
        fprintf( stderr,
                 "CPLMutexHolder: Acquired %p for pid %ld at %d/%s.\n",
                 *phMutex, static_cast<long>(CPLGetPID()), nLine, pszFile );
#endif

        hMutex = *phMutex;
    }
}

 

CPLMutexHolder::~CPLMutexHolder()
{
    if( hMutex != nullptr )
    {
#ifdef DEBUG_MUTEX
        fprintf( stderr,
                 "~CPLMutexHolder: Release %p for pid %ld at %d/%s.\n",
                 hMutex, static_cast<long>(CPLGetPID()), nLine, pszFile );
#endif
        CPLReleaseMutex( hMutex );
    }
}

void CPLReleaseMutex( CPLMutex *hMutex )
{
    unsigned char *pabyMutex = reinterpret_cast<unsigned char *>(hMutex);

    CPLAssert( pabyMutex[1] == 'r' && pabyMutex[2] == 'e'
               && pabyMutex[3] == 'd' );

    if( pabyMutex[0] < 1 )
        CPLDebug( "CPLMultiProc",
                  "CPLReleaseMutex() called on mutex with %d as ref count!",
                  pabyMutex[0] );

    pabyMutex[0] -= 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值