17#ifndef PERFC_REGISTER_HPP_
18#define PERFC_REGISTER_HPP_
46template <
class... CounterType>
57template <
class T,
class Mutex>
78 Locked(
Locked&& other) noexcept : m_lock(std::move(other.m_lock)), m_ptr(other.m_ptr) {
80 other.m_ptr =
nullptr;
91 m_lock = std::move(other.m_lock);
92 other.m_ptr =
nullptr;
118 operator bool() const noexcept {
119 return m_ptr !=
nullptr;
137 std::unique_lock<Mutex> m_lock;
141template <
class CounterType,
class MetadataType,
class Mutex>
166 return m_dereg !=
nullptr;
171 operator bool() const noexcept {
172 return m_dereg !=
nullptr;
210 template <
class CounterType,
class MetadataType,
class Mutex>
215 std::function<void()> m_dereg =
nullptr;
257template <
class CounterTypes,
class TMetadata,
class TMutex = std::recursive_mutex>
330 std::unique_lock<Mutex> lk(m_mutex);
331 if (
auto it = FindCounter(counter); it != std::end(m_counters)) {
335 m_counters.push_back({std::move(counter), std::move(metadata)});
348 std::unique_lock<Mutex> lk(m_mutex);
350 if (
auto it = FindCounter(counter); it != std::end(m_counters)) {
351 m_counters.erase(it);
359 typename Container::iterator FindCounter(
CounterVariant counter)
noexcept {
361 std::begin(m_counters), std::end(m_counters), [&counter](Entry
const& entry) ->
bool {
362 return entry.counter == counter;
365 mutable Mutex m_mutex;
367 std::uint64_t m_next_id;
Simple type that provide synchronized access by holding a lock to T container.
void Unlock() noexcept
Unlocks lock.
ReferenceType operator*() const noexcept
std::add_lvalue_reference_t< T > ReferenceType
std::add_pointer_t< T > PointerType
PointerType operator->() const noexcept
Locked & operator=(Locked &&other) noexcept
Move constructs by taking owneship of internal state of other.
Locked(Locked &&other) noexcept
Move constructs by taking owneship of internal state of other.
PointerType Get() const noexcept
Locked(ReferenceType t, Mutex &mutex)
Construct by providing reference to object requiring synchronized access and the mutex protecting it.
Register of counter variants, used for example to facilitate discovery/monitoring.
Locked< Container const, Mutex > LockedContainerConst
LockedContainerConst Lock() const
typename CounterTypes::CounterVariant CounterVariant
ScopedRegistration Add(CounterVariant counter, MetadataType metadata)
Add a counter, identified by its address, together with metadata to the register.
bool Remove(CounterVariant counter)
Remove counter from register.
Locked< Container, Mutex > LockedContainer
std::vector< Entry > Container
RAII object to manage automatic deregistration.
ScopedRegistration(std::function< void()> dereg)
bool IsValid() const noexcept
void Reset() noexcept
Deregisters counter if object is managing a registration.
ScopedRegistration() noexcept=default
void Swap(ScopedRegistration &other) noexcept
Swaps two entries.
void Release() noexcept
Release ownership such that destroying object will not deregister counter.
Helper used when declaring counter types in perfc::Register.
std::variant< std::add_pointer_t< CounterType >... > CounterVariant
Register entry representing the counter and associated metadata.