CppCommon  1.0.4.1
C++ Common Library
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 
18 namespace CppCommon {
19 
21 
30 {
31 public:
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 
85 private:
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.
NamedCriticalSection(const std::string &name)
Default class constructor.
bool TryLockFor(const Timespan &timespan)
Try to acquire critical section for the given timespan.
UTC timestamp.
Definition: timestamp.h:248
Locker synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Timestamp definition.