CppCommon  1.0.4.1
C++ Common Library
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 
17 namespace CppCommon {
18 
20 
30 {
31  friend class ConditionVariable;
32 
33 public:
35  CriticalSection(const CriticalSection&) = delete;
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 
80 private:
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.
CriticalSection & operator=(CriticalSection &&cs)=delete
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(const CriticalSection &)=delete
CriticalSection(CriticalSection &&cs)=delete
void Unlock()
Release critical section.
CriticalSection & operator=(const CriticalSection &)=delete
UTC timestamp.
Definition: timestamp.h:248
Locker synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Timestamp definition.