Skip to content

Commit 2aa14bb

Browse files
committed
restore threadtools and vstdlib from 12716fd commit
1 parent e4f5549 commit 2aa14bb

File tree

3 files changed

+44
-73
lines changed

3 files changed

+44
-73
lines changed

public/tier0/threadtools.h

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
#pragma once
5353
#pragma warning(push)
5454
#pragma warning(disable:4251)
55+
56+
extern "C"
57+
{
58+
void __declspec(dllimport) __stdcall Sleep( unsigned long );
59+
}
60+
5561
#endif
5662

5763
#ifdef COMPILER_MSVC64
@@ -194,8 +200,6 @@ PLATFORM_INTERFACE bool ReleaseThreadHandle( ThreadHandle_t );
194200

195201
//-----------------------------------------------------------------------------
196202

197-
PLATFORM_INTERFACE void ThreadSleep(unsigned duration = 0);
198-
PLATFORM_INTERFACE void ThreadNanoSleep(unsigned ns);
199203
PLATFORM_INTERFACE ThreadId_t ThreadGetCurrentId();
200204
PLATFORM_INTERFACE ThreadHandle_t ThreadGetCurrentHandle();
201205
PLATFORM_INTERFACE int ThreadGetPriority( ThreadHandle_t hThread = NULL );
@@ -229,10 +233,10 @@ inline void ThreadPause()
229233
{
230234
#if defined( COMPILER_PS3 )
231235
__db16cyc();
232-
#elif defined(__arm__) || defined(__aarch64__)
233-
sched_yield();
234-
#elif defined( COMPILER_GCC )
236+
#elif defined( COMPILER_GCC ) && (defined( __i386__ ) || defined( __x86_64__ ))
235237
__asm __volatile( "pause" );
238+
#elif defined( POSIX )
239+
sched_yield();
236240
#elif defined ( COMPILER_MSVC64 )
237241
_mm_pause();
238242
#elif defined( COMPILER_MSVC32 )
@@ -247,6 +251,36 @@ inline void ThreadPause()
247251
#endif
248252
}
249253

254+
inline void ThreadSleep(unsigned nMilliseconds = 0)
255+
{
256+
if( nMilliseconds == 0 )
257+
{
258+
ThreadPause();
259+
return;
260+
}
261+
262+
#ifdef _WIN32
263+
264+
#ifdef _WIN32_PC
265+
static bool bInitialized = false;
266+
if ( !bInitialized )
267+
{
268+
bInitialized = true;
269+
// Set the timer resolution to 1 ms (default is 10.0, 15.6, 2.5, 1.0 or
270+
// some other value depending on hardware and software) so that we can
271+
// use Sleep( 1 ) to avoid wasting CPU time without missing our frame
272+
// rate.
273+
timeBeginPeriod( 1 );
274+
}
275+
#endif
276+
Sleep( nMilliseconds );
277+
#elif PS3
278+
sys_timer_usleep( nMilliseconds * 1000 );
279+
#elif defined(POSIX)
280+
usleep( nMilliseconds * 1000 );
281+
#endif
282+
}
283+
250284
PLATFORM_INTERFACE bool ThreadJoin( ThreadHandle_t, unsigned timeout = TT_INFINITE );
251285

252286
PLATFORM_INTERFACE void ThreadSetDebugName( ThreadHandle_t hThread, const char *pszName );

tier0/threadtools.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -485,59 +485,6 @@ bool ReleaseThreadHandle( ThreadHandle_t hThread )
485485
//
486486
//-----------------------------------------------------------------------------
487487

488-
void ThreadSleep(unsigned nMilliseconds)
489-
{
490-
#ifdef _WIN32
491-
492-
#ifdef _WIN32_PC
493-
static bool bInitialized = false;
494-
if ( !bInitialized )
495-
{
496-
bInitialized = true;
497-
// Set the timer resolution to 1 ms (default is 10.0, 15.6, 2.5, 1.0 or
498-
// some other value depending on hardware and software) so that we can
499-
// use Sleep( 1 ) to avoid wasting CPU time without missing our frame
500-
// rate.
501-
timeBeginPeriod( 1 );
502-
}
503-
#endif
504-
505-
Sleep( nMilliseconds );
506-
#elif PS3
507-
if( nMilliseconds == 0 )
508-
{
509-
// sys_ppu_thread_yield doesn't seem to function properly, so sleep instead.
510-
// sys_timer_usleep( 60 );
511-
sys_ppu_thread_yield();
512-
}
513-
else
514-
{
515-
sys_timer_usleep( nMilliseconds * 1000 );
516-
}
517-
#elif defined(POSIX)
518-
usleep( nMilliseconds * 1000 );
519-
#endif
520-
}
521-
522-
//-----------------------------------------------------------------------------
523-
void ThreadNanoSleep(unsigned ns)
524-
{
525-
#ifdef _WIN32
526-
// ceil
527-
Sleep( ( ns + 999 ) / 1000 );
528-
#elif PS3
529-
sys_timer_usleep( ns );
530-
#elif defined(POSIX)
531-
struct timespec tm;
532-
tm.tv_sec = 0;
533-
tm.tv_nsec = ns;
534-
nanosleep( &tm, NULL );
535-
#endif
536-
}
537-
538-
539-
//-----------------------------------------------------------------------------
540-
541488
#ifndef ThreadGetCurrentId
542489
ThreadId_t ThreadGetCurrentId()
543490
{

vstdlib/jobthread.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ class CThreadPool : public CRefCounted1<IThreadPool, CRefCountServiceMT>
214214
//-----------------------------------------------------
215215
virtual int YieldWait( CThreadEvent **pEvents, int nEvents, bool bWaitAll = true, unsigned timeout = TT_INFINITE );
216216
virtual int YieldWait( CJob **, int nJobs, bool bWaitAll = true, unsigned timeout = TT_INFINITE );
217-
void Yield( unsigned timeout );
217+
inline void Yield( unsigned timeout )
218+
{
219+
Assert( ThreadInMainThread() );
220+
ThreadSleep( timeout );
221+
}
218222

219223
//-----------------------------------------------------
220224
// Add a native job to the queue (master thread)
@@ -656,20 +660,6 @@ int CThreadPool::YieldWait( CJob **ppJobs, int nJobs, bool bWaitAll, unsigned ti
656660
return YieldWait( handles.Base(), handles.Count(), bWaitAll, timeout);
657661
}
658662

659-
//---------------------------------------------------------
660-
661-
void CThreadPool::Yield( unsigned timeout )
662-
{
663-
// @MULTICORE (toml 10/24/2006): not implemented
664-
Assert( ThreadInMainThread() );
665-
if ( !ThreadInMainThread() )
666-
{
667-
ThreadSleep( timeout );
668-
return;
669-
}
670-
ThreadSleep( timeout );
671-
}
672-
673663
//---------------------------------------------------------
674664
// Add a job to the queue
675665
//---------------------------------------------------------

0 commit comments

Comments
 (0)