1 //===------------------SharedCluster.h --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef utility_SharedCluster_h_
11 #define utility_SharedCluster_h_
13 #include "lldb/Utility/SharingPtr.h"
14 #include "lldb/Host/Mutex.h"
16 #include "llvm/ADT/SmallPtrSet.h"
18 namespace lldb_private {
23 class shared_ptr_refcount : public lldb_private::imp::shared_count
26 template<class Y> shared_ptr_refcount (Y *in) : shared_count (0), manager(in) {}
28 shared_ptr_refcount() : shared_count (0) {}
30 ~shared_ptr_refcount() override
34 void on_zero_shared() override
36 manager->DecrementRefCount();
52 m_mutex(Mutex::eMutexTypeNormal) {}
56 for (typename llvm::SmallPtrSet<T *, 16>::iterator pos = m_objects.begin(), end = m_objects.end(); pos != end; ++pos)
62 // Decrement refcount should have been called on this ClusterManager,
63 // and it should have locked the mutex, now we will unlock it before
68 void ManageObject (T *new_object)
70 Mutex::Locker locker (m_mutex);
71 m_objects.insert (new_object);
74 typename lldb_private::SharingPtr<T> GetSharedPointer(T *desired_object)
77 Mutex::Locker locker (m_mutex);
79 assert (m_objects.count(desired_object));
81 return typename lldb_private::SharingPtr<T> (desired_object, new imp::shared_ptr_refcount<ClusterManager> (this));
86 void DecrementRefCount ()
90 if (m_external_ref == 0)
96 friend class imp::shared_ptr_refcount<ClusterManager>;
98 llvm::SmallPtrSet<T *, 16> m_objects;
103 } // namespace lldb_private
105 #endif // utility_SharedCluster_h_