Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Core/ThreadSafeValue.h
Line
Count
Source
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
5.34k
  ThreadSafeValue(const T &value) : m_value(value) {}
22
23
5.14k
  ~ThreadSafeValue() = default;
24
25
1.36M
  T GetValue() const {
26
1.36M
    T value;
27
1.36M
    {
28
1.36M
      std::lock_guard<std::recursive_mutex> guard(m_mutex);
29
1.36M
      value = m_value;
30
1.36M
    }
31
1.36M
    return value;
32
1.36M
  }
33
34
  // Call this if you have already manually locked the mutex using the
35
  // GetMutex() accessor
36
31.9k
  const T &GetValueNoLock() const { return m_value; }
37
38
19.8k
  void SetValue(const T &value) {
39
19.8k
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
40
19.8k
    m_value = value;
41
19.8k
  }
42
43
  // Call this if you have already manually locked the mutex using the
44
  // GetMutex() accessor
45
  // coverity[missing_lock]
46
31.9k
  void SetValueNoLock(const T &value) { m_value = value; }
47
48
31.9k
  std::recursive_mutex &GetMutex() { return m_mutex; }
49
50
private:
51
  T m_value;
52
  mutable std::recursive_mutex m_mutex;
53
54
  // For ThreadSafeValue only
55
  ThreadSafeValue(const ThreadSafeValue &) = delete;
56
  const ThreadSafeValue &operator=(const ThreadSafeValue &) = delete;
57
};
58
59
} // namespace lldb_private
60
#endif // LLDB_CORE_THREADSAFEVALUE_H