CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
semaphore.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_THREADS_SEMAPHORE_H
10#define CPPCOMMON_THREADS_SEMAPHORE_H
11
12#include "threads/locker.h"
13#include "time/timestamp.h"
14
15#include <memory>
16
17namespace CppCommon {
18
20
30{
31public:
33
36 explicit Semaphore(int resources);
37 Semaphore(const Semaphore&) = delete;
38 Semaphore(Semaphore&& semaphore) = delete;
39 ~Semaphore();
40
41 Semaphore& operator=(const Semaphore&) = delete;
42 Semaphore& operator=(Semaphore&& semaphore) = delete;
43
45 int resources() const noexcept;
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
85private:
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 = 40;
92 static const size_t StorageAlign = 8;
93 alignas(StorageAlign) std::byte _storage[StorageSize];
94};
95
98} // namespace CppCommon
99
100#endif // CPPCOMMON_THREADS_SEMAPHORE_H
Semaphore synchronization primitive.
Definition semaphore.h:30
bool TryLock()
Try to acquire semaphore without block.
Semaphore(const Semaphore &)=delete
bool TryLockFor(const Timespan &timespan)
Try to acquire semaphore for the given timespan.
int resources() const noexcept
Get the semaphore resources counter.
Semaphore(Semaphore &&semaphore)=delete
Semaphore & operator=(Semaphore &&semaphore)=delete
Semaphore & operator=(const Semaphore &)=delete
bool TryLockUntil(const UtcTimestamp &timestamp)
Try to acquire semaphore until the given timestamp.
Definition semaphore.h:70
void Unlock()
Release semaphore.
void Lock()
Acquire semaphore with block.
Locker synchronization primitive definition.
C++ Common project definitions.
Timestamp definition.