Files
nitro/packages/react-native-nitro-modules/cpp/utils/ReferenceState.hpp
T
Marc RousavyandGitHub 6998588b70 chore: Rename BorrowingReference<T> -> WeakReference<T>, and OwningReference<T> -> BorrowingReference<T> (#523)
* chore: Rename `BorrowingReference<T>` -> `WeakReference<T>`, and `OwningReference<T>` -> `BorrowingReference<T>`

* chore: Remove unused flag

* chore: Lint

* fix: remove unnecessary nullptr compare
2025-02-03 18:30:04 +01:00

41 lines
1000 B
C++

//
// ReferenceState.hpp
// react-native-nitro
//
// Created by Marc Rousavy on 03.02.24.
//
#pragma once
#include <atomic>
#include <mutex>
namespace margelo::nitro {
/**
* Holds state for an `BorrowingReference` (or `WeakReference`).
*
* The state tracks the amount of strong- and weak- references to any kind of value,
* including an extra `isDeleted` flag that specifies whether the value has been force-deleted.
*
* Also, a `mutex` allows for thread-safe access of the `isDeleted` flag.
*/
struct ReferenceState {
std::atomic_size_t strongRefCount;
std::atomic_size_t weakRefCount;
bool isDeleted;
std::mutex mutex;
/**
* Decrements the strong ref count by one, and returns whether the value should be deleted.
*/
inline bool decrementStrongRefCount() {
size_t oldRefCount = strongRefCount.fetch_sub(1);
return oldRefCount <= 1;
}
explicit ReferenceState() : strongRefCount(1), weakRefCount(0), isDeleted(false) {}
};
} // namespace margelo::nitro