diff --git a/lldb/include/lldb/Host/Condition.h b/lldb/include/lldb/Host/Condition.h index 5ae50ae3fac3..98439ee2ebdf 100644 --- a/lldb/include/lldb/Host/Condition.h +++ b/lldb/include/lldb/Host/Condition.h @@ -13,6 +13,7 @@ #include +#include "lldb/Host/Mutex.h" namespace lldb_private { @@ -55,15 +56,6 @@ public: int Broadcast (); - //------------------------------------------------------------------ - /// Get accessor to the pthread condition object. - /// - /// @return - /// A pointer to the condition variable owned by this object. - //------------------------------------------------------------------ - pthread_cond_t * - GetCondition (); - //------------------------------------------------------------------ /// Unblocks one thread waiting for the condition variable /// @@ -107,13 +99,22 @@ public: /// @see Condition::Signal() //------------------------------------------------------------------ int - Wait (pthread_mutex_t *mutex, const TimeValue *abstime = NULL, bool *timed_out = NULL); + Wait (Mutex &mutex, const TimeValue *abstime = NULL, bool *timed_out = NULL); protected: //------------------------------------------------------------------ // Member variables //------------------------------------------------------------------ pthread_cond_t m_condition; ///< The condition variable. + + //------------------------------------------------------------------ + /// Get accessor to the pthread condition object. + /// + /// @return + /// A pointer to the condition variable owned by this object. + //------------------------------------------------------------------ + pthread_cond_t * + GetCondition (); }; } // namespace lldb_private diff --git a/lldb/include/lldb/Host/Mutex.h b/lldb/include/lldb/Host/Mutex.h index f361ad7f471b..b848c1512859 100644 --- a/lldb/include/lldb/Host/Mutex.h +++ b/lldb/include/lldb/Host/Mutex.h @@ -23,6 +23,9 @@ namespace lldb_private { class Mutex { public: + friend class Locker; + friend class Condition; + enum Type { eMutexTypeNormal, ///< Mutex that can't recursively entered by the same thread @@ -77,18 +80,6 @@ public: //-------------------------------------------------------------- Locker(Mutex* m); - //-------------------------------------------------------------- - /// Constructor with a raw pthread mutex object pointer. - /// - /// This will create a scoped mutex locking object that locks - /// \a mutex. - /// - /// @param[in] mutex - /// A pointer to a pthread_mutex_t that will get locked if - /// non-NULL. - //-------------------------------------------------------------- - Locker(pthread_mutex_t *mutex); - //-------------------------------------------------------------- /// Desstructor /// @@ -105,7 +96,14 @@ public: /// non-NULL. //-------------------------------------------------------------- void - Lock (pthread_mutex_t *mutex); + Lock (Mutex &mutex); + + void + Lock (Mutex *mutex) + { + if (mutex) + Lock(*mutex); + } //-------------------------------------------------------------- /// Change the contained mutex only if the mutex can be locked. @@ -123,7 +121,16 @@ public: /// returns \b false otherwise. //-------------------------------------------------------------- bool - TryLock (pthread_mutex_t *mutex); + TryLock (Mutex &mutex); + + bool + TryLock (Mutex *mutex) + { + if (mutex) + return TryLock(*mutex); + else + return false; + } void Unlock (); @@ -171,15 +178,6 @@ public: //------------------------------------------------------------------ ~Mutex(); - //------------------------------------------------------------------ - /// Mutex get accessor. - /// - /// @return - /// A pointer to the pthread mutex object owned by this object. - //------------------------------------------------------------------ - pthread_mutex_t * - GetMutex(); - //------------------------------------------------------------------ /// Lock the mutex. /// @@ -234,7 +232,17 @@ protected: // Member variables //------------------------------------------------------------------ pthread_mutex_t m_mutex; ///< The pthread mutex object. + private: + //------------------------------------------------------------------ + /// Mutex get accessor. + /// + /// @return + /// A pointer to the pthread mutex object owned by this object. + //------------------------------------------------------------------ + pthread_mutex_t * + GetMutex(); + Mutex(const Mutex&); const Mutex& operator=(const Mutex&); }; diff --git a/lldb/include/lldb/Host/Predicate.h b/lldb/include/lldb/Host/Predicate.h index 3c52fe1f2412..efa85348cf38 100644 --- a/lldb/include/lldb/Host/Predicate.h +++ b/lldb/include/lldb/Host/Predicate.h @@ -314,7 +314,7 @@ public: while (err == 0 && m_value != value) { - err = m_condition.Wait (m_mutex.GetMutex(), abstime, timed_out); + err = m_condition.Wait (m_mutex, abstime, timed_out); } return m_value == value; @@ -339,7 +339,7 @@ public: while (err == 0 && m_value != wait_value) { - err = m_condition.Wait (m_mutex.GetMutex(), abstime, timed_out); + err = m_condition.Wait (m_mutex, abstime, timed_out); } if (m_value == wait_value) diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp index 7dd4b2ecf908..515fb2869fd7 100644 --- a/lldb/source/API/SBCommandInterpreter.cpp +++ b/lldb/source/API/SBCommandInterpreter.cpp @@ -93,7 +93,7 @@ SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnOb TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); Mutex::Locker api_locker; if (target_sp) - api_locker.Lock(target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock(target_sp->GetAPIMutex()); m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref()); } else @@ -238,7 +238,7 @@ SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &resu TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); Mutex::Locker api_locker; if (target_sp) - api_locker.Lock(target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock(target_sp->GetAPIMutex()); m_opaque_ptr->SourceInitFile (false, result.ref()); } else @@ -263,7 +263,7 @@ SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnOb TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); Mutex::Locker api_locker; if (target_sp) - api_locker.Lock(target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock(target_sp->GetAPIMutex()); m_opaque_ptr->SourceInitFile (true, result.ref()); } else diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp index b052f77ff6ed..46d5118ef510 100644 --- a/lldb/source/API/SBDebugger.cpp +++ b/lldb/source/API/SBDebugger.cpp @@ -308,7 +308,7 @@ SBDebugger::HandleCommand (const char *command) TargetSP target_sp (m_opaque_sp->GetSelectedTarget()); Mutex::Locker api_locker; if (target_sp) - api_locker.Lock(target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock(target_sp->GetAPIMutex()); SBCommandInterpreter sb_interpreter(GetCommandInterpreter ()); SBCommandReturnObject result; @@ -830,7 +830,7 @@ SBDebugger::PushInputReader (SBInputReader &reader) TargetSP target_sp (m_opaque_sp->GetSelectedTarget()); Mutex::Locker api_locker; if (target_sp) - api_locker.Lock(target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock(target_sp->GetAPIMutex()); InputReaderSP reader_sp(*reader); m_opaque_sp->PushInputReader (reader_sp); } diff --git a/lldb/source/API/SBFunction.cpp b/lldb/source/API/SBFunction.cpp index 4f967b7d51a1..7ac447e5f1b1 100644 --- a/lldb/source/API/SBFunction.cpp +++ b/lldb/source/API/SBFunction.cpp @@ -130,7 +130,7 @@ SBFunction::GetInstructions (SBTarget target) TargetSP target_sp (target.GetSP()); if (target_sp) { - api_locker.Lock (target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock (target_sp->GetAPIMutex()); target_sp->CalculateExecutionContext (exe_ctx); exe_ctx.SetProcessSP(target_sp->GetProcessSP()); } diff --git a/lldb/source/API/SBInstruction.cpp b/lldb/source/API/SBInstruction.cpp index 996b01debc01..0adb83e1fd95 100644 --- a/lldb/source/API/SBInstruction.cpp +++ b/lldb/source/API/SBInstruction.cpp @@ -78,7 +78,7 @@ SBInstruction::GetMnemonic(SBTarget target) TargetSP target_sp (target.GetSP()); if (target_sp) { - api_locker.Lock (target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock (target_sp->GetAPIMutex()); target_sp->CalculateExecutionContext (exe_ctx); exe_ctx.SetProcessSP(target_sp->GetProcessSP()); } @@ -97,7 +97,7 @@ SBInstruction::GetOperands(SBTarget target) TargetSP target_sp (target.GetSP()); if (target_sp) { - api_locker.Lock (target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock (target_sp->GetAPIMutex()); target_sp->CalculateExecutionContext (exe_ctx); exe_ctx.SetProcessSP(target_sp->GetProcessSP()); } @@ -116,7 +116,7 @@ SBInstruction::GetComment(SBTarget target) TargetSP target_sp (target.GetSP()); if (target_sp) { - api_locker.Lock (target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock (target_sp->GetAPIMutex()); target_sp->CalculateExecutionContext (exe_ctx); exe_ctx.SetProcessSP(target_sp->GetProcessSP()); } diff --git a/lldb/source/API/SBSymbol.cpp b/lldb/source/API/SBSymbol.cpp index 89dff621dc95..3fd06f84eb12 100644 --- a/lldb/source/API/SBSymbol.cpp +++ b/lldb/source/API/SBSymbol.cpp @@ -126,7 +126,7 @@ SBSymbol::GetInstructions (SBTarget target) TargetSP target_sp (target.GetSP()); if (target_sp) { - api_locker.Lock (target_sp->GetAPIMutex().GetMutex()); + api_locker.Lock (target_sp->GetAPIMutex()); target_sp->CalculateExecutionContext (exe_ctx); } if (m_opaque_ptr->ValueIsAddress()) diff --git a/lldb/source/Breakpoint/BreakpointList.cpp b/lldb/source/Breakpoint/BreakpointList.cpp index 9ec6d8daec82..e1aa2dbcb1e4 100644 --- a/lldb/source/Breakpoint/BreakpointList.cpp +++ b/lldb/source/Breakpoint/BreakpointList.cpp @@ -223,5 +223,5 @@ BreakpointList::ClearAllBreakpointSites () void BreakpointList::GetListMutex (Mutex::Locker &locker) { - return locker.Lock (m_mutex.GetMutex()); + return locker.Lock (m_mutex); } diff --git a/lldb/source/Breakpoint/WatchpointList.cpp b/lldb/source/Breakpoint/WatchpointList.cpp index f95265993ae2..bfd0b4cdc5db 100644 --- a/lldb/source/Breakpoint/WatchpointList.cpp +++ b/lldb/source/Breakpoint/WatchpointList.cpp @@ -273,5 +273,5 @@ WatchpointList::RemoveAll () void WatchpointList::GetListMutex (Mutex::Locker &locker) { - return locker.Lock (m_mutex.GetMutex()); + return locker.Lock (m_mutex); } diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp index 2c2ab436838c..50364327ea46 100644 --- a/lldb/source/Commands/CommandObjectTarget.cpp +++ b/lldb/source/Commands/CommandObjectTarget.cpp @@ -2839,7 +2839,7 @@ public: if (use_global_module_list) { - locker.Lock (Module::GetAllocationModuleCollectionMutex()->GetMutex()); + locker.Lock (Module::GetAllocationModuleCollectionMutex()); num_modules = Module::GetNumberAllocatedModules(); } else diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp index e573555625e6..c5a89554c62d 100644 --- a/lldb/source/Core/ModuleList.cpp +++ b/lldb/source/Core/ModuleList.cpp @@ -118,12 +118,12 @@ ModuleList::RemoveOrphans (bool mandatory) if (mandatory) { - locker.Lock (m_modules_mutex.GetMutex()); + locker.Lock (m_modules_mutex); } else { // Not mandatory, remove orphans if we can get the mutex - if (!locker.TryLock(m_modules_mutex.GetMutex())) + if (!locker.TryLock(m_modules_mutex)) return 0; } collection::iterator pos = m_modules.begin(); diff --git a/lldb/source/Host/common/Condition.cpp b/lldb/source/Host/common/Condition.cpp index 535afcb04397..daa729cadca5 100644 --- a/lldb/source/Host/common/Condition.cpp +++ b/lldb/source/Host/common/Condition.cpp @@ -78,7 +78,7 @@ Condition::Signal () // The current thread re-acquires the lock on "mutex". //---------------------------------------------------------------------- int -Condition::Wait (pthread_mutex_t *mutex, const TimeValue *abstime, bool *timed_out) +Condition::Wait (Mutex &mutex, const TimeValue *abstime, bool *timed_out) { int err = 0; do @@ -86,10 +86,10 @@ Condition::Wait (pthread_mutex_t *mutex, const TimeValue *abstime, bool *timed_o if (abstime && abstime->IsValid()) { struct timespec abstime_ts = abstime->GetAsTimeSpec(); - err = ::pthread_cond_timedwait (&m_condition, mutex, &abstime_ts); + err = ::pthread_cond_timedwait (&m_condition, mutex.GetMutex(), &abstime_ts); } else - err = ::pthread_cond_wait (&m_condition, mutex); + err = ::pthread_cond_wait (&m_condition, mutex.GetMutex()); } while (err == EINTR); if (timed_out != NULL) diff --git a/lldb/source/Host/common/Mutex.cpp b/lldb/source/Host/common/Mutex.cpp index 06908b2344c2..d519baf45b6a 100644 --- a/lldb/source/Host/common/Mutex.cpp +++ b/lldb/source/Host/common/Mutex.cpp @@ -108,7 +108,7 @@ Mutex::Locker::Locker () : Mutex::Locker::Locker (Mutex& m) : m_mutex_ptr(NULL) { - Lock (m.GetMutex()); + Lock (m); } //---------------------------------------------------------------------- @@ -121,18 +121,7 @@ Mutex::Locker::Locker (Mutex* m) : m_mutex_ptr(NULL) { if (m) - Lock (m->GetMutex()); -} - -//---------------------------------------------------------------------- -// Constructor with a raw pthread mutex object pointer. -// -// This will create a scoped mutex locking object that locks "mutex" -//---------------------------------------------------------------------- -Mutex::Locker::Locker (pthread_mutex_t *mutex_ptr) : - m_mutex_ptr (NULL) -{ - Lock (mutex_ptr); + Lock (m); } //---------------------------------------------------------------------- @@ -150,8 +139,10 @@ Mutex::Locker::~Locker () // mutex) and lock the new "mutex" object if it is non-NULL. //---------------------------------------------------------------------- void -Mutex::Locker::Lock (pthread_mutex_t *mutex_ptr) +Mutex::Locker::Lock (Mutex &mutex) { + pthread_mutex_t *mutex_ptr = mutex.GetMutex(); + // We already have this mutex locked or both are NULL... if (m_mutex_ptr == mutex_ptr) return; @@ -176,8 +167,10 @@ Mutex::Locker::Unlock () } bool -Mutex::Locker::TryLock (pthread_mutex_t *mutex_ptr) +Mutex::Locker::TryLock (Mutex &mutex) { + pthread_mutex_t *mutex_ptr = mutex.GetMutex(); + // We already have this mutex locked! if (m_mutex_ptr == mutex_ptr) return m_mutex_ptr != NULL; diff --git a/lldb/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp b/lldb/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp index c1d9f9551598..0b49f1fc2795 100644 --- a/lldb/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp +++ b/lldb/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp @@ -153,7 +153,7 @@ CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packe bool CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker) { - return locker.TryLock (m_sequence_mutex.GetMutex()); + return locker.TryLock (m_sequence_mutex); } diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp index a6d2d114da94..e55328b93e77 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp @@ -264,7 +264,7 @@ GDBRemoteCommunication::GetAck () bool GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker) { - return locker.TryLock (m_sequence_mutex.GetMutex()); + return locker.TryLock (m_sequence_mutex); } diff --git a/lldb/source/Target/StackFrameList.cpp b/lldb/source/Target/StackFrameList.cpp index 3c580bca0d41..84fce4a63451 100644 --- a/lldb/source/Target/StackFrameList.cpp +++ b/lldb/source/Target/StackFrameList.cpp @@ -466,8 +466,8 @@ StackFrameList::InvalidateFrames (uint32_t start_idx) void StackFrameList::Merge (std::auto_ptr& curr_ap, lldb::StackFrameListSP& prev_sp) { - Mutex::Locker curr_locker (curr_ap.get() ? curr_ap->m_mutex.GetMutex() : NULL); - Mutex::Locker prev_locker (prev_sp.get() ? prev_sp->m_mutex.GetMutex() : NULL); + Mutex::Locker curr_locker (curr_ap.get() ? &curr_ap->m_mutex : NULL); + Mutex::Locker prev_locker (prev_sp.get() ? &prev_sp->m_mutex : NULL); #if defined (DEBUG_STACK_FRAMES) StreamFile s(stdout, false);