summaryrefslogtreecommitdiff
path: root/lldb/include/lldb/Utility/SharedCluster.h
blob: b3f41dbaa64b07fa4fb7c59da3f036e73bad978f (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
//===------------------SharedCluster.h --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_UTILITY_SHAREDCLUSTER_H
#define LLDB_UTILITY_SHAREDCLUSTER_H

#include "lldb/Utility/LLDBAssert.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"

#include <memory>
#include <mutex>

namespace lldb_private {

template <class T>
class ClusterManager : public std::enable_shared_from_this<ClusterManager<T>> {
public:
  static std::shared_ptr<ClusterManager> Create() {
    return std::shared_ptr<ClusterManager>(new ClusterManager());
  }

  ~ClusterManager() {
    for (T *obj : m_objects)
      delete obj;
  }

  void ManageObject(T *new_object) {
    std::lock_guard<std::mutex> guard(m_mutex);
    assert(!llvm::is_contained(m_objects, new_object) &&
           "ManageObject called twice for the same object?");
    m_objects.push_back(new_object);
  }

  std::shared_ptr<T> GetSharedPointer(T *desired_object) {
    std::lock_guard<std::mutex> guard(m_mutex);
    auto this_sp = this->shared_from_this();
    if (!llvm::is_contained(m_objects, desired_object)) {
      lldbassert(false && "object not found in shared cluster when expected");
      desired_object = nullptr;
    }
    return {std::move(this_sp), desired_object};
  }

private:
  ClusterManager() : m_objects() {}

  llvm::SmallVector<T *, 16> m_objects;
  std::mutex m_mutex;
};

} // namespace lldb_private

#endif // LLDB_UTILITY_SHAREDCLUSTER_H