1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #include "ABI48_0_0LongLivedObject.h"
9 
10 namespace ABI48_0_0facebook {
11 namespace ABI48_0_0React {
12 
13 // LongLivedObjectCollection
get()14 LongLivedObjectCollection &LongLivedObjectCollection::get() {
15   static LongLivedObjectCollection instance;
16   return instance;
17 }
18 
LongLivedObjectCollection()19 LongLivedObjectCollection::LongLivedObjectCollection() {}
20 
add(std::shared_ptr<LongLivedObject> so) const21 void LongLivedObjectCollection::add(std::shared_ptr<LongLivedObject> so) const {
22   std::lock_guard<std::mutex> lock(collectionMutex_);
23   collection_.insert(so);
24 }
25 
remove(const LongLivedObject * o) const26 void LongLivedObjectCollection::remove(const LongLivedObject *o) const {
27   std::lock_guard<std::mutex> lock(collectionMutex_);
28   auto p = collection_.begin();
29   for (; p != collection_.end(); p++) {
30     if (p->get() == o) {
31       break;
32     }
33   }
34   if (p != collection_.end()) {
35     collection_.erase(p);
36   }
37 }
38 
clear() const39 void LongLivedObjectCollection::clear() const {
40   std::lock_guard<std::mutex> lock(collectionMutex_);
41   collection_.clear();
42 }
43 
size() const44 size_t LongLivedObjectCollection::size() const {
45   std::lock_guard<std::mutex> lock(collectionMutex_);
46   return collection_.size();
47 }
48 
49 // LongLivedObject
LongLivedObject()50 LongLivedObject::LongLivedObject() {}
~LongLivedObject()51 LongLivedObject::~LongLivedObject() {}
52 
allowRelease()53 void LongLivedObject::allowRelease() {
54   LongLivedObjectCollection::get().remove(this);
55 }
56 
57 } // namespace ABI48_0_0React
58 } // namespace ABI48_0_0facebook
59