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_
17 // Other libraries and framework includes
19 #include "lldb/lldb-defines.h"
20 #include "lldb/Host/Mutex.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 //------------------------------------------------------------------
36 m_mutex (Mutex::eMutexTypeRecursive)
47 Mutex::Locker locker(m_mutex);
48 return m_collection.empty();
54 Mutex::Locker locker(m_mutex);
55 return m_collection.clear();
59 Erase (const _Key& key)
61 Mutex::Locker locker(m_mutex);
62 return EraseNoLock (key);
66 EraseNoLock (const _Key& key)
68 return m_collection.erase (key);
72 GetValueForKey (const _Key& key, _Tp &value) const
74 Mutex::Locker locker(m_mutex);
75 return GetValueForKeyNoLock (key, value);
78 // Call this if you have already manually locked the mutex using the
79 // GetMutex() accessor
81 GetValueForKeyNoLock (const _Key& key, _Tp &value) const
83 const_iterator pos = m_collection.find(key);
84 if (pos != m_collection.end())
93 GetFirstKeyForValue (const _Tp &value, _Key& key) const
95 Mutex::Locker locker(m_mutex);
96 return GetFirstKeyForValueNoLock (value, key);
100 GetFirstKeyForValueNoLock (const _Tp &value, _Key& key) const
102 const_iterator pos, end = m_collection.end();
103 for (pos = m_collection.begin(); pos != end; ++pos)
105 if (pos->second == value)
115 LowerBound (const _Key& key,
118 bool decrement_if_not_equal) const
120 Mutex::Locker locker(m_mutex);
121 return LowerBoundNoLock (key, match_key, match_value, decrement_if_not_equal);
125 LowerBoundNoLock (const _Key& key,
128 bool decrement_if_not_equal) const
130 const_iterator pos = m_collection.lower_bound (key);
131 if (pos != m_collection.end())
133 match_key = pos->first;
134 if (decrement_if_not_equal && key != match_key && pos != m_collection.begin())
137 match_key = pos->first;
139 match_value = pos->second;
146 lower_bound_unsafe (const _Key& key)
148 return m_collection.lower_bound (key);
152 SetValueForKey (const _Key& key, const _Tp &value)
154 Mutex::Locker locker(m_mutex);
155 SetValueForKeyNoLock (key, value);
158 // Call this if you have already manually locked the mutex using the
159 // GetMutex() accessor
161 SetValueForKeyNoLock (const _Key& key, const _Tp &value)
163 m_collection[key] = value;
173 collection m_collection;
174 mutable Mutex m_mutex;
176 //------------------------------------------------------------------
177 // For ThreadSafeSTLMap only
178 //------------------------------------------------------------------
179 DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLMap);
183 } // namespace lldb_private
185 #endif // liblldb_ThreadSafeSTLMap_h_