Ocean
Loading...
Searching...
No Matches
Signal.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_SIGNAL_H
9#define META_OCEAN_BASE_SIGNAL_H
10
11#include "ocean/base/Base.h"
12
13#if defined(_ANDROID) || defined(__linux__) || defined(__EMSCRIPTEN__)
14 #include <semaphore.h>
15#endif
16
17#ifdef __APPLE__
18 #include <dispatch/dispatch.h>
19#endif
20
21#include <atomic>
22
23namespace Ocean
24{
25
26/**
27 * This class implements a signal.
28 * @ingroup base
29 */
30class OCEAN_BASE_EXPORT Signal
31{
32 friend class Signals;
33
34 public:
35
36 /**
37 * Creates a new signal object.
38 */
40
41 /**
42 * Destructs a signal object.
43 */
45
46 /**
47 * Returns after the signal has been pulsed.
48 */
49 void wait() const;
50
51 /**
52 * Returns after the signal has been pulsed or the specified time elapsed.
53 * @param time Maximal wait time in ms
54 * @return True, if the signal has been pulsed, False otherwise
55 */
56 bool wait(const unsigned int time) const;
57
58 /**
59 * Pulses this signal.
60 * @return True, if succeeded
61 */
62 bool pulse();
63
64 /**
65 * Resets the signal.
66 * @return True, if succeeded
67 */
68 bool reset();
69
70 /**
71 * Pulses the signal for the last time to allow a waiting process to proceed and releases the signal.
72 */
73 void release();
74
75#ifdef _WINDOWS
76
77 /**
78 * Returns the signal handle.
79 * @return Signal handle
80 */
81 inline void* handle() const;
82
83#endif
84
85 protected:
86
87 /**
88 * Disabled copy constructor.
89 * @param otherSignal Object which would be copied
90 */
91 Signal(const Signal& otherSignal) = delete;
92
93 /**
94 * Disabled assign operator.
95 * @param otherSignal Object which would be copied
96 * @return Reference to this object
97 */
98 Signal& operator=(const Signal& otherSignal) = delete;
99
100#if defined(_WINDOWS)
101
102 /// Signal handle for Windows platforms.
104
105#elif defined(__APPLE__)
106
107 /// Semaphore object for Apple platforms.
108 dispatch_semaphore_t semaphoreObject;
109
110 /// True, if the semaphore object has been released and should not be used anymore.
111 std::atomic<bool> semaphoreObjectReleased;
112
113#else
114
115 /// Semaphore object for e.g., Android platforms.
116 mutable sem_t semaphoreObject;
117
118 /// True, if the semaphore object (for e.g., Android platforms) is valid and has been initialized.
120
121 /// True, if the semaphore object (for e.g., Android platforms) has been released and should not be used anymore.
122 std::atomic<bool> semaphoreObjectReleased;
123
124#endif
125};
126
127/**
128 * This class implements a signal array.
129 * @ingroup base
130 */
131class OCEAN_BASE_EXPORT Signals
132{
133 public:
134
135 /**
136 * Creates an empty signal array.
137 */
139
140 /**
141 * Creates a new signal array object by the number of requested signals.
142 * @param size Number of requested signals
143 */
144 explicit Signals(const unsigned int size);
145
146 /**
147 * Destructs a signal array.
148 */
150
151 /**
152 * Returns after all signals has been pulsed.
153 */
154 void wait() const;
155
156 /**
157 * Returns after a subset of signals has been pulsed.
158 * @param signalsCount Number of the first signals to wait for
159 */
160 void waitSubset(const unsigned int signalsCount) const;
161
162 /**
163 * Returns after all signals has been pulsed or the specified time has been elapsed.
164 * @param time Maximal wait time in ms
165 * @return True, if all signals has been pulsed
166 */
167 bool wait(const unsigned int time) const;
168
169 /**
170 * Returns after a subset of signals has been pulsed or the specified time has been elapsed.
171 * @param signalsCount Number of the first signals to wait for
172 * @param time Maximal wait time in ms
173 * @return True, if all specified signals has been pulsed
174 */
175 bool waitSubset(const unsigned int signalsCount, const unsigned int time);
176
177 /**
178 * Pulses all signals.
179 * @return True, if succeeded
180 */
181 bool pulse();
182
183 /**
184 * Resets all the signal.
185 * @return True, if succeeded
186 */
187 bool reset();
188
189 /**
190 * Returns the number of signals.
191 * @return Signal number
192 */
193 inline unsigned int size() const;
194
195 /**
196 * Changes the size of the signal array.
197 * All existing signals will be released before the new array is created.
198 * @param size New signal array size
199 */
200 void setSize(const unsigned int size);
201
202 /**
203 * Returns one specific signal of this array.
204 * @param index Signal index to return
205 * @return Specified signal
206 */
207 Signal& operator[](const unsigned int index) const;
208
209 private:
210
211 /**
212 * Disabled copy constructor.
213 * @param otherSignals Object which would be copied
214 */
215 Signals(const Signals& otherSignals) = delete;
216
217 /**
218 * Disabled copy operator.
219 * @param otherSignals Object which would be copied
220 * @return Reference to this object
221 */
222 Signals& operator=(const Signals& otherSignals) = delete;
223
224 private:
225
226 /// Array of stored signals.
228
229 /// Number of signals stored in the array.
230 unsigned int signalsSize;
231};
232
233#ifdef _WINDOWS
234
235inline void* Signal::handle() const
236{
237 return signalHandle;
238}
239
240#endif
241
242inline unsigned int Signals::size() const
243{
244 return signalsSize;
245}
246
247}
248
249#endif // META_OCEAN_BASE_SIGNAL_H
This class implements a signal.
Definition Signal.h:31
dispatch_semaphore_t semaphoreObject
Semaphore object for Apple platforms.
Definition Signal.h:108
bool pulse()
Pulses this signal.
sem_t semaphoreObject
Semaphore object for e.g., Android platforms.
Definition Signal.h:116
Signal(const Signal &otherSignal)=delete
Disabled copy constructor.
void release()
Pulses the signal for the last time to allow a waiting process to proceed and releases the signal.
void * signalHandle
Signal handle for Windows platforms.
Definition Signal.h:103
Signal()
Creates a new signal object.
bool semaphoreObjectState
True, if the semaphore object (for e.g., Android platforms) is valid and has been initialized.
Definition Signal.h:119
void * handle() const
Returns the signal handle.
Definition Signal.h:235
std::atomic< bool > semaphoreObjectReleased
True, if the semaphore object has been released and should not be used anymore.
Definition Signal.h:111
Signal & operator=(const Signal &otherSignal)=delete
Disabled assign operator.
void wait() const
Returns after the signal has been pulsed.
bool reset()
Resets the signal.
~Signal()
Destructs a signal object.
bool wait(const unsigned int time) const
Returns after the signal has been pulsed or the specified time elapsed.
This class implements a signal array.
Definition Signal.h:132
~Signals()
Destructs a signal array.
Signals & operator=(const Signals &otherSignals)=delete
Disabled copy operator.
void waitSubset(const unsigned int signalsCount) const
Returns after a subset of signals has been pulsed.
bool waitSubset(const unsigned int signalsCount, const unsigned int time)
Returns after a subset of signals has been pulsed or the specified time has been elapsed.
Signals(const Signals &otherSignals)=delete
Disabled copy constructor.
bool wait(const unsigned int time) const
Returns after all signals has been pulsed or the specified time has been elapsed.
Signals()
Creates an empty signal array.
bool pulse()
Pulses all signals.
void wait() const
Returns after all signals has been pulsed.
bool reset()
Resets all the signal.
Signal & operator[](const unsigned int index) const
Returns one specific signal of this array.
unsigned int size() const
Returns the number of signals.
Definition Signal.h:242
Signals(const unsigned int size)
Creates a new signal array object by the number of requested signals.
void setSize(const unsigned int size)
Changes the size of the signal array.
Signal * signalObjects
Array of stored signals.
Definition Signal.h:227
unsigned int signalsSize
Number of signals stored in the array.
Definition Signal.h:230
The namespace covering the entire Ocean framework.
Definition Accessor.h:15