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/Host/Mutex.h"
21 namespace lldb_private {
23 template <typename _Key, typename _Tp>
24 class ThreadSafeSTLMap
27 typedef std::map<_Key,_Tp> collection;
28 typedef typename collection::iterator iterator;
29 typedef typename collection::const_iterator const_iterator;
30 //------------------------------------------------------------------
31 // Constructors and Destructors
32 //------------------------------------------------------------------
35 m_mutex (Mutex::eMutexTypeRecursive)
46 Mutex::Locker locker(m_mutex);
47 return m_collection.empty();
53 Mutex::Locker locker(m_mutex);
54 return m_collection.clear();
58 Erase (const _Key& key)
60 Mutex::Locker locker(m_mutex);
61 return EraseNoLock (key);
65 EraseNoLock (const _Key& key)
67 return m_collection.erase (key);
71 GetValueForKey (const _Key& key, _Tp &value) const
73 Mutex::Locker locker(m_mutex);
74 return GetValueForKeyNoLock (key, value);
77 // Call this if you have already manually locked the mutex using the
78 // GetMutex() accessor
80 GetValueForKeyNoLock (const _Key& key, _Tp &value) const
82 const_iterator pos = m_collection.find(key);
83 if (pos != m_collection.end())
92 GetFirstKeyForValue (const _Tp &value, _Key& key) const
94 Mutex::Locker locker(m_mutex);
95 return GetFirstKeyForValueNoLock (value, key);
99 GetFirstKeyForValueNoLock (const _Tp &value, _Key& key) const
101 const_iterator pos, end = m_collection.end();
102 for (pos = m_collection.begin(); pos != end; ++pos)
104 if (pos->second == value)
114 LowerBound (const _Key& key,
117 bool decrement_if_not_equal) const
119 Mutex::Locker locker(m_mutex);
120 return LowerBoundNoLock (key, match_key, match_value, decrement_if_not_equal);
124 LowerBoundNoLock (const _Key& key,
127 bool decrement_if_not_equal) const
129 const_iterator pos = m_collection.lower_bound (key);
130 if (pos != m_collection.end())
132 match_key = pos->first;
133 if (decrement_if_not_equal && key != match_key && pos != m_collection.begin())
136 match_key = pos->first;
138 match_value = pos->second;
145 lower_bound_unsafe (const _Key& key)
147 return m_collection.lower_bound (key);
151 SetValueForKey (const _Key& key, const _Tp &value)
153 Mutex::Locker locker(m_mutex);
154 SetValueForKeyNoLock (key, value);
157 // Call this if you have already manually locked the mutex using the
158 // GetMutex() accessor
160 SetValueForKeyNoLock (const _Key& key, const _Tp &value)
162 m_collection[key] = value;
172 collection m_collection;
173 mutable Mutex m_mutex;
175 //------------------------------------------------------------------
176 // For ThreadSafeSTLMap only
177 //------------------------------------------------------------------
178 DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLMap);
182 } // namespace lldb_private
184 #endif // liblldb_ThreadSafeSTLMap_h_