LLDB  mainline
ThreadSafeValue.h
Go to the documentation of this file.
1 //===-- ThreadSafeValue.h ---------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_CORE_THREADSAFEVALUE_H
10 #define LLDB_CORE_THREADSAFEVALUE_H
11 
12 #include <mutex>
13 
14 #include "lldb/lldb-defines.h"
15 
16 namespace lldb_private {
17 
18 template <class T> class ThreadSafeValue {
19 public:
20  ThreadSafeValue() = default;
21  ThreadSafeValue(const T &value) : m_value(value), m_mutex() {}
22 
23  ~ThreadSafeValue() = default;
24 
25  T GetValue() const {
26  T value;
27  {
28  std::lock_guard<std::recursive_mutex> guard(m_mutex);
29  value = m_value;
30  }
31  return value;
32  }
33 
34  // Call this if you have already manually locked the mutex using the
35  // GetMutex() accessor
36  const T &GetValueNoLock() const { return m_value; }
37 
38  void SetValue(const T &value) {
39  std::lock_guard<std::recursive_mutex> guard(m_mutex);
40  m_value = value;
41  }
42 
43  // Call this if you have already manually locked the mutex using the
44  // GetMutex() accessor
45  void SetValueNoLock(const T &value) { m_value = value; }
46 
47  std::recursive_mutex &GetMutex() { return m_mutex; }
48 
49 private:
51  mutable std::recursive_mutex m_mutex;
52 
53  // For ThreadSafeValue only
54  ThreadSafeValue(const ThreadSafeValue &) = delete;
55  const ThreadSafeValue &operator=(const ThreadSafeValue &) = delete;
56 };
57 
58 } // namespace lldb_private
59 #endif // LLDB_CORE_THREADSAFEVALUE_H
lldb_private::ThreadSafeValue::GetValueNoLock
const T & GetValueNoLock() const
Definition: ThreadSafeValue.h:36
lldb_private::ThreadSafeValue::GetValue
T GetValue() const
Definition: ThreadSafeValue.h:25
lldb_private::ThreadSafeValue::m_value
T m_value
Definition: ThreadSafeValue.h:50
lldb_private::ThreadSafeValue::operator=
const ThreadSafeValue & operator=(const ThreadSafeValue &)=delete
lldb-defines.h
lldb_private::ThreadSafeValue::m_mutex
std::recursive_mutex m_mutex
Definition: ThreadSafeValue.h:51
lldb_private::ThreadSafeValue::SetValueNoLock
void SetValueNoLock(const T &value)
Definition: ThreadSafeValue.h:45
lldb_private::ThreadSafeValue::GetMutex
std::recursive_mutex & GetMutex()
Definition: ThreadSafeValue.h:47
lldb_private::ThreadSafeValue::~ThreadSafeValue
~ThreadSafeValue()=default
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ThreadSafeValue::ThreadSafeValue
ThreadSafeValue(const T &value)
Definition: ThreadSafeValue.h:21
lldb_private::ThreadSafeValue::SetValue
void SetValue(const T &value)
Definition: ThreadSafeValue.h:38
lldb_private::ThreadSafeValue
Definition: ThreadSafeValue.h:18
lldb_private::ThreadSafeValue::ThreadSafeValue
ThreadSafeValue()=default