Coverage Report

Created: 2022-01-18 06:27

/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
6.05k
  ThreadSafeValue(const T &value) : m_value(value) {}
22
23
5.80k
  ~ThreadSafeValue() = default;
24
25
1.52M
  T GetValue() const {
26
1.52M
    T value;
27
1.52M
    {
28
1.52M
      std::lock_guard<std::recursive_mutex> guard(m_mutex);
29
1.52M
      value = m_value;
30
1.52M
    }
31
1.52M
    return value;
32
1.52M
  }
33
34
  // Call this if you have already manually locked the mutex using the
35
  // GetMutex() accessor
36
39.7k
  const T &GetValueNoLock() const { return m_value; }
37
38
25.1k
  void SetValue(const T &value) {
39
25.1k
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
40
25.1k
    m_value = value;
41
25.1k
  }
42
43
  // Call this if you have already manually locked the mutex using the
44
  // GetMutex() accessor
45
39.7k
  void SetValueNoLock(const T &value) { m_value = value; }
46
47
39.7k
  std::recursive_mutex &GetMutex() { return m_mutex; }
48
49
private:
50
  T m_value;
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