1 //===-- ThreadSafeSTLVector.h ------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef liblldb_ThreadSafeSTLVector_h_
12 #define liblldb_ThreadSafeSTLVector_h_
13 
14 #include <mutex>
15 #include <vector>
16 
17 #include "lldb/lldb-defines.h"
18 
19 namespace lldb_private {
20 
21 template <typename _Object> class ThreadSafeSTLVector {
22 public:
23   typedef std::vector<_Object> collection;
24   typedef typename collection::iterator iterator;
25   typedef typename collection::const_iterator const_iterator;
26   //------------------------------------------------------------------
27   // Constructors and Destructors
28   //------------------------------------------------------------------
ThreadSafeSTLVector()29   ThreadSafeSTLVector() : m_collection(), m_mutex() {}
30 
31   ~ThreadSafeSTLVector() = default;
32 
IsEmpty()33   bool IsEmpty() const {
34     std::lock_guard<std::recursive_mutex> guard(m_mutex);
35     return m_collection.empty();
36   }
37 
Clear()38   void Clear() {
39     std::lock_guard<std::recursive_mutex> guard(m_mutex);
40     return m_collection.clear();
41   }
42 
GetCount()43   size_t GetCount() {
44     std::lock_guard<std::recursive_mutex> guard(m_mutex);
45     return m_collection.size();
46   }
47 
AppendObject(_Object & object)48   void AppendObject(_Object &object) {
49     std::lock_guard<std::recursive_mutex> guard(m_mutex);
50     m_collection.push_back(object);
51   }
52 
GetObject(size_t index)53   _Object GetObject(size_t index) {
54     std::lock_guard<std::recursive_mutex> guard(m_mutex);
55     return m_collection.at(index);
56   }
57 
SetObject(size_t index,const _Object & object)58   void SetObject(size_t index, const _Object &object) {
59     std::lock_guard<std::recursive_mutex> guard(m_mutex);
60     m_collection.at(index) = object;
61   }
62 
GetMutex()63   std::recursive_mutex &GetMutex() { return m_mutex; }
64 
65 private:
66   collection m_collection;
67   mutable std::recursive_mutex m_mutex;
68 
69   //------------------------------------------------------------------
70   // For ThreadSafeSTLVector only
71   //------------------------------------------------------------------
72   DISALLOW_COPY_AND_ASSIGN(ThreadSafeSTLVector);
73 };
74 
75 } // namespace lldb_private
76 
77 #endif // liblldb_ThreadSafeSTLVector_h_
78