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_
16 #include "lldb/lldb-defines.h"
18 namespace lldb_private {
20 template <class T> class ThreadSafeValue {
22 //------------------------------------------------------------------
23 // Constructors and Destructors
24 //------------------------------------------------------------------
25 ThreadSafeValue() : m_value(), m_mutex() {}
27 ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
34 std::lock_guard<std::recursive_mutex> guard(m_mutex);
40 // Call this if you have already manually locked the mutex using the
41 // GetMutex() accessor
42 const T &GetValueNoLock() const { return m_value; }
44 void SetValue(const T &value) {
45 std::lock_guard<std::recursive_mutex> guard(m_mutex);
49 // Call this if you have already manually locked the mutex using the
50 // GetMutex() accessor
51 void SetValueNoLock(const T &value) { m_value = value; }
53 std::recursive_mutex &GetMutex() { return m_mutex; }
57 mutable std::recursive_mutex m_mutex;
59 //------------------------------------------------------------------
60 // For ThreadSafeValue only
61 //------------------------------------------------------------------
62 DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
65 } // namespace lldb_private
66 #endif // liblldb_ThreadSafeValue_h_