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 "ABI49_0_0LongLivedObject.h"
9 
10 namespace ABI49_0_0facebook {
11 namespace ABI49_0_0React {
12 
13 // LongLivedObjectCollection
get()14 LongLivedObjectCollection &LongLivedObjectCollection::get() {
15   static LongLivedObjectCollection instance;
16   return instance;
17 }
18 
add(std::shared_ptr<LongLivedObject> so)19 void LongLivedObjectCollection::add(std::shared_ptr<LongLivedObject> so) {
20   std::lock_guard<std::mutex> lock(collectionMutex_);
21   collection_.insert(std::move(so));
22 }
23 
remove(const LongLivedObject * o)24 void LongLivedObjectCollection::remove(const LongLivedObject *o) {
25   std::lock_guard<std::mutex> lock(collectionMutex_);
26   for (auto p = collection_.begin(); p != collection_.end(); p++) {
27     if (p->get() == o) {
28       collection_.erase(p);
29       break;
30     }
31   }
32 }
33 
clear()34 void LongLivedObjectCollection::clear() {
35   std::lock_guard<std::mutex> lock(collectionMutex_);
36   collection_.clear();
37 }
38 
size() const39 size_t LongLivedObjectCollection::size() const {
40   std::lock_guard<std::mutex> lock(collectionMutex_);
41   return collection_.size();
42 }
43 
44 // LongLivedObject
45 
allowRelease()46 void LongLivedObject::allowRelease() {
47   LongLivedObjectCollection::get().remove(this);
48 }
49 
50 } // namespace ABI49_0_0React
51 } // namespace ABI49_0_0facebook
52