1 //===-- ThreadSafeSTLMap.h --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ThreadSafeSTLMap_h_
10 #define liblldb_ThreadSafeSTLMap_h_
15 #include "lldb/lldb-defines.h"
17 namespace lldb_private {
19 template <typename _Key, typename _Tp> class ThreadSafeSTLMap {
21 typedef std::map<_Key, _Tp> collection;
22 typedef typename collection::iterator iterator;
23 typedef typename collection::const_iterator const_iterator;
24 // Constructors and Destructors
25 ThreadSafeSTLMap() : m_collection(), m_mutex() {}
27 ~ThreadSafeSTLMap() {}
29 bool IsEmpty() const {
30 std::lock_guard<std::recursive_mutex> guard(m_mutex);
31 return m_collection.empty();
35 std::lock_guard<std::recursive_mutex> guard(m_mutex);
36 return m_collection.clear();
39 size_t Erase(const _Key &key) {
40 std::lock_guard<std::recursive_mutex> guard(m_mutex);
41 return EraseNoLock(key);
44 size_t EraseNoLock(const _Key &key) { return m_collection.erase(key); }
46 bool GetValueForKey(const _Key &key, _Tp &value) const {
47 std::lock_guard<std::recursive_mutex> guard(m_mutex);
48 return GetValueForKeyNoLock(key, value);
51 // Call this if you have already manually locked the mutex using the
52 // GetMutex() accessor
53 bool GetValueForKeyNoLock(const _Key &key, _Tp &value) const {
54 const_iterator pos = m_collection.find(key);
55 if (pos != m_collection.end()) {
62 bool GetFirstKeyForValue(const _Tp &value, _Key &key) const {
63 std::lock_guard<std::recursive_mutex> guard(m_mutex);
64 return GetFirstKeyForValueNoLock(value, key);
67 bool GetFirstKeyForValueNoLock(const _Tp &value, _Key &key) const {
68 const_iterator pos, end = m_collection.end();
69 for (pos = m_collection.begin(); pos != end; ++pos) {
70 if (pos->second == value) {
78 bool LowerBound(const _Key &key, _Key &match_key, _Tp &match_value,
79 bool decrement_if_not_equal) const {
80 std::lock_guard<std::recursive_mutex> guard(m_mutex);
81 return LowerBoundNoLock(key, match_key, match_value,
82 decrement_if_not_equal);
85 bool LowerBoundNoLock(const _Key &key, _Key &match_key, _Tp &match_value,
86 bool decrement_if_not_equal) const {
87 const_iterator pos = m_collection.lower_bound(key);
88 if (pos != m_collection.end()) {
89 match_key = pos->first;
90 if (decrement_if_not_equal && key != match_key &&
91 pos != m_collection.begin()) {
93 match_key = pos->first;
95 match_value = pos->second;
101 iterator lower_bound_unsafe(const _Key &key) {
102 return m_collection.lower_bound(key);
105 void SetValueForKey(const _Key &key, const _Tp &value) {
106 std::lock_guard<std::recursive_mutex> guard(m_mutex);
107 SetValueForKeyNoLock(key, value);
110 // Call this if you have already manually locked the mutex using the
111 // GetMutex() accessor
112 void SetValueForKeyNoLock(const _Key &key, const _Tp &value) {
113 m_collection[key] = value;
116 std::recursive_mutex &GetMutex() { return m_mutex; }
119 collection m_collection;
120 mutable std::recursive_mutex m_mutex;
122 // For ThreadSafeSTLMap only
123 DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLMap);
126 } // namespace lldb_private
128 #endif // liblldb_ThreadSafeSTLMap_h_