Don't expose the pthread_mutex_t underlying the Mutex & Mutex::Locker classes.

No one was using it and Locker(pthread_mutex_t *) immediately asserts for 
pthread_mutex_t's that don't come from a Mutex anyway.  Rather than try to make
that work, we should maintain the Mutex abstraction and not pass around the
platform implementation...

Make Mutex::Locker::Lock take a Mutex & or a Mutex *, and remove the constructor
taking a pthread_mutex_t *.  You no longer need to call Mutex::GetMutex to pass
your mutex to a Locker (you can't in fact, since I made it private.)

llvm-svn: 156221
This commit is contained in:
Jim Ingham 2012-05-04 23:02:50 +00:00
parent e326ed33a8
commit 10ebffa48a
17 changed files with 74 additions and 72 deletions

View File

@ -13,6 +13,7 @@
#include <pthread.h>
#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

View File

@ -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&);
};

View File

@ -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)

View File

@ -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

View File

@ -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);
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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())

View File

@ -223,5 +223,5 @@ BreakpointList::ClearAllBreakpointSites ()
void
BreakpointList::GetListMutex (Mutex::Locker &locker)
{
return locker.Lock (m_mutex.GetMutex());
return locker.Lock (m_mutex);
}

View File

@ -273,5 +273,5 @@ WatchpointList::RemoveAll ()
void
WatchpointList::GetListMutex (Mutex::Locker &locker)
{
return locker.Lock (m_mutex.GetMutex());
return locker.Lock (m_mutex);
}

View File

@ -2839,7 +2839,7 @@ public:
if (use_global_module_list)
{
locker.Lock (Module::GetAllocationModuleCollectionMutex()->GetMutex());
locker.Lock (Module::GetAllocationModuleCollectionMutex());
num_modules = Module::GetNumberAllocatedModules();
}
else

View File

@ -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();

View File

@ -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)

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}

View File

@ -466,8 +466,8 @@ StackFrameList::InvalidateFrames (uint32_t start_idx)
void
StackFrameList::Merge (std::auto_ptr<StackFrameList>& 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);