1 //===-- ThreadSafeValue.h ---------------------------------------*- 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 #ifndef liblldb_ThreadSafeValue_h_
11 #define liblldb_ThreadSafeValue_h_
18 // Other libraries and framework includes
21 namespace lldb_private {
23 template <class T> class ThreadSafeValue {
25 //------------------------------------------------------------------
26 // Constructors and Destructors
27 //------------------------------------------------------------------
28 ThreadSafeValue() : m_value(), m_mutex() {}
30 ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
37 std::lock_guard<std::recursive_mutex> guard(m_mutex);
43 // Call this if you have already manually locked the mutex using the
44 // GetMutex() accessor
45 const T &GetValueNoLock() const { return m_value; }
47 void SetValue(const T &value) {
48 std::lock_guard<std::recursive_mutex> guard(m_mutex);
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; }
56 std::recursive_mutex &GetMutex() { return m_mutex; }
60 mutable std::recursive_mutex m_mutex;
62 //------------------------------------------------------------------
63 // For ThreadSafeValue only
64 //------------------------------------------------------------------
65 DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
68 } // namespace lldb_private
69 #endif // liblldb_ThreadSafeValue_h_