@ -6,29 +6,19 @@
# include "tier0/platform.h"
# include "tier0/platform.h"
# if defined( PLATFORM_WINDOWS_PC )
# if defined(_WIN32 )
# define WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# define _WIN32_WINNT 0x0403
# define _WIN32_WINNT 0x0403
# include <windows.h>
# include <windows.h>
# endif
# endif
# ifdef PLATFORM_WINDOWS
# ifdef _WIN32
# include <process.h>
# include <process.h>
# ifdef PLATFORM_WINDOWS_PC
# ifdef _WIN32
# include <Mmsystem.h>
# include <Mmsystem.h>
# pragma comment(lib, "winmm.lib")
# pragma comment(lib, "winmm.lib")
# endif
# endif
# elif PLATFORM_PS3
# elif POSIX
# include <sched.h>
# include <unistd.h>
# include <exception>
# include <errno.h>
# include <pthread.h>
# include <sys/time.h>
# include <sys/timer.h>
# define GetLastError() errno
typedef void * LPVOID ;
# elif PLATFORM_POSIX
# include <sched.h>
# include <sched.h>
# include <exception>
# include <exception>
# include <errno.h>
# include <errno.h>
@ -38,8 +28,13 @@
# define GetLastError() errno
# define GetLastError() errno
typedef void * LPVOID ;
typedef void * LPVOID ;
# if !defined(OSX)
# if !defined(OSX)
# if defined(ANDROID)
# include <fcntl.h>
# include <unistd.h>
# else
# include <sys/fcntl.h>
# include <sys/fcntl.h>
# include <sys/unistd.h>
# include <sys/unistd.h>
# endif
# define sem_unlink( arg )
# define sem_unlink( arg )
# define OS_TO_PTHREAD(x) (x)
# define OS_TO_PTHREAD(x) (x)
# else
# else
@ -155,7 +150,7 @@ struct ThreadProcInfo_t
//---------------------------------------------------------
//---------------------------------------------------------
# ifdef PLATFORM_WINDOWS
# ifdef _WIN32
static DWORD WINAPI ThreadProcConvert ( void * pParam )
static DWORD WINAPI ThreadProcConvert ( void * pParam )
{
{
ThreadProcInfo_t info = * ( ( ThreadProcInfo_t * ) pParam ) ;
ThreadProcInfo_t info = * ( ( ThreadProcInfo_t * ) pParam ) ;
@ -165,7 +160,7 @@ static DWORD WINAPI ThreadProcConvert( void *pParam )
FreeThreadID ( ) ;
FreeThreadID ( ) ;
return nRet ;
return nRet ;
}
}
# elif defined( PLATFORM_P S3 )
# elif defined( PS3 )
union ThreadProcInfoUnion_t
union ThreadProcInfoUnion_t
{
{
struct Val_t
struct Val_t
@ -262,7 +257,7 @@ void TlsSetValue( uint32 index, void *pValue )
# ifdef PLATFORM_WINDOWS
# ifdef _WIN32
class CThreadHandleToIDMap
class CThreadHandleToIDMap
{
{
public :
public :
@ -421,12 +416,12 @@ void JoinTestThreads( ThreadHandle_t *pHandles )
ThreadHandle_t CreateSimpleThread ( ThreadFunc_t pfnThread , void * pParam , unsigned stackSize )
ThreadHandle_t CreateSimpleThread ( ThreadFunc_t pfnThread , void * pParam , unsigned stackSize )
{
{
# ifdef PLATFORM_WINDOWS
# ifdef _WIN32
DWORD threadID ;
DWORD threadID ;
HANDLE hThread = ( HANDLE ) CreateThread ( NULL , stackSize , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) , stackSize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0 , & threadID ) ;
HANDLE hThread = ( HANDLE ) CreateThread ( NULL , stackSize , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) , stackSize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0 , & threadID ) ;
AddThreadHandleToIDMap ( hThread , threadID ) ;
AddThreadHandleToIDMap ( hThread , threadID ) ;
return ( ThreadHandle_t ) hThread ;
return ( ThreadHandle_t ) hThread ;
# elif PLATFORM_P S3
# elif PS3
//TestThreads();
//TestThreads();
ThreadHandle_t th ;
ThreadHandle_t th ;
ThreadProcInfoUnion_t info ;
ThreadProcInfoUnion_t info ;
@ -439,7 +434,7 @@ ThreadHandle_t CreateSimpleThread( ThreadFunc_t pfnThread, void *pParam, unsigne
return 0 ;
return 0 ;
}
}
return th ;
return th ;
# elif PLATFORM_P OSIX
# elif POSIX
pthread_t tid ;
pthread_t tid ;
pthread_create ( & tid , NULL , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) ) ;
pthread_create ( & tid , NULL , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) ) ;
return ( ThreadHandle_t ) tid ;
return ( ThreadHandle_t ) tid ;
@ -452,14 +447,14 @@ ThreadHandle_t CreateSimpleThread( ThreadFunc_t pfnThread, void *pParam, unsigne
ThreadHandle_t CreateSimpleThread ( ThreadFunc_t pfnThread , void * pParam , ThreadId_t * pID , unsigned stackSize )
ThreadHandle_t CreateSimpleThread ( ThreadFunc_t pfnThread , void * pParam , ThreadId_t * pID , unsigned stackSize )
{
{
# ifdef PLATFORM_WINDOWS
# ifdef _WIN32
DWORD threadID ;
DWORD threadID ;
HANDLE hThread = ( HANDLE ) CreateThread ( NULL , stackSize , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) , stackSize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0 , & threadID ) ;
HANDLE hThread = ( HANDLE ) CreateThread ( NULL , stackSize , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) , stackSize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0 , & threadID ) ;
if ( pID )
if ( pID )
* pID = ( ThreadId_t ) threadID ;
* pID = ( ThreadId_t ) threadID ;
AddThreadHandleToIDMap ( hThread , threadID ) ;
AddThreadHandleToIDMap ( hThread , threadID ) ;
return ( ThreadHandle_t ) hThread ;
return ( ThreadHandle_t ) hThread ;
# elif PLATFORM_P OSIX
# elif POSIX
pthread_t tid ;
pthread_t tid ;
pthread_create ( & tid , NULL , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) ) ;
pthread_create ( & tid , NULL , ThreadProcConvert , new ThreadProcInfo_t ( pfnThread , pParam ) ) ;
if ( pID )
if ( pID )
@ -494,7 +489,7 @@ void ThreadSleep(unsigned nMilliseconds)
{
{
# ifdef _WIN32
# ifdef _WIN32
# ifdef PLATFORM_WINDOWS _PC
# ifdef _WIN32 _PC
static bool bInitialized = false ;
static bool bInitialized = false ;
if ( ! bInitialized )
if ( ! bInitialized )
{
{
@ -508,7 +503,7 @@ void ThreadSleep(unsigned nMilliseconds)
# endif
# endif
Sleep ( nMilliseconds ) ;
Sleep ( nMilliseconds ) ;
# elif PLATFORM_P S3
# elif PS3
if ( nMilliseconds = = 0 )
if ( nMilliseconds = = 0 )
{
{
// sys_ppu_thread_yield doesn't seem to function properly, so sleep instead.
// sys_ppu_thread_yield doesn't seem to function properly, so sleep instead.
@ -530,7 +525,7 @@ void ThreadNanoSleep(unsigned ns)
# ifdef _WIN32
# ifdef _WIN32
// ceil
// ceil
Sleep ( ( ns + 999 ) / 1000 ) ;
Sleep ( ( ns + 999 ) / 1000 ) ;
# elif PLATFORM_P S3
# elif PS3
sys_timer_usleep ( ns ) ;
sys_timer_usleep ( ns ) ;
# elif defined(POSIX)
# elif defined(POSIX)
struct timespec tm ;
struct timespec tm ;
@ -814,7 +809,7 @@ sys_lwmutex_t CThreadSyncObject::m_staticMutex;
CThreadSyncObject : : CThreadSyncObject ( )
CThreadSyncObject : : CThreadSyncObject ( )
# ifdef _WIN32
# ifdef _WIN32
: m_hSyncObject ( NULL ) , m_bCreatedHandle ( false )
: m_hSyncObject ( NULL ) , m_bCreatedHandle ( false )
# elif defined(POSIX) && !defined(PLATFORM_P S3)
# elif defined(POSIX) && !defined(PS3)
: m_bInitalized ( false )
: m_bInitalized ( false )
# endif
# endif
{
{
@ -857,7 +852,7 @@ CThreadSyncObject::~CThreadSyncObject()
Assert ( 0 ) ;
Assert ( 0 ) ;
}
}
}
}
# elif defined(POSIX) && !defined( PLATFORM_P S3 )
# elif defined(POSIX) && !defined( PS3 )
if ( m_bInitalized )
if ( m_bInitalized )
{
{
pthread_cond_destroy ( & m_Condition ) ;
pthread_cond_destroy ( & m_Condition ) ;
@ -871,7 +866,7 @@ CThreadSyncObject::~CThreadSyncObject()
bool CThreadSyncObject : : operator ! ( ) const
bool CThreadSyncObject : : operator ! ( ) const
{
{
# if PLATFORM_P S3
# if PS3
return m_bstaticMutexInitialized ;
return m_bstaticMutexInitialized ;
# elif defined( _WIN32 )
# elif defined( _WIN32 )
return ! m_hSyncObject ;
return ! m_hSyncObject ;
@ -885,7 +880,7 @@ bool CThreadSyncObject::operator!() const
void CThreadSyncObject : : AssertUseable ( )
void CThreadSyncObject : : AssertUseable ( )
{
{
# ifdef THREADS_DEBUG
# ifdef THREADS_DEBUG
# if PLATFORM_P S3
# if PS3
AssertMsg ( m_bstaticMutexInitialized , " Thread synchronization object is unuseable " ) ;
AssertMsg ( m_bstaticMutexInitialized , " Thread synchronization object is unuseable " ) ;
# elif defined( _WIN32 )
# elif defined( _WIN32 )
AssertMsg ( m_hSyncObject , " Thread synchronization object is unuseable " ) ;
AssertMsg ( m_hSyncObject , " Thread synchronization object is unuseable " ) ;
@ -905,7 +900,7 @@ bool CThreadSyncObject::Wait( uint32 dwTimeout )
# endif
# endif
# ifdef _WIN32
# ifdef _WIN32
return ( WaitForSingleObject ( m_hSyncObject , dwTimeout ) = = WAIT_OBJECT_0 ) ;
return ( WaitForSingleObject ( m_hSyncObject , dwTimeout ) = = WAIT_OBJECT_0 ) ;
# elif defined( POSIX ) && !defined( PLATFORM_P S3 )
# elif defined( POSIX ) && !defined( PS3 )
pthread_mutex_lock ( & m_Mutex ) ;
pthread_mutex_lock ( & m_Mutex ) ;
bool bRet = false ;
bool bRet = false ;
if ( m_cSet > 0 )
if ( m_cSet > 0 )
@ -1263,7 +1258,7 @@ void CThreadEvent::UnregisterWaitingThread(sys_semaphore_t *pSemaphore)
# endif // _PS3
# endif // _PS3
# ifdef PLATFORM_WINDOWS
# ifdef _WIN32
CThreadEvent : : CThreadEvent ( const char * name , bool initialState , bool bManualReset )
CThreadEvent : : CThreadEvent ( const char * name , bool initialState , bool bManualReset )
{
{
m_hSyncObject = CreateEvent ( NULL , bManualReset , ( BOOL ) initialState , name ) ;
m_hSyncObject = CreateEvent ( NULL , bManualReset , ( BOOL ) initialState , name ) ;