pthreadthreads.cpp
来自「symbian 下的helix player源代码」· C++ 代码 · 共 601 行 · 第 1/2 页
CPP
601 行
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: $Id: pthreadthreads.cpp,v 1.7.2.3 2004/07/09 01:43:30 hubbe Exp $
*
* Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file,
* are subject to the current version of the RealNetworks Public
* Source License (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the current version of the RealNetworks Community
* Source License (the "RCSL") available at
* http://www.helixcommunity.org/content/rcsl, in which case the RCSL
* will apply. You may also obtain the license terms directly from
* RealNetworks. You may not use this file except in compliance with
* the RPSL or, if you have a valid RCSL with RealNetworks applicable
* to this file, the RCSL. Please see the applicable RPSL or RCSL for
* the rights, obligations and limitations governing use of the
* contents of the file.
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL") in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your version of
* this file only under the terms of the GPL, and not to allow others
* to use your version of this file under the terms of either the RPSL
* or RCSL, indicate your decision by deleting the provisions above
* and replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient may
* use your version of this file under the terms of any one of the
* RPSL, the RCSL or the GPL.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the
* portions it created.
*
* This file, and the files included with this file, is distributed
* and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
* ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
//This is used to turn off threads in libc5 builds.
#ifdef _UNIX_THREADS_SUPPORTED
#include <errno.h>
#include "hxtypes.h"
#include "hxresult.h"
#include <pthread.h>
#include <sys/time.h>
#include <semaphore.h>
#include "pthreadthreads.h"
//=======================================================================
//
// HXPthreadThread
// ----------------------
//
//=======================================================================
HXPthreadThread::HXPthreadThread()
: HXUnixThread()
{}
HXPthreadThread::~HXPthreadThread()
{}
HX_RESULT HXPthreadThread::_thread_create( ULONG32& ulThreadID, void*(pfExecFunc(void*)), void* pArg )
{
HX_RESULT retVal = HXR_OK;
pthread_t threadID=0;
int nCode = pthread_create( &threadID, NULL, pfExecFunc, pArg );
ulThreadID = threadID;
if(nCode!=0)
{
ulThreadID = 0;
retVal = HXR_FAIL;
HX_ASSERT( "Failed to create thread"==NULL );
}
return retVal;
}
ULONG32 HXPthreadThread::_thread_self()
{
return pthread_self();
}
void HXPthreadThread::_thread_exit(UINT32 unExitCode)
{
pthread_exit( (void*)unExitCode );
}
void HXPthreadThread::_thread_cancel(ULONG32 ulThreadID)
{
pthread_cancel( ulThreadID );
}
ULONG32 HXPthreadThread::_thread_join(ULONG32 ulThreadID)
{
void* pvRetVal = NULL;
pthread_join( ulThreadID, &pvRetVal );
return (ULONG32)(PTR_INT)pvRetVal;
}
//=======================================================================
//
// HXPthreadMutex
// ------------------
//
//=======================================================================
HXPthreadMutex::HXPthreadMutex()
: HXUnixMutex(),
m_ulOwnerThread(0),
m_ulLockCount(0)
{
memset(&m_mutex, 0, sizeof(m_mutex));
memset(&m_mtxLockLock, 0, sizeof(m_mtxLockLock));
#ifdef _TIMEDWAITS_RECURSIVE_MUTEXES
pthread_mutexattr_t attr;
pthread_mutexattr_init( &attr );
#ifdef PTHREAD_MUTEX_RECURSIVE_NP
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
#else
pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
#endif
pthread_mutex_init( &m_mutex, &attr );
pthread_mutexattr_destroy( &attr );
#else
pthread_mutex_init( &m_mutex,NULL);
pthread_mutex_init( &m_mtxLockLock, NULL );
#endif
}
HXPthreadMutex::~HXPthreadMutex()
{
pthread_mutex_destroy( &m_mutex );
#ifndef _TIMEDWAITS_RECURSIVE_MUTEXES
m_ulLockCount = 0;
m_ulOwnerThread = 0;
pthread_mutex_destroy( &m_mtxLockLock );
#endif
}
HX_RESULT HXPthreadMutex::_Lock()
{
//We simulate recursive mutexes.
HX_RESULT res = HXR_OK;
int nResult = 0;
#ifndef _TIMEDWAITS_RECURSIVE_MUTEXES
nResult = pthread_mutex_lock(&m_mtxLockLock);
if (nResult != 0 )
{
res = HXR_FAIL;
return res;
}
if( m_ulOwnerThread != pthread_self() )
{
pthread_mutex_unlock(&m_mtxLockLock);
//We are going to block for sure.
nResult = pthread_mutex_lock(&m_mutex);
//Take ownership.
if ( pthread_mutex_lock(&m_mtxLockLock) != 0 )
{
//This should not happen but if does then unlock the
//main mutex and return failure.
if ( nResult == 0 )
{
pthread_mutex_unlock(&m_mutex);
}
return HXR_FAIL;
}
if ( nResult == 0 )
{
m_ulOwnerThread = pthread_self();
m_ulLockCount = 1;
}
else
{
res = HXR_FAIL;
}
}
else
{
//We alread have it locked. Just increment the lock count
m_ulLockCount++;
}
pthread_mutex_unlock(&m_mtxLockLock);
#else
pthread_mutex_lock(&m_mutex);
#endif
return res;
}
HX_RESULT HXPthreadMutex::_Unlock()
{
HX_RESULT res = HXR_OK;
int nResult = 0;
#ifndef _TIMEDWAITS_RECURSIVE_MUTEXES
nResult = pthread_mutex_lock(&m_mtxLockLock);
if ( nResult != 0 )
{
res = HXR_FAIL;
return res;
}
//Sanity checks.
HX_ASSERT( m_ulLockCount != 0 && m_ulOwnerThread == pthread_self() );
if( m_ulLockCount == 0 || m_ulOwnerThread!=pthread_self() )
{
pthread_mutex_unlock(&m_mtxLockLock);
return HXR_FAIL;
}
if( m_ulLockCount == 1 )
{
//We are really done with it. Do the real unlock now.
nResult = pthread_mutex_unlock(&m_mutex);
if ( nResult == 0 )
{
m_ulOwnerThread = 0;
m_ulLockCount=0;
}
else
{
res = HXR_FAIL;
}
}
else
{
m_ulLockCount--;
}
pthread_mutex_unlock(&m_mtxLockLock);
#else
pthread_mutex_unlock(&m_mutex);
#endif
return res;
}
HX_RESULT HXPthreadMutex::_TryLock()
{
HX_RESULT res = HXR_OK;
int nResult = 0;
#ifndef _TIMEDWAITS_RECURSIVE_MUTEXES
nResult = pthread_mutex_lock(&m_mtxLockLock);
if (nResult != 0 )
{
res = HXR_FAIL;
return res;
}
if( m_ulOwnerThread != pthread_self() )
{
nResult = pthread_mutex_trylock(&m_mutex);
if ( nResult == 0 )
{
m_ulOwnerThread = pthread_self();
m_ulLockCount = 1;
}
else
{
res = HXR_FAIL;
}
}
else
{
//We alread have it locked. Just increment the lock count
m_ulLockCount++;
}
pthread_mutex_unlock(&m_mtxLockLock);
#else
nResult = pthread_mutex_trylock(&m_mutex);
if ( nResult != 0 )
{
res = HXR_FAIL;
}
#endif
return res;
}
pthread_mutex_t* HXPthreadMutex::_GetPthreadMutex()
{
return &m_mutex;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?