2013-08-23 20:44:05 +08:00
|
|
|
//===-- Mutex.cpp -----------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Host/Mutex.h"
|
|
|
|
#include "lldb/Host/Host.h"
|
2013-09-04 21:56:11 +08:00
|
|
|
#include "lldb/Host/windows/windows.h"
|
2013-08-23 20:44:05 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// This logging is way too verbose to enable even for a log channel.
|
|
|
|
// This logging can be enabled by changing the "#if 0", but should be
|
|
|
|
// reverted prior to checking in.
|
|
|
|
#include <cstdio>
|
|
|
|
#define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DEBUG_LOG(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default constructor.
|
|
|
|
//
|
|
|
|
// Creates a pthread mutex with no attributes.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Mutex () :
|
|
|
|
m_mutex()
|
|
|
|
{
|
2013-09-04 21:56:11 +08:00
|
|
|
m_mutex = static_cast<PCRITICAL_SECTION>(malloc(sizeof(CRITICAL_SECTION)));
|
|
|
|
InitializeCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
|
2013-08-23 20:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default constructor.
|
|
|
|
//
|
|
|
|
// Creates a pthread mutex with "type" as the mutex type.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::Mutex (Mutex::Type type) :
|
|
|
|
m_mutex()
|
|
|
|
{
|
2013-09-04 21:56:11 +08:00
|
|
|
m_mutex = static_cast<PCRITICAL_SECTION>(malloc(sizeof(CRITICAL_SECTION)));
|
|
|
|
InitializeCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
|
2013-08-23 20:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Destructor.
|
|
|
|
//
|
|
|
|
// Destroys the mutex owned by this object.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mutex::~Mutex()
|
|
|
|
{
|
2013-09-04 21:56:11 +08:00
|
|
|
DeleteCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
|
|
|
|
free(m_mutex);
|
2013-08-23 20:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Locks the mutex owned by this object, if the mutex is already
|
|
|
|
// locked, the calling thread will block until the mutex becomes
|
|
|
|
// available.
|
|
|
|
//
|
|
|
|
// RETURNS
|
|
|
|
// The error code from the pthread_mutex_lock() function call.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
Mutex::Lock()
|
|
|
|
{
|
2013-09-04 21:56:11 +08:00
|
|
|
DEBUG_LOG ("[%4.4" PRIx64 "/%4.4" PRIx64 "] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), m_mutex);
|
2013-08-23 20:44:05 +08:00
|
|
|
|
2013-09-04 21:56:11 +08:00
|
|
|
EnterCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
|
2013-08-23 20:44:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Attempts to lock the mutex owned by this object without blocking.
|
|
|
|
// If the mutex is already locked, TryLock() will not block waiting
|
|
|
|
// for the mutex, but will return an error condition.
|
|
|
|
//
|
|
|
|
// RETURNS
|
|
|
|
// The error code from the pthread_mutex_trylock() function call.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
Mutex::TryLock(const char *failure_message)
|
|
|
|
{
|
2013-09-04 21:56:11 +08:00
|
|
|
return TryEnterCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex)) == 0;
|
2013-08-23 20:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// If the current thread holds the lock on the owned mutex, then
|
|
|
|
// Unlock() will unlock the mutex. Calling Unlock() on this object
|
|
|
|
// that the calling thread does not hold will result in undefined
|
|
|
|
// behavior.
|
|
|
|
//
|
|
|
|
// RETURNS
|
|
|
|
// The error code from the pthread_mutex_unlock() function call.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int
|
|
|
|
Mutex::Unlock()
|
|
|
|
{
|
2013-09-04 21:56:11 +08:00
|
|
|
LeaveCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
|
2013-08-23 20:44:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|