]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ThreadSafeValue.h
Merge ^/head r317971 through r318379.
[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
21 namespace lldb_private {
22
23 template <class T> class ThreadSafeValue {
24 public:
25   //------------------------------------------------------------------
26   // Constructors and Destructors
27   //------------------------------------------------------------------
28   ThreadSafeValue() : m_value(), m_mutex() {}
29
30   ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
31
32   ~ThreadSafeValue() {}
33
34   T GetValue() const {
35     T value;
36     {
37       std::lock_guard<std::recursive_mutex> guard(m_mutex);
38       value = m_value;
39     }
40     return value;
41   }
42
43   // Call this if you have already manually locked the mutex using the
44   // GetMutex() accessor
45   const T &GetValueNoLock() const { return m_value; }
46
47   void SetValue(const T &value) {
48     std::lock_guard<std::recursive_mutex> guard(m_mutex);
49     m_value = value;
50   }
51
52   // Call this if you have already manually locked the mutex using the
53   // GetMutex() accessor
54   void SetValueNoLock(const T &value) { m_value = value; }
55
56   std::recursive_mutex &GetMutex() { return m_mutex; }
57
58 private:
59   T m_value;
60   mutable std::recursive_mutex m_mutex;
61
62   //------------------------------------------------------------------
63   // For ThreadSafeValue only
64   //------------------------------------------------------------------
65   DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
66 };
67
68 } // namespace lldb_private
69 #endif // liblldb_ThreadSafeValue_h_