aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Core/ThreadSafeSTLMap.h
blob: 4a885ff1a480fb65023ee163ee739b910da45dd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//===-- ThreadSafeSTLMap.h --------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_ThreadSafeSTLMap_h_
#define liblldb_ThreadSafeSTLMap_h_

// C Includes
// C++ Includes
#include <map>
#include <mutex>

// Other libraries and framework includes
// Project includes
#include "lldb/lldb-defines.h"

namespace lldb_private {

template <typename _Key, typename _Tp>
class ThreadSafeSTLMap
{
public:
    typedef std::map<_Key,_Tp> collection;
    typedef typename collection::iterator iterator;
    typedef typename collection::const_iterator const_iterator;
    //------------------------------------------------------------------
    // Constructors and Destructors
    //------------------------------------------------------------------
    ThreadSafeSTLMap() : m_collection(), m_mutex() {}

    ~ThreadSafeSTLMap()
    {
    }

    bool
    IsEmpty() const
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return m_collection.empty();
    }

    void
    Clear()
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return m_collection.clear();
    }

    size_t
    Erase(const _Key &key)
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return EraseNoLock(key);
    }

    size_t
    EraseNoLock (const _Key& key)
    {
        return m_collection.erase (key);
    }

    bool
    GetValueForKey(const _Key &key, _Tp &value) const
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return GetValueForKeyNoLock(key, value);
    }

    // Call this if you have already manually locked the mutex using the
    // GetMutex() accessor
    bool
    GetValueForKeyNoLock (const _Key& key, _Tp &value) const
    {
        const_iterator pos = m_collection.find(key);
        if (pos != m_collection.end())
        {
            value = pos->second;
            return true;
        }
        return false;
    }

    bool
    GetFirstKeyForValue(const _Tp &value, _Key &key) const
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return GetFirstKeyForValueNoLock(value, key);
    }

    bool
    GetFirstKeyForValueNoLock (const _Tp &value, _Key& key) const
    {
        const_iterator pos, end = m_collection.end();
        for (pos = m_collection.begin(); pos != end; ++pos)
        {
            if (pos->second == value)
            {
                key = pos->first;
                return true;
            }
        }
        return false;
    }

    bool
    LowerBound(const _Key &key, _Key &match_key, _Tp &match_value, bool decrement_if_not_equal) const
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return LowerBoundNoLock(key, match_key, match_value, decrement_if_not_equal);
    }

    bool
    LowerBoundNoLock (const _Key& key,
                      _Key& match_key,
                      _Tp &match_value,
                      bool decrement_if_not_equal) const
    {
        const_iterator pos = m_collection.lower_bound (key);
        if (pos != m_collection.end())
        {
            match_key = pos->first;
            if (decrement_if_not_equal && key != match_key && pos != m_collection.begin())
            {
                --pos;
                match_key = pos->first;
            }
            match_value = pos->second;
            return true;
        }
        return false;
    }

    iterator
    lower_bound_unsafe (const _Key& key)
    {
        return m_collection.lower_bound (key);
    }

    void
    SetValueForKey(const _Key &key, const _Tp &value)
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        SetValueForKeyNoLock(key, value);
    }

    // Call this if you have already manually locked the mutex using the
    // GetMutex() accessor
    void
    SetValueForKeyNoLock (const _Key& key, const _Tp &value)
    {
        m_collection[key] = value;
    }

    std::recursive_mutex &
    GetMutex()
    {
        return m_mutex;
    }

private:
    collection m_collection;
    mutable std::recursive_mutex m_mutex;

    //------------------------------------------------------------------
    // For ThreadSafeSTLMap only
    //------------------------------------------------------------------
    DISALLOW_COPY_AND_ASSIGN (ThreadSafeSTLMap);
};


} // namespace lldb_private

#endif  // liblldb_ThreadSafeSTLMap_h_