chxresourcetoken.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:2k
- /*============================================================================*
- *
- * (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;
- }
- }