Files
itgmania212121/stepmania/src/arch/Threads/Threads_Pthreads.cpp
T

374 lines
8.4 KiB
C++
Raw Normal View History

#include "global.h"
#include "Threads_Pthreads.h"
2004-06-14 05:25:25 +00:00
#include "RageUtil.h"
#include <sys/time.h>
2004-06-11 05:26:00 +00:00
#include <errno.h>
#if defined(LINUX)
#include "archutils/Unix/LinuxThreadHelpers.h"
2004-09-06 08:32:25 +00:00
#include "archutils/Unix/RunningUnderValgrind.h"
#endif
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
#include "archutils/Unix/Backtrace.h"
#include "archutils/Unix/CrashHandler.h"
#endif
#if defined(DARWIN)
#include <mach/mach_init.h>
#include <mach/thread_act.h>
#endif
void ThreadImpl_Pthreads::Halt( bool Kill )
{
#if defined(PID_BASED_THREADS)
/*
* Send a SIGSTOP to the thread. If we send a SIGKILL, pthreads will
* "helpfully" propagate it to the other threads, and we'll get killed, too.
*
* This isn't ideal, since it can cause the process to background as far as
* the shell is concerned, so the shell prompt can display before the crash
* handler actually displays a message.
*/
SuspendThread( pid );
#elif defined(DARWIN)
thread_suspend( MachThreadHandle );
#endif
}
void ThreadImpl_Pthreads::Resume()
{
#if defined(PID_BASED_THREADS)
/* Send a SIGCONT to the thread. */
ResumeThread( pid );
#elif defined(DARWIN)
thread_resume( MachThreadHandle );
#endif
}
uint64_t ThreadImpl_Pthreads::GetThreadId() const
{
#if defined(PID_BASED_THREADS)
return (uint64_t) pid;
#elif defined(DARWIN)
return MachThreadHandle;
#endif
}
int ThreadImpl_Pthreads::Wait()
{
void *val;
int ret = pthread_join( thread, &val );
if( ret )
2004-06-16 02:20:14 +00:00
RageException::Throw( "pthread_join: %s", strerror(errno) );
return (int) val;
}
2004-06-11 05:26:00 +00:00
ThreadImpl *MakeThisThread()
{
ThreadImpl_Pthreads *thread = new ThreadImpl_Pthreads;
thread->thread = pthread_self();
#if defined(PID_BASED_THREADS)
thread->pid = GetCurrentThreadId(); /* in LinuxThreadHelpers.cpp */
#endif
#if defined(DARWIN)
2004-06-11 07:39:53 +00:00
thread->MachThreadHandle = mach_thread_self();
#endif
return thread;
}
static void *StartThread( void *pData )
{
ThreadImpl_Pthreads *pThis = (ThreadImpl_Pthreads *) pData;
#if defined(PID_BASED_THREADS)
pThis->pid = GetCurrentThreadId(); /* in LinuxThreadHelpers.cpp */
#endif
#if defined(DARWIN)
2004-06-11 07:39:53 +00:00
pThis->MachThreadHandle = mach_thread_self();
#endif
2004-06-16 01:53:27 +00:00
*pThis->m_piThreadID = pThis->GetThreadId();
/* Tell MakeThread that we've set m_piThreadID, so it's safe to return. */
pThis->m_StartFinishedSem->Post();
2004-06-16 01:53:27 +00:00
return (void *) pThis->m_pFunc( pThis->m_pData );
}
2004-06-16 01:53:27 +00:00
ThreadImpl *MakeThread( int (*pFunc)(void *pData), void *pData, uint64_t *piThreadID )
{
2004-06-11 05:26:00 +00:00
ThreadImpl_Pthreads *thread = new ThreadImpl_Pthreads;
thread->m_pFunc = pFunc;
thread->m_pData = pData;
2004-06-16 01:53:27 +00:00
thread->m_piThreadID = piThreadID;
thread->m_StartFinishedSem = new SemaImpl_Pthreads( 0 );
int ret = pthread_create( &thread->thread, NULL, StartThread, thread );
if( ret )
2004-06-16 02:20:14 +00:00
FAIL_M( ssprintf( "MakeThread: pthread_create: %s", strerror(errno)) );
2004-06-16 01:53:27 +00:00
/* Don't return until StartThread sets m_piThreadID. */
thread->m_StartFinishedSem->Wait();
delete thread->m_StartFinishedSem;
return thread;
}
MutexImpl_Pthreads::MutexImpl_Pthreads( RageMutex *pParent ):
MutexImpl( pParent )
{
pthread_mutex_init( &mutex, NULL );
}
MutexImpl_Pthreads::~MutexImpl_Pthreads()
{
int ret = pthread_mutex_destroy( &mutex ) == -1;
if( ret )
2004-06-16 02:20:14 +00:00
RageException::Throw( "Error deleting mutex: %s", strerror(errno) );
}
2004-08-22 01:31:12 +00:00
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
static bool UseTimedlock()
{
2004-09-06 08:32:25 +00:00
#if defined(LINUX)
2004-08-22 01:31:12 +00:00
/* Valgrind crashes and burns on pthread_mutex_timedlock. */
2004-09-06 08:32:25 +00:00
if( RunningUnderValgrind() )
2004-08-22 01:31:12 +00:00
return false;
#endif
return true;
}
#endif
bool MutexImpl_Pthreads::Lock()
{
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
2004-08-22 01:31:12 +00:00
if( UseTimedlock() )
{
2004-08-22 01:31:12 +00:00
int len = 10; /* seconds */
int tries = 2;
2004-08-22 01:31:12 +00:00
while( tries-- )
{
2004-08-22 01:31:12 +00:00
/* Wait for ten seconds. If it takes longer than that, we're probably deadlocked. */
timeval tv;
gettimeofday( &tv, NULL );
2004-08-22 01:31:12 +00:00
timespec ts;
ts.tv_sec = tv.tv_sec + len;
ts.tv_nsec = tv.tv_usec * 1000;
int ret = pthread_mutex_timedlock( &mutex, &ts );
switch( ret )
{
case 0:
return true;
2004-06-14 05:21:46 +00:00
2004-08-22 01:31:12 +00:00
case EINTR:
/* Ignore it. */
++tries;
continue;
2004-08-22 01:31:12 +00:00
case ETIMEDOUT:
/* Timed out. Probably deadlocked. Try again one more time, with a smaller
* timeout, just in case we're debugging and happened to stop while waiting
* on the mutex. */
len = 1;
break;
default:
FAIL_M( ssprintf("pthread_mutex_timedlock: %s", strerror(errno)) );
}
}
2004-08-22 01:31:12 +00:00
return false;
}
#endif
2004-06-14 05:21:46 +00:00
int ret;
do
{
ret = pthread_mutex_lock( &mutex );
}
while( ret == -1 && ret == EINTR );
ASSERT_M( ret == 0, ssprintf("pthread_mutex_lock: %s", strerror(errno)) );
return true;
}
2004-06-14 05:21:46 +00:00
bool MutexImpl_Pthreads::TryLock()
{
int ret = pthread_mutex_trylock( &mutex );
if( ret == EBUSY )
return false;
if( ret )
2004-06-16 02:20:14 +00:00
RageException::Throw( "pthread_mutex_lock failed: %s", strerror(errno) );
2004-06-14 05:21:46 +00:00
return true;
}
void MutexImpl_Pthreads::Unlock()
{
pthread_mutex_unlock( &mutex );
}
uint64_t GetThisThreadId()
{
#if defined(PID_BASED_THREADS)
return GetCurrentThreadId();
#elif defined(DARWIN)
return (int) mach_thread_self();
#endif
}
uint64_t GetInvalidThreadId()
{
return 0;
}
MutexImpl *MakeMutex( RageMutex *pParent )
{
return new MutexImpl_Pthreads( pParent );
}
#if 0
2004-06-14 05:21:46 +00:00
SemaImpl_Pthreads::SemaImpl_Pthreads( int iInitialValue )
{
sem_init( &sem, 0, iInitialValue );
}
SemaImpl_Pthreads::~SemaImpl_Pthreads()
{
sem_destroy( &sem );
}
int SemaImpl_Pthreads::GetValue() const
{
int ret;
2004-06-14 05:25:25 +00:00
sem_getvalue( const_cast<sem_t *>(&sem), &ret );
2004-06-14 05:21:46 +00:00
return ret;
}
void SemaImpl_Pthreads::Post()
{
sem_post( &sem );
}
bool SemaImpl_Pthreads::Wait()
{
int ret;
do
{
ret = sem_wait( &sem );
}
while( ret == -1 && errno == EINTR );
2004-06-16 02:20:14 +00:00
ASSERT_M( ret == 0, ssprintf("Wait: sem_wait: %s", strerror(errno)) );
2004-06-14 05:21:46 +00:00
return true;
}
bool SemaImpl_Pthreads::TryWait()
{
int ret = sem_trywait( &sem );
if( ret == EBUSY )
return false;
if( ret )
2004-06-16 02:20:14 +00:00
RageException::Throw( "TryWait: sem_trywait failed: %s", strerror(errno) );
2004-06-14 05:21:46 +00:00
return true;
}
#else
/* Use conditions, to work around OSX "forgetting" to implement semaphores. */
SemaImpl_Pthreads::SemaImpl_Pthreads( int iInitialValue )
{
int ret = pthread_cond_init( &m_Cond, NULL );
ASSERT_M( ret == 0, ssprintf( "SemaImpl_Pthreads: pthread_cond_init: %s", strerror(errno)) );
ret = pthread_mutex_init( &m_Mutex, NULL );
ASSERT_M( ret == 0, ssprintf( "SemaImpl_Pthreads: pthread_mutex_init: %s", strerror(errno)) );
m_iValue = iInitialValue;
}
SemaImpl_Pthreads::~SemaImpl_Pthreads()
{
pthread_cond_destroy( &m_Cond );
pthread_mutex_destroy( &m_Mutex );
}
void SemaImpl_Pthreads::Post()
{
pthread_mutex_lock( &m_Mutex );
++m_iValue;
if( m_iValue == 1 )
pthread_cond_signal( &m_Cond );
pthread_mutex_unlock( &m_Mutex );
}
bool SemaImpl_Pthreads::Wait()
{
pthread_mutex_lock( &m_Mutex );
while( !m_iValue )
pthread_cond_wait( &m_Cond, &m_Mutex );
--m_iValue;
pthread_mutex_unlock( &m_Mutex);
return true;
}
bool SemaImpl_Pthreads::TryWait()
{
pthread_mutex_lock( &m_Mutex );
if( !m_iValue )
{
pthread_mutex_unlock( &m_Mutex);
return false;
}
--m_iValue;
pthread_mutex_unlock( &m_Mutex);
return true;
}
#endif
2004-06-14 05:21:46 +00:00
SemaImpl *MakeSemaphore( int iInitialValue )
{
return new SemaImpl_Pthreads( iInitialValue );
}
/*
* (c) 2001-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/