1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/**
* \file
* \brief ThisThread::Signals namespace header
*
* \author Copyright (C) 2015 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
*
* \par License
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDE_DISTORTOS_THISTHREAD_SIGNALS_HPP_
#define INCLUDE_DISTORTOS_THISTHREAD_SIGNALS_HPP_
#include "distortos/SignalInformation.hpp"
#include "distortos/TickClock.hpp"
#include <utility>
#include <cstdint>
namespace distortos
{
class SignalAction;
class SignalSet;
namespace ThisThread
{
namespace Signals
{
/// \addtogroup signals
/// \{
/**
* \brief Generates signal for current thread.
*
* Similar to raise() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/raise.html
*
* Adds the signalNumber to set of pending signals of current thread.
*
* \param [in] signalNumber is the signal that will be generated, [0; 31]
*
* \return 0 on success, error code otherwise:
* - EINVAL - \a signalNumber value is invalid;
* - ENOTSUP - reception of signals is disabled for current thread;
*/
int generateSignal(uint8_t signalNumber);
/**
* \brief Gets set of currently pending signals for current thread.
*
* Similar to sigpending() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigpending.html
*
* This function shall return the set of signals that are blocked from delivery and are pending on the current thread.
*
* \return set of currently pending signals for current thread
*/
SignalSet getPendingSignalSet();
/**
* \brief Gets SignalAction associated with given signal number.
*
* Similar to sigaction() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html
*
* \param [in] signalNumber is the signal for which the association is requested, [0; 31]
*
* \return pair with return code (0 on success, error code otherwise) and SignalAction that is associated with
* \a signalNumber, default-constructed object if no association was found;
* error codes:
* - EINVAL - \a signalNumber value is invalid;
* - ENOTSUP - reception or catching/handling of signals are disabled for current thread;
*/
std::pair<int, SignalAction> getSignalAction(uint8_t signalNumber);
/**
* \brief Gets signal mask for current thread.
*
* Similar to pthread_sigmask() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_sigmask.html#
*
* \return SignalSet with signal mask for current thread
*/
SignalSet getSignalMask();
/**
* \brief Queues signal for current thread.
*
* Similar to sigqueue() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigqueue.html
*
* Adds the signalNumber and signal value (sigval union) to queue of SignalInformation objects.
*
* \param [in] signalNumber is the signal that will be queued, [0; 31]
* \param [in] value is the signal value
*
* \return 0 on success, error code otherwise:
* - EAGAIN - no resources are available to queue the signal, maximal number of signals is already queued in
* associated queue of SignalInformation objects;
* - EINVAL - \a signalNumber value is invalid;
* - ENOTSUP - reception or queuing of signals are disabled for current thread;
*/
int queueSignal(uint8_t signalNumber, sigval value);
/**
* \brief Sets association for given signal number.
*
* Similar to sigaction() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html
*
* \param [in] signalNumber is the signal for which the association will be set, [0; 31]
* \param [in] signalAction is a reference to SignalAction that will be associated with given signal number, object in
* internal storage is copy-constructed
*
* \return pair with return code (0 on success, error code otherwise) and SignalAction that was associated with
* \a signalNumber, default-constructed object if no association was found;
* error codes:
* - EAGAIN - no resources are available to associate \a signalNumber with \a signalAction;
* - EINVAL - \a signalNumber value is invalid;
* - ENOTSUP - reception or catching/handling of signals are disabled for current thread;
*/
std::pair<int, SignalAction> setSignalAction(uint8_t signalNumber, const SignalAction& signalAction);
/**
* \brief Sets signal mask for current thread.
*
* Similar to pthread_sigmask() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_sigmask.html#
*
* \param [in] signalMask is the SignalSet with new signal mask for current thread
*
* \return 0 on success, error code otherwise:
* - ENOTSUP - reception or catching/handling of signals are disabled for current thread;
*/
int setSignalMask(SignalSet signalMask);
/**
* \brief Tries to accept pending signals.
*
* Similar to sigtimedwait() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigtimedwait.html
*
* This function shall select the lowest pending signal from provided set, atomically clear it from current thread's set
* of pending signals and return that signal number. If no signal in provided set is pending at the time of the call,
* then this function shall return immediately with an error.
*
* \param [in] signalSet is a reference to set of signals that may be accepted
*
* \return pair with return code (0 on success, error code otherwise) and SignalInformation object for accepted signal;
* error codes:
* - EAGAIN - no signal specified by \a signalSet was pending;
* - ENOTSUP - reception of signals is disabled for current thread;
*/
std::pair<int, SignalInformation> tryWait(const SignalSet& signalSet);
/**
* \brief Tries to wait for signals for given duration of time.
*
* Similar to sigtimedwait() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigtimedwait.html
*
* This function shall select the lowest pending signal from provided set, atomically clear it from current thread's set
* of pending signals and return that signal number. If no signal in provided set is pending at the time of the call,
* the thread shall be suspended until one or more becomes pending or until given duration of time expires.
*
* \param [in] signalSet is a reference to set of signals that will be waited for
* \param [in] duration is the duration after which the wait for signals will be terminated
*
* \return pair with return code (0 on success, error code otherwise) and SignalInformation object for accepted signal;
* error codes:
* - EINTR - the wait was interrupted by an unmasked, caught signal;
* - ENOTSUP - reception of signals is disabled for current thread;
* - ETIMEDOUT - no signal specified by \a signalSet was generated before the specified \a duration passed;
*/
std::pair<int, SignalInformation> tryWaitFor(const SignalSet& signalSet, TickClock::duration duration);
/**
* \brief Tries to wait for signals for given duration of time.
*
* Template variant of tryWaitFor(const SignalSet&, TickClock::duration).
*
* \tparam Rep is type of tick counter
* \tparam Period is std::ratio type representing the tick period of the clock, in seconds
*
* \param [in] signalSet is a reference to set of signals that will be waited for
* \param [in] duration is the duration after which the wait for signals will be terminated
*
* \return pair with return code (0 on success, error code otherwise) and SignalInformation object for accepted signal;
* error codes:
* - EINTR - the wait was interrupted by an unmasked, caught signal;
* - ENOTSUP - reception of signals is disabled for current thread;
* - ETIMEDOUT - no signal specified by \a signalSet was generated before the specified \a duration passed;
*/
template<typename Rep, typename Period>
std::pair<int, SignalInformation> tryWaitFor(const SignalSet& signalSet,
const std::chrono::duration<Rep, Period> duration)
{
return tryWaitFor(signalSet, std::chrono::duration_cast<TickClock::duration>(duration));
}
/**
* \brief Tries to wait for signals until given time point.
*
* Similar to sigtimedwait() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigtimedwait.html
*
* This function shall select the lowest pending signal from provided set, atomically clear it from current thread's set
* of pending signals and return that signal number. If no signal in provided set is pending at the time of the call,
* the thread shall be suspended until one or more becomes pending or until given time point is reached
*
* \param [in] signalSet is a reference to set of signals that will be waited for
* \param [in] timePoint is the time point at which the wait for signals will be terminated
*
* \return pair with return code (0 on success, error code otherwise) and SignalInformation object for accepted signal;
* error codes:
* - EINTR - the wait was interrupted by an unmasked, caught signal;
* - ENOTSUP - reception of signals is disabled for current thread;
* - ETIMEDOUT - no signal specified by \a signalSet was generated before specified \a timePoint;
*/
std::pair<int, SignalInformation> tryWaitUntil(const SignalSet& signalSet, TickClock::time_point timePoint);
/**
* \brief Tries to wait for signals until given time point.
*
* Template variant of tryWaitUntil(const SignalSet&, TickClock::time_point).
*
* \tparam Duration is a std::chrono::duration type used to measure duration
*
* \param [in] signalSet is a reference to set of signals that will be waited for
* \param [in] timePoint is the time point at which the wait for signals will be terminated
*
* \return pair with return code (0 on success, error code otherwise) and SignalInformation object for accepted signal;
* error codes:
* - EINTR - the wait was interrupted by an unmasked, caught signal;
* - ENOTSUP - reception of signals is disabled for current thread;
* - ETIMEDOUT - no signal specified by \a signalSet was generated before specified \a timePoint;
*/
template<typename Duration>
std::pair<int, SignalInformation> tryWaitUntil(const SignalSet& signalSet,
const std::chrono::time_point<TickClock, Duration> timePoint)
{
return tryWaitUntil(signalSet, std::chrono::time_point_cast<TickClock::duration>(timePoint));
}
/**
* \brief Waits for signals.
*
* Similar to sigwait() - http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigwait.html
*
* This function shall select the lowest pending signal from provided set, atomically clear it from current thread's set
* of pending signals and return that signal number. If no signal in provided set is pending at the time of the call,
* the thread shall be suspended until one or more becomes pending.
*
* \param [in] signalSet is a reference to set of signals that will be waited for
*
* \return pair with return code (0 on success, error code otherwise) and SignalInformation object for accepted signal;
* error codes:
* - EINTR - the wait was interrupted by an unmasked, caught signal;
* - ENOTSUP - reception of signals is disabled for current thread;
*/
std::pair<int, SignalInformation> wait(const SignalSet& signalSet);
/// \}
} // namespace Signals
} // namespace ThisThread
} // namespace distortos
#endif // INCLUDE_DISTORTOS_THISTHREAD_SIGNALS_HPP_
|