IntrOS
Loading...
Searching...
No Matches
osflag.h
1/******************************************************************************
2
3 @file IntrOS: osflag.h
4 @author Rajmund Szymanski
5 @date 17.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_FLG_H
33#define __INTROS_FLG_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
44/* -------------------------------------------------------------------------- */
45
46#define flgAny 0U
47#define flgAll 1U
48#define flgNew 2U
49
50#define flgAnyNew ( flgAny | flgNew )
51#define flgAllNew ( flgAll | flgNew )
52
56
57typedef struct __flg flg_t;
58
59struct __flg
60{
61 obj_t obj;
62
63 unsigned flags;
64};
65
66typedef struct __flg flg_id [];
67
68/******************************************************************************
69 *
70 * Name : _FLG_INIT
71 *
72 * Description : create and initialize a flag object
73 *
74 * Parameters
75 * init : initial value of flag
76 *
77 * Return : flag object
78 *
79 * Note : for internal use
80 *
81 ******************************************************************************/
82
83#define _FLG_INIT( _init ) { _OBJ_INIT(), _init }
84
85/******************************************************************************
86 *
87 * Name : _VA_FLG
88 *
89 * Description : calculate initial value of flag from optional parameter
90 *
91 * Note : for internal use
92 *
93 ******************************************************************************/
94
95#define _VA_FLG( _init ) ( _init + 0 )
96
104#define OS_FLG( flg, ... ) \
105 flg_t flg[] = { _FLG_INIT( _VA_FLG(__VA_ARGS__) ) }
106
107/** static alias of OS_FLG() */
108#define static_FLG( flg, ... ) \
109 static flg_t flg[] = { _FLG_INIT( _VA_FLG(__VA_ARGS__) ) }
110
120
121#ifndef __cplusplus
122#define FLG_INIT( ... ) \
123 _FLG_INIT( _VA_FLG(__VA_ARGS__) )
124#endif
125
135
136#ifndef __cplusplus
137#define FLG_CREATE( ... ) \
138 { FLG_INIT ( _VA_FLG(__VA_ARGS__) ) }
139/** alias of FLG_CREATE() */
140#define FLG_NEW \
141 FLG_CREATE
142#endif
143
144#ifdef __cplusplus
145extern "C" {
146#endif
147
155void flg_init( flg_t *flg, unsigned init );
156
165void flg_reset( flg_t *flg );
166
168__STATIC_INLINE
169void flg_kill( flg_t *flg ) { flg_reset(flg); }
170
186unsigned flg_take( flg_t *flg, unsigned flags, unsigned mode );
187
189__STATIC_INLINE
190unsigned flg_tryWait( flg_t *flg, unsigned flags, unsigned mode ) { return flg_take(flg, flags, mode); }
191
211int flg_waitFor( flg_t *flg, unsigned flags, unsigned mode, cnt_t delay );
212
230int flg_waitUntil( flg_t *flg, unsigned flags, unsigned mode, cnt_t time );
231
246
247__STATIC_INLINE
248int flg_wait( flg_t *flg, unsigned flags, unsigned mode ) { return flg_waitFor(flg, flags, mode, INFINITE); }
249
259unsigned flg_give( flg_t *flg, unsigned flags );
260
262__STATIC_INLINE
263unsigned flg_set( flg_t *flg, unsigned flags ) { return flg_give(flg, flags); }
264
274unsigned flg_clear( flg_t *flg, unsigned flags );
275
284unsigned flg_get( flg_t *flg );
285
286#ifdef __cplusplus
287}
288#endif
289
290/* -------------------------------------------------------------------------- */
291
292#if defined(__cplusplus)
293namespace intros {
294
302struct Flag : public __flg
303{
305 constexpr
306 Flag( const unsigned _init = 0 ): __flg _FLG_INIT(_init) {}
307
308 ~Flag() { assert(__flg::obj.queue == nullptr); }
309
310 /// move constructor
311 Flag( Flag&& _src ): __flg(_src)
312 {
313 assert(_src.obj.queue == nullptr);
314 }
315
316 Flag( const Flag& ) = delete;
317 Flag& operator=( Flag&& ) = delete;
318 Flag& operator=( const Flag& ) = delete;
320 void reset () { flg_reset (this); }
321 void kill () { flg_kill (this); }
322 unsigned take ( unsigned _flags, unsigned _mode = flgAll ) { return flg_take (this, _flags, _mode); }
323 unsigned tryWait ( unsigned _flags, unsigned _mode = flgAll ) { return flg_tryWait (this, _flags, _mode); }
324 template<typename T>
325 int waitFor ( unsigned _flags, unsigned _mode, const T& _delay ) { return flg_waitFor (this, _flags, _mode, Clock::count(_delay)); }
326 template<typename T>
327 int waitUntil( unsigned _flags, unsigned _mode, const T& _time ) { return flg_waitUntil(this, _flags, _mode, Clock::until(_time)); }
328 int wait ( unsigned _flags, unsigned _mode = flgAll ) { return flg_wait (this, _flags, _mode); }
329 unsigned give ( unsigned _flags ) { return flg_give (this, _flags); }
330 unsigned set ( unsigned _flags ) { return flg_set (this, _flags); }
331 unsigned clear ( unsigned _flags ) { return flg_clear (this, _flags); }
332 unsigned get () { return flg_get (this); }
333};
334
335} // namespace
336#endif//__cplusplus
337
338/* -------------------------------------------------------------------------- */
339
341
342#endif//__INTROS_FLG_H
#define INFINITE
infinite waiting
Definition osbase.h:250
unsigned flg_tryWait(flg_t *flg, unsigned flags, unsigned mode)
alias of flg_take()
Definition osflag.h:189
unsigned flg_set(flg_t *flg, unsigned flags)
alias of flg_give()
Definition osflag.h:262
void flg_kill(flg_t *flg)
alias of flg_reset()
Definition osflag.h:168
unsigned flg_take(flg_t *flg, unsigned flags, unsigned mode)
check if required flags have been set in the flag object
unsigned flg_get(flg_t *flg)
get current flags from flag object
unsigned flg_give(flg_t *flg, unsigned flags)
set given flags in flag object
unsigned flg_clear(flg_t *flg, unsigned flags)
clear given flags in flag object
void flg_reset(flg_t *flg)
reset the flag object and wake up all waiting tasks with 'E_STOPPED' event value
#define flgAll
wait for all flags to be set
Definition osflag.h:47
void flg_init(flg_t *flg, unsigned init)
initialize a flag object
int flg_waitFor(flg_t *flg, unsigned flags, unsigned mode, cnt_t delay)
wait on flag object for given flags for given duration of time
int flg_waitUntil(flg_t *flg, unsigned flags, unsigned mode, cnt_t time)
wait on flag object for given flags until given timepoint
int flg_wait(flg_t *flg, unsigned flags, unsigned mode)
wait indefinitely on flag object until required flags have been set
Definition osflag.h:247
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 flag object
Definition osflag.h:302
int wait(unsigned _flags, unsigned _mode=flgAll)
flg_wait()
Definition osflag.h:327
unsigned set(unsigned _flags)
flg_set()
Definition osflag.h:329
int waitFor(unsigned _flags, unsigned _mode, const T &_delay)
flg_waitFor()
Definition osflag.h:324
void reset()
flg_reset()
Definition osflag.h:319
unsigned clear(unsigned _flags)
flg_clear()
Definition osflag.h:330
unsigned get()
flg_get()
Definition osflag.h:331
unsigned give(unsigned _flags)
flg_give()
Definition osflag.h:328
void kill()
flg_kill()
Definition osflag.h:320
unsigned take(unsigned _flags, unsigned _mode=flgAll)
flg_take()
Definition osflag.h:321
int waitUntil(unsigned _flags, unsigned _mode, const T &_time)
flg_waitUntil()
Definition osflag.h:326
unsigned tryWait(unsigned _flags, unsigned _mode=flgAll)
flg_tryWait()
Definition osflag.h:322
constexpr Flag(const unsigned _init=0)
constructor, see the constructor parameters of the class
Definition osflag.h:305