1 //===-- ThreadSafeSTLMap.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ThreadSafeSTLMap_h_
11 #define liblldb_ThreadSafeSTLMap_h_
18 // Other libraries and framework includes
20 #include "lldb/lldb-defines.h"
22 namespace lldb_private {
24 template <typename _Key, typename _Tp> class ThreadSafeSTLMap {
26 typedef std::map<_Key, _Tp> collection;
27 typedef typename collection::iterator iterator;
28 typedef typename collection::const_iterator const_iterator;
29 //------------------------------------------------------------------
30 // Constructors and Destructors
31 //------------------------------------------------------------------
32 ThreadSafeSTLMap() : m_collection(), m_mutex() {}
34 ~ThreadSafeSTLMap() {}
36 bool IsEmpty() const {
37 std::lock_guard<std::recursive_mutex> guard(m_mutex);
38 return m_collection.empty();
42 std::lock_guard<std::recursive_mutex> guard(m_mutex);
43 return m_collection.clear();
46 size_t Erase(const _Key &key) {
47 std::lock_guard<std::recursive_mutex> guard(m_mutex);
48 return EraseNoLock(key);
51 size_t EraseNoLock(const _Key &key) { return m_collection.erase(key); }
53 bool GetValueForKey(const _Key &key, _Tp &value) const {
54 std::lock_guard<std::recursive_mutex> guard(m_mutex);
55 return GetValueForKeyNoLock(key, value);
58 // Call this if you have already manually locked the mutex using the
59 // GetMutex() accessor
60 bool GetValueForKeyNoLock(const _Key &key, _Tp &value) const {
61 const_iterator pos = m_collection.find(key);
62 if (pos != m_collection.end()) {
69 bool GetFirstKeyForValue(const _Tp &value, _Key &key) const {
70 std::lock_guard<std::recursive_mutex> guard(m_mutex);
71 return GetFirstKeyForValueNoLock(value, key);
74 bool GetFirstKeyForValueNoLock(const _Tp &value, _Key &key) const {
75 const_iterator pos, end = m_collection.end();
76 for (pos = m_collection.begin(); pos != end; ++pos) {
77 if (pos->second == value) {
85 bool LowerBound(const _Key &key, _Key &match_key, _Tp &match_value,
86 bool decrement_if_not_equal) const {
87 std::lock_guard<std::recursive_mutex> guard(m_mutex);
88 return LowerBoundNoLock(key, match_key, match_value,
89 decrement_if_not_equal);
92 bool LowerBoundNoLock(const _Key &key, _Key &match_key, _Tp &match_value,
93 bool decrement_if_not_equal) const {
94 const_iterator pos = m_collection.lower_bound(key);
95 if (pos != m_collection.end()) {
96 match_key = pos->first;
97 if (decrement_if_not_equal && key != match_key &&
98 pos != m_collection.begin()) {
100 match_key = pos->first;
102 match_value = pos->second;
108 iterator lower_bound_unsafe(const _Key &key) {
109 return m_collection.lower_bound(key);
112 void SetValueForKey(const _Key &key, const _Tp &value) {
113 std::lock_guard<std::recursive_mutex> guard(m_mutex);
114 SetValueForKeyNoLock(key, value);
117 // Call this if you have already manually locked the mutex using the
118 // GetMutex() accessor
119 void SetValueForKeyNoLock(const _Key &key, const _Tp &value) {
120 m_collection[key] = value;
123 std::recursive_mutex &GetMutex() { return m_mutex; }
126 collection m_collection;
127 mutable std::recursive_mutex m_mutex;
129 //------------------------------------------------------------------
130 // For ThreadSafeSTLMap only
131 //------------------------------------------------------------------
132 DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLMap);
135 } // namespace lldb_private
137 #endif // liblldb_ThreadSafeSTLMap_h_