1 //===-- ThreadSafeValue.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ThreadSafeValue_h_
10 #define liblldb_ThreadSafeValue_h_
15 #include "lldb/lldb-defines.h"
17 namespace lldb_private {
19 template <class T> class ThreadSafeValue {
21 // Constructors and Destructors
22 ThreadSafeValue() : m_value(), m_mutex() {}
24 ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
31 std::lock_guard<std::recursive_mutex> guard(m_mutex);
37 // Call this if you have already manually locked the mutex using the
38 // GetMutex() accessor
39 const T &GetValueNoLock() const { return m_value; }
41 void SetValue(const T &value) {
42 std::lock_guard<std::recursive_mutex> guard(m_mutex);
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; }
50 std::recursive_mutex &GetMutex() { return m_mutex; }
54 mutable std::recursive_mutex m_mutex;
56 // For ThreadSafeValue only
57 DISALLOW_COPY_AND_ASSIGN(ThreadSafeValue);
60 } // namespace lldb_private
61 #endif // liblldb_ThreadSafeValue_h_