Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 1 | //===------------------SharedCluster.h --------------------------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 9 | #ifndef LLDB_UTILITY_SHAREDCLUSTER_H |
| 10 | #define LLDB_UTILITY_SHAREDCLUSTER_H |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 11 | |
| 12 | #include "lldb/Utility/LLDBAssert.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
| 14 | #include "llvm/ADT/SmallVector.h" |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 15 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 16 | #include <memory> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 17 | #include <mutex> |
| 18 | |
| 19 | namespace lldb_private { |
| 20 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 21 | template <class T> |
| 22 | class ClusterManager : public std::enable_shared_from_this<ClusterManager<T>> { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 23 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 24 | static std::shared_ptr<ClusterManager> Create() { |
| 25 | return std::shared_ptr<ClusterManager>(new ClusterManager()); |
| 26 | } |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 27 | |
| 28 | ~ClusterManager() { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 29 | for (T *obj : m_objects) |
| 30 | delete obj; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void ManageObject(T *new_object) { |
| 34 | std::lock_guard<std::mutex> guard(m_mutex); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 35 | assert(!llvm::is_contained(m_objects, new_object) && |
| 36 | "ManageObject called twice for the same object?"); |
| 37 | m_objects.push_back(new_object); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 40 | std::shared_ptr<T> GetSharedPointer(T *desired_object) { |
| 41 | std::lock_guard<std::mutex> guard(m_mutex); |
| 42 | auto this_sp = this->shared_from_this(); |
| 43 | if (!llvm::is_contained(m_objects, desired_object)) { |
| 44 | lldbassert(false && "object not found in shared cluster when expected"); |
| 45 | desired_object = nullptr; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 46 | } |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 47 | return {std::move(this_sp), desired_object}; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | private: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 51 | ClusterManager() : m_objects(), m_mutex() {} |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 52 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 53 | llvm::SmallVector<T *, 16> m_objects; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 54 | std::mutex m_mutex; |
| 55 | }; |
| 56 | |
| 57 | } // namespace lldb_private |
| 58 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 59 | #endif // LLDB_UTILITY_SHAREDCLUSTER_H |