pthreadthreads.cpp
来自「symbian 下的helix player源代码」· C++ 代码 · 共 601 行 · 第 1/2 页
CPP
601 行
}
//=======================================================================
//
// HXPthreadCondition
// ----------------------
//
//=======================================================================
HXPthreadCondition::HXPthreadCondition(HXUnixMutex*& pMutex)
{
HX_ASSERT( pMutex == NULL );
//Create the mutex we need to associate with this cond.
m_pMutex = new HXPthreadMutex();
pMutex = (HXUnixMutex*)m_pMutex;
//Init our cond var.
pthread_cond_init( &m_cond, NULL );
}
HXPthreadCondition::~HXPthreadCondition()
{
pthread_cond_destroy(&m_cond);
HX_DELETE( m_pMutex );
}
HX_RESULT HXPthreadCondition::_Signal()
{
pthread_cond_signal(&m_cond);
return HXR_OK;
}
HX_RESULT HXPthreadCondition::_Broadcast()
{
pthread_cond_broadcast(&m_cond);
return HXR_OK;
}
HX_RESULT HXPthreadCondition::_Wait()
{
HX_ASSERT( m_pMutex );
//m_pMuex MUST BE LOCKED ALL READY!
pthread_cond_wait(&m_cond, m_pMutex->_GetPthreadMutex());
return HXR_OK;
}
HX_RESULT HXPthreadCondition::_TimedWait(UINT32 unTimeOut)
{
//m_pMuex MUST BE LOCKED ALL READY!
HX_RESULT ret = HXR_OK;
struct timeval now;
struct timespec timeout;
int retcode;
gettimeofday(&now, NULL);
long int waitSeconds = unTimeOut/1000;
long int nanoSeconds = (unTimeOut-(waitSeconds*1000))*1000000;
timeout.tv_sec = now.tv_sec+waitSeconds;
timeout.tv_nsec = now.tv_usec*1000+nanoSeconds;
if( timeout.tv_nsec >= 1000000000 )
{
timeout.tv_nsec -= 1000000000;
timeout.tv_sec += 1;
}
retcode = pthread_cond_timedwait(&m_cond, m_pMutex->_GetPthreadMutex(), &timeout);
if(retcode==-1)
{
ret = HXR_FAIL;
//We really could use a HXR_TIMEDOUT.
if( errno == ETIMEDOUT )
ret = HXR_WOULD_BLOCK;
}
return ret;
}
#ifndef _MAC_UNIX
//=======================================================================
//
// HXPthreadSemaphore
// ------------------
//
//=======================================================================
HXPthreadSemaphore::HXPthreadSemaphore(UINT32 unInitialCount)
: HXUnixSemaphore( unInitialCount )
{
//Init the sem to non-shared and count passed in.
if( sem_init( &m_semaphore, 0, m_unInitialCount ) < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't init semaphore: %d %s\n", errno, strerror(errno) );
#endif
}
}
HXPthreadSemaphore::~HXPthreadSemaphore()
{
sem_destroy( &m_semaphore );
}
HX_RESULT HXPthreadSemaphore::_Post()
{
HX_RESULT retVal = HXR_OK;
//Init the sem to non-shared and count passed in.
if( sem_post(&m_semaphore) < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't post to semaphore: %d %s\n", errno, strerror(errno) );
#endif
retVal = HXR_FAIL;
}
return retVal;
}
HX_RESULT HXPthreadSemaphore::_Wait()
{
//sem_wait always returns zero.
sem_wait( &m_semaphore );
return HXR_OK;
}
HX_RESULT HXPthreadSemaphore::_TryWait()
{
HX_RESULT retVal = HXR_OK;
int nResult = 0;
nResult = sem_trywait( &m_semaphore );
if( nResult<0 && errno == EAGAIN )
{
retVal = HXR_WOULD_BLOCK;
}
else if( nResult < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't wait on semaphore: %d %s\n", errno, strerror(errno) );
#endif
retVal = HXR_FAIL;
}
return retVal;
}
// #ifdef _TIMEDWAITS_RECURSIVE_MUTEXES
// HX_RESULT HXPthreadSemaphore::_TimedWait(UINT32 unTimeOut )
// {
// HX_RESULT ret = HXR_OK;
// struct timeval now;
// struct timespec timeout;
// int retcode;
// gettimeofday(&now, NULL);
// long int waitSeconds = unTimeOut/1000;
// long int nanoSeconds = (unTimeOut-(waitSeconds*1000))*1000000;
// if( nanoSeconds >= 1000000000 )
// {
// nanoSeconds -= 1000000000;
// waitSeconds += 1;
// }
// timeout.tv_sec = now.tv_sec+waitSeconds;
// timeout.tv_nsec = now.tv_usec*1000+nanoSeconds;
// if( timeout.tv_nsec >= 1000000000 )
// {
// timeout.tv_nsec -= 1000000000;
// timeout.tv_sec += 1;
// }
// //XXXgfw TEST TEST TEST
// retcode = sem_timedwait(&m_semaphore, &timeout);
// if(retcode==-1)
// {
// ret = HXR_FAIL;
// //We really could use a HXR_TIMEDOUT.
// if( errno == ETIMEDOUT )
// ret = HXR_WOULD_BLOCK;
// }
// return ret;
// }
// #endif
HX_RESULT HXPthreadSemaphore::_GetValue( int* pnCount)
{
//sem_getvalue always returns zero.
sem_getvalue( &m_semaphore, pnCount );
return HXR_OK;
}
#else
// now the _MAC_UNIX case...
//=======================================================================
//
// HXPthreadMacSemaphore
// ---------------------
//
//=======================================================================
HXPthreadMacSemaphore::HXPthreadMacSemaphore(UINT32 unInitialCount)
: HXUnixSemaphore( unInitialCount )
{
//Init the sem to non-shared and count passed in.
char buf[32];
sprintf(buf, "%s", tmpnam(NULL));
sem_t* sem = sem_open(buf, O_CREAT, 0, m_unInitialCount);
if ((int)sem == SEM_FAILED)
{
#ifdef _DEBUG
fprintf( stderr, "Can't open semaphore: %d %s\n", errno, strerror(errno) );
#endif
}
else
{
m_semaphore = sem;
}
}
HXPthreadMacSemaphore::~HXPthreadMacSemaphore()
{
if ( sem_close(m_semaphore) < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't close semaphore: %d %s\n", errno, strerror(errno) );
#endif
}
}
HX_RESULT HXPthreadMacSemaphore::_Post()
{
HX_RESULT retVal = HXR_OK;
//Init the sem to non-shared and count passed in.
if( sem_post(m_semaphore) < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't post to semaphore: %d %s\n", errno, strerror(errno) );
#endif
retVal = HXR_FAIL;
}
return retVal;
}
HX_RESULT HXPthreadMacSemaphore::_Wait()
{
//sem_wait always returns zero.
if ( sem_wait( m_semaphore ) < 0)
{
#ifdef _DEBUG
fprintf( stderr, "sem_wait failed: %d %s\n", errno, strerror(errno) );
#endif
}
return HXR_OK;
}
HX_RESULT HXPthreadMacSemaphore::_TryWait()
{
HX_RESULT retVal = HXR_OK;
int nResult = 0;
nResult = sem_trywait( m_semaphore );
if( nResult<0 && errno == EAGAIN )
{
retVal = HXR_WOULD_BLOCK;
}
else if( nResult < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't wait on semaphore: %d %s\n", errno, strerror(errno) );
#endif
retVal = HXR_FAIL;
}
return retVal;
}
HX_RESULT HXPthreadMacSemaphore::_GetValue( int* pnCount)
{
//sem_getvalue always returns zero.
if ( sem_getvalue( m_semaphore, pnCount ) < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "sem_getvalue failed: %d %s\n", errno, strerror(errno) );
#endif
}
return HXR_OK;
}
#endif // _MAC_UNIX
#endif //_UNIX_THREADS_SUPPORTED
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?