Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Semaphore.h
Go to the documentation of this file.
1#pragma once
2#include <misc/gen/TimeSpec.h>
3#include <misc/global.h>
4#include <semaphore.h>
5
6namespace sf
7{
8
13{
14 public:
18 explicit Semaphore(int initialCount = 0);
19
24
29 bool post();
30
35 [[nodiscard]] int value() const;
36
41 {
42 public:
46 inline Lock();
47
53 explicit inline Lock(const Semaphore& semaphore, const TimeSpec& timeout);
54
60 explicit inline Lock(const Semaphore& semaphore, bool tryWait = false);
61
65 inline ~Lock();
66
76 bool acquire(const Semaphore& semaphore, const TimeSpec& timeout);
77
86 bool acquire(const Semaphore& semaphore, bool tryWait = false);
87
92 [[nodiscard]] inline bool isAcquired() const;
93
98 void release(bool relinquish = true);
99
100 private:
101 // Holds the semaphore pointer when signaled.
102 const Semaphore* _semaphore;
103 };
104
105 protected:
109 typedef sem_t handle_type;
110
114 void release();
115
120};
121
123 : _semaphore(nullptr)
124{}
125
126inline Semaphore::Lock::Lock(const Semaphore& semaphore, const TimeSpec& timeout)
127 : _semaphore(nullptr)
128{
129 acquire(semaphore, timeout);
130}
131
132inline Semaphore::Lock::Lock(const Semaphore& semaphore, bool tryWait)
133 : _semaphore(nullptr)
134{
135 acquire(semaphore, tryWait);
136}
137
139{
140 release();
141}
142
144{
145 // Object was signaled.
146 return _semaphore != nullptr;
147}
148
149}// namespace sf
Locking class.
Definition Semaphore.h:41
bool acquire(const Semaphore &semaphore, const TimeSpec &timeout)
Waits to acquire the semaphore. Is called from the constructor. The timeout is the time the function ...
void release(bool relinquish=true)
Releases.
bool acquire(const Semaphore &semaphore, bool tryWait=false)
Waits to acquire the semaphore. Is also called from the constructor. When the 'tryWait' argument is t...
Lock()
Default constructor which sees to it that it is released.
Definition Semaphore.h:122
~Lock()
Destructor cleaning up.
Definition Semaphore.h:138
bool isAcquired() const
See if semaphore was acquired or timed-out.
Definition Semaphore.h:143
Semaphore class built around posix sem_xxx functionality.
Definition Semaphore.h:13
int value() const
Gets the semaphore integer value.
void release()
Called by Lock destructor.
handle_type _handle
Holds the semaphore handle/structure.
Definition Semaphore.h:119
~Semaphore()
Destructor cleaning up the allocated semaphore.
bool post()
Signals any thread which is locked.
Semaphore(int initialCount=0)
Constructor initializing the counter.
sem_t handle_type
Definition Semaphore.h:109
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
Class wrapper for timespec structure to modify.
Definition TimeSpec.h:12