1 //===-- Mutex.cpp -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/Mutex.h"
11 #include "lldb/Host/Host.h"
12 #include "lldb/Host/windows/windows.h"
18 // This logging is way too verbose to enable even for a log channel.
19 // This logging can be enabled by changing the "#if 0", but should be
20 // reverted prior to checking in.
22 #define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
24 #define DEBUG_LOG(fmt, ...)
27 using namespace lldb_private;
29 //----------------------------------------------------------------------
30 // Default constructor.
32 // Creates a pthread mutex with no attributes.
33 //----------------------------------------------------------------------
37 m_mutex = static_cast<PCRITICAL_SECTION>(malloc(sizeof(CRITICAL_SECTION)));
38 InitializeCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
41 //----------------------------------------------------------------------
42 // Default constructor.
44 // Creates a pthread mutex with "type" as the mutex type.
45 //----------------------------------------------------------------------
46 Mutex::Mutex (Mutex::Type type) :
49 m_mutex = static_cast<PCRITICAL_SECTION>(malloc(sizeof(CRITICAL_SECTION)));
50 InitializeCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
53 //----------------------------------------------------------------------
56 // Destroys the mutex owned by this object.
57 //----------------------------------------------------------------------
60 DeleteCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
64 //----------------------------------------------------------------------
65 // Locks the mutex owned by this object, if the mutex is already
66 // locked, the calling thread will block until the mutex becomes
70 // The error code from the pthread_mutex_lock() function call.
71 //----------------------------------------------------------------------
75 DEBUG_LOG ("[%4.4" PRIx64 "/%4.4" PRIx64 "] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), m_mutex);
77 EnterCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
81 //----------------------------------------------------------------------
82 // Attempts to lock the mutex owned by this object without blocking.
83 // If the mutex is already locked, TryLock() will not block waiting
84 // for the mutex, but will return an error condition.
87 // The error code from the pthread_mutex_trylock() function call.
88 //----------------------------------------------------------------------
90 Mutex::TryLock(const char *failure_message)
92 return TryEnterCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex)) == 0;
95 //----------------------------------------------------------------------
96 // If the current thread holds the lock on the owned mutex, then
97 // Unlock() will unlock the mutex. Calling Unlock() on this object
98 // that the calling thread does not hold will result in undefined
102 // The error code from the pthread_mutex_unlock() function call.
103 //----------------------------------------------------------------------
107 LeaveCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));