CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
named_semaphore.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_THREADS_NAMED_SEMAPHORE_H
10#define CPPCOMMON_THREADS_NAMED_SEMAPHORE_H
11
12#include "threads/locker.h"
13#include "time/timestamp.h"
14
15#include <memory>
16#include <string>
17
18namespace CppCommon {
19
21
30{
31public:
33
37 explicit NamedSemaphore(const std::string& name, int resources);
39 NamedSemaphore(NamedSemaphore&& semaphore) = delete;
41
43 NamedSemaphore& operator=(NamedSemaphore&& semaphore) = delete;
44
46 const std::string& name() const;
48 int resources() const noexcept;
49
51
56 bool TryLock();
57
59
65 bool TryLockFor(const Timespan& timespan);
67
73 bool TryLockUntil(const UtcTimestamp& timestamp)
74 { return TryLockFor(timestamp - UtcTimestamp()); }
75
77
80 void Lock();
81
83
86 void Unlock();
87
88private:
89 class Impl;
90
91 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
92 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
93
94 static const size_t StorageSize = 56;
95 static const size_t StorageAlign = 8;
96 alignas(StorageAlign) std::byte _storage[StorageSize];
97};
98
101} // namespace CppCommon
102
103#endif // CPPCOMMON_THREADS_NAMED_SEMAPHORE_H
Named semaphore synchronization primitive.
NamedSemaphore(NamedSemaphore &&semaphore)=delete
int resources() const noexcept
Get the semaphore resources counter.
bool TryLockFor(const Timespan &timespan)
Try to acquire semaphore for the given timespan.
void Unlock()
Release semaphore.
const std::string & name() const
Get the semaphore name.
bool TryLockUntil(const UtcTimestamp &timestamp)
Try to acquire semaphore until the given timestamp.
NamedSemaphore & operator=(const NamedSemaphore &)=delete
NamedSemaphore & operator=(NamedSemaphore &&semaphore)=delete
void Lock()
Acquire semaphore with block.
bool TryLock()
Try to acquire semaphore without block.
NamedSemaphore(const NamedSemaphore &)=delete
Locker synchronization primitive definition.
C++ Common project definitions.
Timestamp definition.