summaryrefslogtreecommitdiff
path: root/include/lldb/Core/ThreadSafeSTLMap.h
blob: cc5a71695ac12f8b4722dfc0cb0784a1b7ca67db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//===-- ThreadSafeSTLMap.h --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_ThreadSafeSTLMap_h_
#define liblldb_ThreadSafeSTLMap_h_

#include <map>
#include <mutex>

#include "lldb/lldb-defines.h"

namespace lldb_private {

template <typename _Key, typename _Tp> class ThreadSafeSTLMap {
public:
  typedef std::map<_Key, _Tp> collection;
  typedef typename collection::iterator iterator;
  typedef typename collection::const_iterator const_iterator;
  //------------------------------------------------------------------
  // Constructors and Destructors
  //------------------------------------------------------------------
  ThreadSafeSTLMap() : m_collection(), m_mutex() {}

  ~ThreadSafeSTLMap() {}

  bool IsEmpty() const {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return m_collection.empty();
  }

  void Clear() {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return m_collection.clear();
  }

  size_t Erase(const _Key &key) {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return EraseNoLock(key);
  }

  size_t EraseNoLock(const _Key &key) { return m_collection.erase(key); }

  bool GetValueForKey(const _Key &key, _Tp &value) const {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return GetValueForKeyNoLock(key, value);
  }

  // Call this if you have already manually locked the mutex using the
  // GetMutex() accessor
  bool GetValueForKeyNoLock(const _Key &key, _Tp &value) const {
    const_iterator pos = m_collection.find(key);
    if (pos != m_collection.end()) {
      value = pos->second;
      return true;
    }
    return false;
  }

  bool GetFirstKeyForValue(const _Tp &value, _Key &key) const {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return GetFirstKeyForValueNoLock(value, key);
  }

  bool GetFirstKeyForValueNoLock(const _Tp &value, _Key &key) const {
    const_iterator pos, end = m_collection.end();
    for (pos = m_collection.begin(); pos != end; ++pos) {
      if (pos->second == value) {
        key = pos->first;
        return true;
      }
    }
    return false;
  }

  bool LowerBound(const _Key &key, _Key &match_key, _Tp &match_value,
                  bool decrement_if_not_equal) const {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return LowerBoundNoLock(key, match_key, match_value,
                            decrement_if_not_equal);
  }

  bool LowerBoundNoLock(const _Key &key, _Key &match_key, _Tp &match_value,
                        bool decrement_if_not_equal) const {
    const_iterator pos = m_collection.lower_bound(key);
    if (pos != m_collection.end()) {
      match_key = pos->first;
      if (decrement_if_not_equal && key != match_key &&
          pos != m_collection.begin()) {
        --pos;
        match_key = pos->first;
      }
      match_value = pos->second;
      return true;
    }
    return false;
  }

  iterator lower_bound_unsafe(const _Key &key) {
    return m_collection.lower_bound(key);
  }

  void SetValueForKey(const _Key &key, const _Tp &value) {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    SetValueForKeyNoLock(key, value);
  }

  // Call this if you have already manually locked the mutex using the
  // GetMutex() accessor
  void SetValueForKeyNoLock(const _Key &key, const _Tp &value) {
    m_collection[key] = value;
  }

  std::recursive_mutex &GetMutex() { return m_mutex; }

private:
  collection m_collection;
  mutable std::recursive_mutex m_mutex;

  //------------------------------------------------------------------
  // For ThreadSafeSTLMap only
  //------------------------------------------------------------------
  DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLMap);
};

} // namespace lldb_private

#endif // liblldb_ThreadSafeSTLMap_h_