]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Core/ThreadSafeSTLMap.h
MFV r313071:
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Core / ThreadSafeSTLMap.h
1 //===-- ThreadSafeSTLMap.h --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_ThreadSafeSTLMap_h_
11 #define liblldb_ThreadSafeSTLMap_h_
12
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <mutex>
17
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-defines.h"
21
22 namespace lldb_private {
23
24 template <typename _Key, typename _Tp>
25 class ThreadSafeSTLMap
26 {
27 public:
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() {}
35
36     ~ThreadSafeSTLMap()
37     {
38     }
39
40     bool
41     IsEmpty() const
42     {
43         std::lock_guard<std::recursive_mutex> guard(m_mutex);
44         return m_collection.empty();
45     }
46
47     void
48     Clear()
49     {
50         std::lock_guard<std::recursive_mutex> guard(m_mutex);
51         return m_collection.clear();
52     }
53
54     size_t
55     Erase(const _Key &key)
56     {
57         std::lock_guard<std::recursive_mutex> guard(m_mutex);
58         return EraseNoLock(key);
59     }
60
61     size_t
62     EraseNoLock (const _Key& key)
63     {
64         return m_collection.erase (key);
65     }
66
67     bool
68     GetValueForKey(const _Key &key, _Tp &value) const
69     {
70         std::lock_guard<std::recursive_mutex> guard(m_mutex);
71         return GetValueForKeyNoLock(key, value);
72     }
73
74     // Call this if you have already manually locked the mutex using the
75     // GetMutex() accessor
76     bool
77     GetValueForKeyNoLock (const _Key& key, _Tp &value) const
78     {
79         const_iterator pos = m_collection.find(key);
80         if (pos != m_collection.end())
81         {
82             value = pos->second;
83             return true;
84         }
85         return false;
86     }
87
88     bool
89     GetFirstKeyForValue(const _Tp &value, _Key &key) const
90     {
91         std::lock_guard<std::recursive_mutex> guard(m_mutex);
92         return GetFirstKeyForValueNoLock(value, key);
93     }
94
95     bool
96     GetFirstKeyForValueNoLock (const _Tp &value, _Key& key) const
97     {
98         const_iterator pos, end = m_collection.end();
99         for (pos = m_collection.begin(); pos != end; ++pos)
100         {
101             if (pos->second == value)
102             {
103                 key = pos->first;
104                 return true;
105             }
106         }
107         return false;
108     }
109
110     bool
111     LowerBound(const _Key &key, _Key &match_key, _Tp &match_value, bool decrement_if_not_equal) const
112     {
113         std::lock_guard<std::recursive_mutex> guard(m_mutex);
114         return LowerBoundNoLock(key, match_key, match_value, decrement_if_not_equal);
115     }
116
117     bool
118     LowerBoundNoLock (const _Key& key,
119                       _Key& match_key,
120                       _Tp &match_value,
121                       bool decrement_if_not_equal) const
122     {
123         const_iterator pos = m_collection.lower_bound (key);
124         if (pos != m_collection.end())
125         {
126             match_key = pos->first;
127             if (decrement_if_not_equal && key != match_key && pos != m_collection.begin())
128             {
129                 --pos;
130                 match_key = pos->first;
131             }
132             match_value = pos->second;
133             return true;
134         }
135         return false;
136     }
137
138     iterator
139     lower_bound_unsafe (const _Key& key)
140     {
141         return m_collection.lower_bound (key);
142     }
143
144     void
145     SetValueForKey(const _Key &key, const _Tp &value)
146     {
147         std::lock_guard<std::recursive_mutex> guard(m_mutex);
148         SetValueForKeyNoLock(key, value);
149     }
150
151     // Call this if you have already manually locked the mutex using the
152     // GetMutex() accessor
153     void
154     SetValueForKeyNoLock (const _Key& key, const _Tp &value)
155     {
156         m_collection[key] = value;
157     }
158
159     std::recursive_mutex &
160     GetMutex()
161     {
162         return m_mutex;
163     }
164
165 private:
166     collection m_collection;
167     mutable std::recursive_mutex m_mutex;
168
169     //------------------------------------------------------------------
170     // For ThreadSafeSTLMap only
171     //------------------------------------------------------------------
172     DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLMap);
173 };
174
175
176 } // namespace lldb_private
177
178 #endif  // liblldb_ThreadSafeSTLMap_h_