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
16namespace lldb_private {
17
18template <class T> class ThreadSafeValue {
19public:
20 ThreadSafeValue() = default;
21 ThreadSafeValue(const T &value) : m_value(value) {}
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 // coverity[missing_lock]
46 void SetValueNoLock(const T &value) { m_value = value; }
47
48 std::recursive_mutex &GetMutex() { return m_mutex; }
49
50private:
52 mutable std::recursive_mutex m_mutex;
53
54 // For ThreadSafeValue only
56 const ThreadSafeValue &operator=(const ThreadSafeValue &) = delete;
57};
58
59} // namespace lldb_private
60#endif // LLDB_CORE_THREADSAFEVALUE_H
ThreadSafeValue(const ThreadSafeValue &)=delete
void SetValue(const T &value)
const T & GetValueNoLock() const
std::recursive_mutex m_mutex
void SetValueNoLock(const T &value)
std::recursive_mutex & GetMutex()
const ThreadSafeValue & operator=(const ThreadSafeValue &)=delete
A class that represents a running process on the host machine.