StateOS
Loading...
Searching...
No Matches
osflag.h
1/******************************************************************************
2
3 @file StateOS: osflag.h
4 @author Rajmund Szymanski
5 @date 17.11.2025
6 @brief This file contains definitions for StateOS.
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 __STATEOS_FLG_H
33#define __STATEOS_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
157void flg_init( flg_t *flg, unsigned init );
158
170flg_t *flg_create( unsigned init );
171
173__STATIC_INLINE
174flg_t *flg_new( unsigned init ) { return flg_create(init); }
175
184void flg_reset( flg_t *flg );
185
187__STATIC_INLINE
188void flg_kill( flg_t *flg ) { flg_reset(flg); }
189
198void flg_destroy( flg_t *flg );
199
201__STATIC_INLINE
202void flg_delete( flg_t *flg ) { flg_destroy(flg); }
203
222unsigned flg_take( flg_t *flg, unsigned flags, unsigned mode );
223
225__STATIC_INLINE
226unsigned flg_tryWait( flg_t *flg, unsigned flags, unsigned mode ) { return flg_take(flg, flags, mode); }
227
229__STATIC_INLINE
230unsigned flg_takeISR( flg_t *flg, unsigned flags, unsigned mode ) { return flg_take(flg, flags, mode); }
231
254int flg_waitFor( flg_t *flg, unsigned flags, unsigned mode, cnt_t delay );
255
276int flg_waitUntil( flg_t *flg, unsigned flags, unsigned mode, cnt_t time );
277
295
296__STATIC_INLINE
297int flg_wait( flg_t *flg, unsigned flags, unsigned mode ) { return flg_waitFor(flg, flags, mode, INFINITE); }
298
311unsigned flg_give( flg_t *flg, unsigned flags );
312
314__STATIC_INLINE
315unsigned flg_set( flg_t *flg, unsigned flags ) { return flg_give(flg, flags); }
316
318__STATIC_INLINE
319unsigned flg_giveISR( flg_t *flg, unsigned flags ) { return flg_give(flg, flags); }
320
333unsigned flg_clear( flg_t *flg, unsigned flags );
334
336__STATIC_INLINE
337unsigned flg_clearISR( flg_t *flg, unsigned flags ) { return flg_clear(flg, flags); }
338
350unsigned flg_get( flg_t *flg );
351
353__STATIC_INLINE
354unsigned flg_getISR( flg_t *flg ) { return flg_get(flg); }
355
356#ifdef __cplusplus
357}
358#endif
359
360/* -------------------------------------------------------------------------- */
361
362#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
363namespace stateos {
364
372struct Flag : public __flg
373{
375 constexpr
376 Flag( const unsigned _init = 0 ): __flg _FLG_INIT(_init) {}
377
378 ~Flag() { assert(__flg::obj.queue == nullptr); }
379
380 /// move constructor
381 Flag( Flag&& _src ): __flg(_src)
382 {
383 assert(_src.obj.queue == nullptr);
384 }
385
386 Flag( const Flag& ) = delete;
387 Flag& operator=( Flag&& ) = delete;
388 Flag& operator=( const Flag& ) = delete;
390 void reset () { flg_reset (this); }
391 void kill () { flg_kill (this); }
392 void destroy () { flg_destroy (this); }
393 unsigned take ( unsigned _flags, unsigned _mode = flgAll ) { return flg_take (this, _flags, _mode); }
394 unsigned tryWait ( unsigned _flags, unsigned _mode = flgAll ) { return flg_tryWait (this, _flags, _mode); }
395 unsigned takeISR ( unsigned _flags, unsigned _mode = flgAll ) { return flg_takeISR (this, _flags, _mode); }
396 template<typename T>
397 int waitFor ( unsigned _flags, unsigned _mode, const T& _delay ) { return flg_waitFor (this, _flags, _mode, Clock::count(_delay)); }
398 template<typename T>
399 int waitUntil( unsigned _flags, unsigned _mode, const T& _time ) { return flg_waitUntil(this, _flags, _mode, Clock::until(_time)); }
400 int wait ( unsigned _flags, unsigned _mode = flgAll ) { return flg_wait (this, _flags, _mode); }
401 unsigned give ( unsigned _flags ) { return flg_give (this, _flags); }
402 unsigned set ( unsigned _flags ) { return flg_set (this, _flags); }
403 unsigned giveISR ( unsigned _flags ) { return flg_giveISR (this, _flags); }
404 unsigned clear ( unsigned _flags ) { return flg_clear (this, _flags); }
405 unsigned clearISR ( unsigned _flags ) { return flg_clearISR (this, _flags); }
406 unsigned get () { return flg_get (this); }
407 unsigned getISR () { return flg_getISR (this); }
408
409#if __cplusplus >= 201402L
410 using Ptr = std::unique_ptr<Flag>;
411#else
412 using Ptr = Flag *;
413#endif
414
424
425 static
426 Ptr Create( const unsigned _init = 0 )
427 {
428 auto flg = new (std::nothrow) Flag(_init);
429 if (flg != nullptr)
430 flg->__flg::obj.res = flg;
431 return Ptr(flg);
432 }
433
434};
435
436} // namespace
437#endif//__cplusplus
438
439/* -------------------------------------------------------------------------- */
440
442
443#endif//__STATEOS_FLG_H
#define INFINITE
infinite waiting
Definition osbase.h:261
unsigned flg_tryWait(flg_t *flg, unsigned flags, unsigned mode)
alias of flg_take()
Definition osflag.h:225
void flg_delete(flg_t *flg)
alias of flg_destroy()
Definition osflag.h:201
unsigned flg_set(flg_t *flg, unsigned flags)
alias of flg_give()
Definition osflag.h:314
void flg_kill(flg_t *flg)
alias of flg_reset()
Definition osflag.h:187
unsigned flg_takeISR(flg_t *flg, unsigned flags, unsigned mode)
ISR alias of flg_take().
Definition osflag.h:229
unsigned flg_take(flg_t *flg, unsigned flags, unsigned mode)
check if required flags have been set in the flag object
unsigned flg_getISR(flg_t *flg)
ISR alias of flg_get().
Definition osflag.h:353
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_giveISR(flg_t *flg, unsigned flags)
ISR alias of flg_give().
Definition osflag.h:318
unsigned flg_clear(flg_t *flg, unsigned flags)
clear given flags in flag object
flg_t * flg_create(unsigned init)
create and initialize a new flag object
void flg_reset(flg_t *flg)
reset the flag object and wake up all waiting tasks with 'E_STOPPED' event value
flg_t * flg_new(unsigned init)
alias of flg_create()
Definition osflag.h:173
#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
void flg_destroy(flg_t *flg)
reset the flag object, wake up all waiting tasks with 'E_DELETED' event value and free allocated reso...
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
unsigned flg_clearISR(flg_t *flg, unsigned flags)
ISR alias of flg_clear().
Definition osflag.h:336
int flg_wait(flg_t *flg, unsigned flags, unsigned mode)
wait indefinitely on flag object until required flags have been set
Definition osflag.h:296
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:109
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:116
create and initialize a flag object
Definition osflag.h:372
unsigned tryWait(unsigned _flags, unsigned _mode=flgAll)
flg_tryWait()
Definition osflag.h:393
int waitFor(unsigned _flags, unsigned _mode, const T &_delay)
flg_waitFor()
Definition osflag.h:396
unsigned set(unsigned _flags)
flg_set()
Definition osflag.h:401
void reset()
flg_reset()
Definition osflag.h:389
unsigned takeISR(unsigned _flags, unsigned _mode=flgAll)
flg_takeISR()
Definition osflag.h:394
void destroy()
flg_destroy()
Definition osflag.h:391
constexpr Flag(const unsigned _init=0)
constructor, see the constructor parameters of the class
Definition osflag.h:375
unsigned give(unsigned _flags)
flg_give()
Definition osflag.h:400
std::unique_ptr< Flag > Ptr
result type of Create()
Definition osflag.h:409
unsigned giveISR(unsigned _flags)
flg_giveISR()
Definition osflag.h:402
unsigned getISR()
flg_getISR()
Definition osflag.h:406
void kill()
flg_kill()
Definition osflag.h:390
static Ptr Create(const unsigned _init=0)
create dynamic object with manageable resources
Definition osflag.h:425
unsigned take(unsigned _flags, unsigned _mode=flgAll)
flg_take()
Definition osflag.h:392
unsigned get()
flg_get()
Definition osflag.h:405
int waitUntil(unsigned _flags, unsigned _mode, const T &_time)
flg_waitUntil()
Definition osflag.h:398
unsigned clearISR(unsigned _flags)
flg_clearISR()
Definition osflag.h:404
int wait(unsigned _flags, unsigned _mode=flgAll)
flg_wait()
Definition osflag.h:399
unsigned clear(unsigned _flags)
flg_clear()
Definition osflag.h:403