summaryrefslogtreecommitdiff
path: root/include/lldb/Core/ThreadSafeSTLVector.h
blob: b65936f81fec1d9184e70f6372d5917fcf99a3f7 (plain)
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
//===-- ThreadSafeSTLVector.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_ThreadSafeSTLVector_h_
#define liblldb_ThreadSafeSTLVector_h_

#include <mutex>
#include <vector>

#include "lldb/lldb-defines.h"

namespace lldb_private {

template <typename _Object> class ThreadSafeSTLVector {
public:
  typedef std::vector<_Object> collection;
  typedef typename collection::iterator iterator;
  typedef typename collection::const_iterator const_iterator;
  //------------------------------------------------------------------
  // Constructors and Destructors
  //------------------------------------------------------------------
  ThreadSafeSTLVector() : m_collection(), m_mutex() {}

  ~ThreadSafeSTLVector() = default;

  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 GetCount() {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return m_collection.size();
  }

  void AppendObject(_Object &object) {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    m_collection.push_back(object);
  }

  _Object GetObject(size_t index) {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    return m_collection.at(index);
  }

  void SetObject(size_t index, const _Object &object) {
    std::lock_guard<std::recursive_mutex> guard(m_mutex);
    m_collection.at(index) = object;
  }

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

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

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

} // namespace lldb_private

#endif // liblldb_ThreadSafeSTLVector_h_