From 146a8e9916623dc665b191d706289048388fc67d Mon Sep 17 00:00:00 2001 From: Ge Wang Date: Tue, 14 May 2024 05:07:33 -0700 Subject: [PATCH] update event mutex --- src/core/chuck_errmsg.cpp | 31 +++------ src/core/chuck_oo.cpp | 136 +++++++++++++------------------------- src/core/chuck_oo.h | 9 +-- src/core/util_buffers.h | 18 ++--- 4 files changed, 65 insertions(+), 129 deletions(-) diff --git a/src/core/chuck_errmsg.cpp b/src/core/chuck_errmsg.cpp index 52831f81c..5755b2b6d 100644 --- a/src/core/chuck_errmsg.cpp +++ b/src/core/chuck_errmsg.cpp @@ -37,16 +37,13 @@ #include "util_platforms.h" #include "util_string.h" -#ifndef __DISABLE_THREADS__ -#include "util_thread.h" -#endif - #include #include #include #include #include #include +#include // c++11 using namespace std; @@ -75,9 +72,7 @@ static std::string g_error2str = ""; // log globals t_CKINT g_loglevel = CK_LOG_CORE; t_CKINT g_logstack = 0; -#ifndef __DISABLE_THREADS__ -XMutex g_logmutex; -#endif +std::mutex g_logmutex; // more local globals std::stringstream g_stdout_stream; @@ -882,6 +877,9 @@ void EM_print2magenta( const char * message, ... ) //----------------------------------------------------------------------------- void EM_log( t_CKINT level, const char * message, ... ) { + // 1.5.2.5 (ge) added + std::lock_guard lock(g_logmutex); + va_list ap; if( level > CK_LOG_ALL ) level = CK_LOG_ALL; @@ -890,10 +888,6 @@ void EM_log( t_CKINT level, const char * message, ... ) // check level if( level > g_loglevel ) return; - #ifndef __DISABLE_THREADS__ - g_logmutex.acquire(); - #endif - TC::off(); CK_FPRINTF_STDERR( "[%s:%s:%s]: ", TC::blue("chuck", true).c_str(), @@ -911,10 +905,6 @@ void EM_log( t_CKINT level, const char * message, ... ) CK_FPRINTF_STDERR( "\n" ); CK_FFLUSH_STDERR(); - - #ifndef __DISABLE_THREADS__ - g_logmutex.release(); - #endif } @@ -926,6 +916,9 @@ void EM_log( t_CKINT level, const char * message, ... ) //----------------------------------------------------------------------------- void EM_log_opts( t_CKINT level, enum em_LogOpts options, const char * message, ... ) { + // 1.5.2.5 (ge) added + std::lock_guard lock(g_logmutex); + va_list ap; if( level > CK_LOG_ALL ) level = CK_LOG_ALL; @@ -939,10 +932,6 @@ void EM_log_opts( t_CKINT level, enum em_LogOpts options, const char * message, // whether to print newline at end t_CKBOOL nl = !(options & EM_LOG_NO_NEWLINE); - #ifndef __DISABLE_THREADS__ - g_logmutex.acquire(); - #endif - // check option if( prefix ) { @@ -967,10 +956,6 @@ void EM_log_opts( t_CKINT level, enum em_LogOpts options, const char * message, // flush CK_FFLUSH_STDERR(); - - #ifndef __DISABLE_THREADS__ - g_logmutex.release(); - #endif } diff --git a/src/core/chuck_oo.cpp b/src/core/chuck_oo.cpp index e2559ebc4..55ae385dc 100644 --- a/src/core/chuck_oo.cpp +++ b/src/core/chuck_oo.cpp @@ -3103,19 +3103,19 @@ t_CKUINT Chuck_Event::our_waiting_on = 0; //----------------------------------------------------------------------------- void Chuck_Event::signal_local() { - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + + // if queue not empty if( !m_queue.empty() ) { // get the shred on top of the queue Chuck_VM_Shred * shred = m_queue.front(); // pop the top m_queue.pop(); - // release it! - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif + // release lock + m_queue_lock.unlock(); + // REFACTOR-2017: BUG-FIX // release the extra ref we added when we started waiting for this event CK_SAFE_RELEASE( shred->event ); @@ -3129,12 +3129,6 @@ void Chuck_Event::signal_local() t_CKTIME *& sp = (t_CKTIME *&)shred->reg->sp; push_( sp, shreduler->now_system ); } - else - { - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif - } } @@ -3146,13 +3140,13 @@ void Chuck_Event::signal_local() //----------------------------------------------------------------------------- t_CKBOOL Chuck_Event::remove( Chuck_VM_Shred * shred ) { + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + + // queue of shred pointers queue temp; t_CKBOOL removed = FALSE; - // lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif // while something in queue while( !m_queue.empty() ) { @@ -3181,10 +3175,6 @@ t_CKBOOL Chuck_Event::remove( Chuck_VM_Shred * shred ) // copy temp back to queue m_queue = temp; - // release lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif return removed; } @@ -3266,13 +3256,12 @@ void Chuck_Event::global_listen( t_CKINT id, void (* cb)(t_CKINT), //----------------------------------------------------------------------------- t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) { + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + // temp queue std::queue temp; t_CKBOOL removed = FALSE; - // lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif // while something in queue while( !m_global_queue.empty() ) { @@ -3294,10 +3283,6 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) // copy temp back to queue m_global_queue = temp; - // release lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif return removed; } @@ -3311,13 +3296,12 @@ t_CKBOOL Chuck_Event::remove_listen( void (* cb)(void) ) //----------------------------------------------------------------------------- t_CKBOOL Chuck_Event::remove_listen( std::string name, void (* cb)(const char *) ) { + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + std::queue temp; t_CKBOOL removed = FALSE; - // lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif // while something in queue while( !m_global_queue.empty() ) { @@ -3339,10 +3323,6 @@ t_CKBOOL Chuck_Event::remove_listen( std::string name, void (* cb)(const char *) // copy temp back to queue m_global_queue = temp; - // release lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif return removed; } @@ -3356,13 +3336,12 @@ t_CKBOOL Chuck_Event::remove_listen( std::string name, void (* cb)(const char *) //----------------------------------------------------------------------------- t_CKBOOL Chuck_Event::remove_listen( t_CKINT id, void (* cb)(t_CKINT) ) { + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + // temp queue of listeners std::queue temp; t_CKBOOL removed = FALSE; - // lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif // while something in queue while( !m_global_queue.empty() ) { @@ -3384,10 +3363,6 @@ t_CKBOOL Chuck_Event::remove_listen( t_CKINT id, void (* cb)(t_CKINT) ) // copy temp back to queue m_global_queue = temp; - // release lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif return removed; } @@ -3401,10 +3376,10 @@ t_CKBOOL Chuck_Event::remove_listen( t_CKINT id, void (* cb)(t_CKINT) ) //----------------------------------------------------------------------------- void Chuck_Event::signal_global() { - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + // if global queue not empty if( !m_global_queue.empty() ) { // get the listener on top of the queue @@ -3439,10 +3414,6 @@ void Chuck_Event::signal_global() m_global_queue.push( listener ); } } - - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif } @@ -3454,11 +3425,12 @@ void Chuck_Event::signal_global() //----------------------------------------------------------------------------- void Chuck_Event::broadcast_global() { - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + // queue of event listeners std::queue< Chuck_Global_Event_Listener > call_again; + // check globals queue while( !m_global_queue.empty() ) { // get the listener on top of the queue @@ -3496,10 +3468,6 @@ void Chuck_Event::broadcast_global() // for those that should be called again, store them again m_global_queue = call_again; - - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif } @@ -3513,27 +3481,22 @@ void Chuck_Event::broadcast_global() //----------------------------------------------------------------------------- void Chuck_Event::queue_broadcast( CBufferSimple * event_buffer ) { + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); // TODO: handle multiple VM - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif + + // if not empty if( !m_queue.empty() ) { // get shred (only to get the VM ref) Chuck_VM_Shred * shred = m_queue.front(); + // release lock - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif + m_queue_lock.unlock(); + // queue the event on the vm (added 1.3.0.0: event_buffer) shred->vm_ref->queue_event( this, 1, event_buffer ); } - else - { - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif - } } @@ -3546,28 +3509,21 @@ void Chuck_Event::queue_broadcast( CBufferSimple * event_buffer ) //----------------------------------------------------------------------------- void Chuck_Event::broadcast_local() { - // lock queue - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); + // while not empty while( !m_queue.empty() ) { // release first - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif + m_queue_lock.unlock(); + // signal the next shred this->signal_local(); + // lock again - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif + m_queue_lock.lock(); } - // release - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif } @@ -3592,14 +3548,12 @@ void Chuck_Event::wait( Chuck_VM_Shred * shred, Chuck_VM * vm ) // suspend shred->is_running = FALSE; + // 1.5.2.5 (ge) updated to std::mutex + std::lock_guard lock(m_queue_lock); // add to waiting list - #ifndef __DISABLE_THREADS__ - m_queue_lock.acquire(); - #endif m_queue.push( shred ); - #ifndef __DISABLE_THREADS__ - m_queue_lock.release(); - #endif + // release + m_queue_lock.unlock(); // add event to shred assert( shred->event == NULL ); diff --git a/src/core/chuck_oo.h b/src/core/chuck_oo.h index 3f2af3f31..82d8d2852 100644 --- a/src/core/chuck_oo.h +++ b/src/core/chuck_oo.h @@ -36,14 +36,12 @@ #include "chuck_def.h" #include "chuck_carrier.h" -#ifndef __DISABLE_THREADS__ -#include "util_thread.h" // added 1.3.0.0 -#endif #include #include #include #include +#include // c++11 @@ -790,11 +788,10 @@ struct Chuck_Event : public Chuck_Object std::queue m_queue; std::queue m_global_queue; - #ifndef __DISABLE_THREADS__ // 1.4.1.0 (ge/jack) TODO: rewrite queue_broadcast to use a lock-free queue // and avoid the use of a lock in events - XMutex m_queue_lock; - #endif + // 1.5.2.5 (ge) updated from XMutex to std::mutex + std::mutex m_queue_lock; }; diff --git a/src/core/util_buffers.h b/src/core/util_buffers.h index 444bd5ad7..1e3679149 100644 --- a/src/core/util_buffers.h +++ b/src/core/util_buffers.h @@ -40,8 +40,8 @@ #include #include #include -#include // added 1.5.2.5 (ge) for "lock-free" circle buffer -#include // added 1.5.2.5 (ge) hmm so much for "lock-free"... +#include // added 1.5.2.5 (ge) for "lock-free" circle buffer | c++11 +#include // added 1.5.2.5 (ge) hmm so much for "lock-free"... | c++11 #define DWORD__ t_CKUINT #define SINT__ t_CKINT @@ -385,7 +385,7 @@ class XCircleBuffer // num elements std::atomic_ulong m_numElements; // mutex | 1.5.2.5 (ge) - // std::mutex m_mutex; + std::mutex m_mutex; // TODO necessary? review code }; @@ -431,7 +431,7 @@ template void XCircleBuffer::init( long length ) { // 1.5.2.5 (ge) added - // std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // clean up is necessary if( m_buffer ) @@ -497,7 +497,7 @@ template void XCircleBuffer::clear() { // 1.5.2.5 (ge) added - // std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // zero out m_readIndex = m_writeIndex = m_numElements = 0; @@ -563,7 +563,7 @@ template void XCircleBuffer::put( const T & item ) { // 1.5.2.5 (ge) added - // std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // sanity check if( m_buffer == NULL ) return; @@ -622,7 +622,7 @@ template long XCircleBuffer::peek( T * array, long numItems, unsigned long stride ) { // 1.5.2.5 (ge) added - // std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // sanity check if( m_buffer == NULL ) return 0; @@ -676,7 +676,7 @@ template long XCircleBuffer::pop( long numItems ) { // 1.5.2.5 (ge) added - // std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // sanity check if( m_buffer == NULL ) return 0; @@ -707,7 +707,7 @@ template bool XCircleBuffer::get( T * result ) { // 1.5.2.5 (ge) added - // std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // sanity check if( m_buffer == NULL || m_readIndex == m_writeIndex ) return false;