IntrOS
Loading...
Searching...
No Matches
ossignal.h
1/******************************************************************************
2
3 @file IntrOS: ossignal.h
4 @author Rajmund Szymanski
5 @date 18.11.2025
6 @brief This file contains definitions for IntrOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __INTROS_SIG_H
33#define __INTROS_SIG_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
44/* -------------------------------------------------------------------------- */
45
46#define SIG_LIMIT (sizeof(unsigned) * CHAR_BIT)
47
48#define SIGSET(signo) (((signo) < (SIG_LIMIT)) ? 1U << (signo) : 0U)
49#define sigAny ( 0U )
50#define sigAll ( 0U-1 )
51
55
56typedef struct __sig sig_t;
57
58struct __sig
59{
60 obj_t obj;
61
62 unsigned sigset;
63};
64
65typedef struct __sig sig_id [];
66
67/******************************************************************************
68 *
69 * Name : _SIG_INIT
70 *
71 * Description : create and initialize a signal object
72 *
73 * Parameters : none
74 *
75 * Return : signal object
76 *
77 * Note : for internal use
78 *
79 ******************************************************************************/
80
81#define _SIG_INIT() { _OBJ_INIT(), 0 }
82
89#define OS_SIG( sig ) \
90 sig_t sig[] = { _SIG_INIT() }
91
92/** static alias of OS_SIG() */
93#define static_SIG( sig ) \
94 static sig_t sig[] = { _SIG_INIT() }
95
103
104#ifndef __cplusplus
105#define SIG_INIT() \
106 _SIG_INIT()
107#endif
108
116
117#ifndef __cplusplus
118#define SIG_CREATE() \
119 { SIG_INIT () }
120/** alias of SIG_CREATE() */
121#define SIG_NEW \
122 SIG_CREATE
123#endif
124
125#ifdef __cplusplus
126extern "C" {
127#endif
128
135void sig_init( sig_t *sig );
136
145void sig_reset( sig_t *sig );
146
148__STATIC_INLINE
149void sig_kill( sig_t *sig ) { sig_reset(sig); }
150
163int sig_take( sig_t *sig, unsigned sigset, unsigned *signo );
164
166__STATIC_INLINE
167int sig_tryWait( sig_t *sig, unsigned sigset, unsigned *signo ) { return sig_take(sig, sigset, signo); }
168
185int sig_waitFor( sig_t *sig, unsigned sigset, unsigned *signo, cnt_t delay );
186
201int sig_waitUntil( sig_t *sig, unsigned sigset, unsigned *signo, cnt_t time );
202
214
215__STATIC_INLINE
216int sig_wait( sig_t *sig, unsigned sigset, unsigned *signo ) { return sig_waitFor(sig, sigset, signo, INFINITE); }
217
227void sig_give( sig_t *sig, unsigned signo );
228
230__STATIC_INLINE
231void sig_set( sig_t *sig, unsigned signo ) { sig_give(sig, signo); }
232
240void sig_clear( sig_t *sig, unsigned signo );
241
242#ifdef __cplusplus
243}
244#endif
245
246/* -------------------------------------------------------------------------- */
247
248#if defined(__cplusplus)
249namespace intros {
250
255struct Signal : public __sig
256{
258 constexpr
259 Signal(): __sig _SIG_INIT() {}
260
261 ~Signal() { assert(__sig::obj.queue == nullptr); }
262
263 /// move constructor
264 Signal( Signal&& _src ): __sig(_src)
265 {
266 assert(_src.obj.queue == nullptr);
267 }
268
269 Signal( const Signal& ) = delete;
270 Signal& operator=( Signal&& ) = delete;
271 Signal& operator=( const Signal& ) = delete;
273 void reset () { sig_reset (this); }
274 void kill () { sig_kill (this); }
275 int take ( unsigned _sigset, unsigned *_signo = nullptr ) { return sig_take (this, _sigset, _signo); }
276 int tryWait ( unsigned _sigset, unsigned *_signo = nullptr ) { return sig_tryWait (this, _sigset, _signo); }
277 template<typename T>
278 int waitFor ( unsigned _sigset, unsigned *_signo, const T& _delay ) { return sig_waitFor (this, _sigset, _signo, Clock::count(_delay)); }
279 template<typename T>
280 int waitUntil( unsigned _sigset, unsigned *_signo, const T& _time ) { return sig_waitUntil(this, _sigset, _signo, Clock::until(_time)); }
281 int wait ( unsigned _sigset, unsigned *_signo = nullptr ) { return sig_wait (this, _sigset, _signo); }
282 void give ( unsigned _signo ) { sig_give (this, _signo); }
283 void set ( unsigned _signo ) { sig_set (this, _signo); }
284 void clear ( unsigned _signo ) { sig_clear (this, _signo); }
285};
286
287} // namespace
288#endif//__cplusplus
289
290/* -------------------------------------------------------------------------- */
291
293
294#endif//__INTROS_SIG_H
#define INFINITE
infinite waiting
Definition osbase.h:250
int sig_tryWait(sig_t *sig, unsigned sigset, unsigned *signo)
alias of sig_take()
Definition ossignal.h:166
int sig_waitFor(sig_t *sig, unsigned sigset, unsigned *signo, cnt_t delay)
wait for a signal from the given set of signals for given duration of time and return the lowest one ...
void sig_clear(sig_t *sig, unsigned signo)
reset given signal in the signal object
int sig_wait(sig_t *sig, unsigned sigset, unsigned *signo)
wait indefinitely for a signal from the given set of signals and return the lowest one in the 'signo'...
Definition ossignal.h:215
void sig_init(sig_t *sig)
initialize a signal object
void sig_kill(sig_t *sig)
alias of sig_reset()
Definition ossignal.h:148
void sig_reset(sig_t *sig)
reset the signal object and wake up all waiting tasks with 'E_STOPPED' event value
int sig_take(sig_t *sig, unsigned sigset, unsigned *signo)
check signal object for a given set of signals and return the lowest one in the 'signo' variable
void sig_give(sig_t *sig, unsigned signo)
set given signal in the signal object
void sig_set(sig_t *sig, unsigned signo)
alias of sig_give()
Definition ossignal.h:230
int sig_waitUntil(sig_t *sig, unsigned sigset, unsigned *signo, cnt_t time)
wait for a signal from the given set of signals until given timepoint and return the lowest one in th...
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:109
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:102
create and initialize a signal object
Definition ossignal.h:255
void kill()
sig_kill()
Definition ossignal.h:273
void clear(unsigned _signo)
sig_clear()
Definition ossignal.h:283
int tryWait(unsigned _sigset, unsigned *_signo=nullptr)
sig_tryWait()
Definition ossignal.h:275
int take(unsigned _sigset, unsigned *_signo=nullptr)
sig_take()
Definition ossignal.h:274
void reset()
sig_reset()
Definition ossignal.h:272
void give(unsigned _signo)
sig_give()
Definition ossignal.h:281
void set(unsigned _signo)
sig_set()
Definition ossignal.h:282
int waitFor(unsigned _sigset, unsigned *_signo, const T &_delay)
sig_waitFor()
Definition ossignal.h:277
int waitUntil(unsigned _sigset, unsigned *_signo, const T &_time)
sig_waitUntil()
Definition ossignal.h:279
int wait(unsigned _sigset, unsigned *_signo=nullptr)
sig_wait()
Definition ossignal.h:280
constexpr Signal()
constructor, see the constructor parameters of the class
Definition ossignal.h:258