CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
critical_section.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_THREADS_CRITICAL_SECTION_H
10#define CPPCOMMON_THREADS_CRITICAL_SECTION_H
11
12#include "threads/locker.h"
13#include "time/timestamp.h"
14
15#include <memory>
16
17namespace CppCommon {
18
20
30{
31 friend class ConditionVariable;
32
33public:
38
41
43
48 bool TryLock();
49
51
57 bool TryLockFor(const Timespan& timespan);
59
65 bool TryLockUntil(const UtcTimestamp& timestamp)
66 { return TryLockFor(timestamp - UtcTimestamp()); }
67
69
72 void Lock();
73
75
78 void Unlock();
79
80private:
81 class Impl;
82
83 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
84 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
85
86 static const size_t StorageSize = 64;
87 static const size_t StorageAlign = 8;
88 alignas(StorageAlign) std::byte _storage[StorageSize];
89
91 void* native() noexcept;
92};
93
96} // namespace CppCommon
97
98#endif // CPPCOMMON_THREADS_CRITICAL_SECTION_H
Condition variable synchronization primitive.
Critical section synchronization primitive.
bool TryLock()
Try to acquire critical section without block.
void Lock()
Acquire critical section with block.
bool TryLockUntil(const UtcTimestamp &timestamp)
Try to acquire critical section until the given timestamp.
bool TryLockFor(const Timespan &timespan)
Try to acquire critical section for the given timespan.
CriticalSection & operator=(const CriticalSection &)=delete
CriticalSection(const CriticalSection &)=delete
CriticalSection & operator=(CriticalSection &&cs)=delete
CriticalSection(CriticalSection &&cs)=delete
void Unlock()
Release critical section.
Locker synchronization primitive definition.
C++ Common project definitions.
Timestamp definition.