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 LLDB_CORE_THREADSAFEVALUE_H
10 #define LLDB_CORE_THREADSAFEVALUE_H
14 #include "lldb/lldb-defines.h"
16 namespace lldb_private {
18 template <class T> class ThreadSafeValue {
20 // Constructors and Destructors
21 ThreadSafeValue() : m_value(), m_mutex() {}
23 ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
30 std::lock_guard<std::recursive_mutex> guard(m_mutex);
36 // Call this if you have already manually locked the mutex using the
37 // GetMutex() accessor
38 const T &GetValueNoLock() const { return m_value; }
40 void SetValue(const T &value) {
41 std::lock_guard<std::recursive_mutex> guard(m_mutex);
45 // Call this if you have already manually locked the mutex using the
46 // GetMutex() accessor
47 void SetValueNoLock(const T &value) { m_value = value; }
49 std::recursive_mutex &GetMutex() { return m_mutex; }
53 mutable std::recursive_mutex m_mutex;
55 // For ThreadSafeValue only
56 ThreadSafeValue(const ThreadSafeValue &) = delete;
57 const ThreadSafeValue &operator=(const ThreadSafeValue &) = delete;
60 } // namespace lldb_private
61 #endif // LLDB_CORE_THREADSAFEVALUE_H