1 //===------------------SharedCluster.h --------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef utility_SharedCluster_h_
10 #define utility_SharedCluster_h_
12 #include "lldb/Utility/LLDBAssert.h"
13 #include "lldb/Utility/SharingPtr.h"
15 #include "llvm/ADT/SmallPtrSet.h"
19 namespace lldb_private {
23 class shared_ptr_refcount : public lldb_private::imp::shared_count {
26 shared_ptr_refcount(Y *in) : shared_count(0), manager(in) {}
28 shared_ptr_refcount() : shared_count(0) {}
30 ~shared_ptr_refcount() override {}
32 void on_zero_shared() override { manager->DecrementRefCount(); }
40 template <class T> class ClusterManager {
42 ClusterManager() : m_objects(), m_external_ref(0), m_mutex() {}
45 for (typename llvm::SmallPtrSet<T *, 16>::iterator pos = m_objects.begin(),
46 end = m_objects.end();
52 // Decrement refcount should have been called on this ClusterManager, and
53 // it should have locked the mutex, now we will unlock it before we destroy
58 void ManageObject(T *new_object) {
59 std::lock_guard<std::mutex> guard(m_mutex);
60 m_objects.insert(new_object);
63 typename lldb_private::SharingPtr<T> GetSharedPointer(T *desired_object) {
65 std::lock_guard<std::mutex> guard(m_mutex);
67 if (0 == m_objects.count(desired_object)) {
68 lldbassert(false && "object not found in shared cluster when expected");
69 desired_object = nullptr;
72 return typename lldb_private::SharingPtr<T>(
73 desired_object, new imp::shared_ptr_refcount<ClusterManager>(this));
77 void DecrementRefCount() {
80 if (m_external_ref == 0)
86 friend class imp::shared_ptr_refcount<ClusterManager>;
88 llvm::SmallPtrSet<T *, 16> m_objects;
93 } // namespace lldb_private
95 #endif // utility_SharedCluster_h_