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
20 #include "lldb/lldb-defines.h"
22 namespace lldb_private {
24 template <class T> class ThreadSafeValue {
26 //------------------------------------------------------------------
27 // Constructors and Destructors
28 //------------------------------------------------------------------
29 ThreadSafeValue() : m_value(), m_mutex() {}
31 ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
38 std::lock_guard<std::recursive_mutex> guard(m_mutex);
44 // Call this if you have already manually locked the mutex using the
45 // GetMutex() accessor
46 const T &GetValueNoLock() const { return m_value; }
48 void SetValue(const T &value) {
49 std::lock_guard<std::recursive_mutex> guard(m_mutex);
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; }
57 std::recursive_mutex &GetMutex() { return m_mutex; }
61 mutable std::recursive_mutex m_mutex;
63 //------------------------------------------------------------------
64 // For ThreadSafeValue only
65 //------------------------------------------------------------------
66 DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
69 } // namespace lldb_private
70 #endif // liblldb_ThreadSafeValue_h_