]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ThreadSafeValue.h
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / ThreadSafeValue.h
1 //===-- ThreadSafeValue.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef liblldb_ThreadSafeValue_h_
10 #define liblldb_ThreadSafeValue_h_
11
12
13 #include <mutex>
14
15 #include "lldb/lldb-defines.h"
16
17 namespace lldb_private {
18
19 template <class T> class ThreadSafeValue {
20 public:
21   // Constructors and Destructors
22   ThreadSafeValue() : m_value(), m_mutex() {}
23
24   ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
25
26   ~ThreadSafeValue() {}
27
28   T GetValue() const {
29     T value;
30     {
31       std::lock_guard<std::recursive_mutex> guard(m_mutex);
32       value = m_value;
33     }
34     return value;
35   }
36
37   // Call this if you have already manually locked the mutex using the
38   // GetMutex() accessor
39   const T &GetValueNoLock() const { return m_value; }
40
41   void SetValue(const T &value) {
42     std::lock_guard<std::recursive_mutex> guard(m_mutex);
43     m_value = value;
44   }
45
46   // Call this if you have already manually locked the mutex using the
47   // GetMutex() accessor
48   void SetValueNoLock(const T &value) { m_value = value; }
49
50   std::recursive_mutex &GetMutex() { return m_mutex; }
51
52 private:
53   T m_value;
54   mutable std::recursive_mutex m_mutex;
55
56   // For ThreadSafeValue only
57   DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
58 };
59
60 } // namespace lldb_private
61 #endif // liblldb_ThreadSafeValue_h_