ThreadSafeSTLMap.h revision 24943d2ee8bfaa7cf5893e4709143924157a5c1e
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
17// Other libraries and framework includes
18// Project includes
19#include "lldb/Host/Mutex.h"
20
21namespace lldb_private {
22
23template <typename _Key, typename _Tp>
24class ThreadSafeSTLMap
25{
26public:
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    //------------------------------------------------------------------
33    ThreadSafeSTLMap() :
34        m_collection (),
35        m_mutex (Mutex::eMutexTypeRecursive)
36    {
37    }
38
39    ~ThreadSafeSTLMap()
40    {
41    }
42
43    size_t
44    Erase (const _Key& key)
45    {
46        Mutex::Locker locker(m_mutex);
47        return EraseNoLock (key);
48    }
49
50    size_t
51    EraseNoLock (const _Key& key)
52    {
53        return m_collection.erase (key);
54    }
55
56    bool
57    GetValueForKey (const _Key& key, _Tp &value) const
58    {
59        Mutex::Locker locker(m_mutex);
60        return GetValueForKeyNoLock (key, value);
61    }
62
63    // Call this if you have already manually locked the mutex using the
64    // GetMutex() accessor
65    bool
66    GetValueForKeyNoLock (const _Key& key, _Tp &value) const
67    {
68        const_iterator pos = m_collection.find(key);
69        if (pos != m_collection.end())
70        {
71            value = pos->second;
72            return true;
73        }
74        return false;
75    }
76
77    bool
78    GetFirstKeyForValue (const _Tp &value, _Key& key) const
79    {
80        Mutex::Locker locker(m_mutex);
81        return GetFirstKeyForValueNoLock (value, key);
82    }
83
84    bool
85    GetFirstKeyForValueNoLock (const _Tp &value, _Key& key) const
86    {
87        const_iterator pos, end = m_collection.end();
88        for (pos = m_collection.begin(); pos != end; ++pos)
89        {
90            if (pos->second == value)
91            {
92                key = pos->first;
93                return true;
94            }
95        }
96        return false;
97    }
98
99    bool
100    LowerBound (const _Key& key,
101                _Key& match_key,
102                _Tp &match_value,
103                bool decrement_if_not_equal) const
104    {
105        Mutex::Locker locker(m_mutex);
106        return LowerBoundNoLock (key, match_key, match_value, decrement_if_not_equal);
107    }
108
109    bool
110    LowerBoundNoLock (const _Key& key,
111                      _Key& match_key,
112                      _Tp &match_value,
113                      bool decrement_if_not_equal) const
114    {
115        const_iterator pos = m_collection.lower_bound (key);
116        if (pos != m_collection.end())
117        {
118            match_key = pos->first;
119            if (decrement_if_not_equal && key != match_key && pos != m_collection.begin())
120            {
121                --pos;
122                match_key = pos->first;
123            }
124            match_value = pos->second;
125            return true;
126        }
127        return false;
128    }
129
130    iterator
131    lower_bound_unsafe (const _Key& key)
132    {
133        return m_collection.lower_bound (key);
134    }
135
136    void
137    SetValueForKey (const _Key& key, const _Tp &value)
138    {
139        Mutex::Locker locker(m_mutex);
140        SetValueForKeyNoLock (key, value);
141    }
142
143    // Call this if you have already manually locked the mutex using the
144    // GetMutex() accessor
145    void
146    SetValueForKeyNoLock (const _Key& key, const _Tp &value)
147    {
148        m_collection[key] = value;
149    }
150
151    Mutex &
152    GetMutex ()
153    {
154        return m_mutex;
155    }
156
157private:
158    collection m_collection;
159    mutable Mutex m_mutex;
160
161    //------------------------------------------------------------------
162    // For ThreadSafeSTLMap only
163    //------------------------------------------------------------------
164    DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLMap);
165};
166
167
168} // namespace lldb_private
169
170#endif  // liblldb_ThreadSafeSTLMap_h_
171