]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ThreadSafeValue.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / ThreadSafeValue.h
1 //===-- ThreadSafeValue.h ---------------------------------------*- 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 #ifndef liblldb_ThreadSafeValue_h_
11 #define liblldb_ThreadSafeValue_h_
12
13 // C Includes
14
15 // C++ Includes
16 #include <mutex>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-defines.h"
21
22 namespace lldb_private {
23
24 template <class T> class ThreadSafeValue {
25 public:
26   //------------------------------------------------------------------
27   // Constructors and Destructors
28   //------------------------------------------------------------------
29   ThreadSafeValue() : m_value(), m_mutex() {}
30
31   ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
32
33   ~ThreadSafeValue() {}
34
35   T GetValue() const {
36     T value;
37     {
38       std::lock_guard<std::recursive_mutex> guard(m_mutex);
39       value = m_value;
40     }
41     return value;
42   }
43
44   // Call this if you have already manually locked the mutex using the
45   // GetMutex() accessor
46   const T &GetValueNoLock() const { return m_value; }
47
48   void SetValue(const T &value) {
49     std::lock_guard<std::recursive_mutex> guard(m_mutex);
50     m_value = value;
51   }
52
53   // Call this if you have already manually locked the mutex using the
54   // GetMutex() accessor
55   void SetValueNoLock(const T &value) { m_value = value; }
56
57   std::recursive_mutex &GetMutex() { return m_mutex; }
58
59 private:
60   T m_value;
61   mutable std::recursive_mutex m_mutex;
62
63   //------------------------------------------------------------------
64   // For ThreadSafeValue only
65   //------------------------------------------------------------------
66   DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
67 };
68
69 } // namespace lldb_private
70 #endif // liblldb_ThreadSafeValue_h_