From 7b43a34f371e84d8a91d89cd040cb203b0694e70 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 14 Dec 2012 11:50:20 +0100 Subject: [PATCH] Changed idSysSignal pthread implementation to consider the manualReset parameter --- neo/idlib/Thread.h | 2 + neo/idlib/sys/posix/posix_thread.cpp | 78 ++++++++++++++++++---------- neo/idlib/sys/sys_assert.cpp | 2 +- neo/sys/posix/posix_main.cpp | 11 ++++ 4 files changed, 64 insertions(+), 29 deletions(-) diff --git a/neo/idlib/Thread.h b/neo/idlib/Thread.h index 47bd3fb1..7b1b2308 100644 --- a/neo/idlib/Thread.h +++ b/neo/idlib/Thread.h @@ -154,7 +154,9 @@ private: pthread_mutex_t mutex; pthread_cond_t cond; bool signaled; + int signalCounter; bool waiting; + bool manualReset; idSysSignal( const idSysSignal& s ) {} void operator=( const idSysSignal& s ) {} diff --git a/neo/idlib/sys/posix/posix_thread.cpp b/neo/idlib/sys/posix/posix_thread.cpp index a1f0c054..f69a234c 100644 --- a/neo/idlib/sys/posix/posix_thread.cpp +++ b/neo/idlib/sys/posix/posix_thread.cpp @@ -155,37 +155,49 @@ void Sys_Yield() idSysSignal::idSysSignal( bool manualReset ) { +#if 0 pthread_mutexattr_t attr; - //pthread_mutexattr_init( &attr ); + pthread_mutexattr_init( &attr ); pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK ); - pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_DEFAULT ); + //pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_DEFAULT ); pthread_mutex_init( &mutex, &attr ); pthread_mutexattr_destroy( &attr ); +#else + pthread_mutex_init( &mutex, NULL ); +#endif pthread_cond_init( &cond, NULL ); signaled = false; + signalCounter = 0; waiting = false; + this->manualReset = manualReset; } idSysSignal::~idSysSignal() { pthread_cond_destroy( &cond ); + pthread_mutex_destroy( &mutex ); } void idSysSignal::Raise() { pthread_mutex_lock( &mutex ); - if( waiting ) + //if( waiting ) { - pthread_cond_signal( &cond ); + //pthread_cond_signal( &cond ); + //pthread_cond_broadcast( &cond ); } - else + //else + if( !signaled ) { // emulate Windows behaviour: if no thread is waiting, leave the signal on so next wait keeps going signaled = true; + signalCounter++; + + pthread_cond_signal( &cond ); } pthread_mutex_unlock( &mutex ); @@ -237,6 +249,7 @@ bool idSysSignal::Wait( int timeout ) The mutex was not owned by the current thread at the time of the call. */ +#if 0 assert( !waiting ); // WaitForEvent from multiple threads? that wouldn't be good if( signaled ) { @@ -245,30 +258,39 @@ bool idSysSignal::Wait( int timeout ) result = 0; } else - { - waiting = true; -#if 0 - result = pthread_cond_wait( &cond, &mutex ); -#else - if( timeout == WAIT_INFINITE ) - { - result = pthread_cond_wait( &cond, &mutex ); - - assert( result == 0 ); - } - else - { - timespec ts; - clock_gettime( CLOCK_REALTIME, &ts ); - - ts.tv_nsec += ( timeout * 1000000 ); - - result = pthread_cond_timedwait( &cond, &mutex, &ts ); - - assert( result == 0 || ( timeout != idSysSignal::WAIT_INFINITE && result == ETIMEDOUT ) ); - } #endif - waiting = false; + { + while( !signaled ) + { + waiting = true; +#if 0 + result = pthread_cond_wait( &cond, &mutex ); +#else + if( timeout == WAIT_INFINITE ) + { + result = pthread_cond_wait( &cond, &mutex ); + + assert( result == 0 ); + } + else + { + timespec ts; + clock_gettime( CLOCK_REALTIME, &ts ); + + ts.tv_nsec += ( timeout * 1000000 ); + + result = pthread_cond_timedwait( &cond, &mutex, &ts ); + + assert( result == 0 || ( timeout != idSysSignal::WAIT_INFINITE && result == ETIMEDOUT ) ); + } +#endif + waiting = false; + } + + if( !manualReset ) + { + signaled = false; + } } pthread_mutex_unlock( &mutex ); diff --git a/neo/idlib/sys/sys_assert.cpp b/neo/idlib/sys/sys_assert.cpp index 43458b52..5f1df4cc 100644 --- a/neo/idlib/sys/sys_assert.cpp +++ b/neo/idlib/sys/sys_assert.cpp @@ -86,7 +86,7 @@ bool AssertFailed( const char* file, int line, const char* expression ) #ifdef _WIN32 if( IsDebuggerPresent() || com_assertOutOfDebugger.GetBool() ) #else - if( com_assertOutOfDebugger.GetBool() ) + //if( com_assertOutOfDebugger.GetBool() ) #endif // RB end { diff --git a/neo/sys/posix/posix_main.cpp b/neo/sys/posix/posix_main.cpp index af0121b1..f9261b55 100644 --- a/neo/sys/posix/posix_main.cpp +++ b/neo/sys/posix/posix_main.cpp @@ -247,6 +247,7 @@ Sys_Microseconds */ uint64 Sys_Microseconds() { +#if 1 static uint64 ticksPerMicrosecondTimes1024 = 0; if( ticksPerMicrosecondTimes1024 == 0 ) @@ -256,6 +257,16 @@ uint64 Sys_Microseconds() } return ( ( uint64 )( ( int64 )Sys_GetClockTicks() << 10 ) ) / ticksPerMicrosecondTimes1024; +#else + uint64 curtime; + struct timespec ts; + + clock_gettime( CLOCK_MONOTONIC, &ts ); + + curtime = ts.tv_sec * 1000000 + ts.tv_nsec / 1000; + + return curtime; +#endif } /*