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_
16 #include "lldb/lldb-defines.h"
18 namespace lldb_private {
20 template <typename _Key, typename _Tp> class ThreadSafeSTLMap {
22 typedef std::map<_Key, _Tp> collection;
23 typedef typename collection::iterator iterator;
24 typedef typename collection::const_iterator const_iterator;
25 //------------------------------------------------------------------
26 // Constructors and Destructors
27 //------------------------------------------------------------------
28 ThreadSafeSTLMap() : m_collection(), m_mutex() {}
30 ~ThreadSafeSTLMap() {}
32 bool IsEmpty() const {
33 std::lock_guard<std::recursive_mutex> guard(m_mutex);
34 return m_collection.empty();
38 std::lock_guard<std::recursive_mutex> guard(m_mutex);
39 return m_collection.clear();
42 size_t Erase(const _Key &key) {
43 std::lock_guard<std::recursive_mutex> guard(m_mutex);
44 return EraseNoLock(key);
47 size_t EraseNoLock(const _Key &key) { return m_collection.erase(key); }
49 bool GetValueForKey(const _Key &key, _Tp &value) const {
50 std::lock_guard<std::recursive_mutex> guard(m_mutex);
51 return GetValueForKeyNoLock(key, value);
54 // Call this if you have already manually locked the mutex using the
55 // GetMutex() accessor
56 bool GetValueForKeyNoLock(const _Key &key, _Tp &value) const {
57 const_iterator pos = m_collection.find(key);
58 if (pos != m_collection.end()) {
65 bool GetFirstKeyForValue(const _Tp &value, _Key &key) const {
66 std::lock_guard<std::recursive_mutex> guard(m_mutex);
67 return GetFirstKeyForValueNoLock(value, key);
70 bool GetFirstKeyForValueNoLock(const _Tp &value, _Key &key) const {
71 const_iterator pos, end = m_collection.end();
72 for (pos = m_collection.begin(); pos != end; ++pos) {
73 if (pos->second == value) {
81 bool LowerBound(const _Key &key, _Key &match_key, _Tp &match_value,
82 bool decrement_if_not_equal) const {
83 std::lock_guard<std::recursive_mutex> guard(m_mutex);
84 return LowerBoundNoLock(key, match_key, match_value,
85 decrement_if_not_equal);
88 bool LowerBoundNoLock(const _Key &key, _Key &match_key, _Tp &match_value,
89 bool decrement_if_not_equal) const {
90 const_iterator pos = m_collection.lower_bound(key);
91 if (pos != m_collection.end()) {
92 match_key = pos->first;
93 if (decrement_if_not_equal && key != match_key &&
94 pos != m_collection.begin()) {
96 match_key = pos->first;
98 match_value = pos->second;
104 iterator lower_bound_unsafe(const _Key &key) {
105 return m_collection.lower_bound(key);
108 void SetValueForKey(const _Key &key, const _Tp &value) {
109 std::lock_guard<std::recursive_mutex> guard(m_mutex);
110 SetValueForKeyNoLock(key, value);
113 // Call this if you have already manually locked the mutex using the
114 // GetMutex() accessor
115 void SetValueForKeyNoLock(const _Key &key, const _Tp &value) {
116 m_collection[key] = value;
119 std::recursive_mutex &GetMutex() { return m_mutex; }
122 collection m_collection;
123 mutable std::recursive_mutex m_mutex;
125 //------------------------------------------------------------------
126 // For ThreadSafeSTLMap only
127 //------------------------------------------------------------------
128 DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLMap);
131 } // namespace lldb_private
133 #endif // liblldb_ThreadSafeSTLMap_h_