From 2aa14bb24c8f7b4f60bd22ad3667627f837d8974 Mon Sep 17 00:00:00 2001 From: nillerusr Date: Thu, 16 Feb 2023 17:30:53 +0300 Subject: [PATCH] restore threadtools and vstdlib from 12716fd commit --- public/tier0/threadtools.h | 44 +++++++++++++++++++++++++++---- tier0/threadtools.cpp | 53 -------------------------------------- vstdlib/jobthread.cpp | 20 ++++---------- 3 files changed, 44 insertions(+), 73 deletions(-) diff --git a/public/tier0/threadtools.h b/public/tier0/threadtools.h index 2b18dda7..b7216193 100644 --- a/public/tier0/threadtools.h +++ b/public/tier0/threadtools.h @@ -52,6 +52,12 @@ #pragma once #pragma warning(push) #pragma warning(disable:4251) + +extern "C" +{ + void __declspec(dllimport) __stdcall Sleep( unsigned long ); +} + #endif #ifdef COMPILER_MSVC64 @@ -194,8 +200,6 @@ PLATFORM_INTERFACE bool ReleaseThreadHandle( ThreadHandle_t ); //----------------------------------------------------------------------------- -PLATFORM_INTERFACE void ThreadSleep(unsigned duration = 0); -PLATFORM_INTERFACE void ThreadNanoSleep(unsigned ns); PLATFORM_INTERFACE ThreadId_t ThreadGetCurrentId(); PLATFORM_INTERFACE ThreadHandle_t ThreadGetCurrentHandle(); PLATFORM_INTERFACE int ThreadGetPriority( ThreadHandle_t hThread = NULL ); @@ -229,10 +233,10 @@ inline void ThreadPause() { #if defined( COMPILER_PS3 ) __db16cyc(); -#elif defined(__arm__) || defined(__aarch64__) - sched_yield(); -#elif defined( COMPILER_GCC ) +#elif defined( COMPILER_GCC ) && (defined( __i386__ ) || defined( __x86_64__ )) __asm __volatile( "pause" ); +#elif defined( POSIX ) + sched_yield(); #elif defined ( COMPILER_MSVC64 ) _mm_pause(); #elif defined( COMPILER_MSVC32 ) @@ -247,6 +251,36 @@ inline void ThreadPause() #endif } +inline void ThreadSleep(unsigned nMilliseconds = 0) +{ + if( nMilliseconds == 0 ) + { + ThreadPause(); + return; + } + +#ifdef _WIN32 + +#ifdef _WIN32_PC + static bool bInitialized = false; + if ( !bInitialized ) + { + bInitialized = true; + // Set the timer resolution to 1 ms (default is 10.0, 15.6, 2.5, 1.0 or + // some other value depending on hardware and software) so that we can + // use Sleep( 1 ) to avoid wasting CPU time without missing our frame + // rate. + timeBeginPeriod( 1 ); + } +#endif + Sleep( nMilliseconds ); +#elif PS3 + sys_timer_usleep( nMilliseconds * 1000 ); +#elif defined(POSIX) + usleep( nMilliseconds * 1000 ); +#endif +} + PLATFORM_INTERFACE bool ThreadJoin( ThreadHandle_t, unsigned timeout = TT_INFINITE ); PLATFORM_INTERFACE void ThreadSetDebugName( ThreadHandle_t hThread, const char *pszName ); diff --git a/tier0/threadtools.cpp b/tier0/threadtools.cpp index b0dbb645..06c8296f 100644 --- a/tier0/threadtools.cpp +++ b/tier0/threadtools.cpp @@ -485,59 +485,6 @@ bool ReleaseThreadHandle( ThreadHandle_t hThread ) // //----------------------------------------------------------------------------- -void ThreadSleep(unsigned nMilliseconds) -{ -#ifdef _WIN32 - -#ifdef _WIN32_PC - static bool bInitialized = false; - if ( !bInitialized ) - { - bInitialized = true; - // Set the timer resolution to 1 ms (default is 10.0, 15.6, 2.5, 1.0 or - // some other value depending on hardware and software) so that we can - // use Sleep( 1 ) to avoid wasting CPU time without missing our frame - // rate. - timeBeginPeriod( 1 ); - } -#endif - - Sleep( nMilliseconds ); -#elif PS3 - if( nMilliseconds == 0 ) - { - // sys_ppu_thread_yield doesn't seem to function properly, so sleep instead. -// sys_timer_usleep( 60 ); - sys_ppu_thread_yield(); - } - else - { - sys_timer_usleep( nMilliseconds * 1000 ); - } -#elif defined(POSIX) - usleep( nMilliseconds * 1000 ); -#endif -} - -//----------------------------------------------------------------------------- -void ThreadNanoSleep(unsigned ns) -{ -#ifdef _WIN32 - // ceil - Sleep( ( ns + 999 ) / 1000 ); -#elif PS3 - sys_timer_usleep( ns ); -#elif defined(POSIX) - struct timespec tm; - tm.tv_sec = 0; - tm.tv_nsec = ns; - nanosleep( &tm, NULL ); -#endif -} - - -//----------------------------------------------------------------------------- - #ifndef ThreadGetCurrentId ThreadId_t ThreadGetCurrentId() { diff --git a/vstdlib/jobthread.cpp b/vstdlib/jobthread.cpp index 922b770f..634d5358 100644 --- a/vstdlib/jobthread.cpp +++ b/vstdlib/jobthread.cpp @@ -214,7 +214,11 @@ public: //----------------------------------------------------- virtual int YieldWait( CThreadEvent **pEvents, int nEvents, bool bWaitAll = true, unsigned timeout = TT_INFINITE ); virtual int YieldWait( CJob **, int nJobs, bool bWaitAll = true, unsigned timeout = TT_INFINITE ); - void Yield( unsigned timeout ); + inline void Yield( unsigned timeout ) + { + Assert( ThreadInMainThread() ); + ThreadSleep( timeout ); + } //----------------------------------------------------- // Add a native job to the queue (master thread) @@ -656,20 +660,6 @@ int CThreadPool::YieldWait( CJob **ppJobs, int nJobs, bool bWaitAll, unsigned ti return YieldWait( handles.Base(), handles.Count(), bWaitAll, timeout); } -//--------------------------------------------------------- - -void CThreadPool::Yield( unsigned timeout ) -{ - // @MULTICORE (toml 10/24/2006): not implemented - Assert( ThreadInMainThread() ); - if ( !ThreadInMainThread() ) - { - ThreadSleep( timeout ); - return; - } - ThreadSleep( timeout ); -} - //--------------------------------------------------------- // Add a job to the queue //---------------------------------------------------------