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>
25 class ThreadSafeSTLMap
28 typedef std::map<_Key,_Tp> collection;
29 typedef typename collection::iterator iterator;
30 typedef typename collection::const_iterator const_iterator;
31 //------------------------------------------------------------------
32 // Constructors and Destructors
33 //------------------------------------------------------------------
34 ThreadSafeSTLMap() : m_collection(), m_mutex() {}
43 std::lock_guard<std::recursive_mutex> guard(m_mutex);
44 return m_collection.empty();
50 std::lock_guard<std::recursive_mutex> guard(m_mutex);
51 return m_collection.clear();
55 Erase(const _Key &key)
57 std::lock_guard<std::recursive_mutex> guard(m_mutex);
58 return EraseNoLock(key);
62 EraseNoLock (const _Key& key)
64 return m_collection.erase (key);
68 GetValueForKey(const _Key &key, _Tp &value) const
70 std::lock_guard<std::recursive_mutex> guard(m_mutex);
71 return GetValueForKeyNoLock(key, value);
74 // Call this if you have already manually locked the mutex using the
75 // GetMutex() accessor
77 GetValueForKeyNoLock (const _Key& key, _Tp &value) const
79 const_iterator pos = m_collection.find(key);
80 if (pos != m_collection.end())
89 GetFirstKeyForValue(const _Tp &value, _Key &key) const
91 std::lock_guard<std::recursive_mutex> guard(m_mutex);
92 return GetFirstKeyForValueNoLock(value, key);
96 GetFirstKeyForValueNoLock (const _Tp &value, _Key& key) const
98 const_iterator pos, end = m_collection.end();
99 for (pos = m_collection.begin(); pos != end; ++pos)
101 if (pos->second == value)
111 LowerBound(const _Key &key, _Key &match_key, _Tp &match_value, bool decrement_if_not_equal) const
113 std::lock_guard<std::recursive_mutex> guard(m_mutex);
114 return LowerBoundNoLock(key, match_key, match_value, decrement_if_not_equal);
118 LowerBoundNoLock (const _Key& key,
121 bool decrement_if_not_equal) const
123 const_iterator pos = m_collection.lower_bound (key);
124 if (pos != m_collection.end())
126 match_key = pos->first;
127 if (decrement_if_not_equal && key != match_key && pos != m_collection.begin())
130 match_key = pos->first;
132 match_value = pos->second;
139 lower_bound_unsafe (const _Key& key)
141 return m_collection.lower_bound (key);
145 SetValueForKey(const _Key &key, const _Tp &value)
147 std::lock_guard<std::recursive_mutex> guard(m_mutex);
148 SetValueForKeyNoLock(key, value);
151 // Call this if you have already manually locked the mutex using the
152 // GetMutex() accessor
154 SetValueForKeyNoLock (const _Key& key, const _Tp &value)
156 m_collection[key] = value;
159 std::recursive_mutex &
166 collection m_collection;
167 mutable std::recursive_mutex m_mutex;
169 //------------------------------------------------------------------
170 // For ThreadSafeSTLMap only
171 //------------------------------------------------------------------
172 DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLMap);
176 } // namespace lldb_private
178 #endif // liblldb_ThreadSafeSTLMap_h_