chxresourcetoken.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 88 行

CPP
88
字号

/*============================================================================*
 *
 * (c) 1995-2002 RealNetworks, Inc. Patents pending. All rights reserved.
 *
 *============================================================================*/


#include "hxassert.h"

#include "chxavutil.h"
#include "hxsym_leaveutil.h"
#include "chxresourcetoken.h"

///////////////////////////////////
// ctor
CHXResourceToken::CHXResourceToken()
: m_bGotIt(false)
, m_bIsOpen(false)
{

}

void CHXResourceToken::ConstructL(const TDesC& name, TInt maxUserCount)
{
    HXSYM_LEAVE_IF_ERR(OpenSemaphoreL(name, maxUserCount));
}

///////////////////////////////////
// dtor
CHXResourceToken::~CHXResourceToken()
{
    Release();
    if(m_bIsOpen)
    {
        m_sem.Close();
    }
}

///////////////////////////////////
//
TInt CHXResourceToken::OpenSemaphoreL(const TDesC& name, TInt maxUserCount)
{
    // first try open in case it exists
    TInt err = m_sem.OpenGlobal(name);
    if( err != KErrNone )
    {
        // first user; create (set thread as owner so if thread leaves/exits, semaphore count auto-increments)
        err = m_sem.CreateGlobal(name, maxUserCount, EOwnerThread);
    }

    m_bIsOpen = (KErrNone == err);
    
    return err;
}

///////////////////////////////////
//
bool CHXResourceToken::TryAcquire()
{
    HX_ASSERT(m_bIsOpen);
    if( !m_bGotIt )
    {
        // acquire the semaphore only if it is available (do not block)
        if(m_sem.Count() > 0)
        {
            // the semaphore is available
            m_sem.Wait();
            m_bGotIt = true;
        }
        
    }
    return m_bGotIt;
}

void CHXResourceToken::Release()
{
    if( m_bGotIt )
    {
        // give up our hold on the semaphore
        m_sem.Signal();
        m_bGotIt = false;
    }
}



⌨️ 快捷键说明

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