CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
named_critical_section.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_THREADS_NAMED_CRITICAL_SECTION_H
10#define CPPCOMMON_THREADS_NAMED_CRITICAL_SECTION_H
11
12#include "threads/locker.h"
13#include "time/timestamp.h"
14
15#include <memory>
16#include <string>
17
18namespace CppCommon {
19
21
30{
31public:
33
36 explicit NamedCriticalSection(const std::string& name);
40
43
45 const std::string& name() const;
46
48
53 bool TryLock();
54
56
62 bool TryLockFor(const Timespan& timespan);
64
70 bool TryLockUntil(const UtcTimestamp& timestamp)
71 { return TryLockFor(timestamp - UtcTimestamp()); }
72
74
77 void Lock();
78
80
83 void Unlock();
84
85private:
86 class Impl;
87
88 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
89 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
90
91 static const size_t StorageSize = 256;
92 static const size_t StorageAlign = 8;
93 alignas(StorageAlign) std::byte _storage[StorageSize];
94};
95
98} // namespace CppCommon
99
100#endif // CPPCOMMON_THREADS_NAMED_CRITICAL_SECTION_H
Named critical section synchronization primitive.
NamedCriticalSection(const NamedCriticalSection &)=delete
bool TryLock()
Try to acquire critical section without block.
NamedCriticalSection & operator=(const NamedCriticalSection &)=delete
NamedCriticalSection & operator=(NamedCriticalSection &&cs)=delete
void Lock()
Acquire critical section with block.
NamedCriticalSection(NamedCriticalSection &&cs)=delete
bool TryLockUntil(const UtcTimestamp &timestamp)
Try to acquire critical section until the given timestamp.
void Unlock()
Release critical section.
const std::string & name() const
Get the critical section name.
bool TryLockFor(const Timespan &timespan)
Try to acquire critical section for the given timespan.
Locker synchronization primitive definition.
C++ Common project definitions.
Timestamp definition.