]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Host/windows/Mutex.cpp
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / source / Host / windows / Mutex.cpp
1 //===-- Mutex.cpp -----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/Mutex.h"
11 #include "lldb/Host/Host.h"
12 #include "lldb/Host/windows/windows.h"
13
14 #include <string.h>
15 #include <stdio.h>
16
17 #if 0
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.
21 #include <cstdio>
22 #define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
23 #else
24 #define DEBUG_LOG(fmt, ...)
25 #endif
26
27 using namespace lldb_private;
28
29 //----------------------------------------------------------------------
30 // Default constructor.
31 //
32 // Creates a pthread mutex with no attributes.
33 //----------------------------------------------------------------------
34 Mutex::Mutex () :
35     m_mutex()
36 {
37     m_mutex = static_cast<PCRITICAL_SECTION>(malloc(sizeof(CRITICAL_SECTION)));
38     InitializeCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
39 }
40
41 //----------------------------------------------------------------------
42 // Default constructor.
43 //
44 // Creates a pthread mutex with "type" as the mutex type.
45 //----------------------------------------------------------------------
46 Mutex::Mutex (Mutex::Type type) :
47     m_mutex()
48 {
49     m_mutex = static_cast<PCRITICAL_SECTION>(malloc(sizeof(CRITICAL_SECTION)));
50     InitializeCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
51 }
52
53 //----------------------------------------------------------------------
54 // Destructor.
55 //
56 // Destroys the mutex owned by this object.
57 //----------------------------------------------------------------------
58 Mutex::~Mutex()
59 {
60     DeleteCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
61     free(m_mutex);
62 }
63
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
67 // available.
68 //
69 // RETURNS
70 //  The error code from the pthread_mutex_lock() function call.
71 //----------------------------------------------------------------------
72 int
73 Mutex::Lock()
74 {
75     DEBUG_LOG ("[%4.4" PRIx64 "/%4.4" PRIx64 "] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), m_mutex);
76
77     EnterCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
78     return 0;
79 }
80
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.
85 //
86 // RETURNS
87 //  The error code from the pthread_mutex_trylock() function call.
88 //----------------------------------------------------------------------
89 int
90 Mutex::TryLock(const char *failure_message)
91 {
92     return TryEnterCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex)) == 0;
93 }
94
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
99 // behavior.
100 //
101 // RETURNS
102 //  The error code from the pthread_mutex_unlock() function call.
103 //----------------------------------------------------------------------
104 int
105 Mutex::Unlock()
106 {
107     LeaveCriticalSection(static_cast<PCRITICAL_SECTION>(m_mutex));
108     return 0;
109 }