CppCommon  1.0.4.1
C++ Common Library
mutex.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_MUTEX_H
10 #define CPPCOMMON_THREADS_MUTEX_H
11 
12 #include "threads/locker.h"
13 #include "time/timestamp.h"
14 
15 #include <memory>
16 
17 namespace CppCommon {
18 
20 
29 class Mutex
30 {
31 public:
32  Mutex();
33  Mutex(const Mutex&) = delete;
34  Mutex(Mutex&& mutex) = delete;
35  ~Mutex();
36 
37  Mutex& operator=(const Mutex&) = delete;
38  Mutex& operator=(Mutex&& mutex) = delete;
39 
41 
46  bool TryLock();
47 
49 
55  bool TryLockFor(const Timespan& timespan);
57 
63  bool TryLockUntil(const UtcTimestamp& timestamp)
64  { return TryLockFor(timestamp - UtcTimestamp()); }
65 
67 
70  void Lock();
71 
73 
76  void Unlock();
77 
78 private:
79  class Impl;
80 
81  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
82  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
83 
84  static const size_t StorageSize = 64;
85  static const size_t StorageAlign = 8;
86  alignas(StorageAlign) std::byte _storage[StorageSize];
87 };
88 
91 } // namespace CppCommon
92 
93 #endif // CPPCOMMON_THREADS_MUTEX_H
Mutex synchronization primitive.
Definition: mutex.h:30
Mutex(const Mutex &)=delete
Mutex(Mutex &&mutex)=delete
Mutex & operator=(const Mutex &)=delete
bool TryLockUntil(const UtcTimestamp &timestamp)
Try to acquire mutex until the given timestamp.
Definition: mutex.h:63
bool TryLock()
Try to acquire mutex without block.
Definition: mutex.cpp:167
void Lock()
Acquire mutex with block.
Definition: mutex.cpp:170
void Unlock()
Release mutex.
Definition: mutex.cpp:171
Mutex & operator=(Mutex &&mutex)=delete
bool TryLockFor(const Timespan &timespan)
Try to acquire mutex for the given timespan.
Definition: mutex.cpp:168
UTC timestamp.
Definition: timestamp.h:248
Locker synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Timestamp definition.