16#ifndef PERFC_REGISTER_HPP_
17#define PERFC_REGISTER_HPP_
45template <
class... CounterType>
56template <
class T,
class Mutex>
77 Locked(
Locked&& other) noexcept : m_lock(std::move(other.m_lock)), m_ptr(other.m_ptr) {
79 other.m_ptr =
nullptr;
90 m_lock = std::move(other.m_lock);
91 other.m_ptr =
nullptr;
117 operator bool() const noexcept {
118 return m_ptr !=
nullptr;
136 std::unique_lock<Mutex> m_lock;
140template <
class CounterType,
class MetadataType,
class Mutex>
165 return m_dereg !=
nullptr;
170 operator bool() const noexcept {
171 return m_dereg !=
nullptr;
209 template <
class CounterType,
class MetadataType,
class Mutex>
214 std::function<void()> m_dereg =
nullptr;
256template <
class CounterTypes,
class TMetadata,
class TMutex = std::recursive_mutex>
329 std::unique_lock<Mutex> lk(m_mutex);
330 if (
auto it = FindCounter(counter); it != std::end(m_counters)) {
334 m_counters.push_back({std::move(counter), std::move(metadata)});
347 std::unique_lock<Mutex> lk(m_mutex);
349 if (
auto it = FindCounter(counter); it != std::end(m_counters)) {
350 m_counters.erase(it);
358 typename Container::iterator FindCounter(
CounterVariant counter)
noexcept {
360 std::begin(m_counters), std::end(m_counters), [&counter](Entry
const& entry) ->
bool {
361 return entry.counter == counter;
364 mutable Mutex m_mutex;
366 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.