CppCommon  1.0.4.1
C++ Common Library
named_mutex.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_NAMED_MUTEX_H
10 #define CPPCOMMON_THREADS_NAMED_MUTEX_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 NamedMutex(const std::string& name);
37  NamedMutex(const NamedMutex&) = delete;
38  NamedMutex(NamedMutex&& mutex) = delete;
39  ~NamedMutex();
40 
41  NamedMutex& operator=(const NamedMutex&) = delete;
42  NamedMutex& operator=(NamedMutex&& mutex) = delete;
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 = 136;
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_MUTEX_H
Named mutex synchronization primitive.
Definition: named_mutex.h:30
NamedMutex(const std::string &name)
Default class constructor.
bool TryLock()
Try to acquire mutex without block.
void Unlock()
Release mutex.
NamedMutex & operator=(const NamedMutex &)=delete
const std::string & name() const
Get the mutex name.
void Lock()
Acquire mutex with block.
NamedMutex(NamedMutex &&mutex)=delete
NamedMutex & operator=(NamedMutex &&mutex)=delete
NamedMutex(const NamedMutex &)=delete
bool TryLockUntil(const UtcTimestamp &timestamp)
Try to acquire mutex until the given timestamp.
Definition: named_mutex.h:70
bool TryLockFor(const Timespan &timespan)
Try to acquire mutex 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.