StateOS
Toggle main menu visibility
Loading...
Searching...
No Matches
osonceflag.h
1
/******************************************************************************
2
3
@file StateOS: osonceflag.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_ONE_H
33
#define __STATEOS_ONE_H
34
35
#include "oskernel.h"
36
#include "oscriticalsection.h"
37
43
47
48
typedef
uint_fast8_t
one_t
;
49
typedef
uint_fast8_t one_id [];
50
51
/******************************************************************************
52
*
53
* Name : _ONE_INIT
54
*
55
* Description : once flag state after initialization
56
*
57
* Parameters : none
58
*
59
* Return : once flag object
60
*
61
* Note : for internal use
62
*
63
******************************************************************************/
64
65
#define _ONE_INIT() 0
66
67
/******************************************************************************
68
*
69
* Name : _ONE_BUSY
70
*
71
* Description : once flag state during execution of the once function
72
*
73
* Parameters : none
74
*
75
* Return : once flag object
76
*
77
* Note : for internal use
78
*
79
******************************************************************************/
80
81
#define _ONE_BUSY() 1
82
83
/******************************************************************************
84
*
85
* Name : _ONE_DONE
86
*
87
* Description : once flag state after use
88
*
89
* Parameters : none
90
*
91
* Return : once flag object
92
*
93
* Note : for internal use
94
*
95
******************************************************************************/
96
97
#define _ONE_DONE() 2
98
104
105
#define OS_ONE( one ) \
106
one_t one[] = { _ONE_INIT() }
107
108
/** static alias of OS_ONE() */
109
#define static_ONE( one ) \
110
static one_t one[] = { _ONE_INIT() }
111
119
120
#ifndef __cplusplus
121
#define ONE_INIT() \
122
_ONE_INIT()
123
#endif
124
132
133
#ifndef __cplusplus
134
#define ONE_CREATE() \
135
{ ONE_INIT () }
136
/** alias of ONE_CREATE() */
137
#define ONE_NEW \
138
ONE_CREATE
139
#endif
140
141
#ifdef __cplusplus
142
extern
"C"
{
143
#endif
144
152
153
__STATIC_INLINE
154
void
one_init
(
one_t
*one ) { *one = 0; }
155
156
/******************************************************************************
157
*
158
* Name : core_one_enter
159
*
160
* Description : start the execution of the once function
161
* if the once function is being executed by another task, wait for its completion
162
*
163
* Parameters
164
* one : pointer to once flag object
165
*
166
* Return
167
* true : the caller must execute the once function and then call core_one_leave
168
* false : the once function has already been executed
169
* (or is being executed and the caller is an interrupt handler)
170
*
171
* Note : for internal use
172
*
173
******************************************************************************/
174
175
bool
core_one_enter(
one_t
*one );
176
177
/******************************************************************************
178
*
179
* Name : core_one_leave
180
*
181
* Description : end the execution of the once function and wake up waiting tasks
182
*
183
* Parameters
184
* one : pointer to once flag object
185
* state : new state of the once flag object
186
* _ONE_DONE(): the once function has been executed
187
* _ONE_INIT(): the once function has failed (exception) and can be executed again
188
*
189
* Return : none
190
*
191
* Note : for internal use
192
*
193
******************************************************************************/
194
195
void
core_one_leave(
one_t
*one,
one_t
state );
196
211
212
__STATIC_INLINE
213
void
one_call
(
one_t
*one,
fun_t
*fun )
214
{
215
assert(one);
216
assert(fun);
217
218
if
(core_one_enter(one))
219
{
220
fun();
221
core_one_leave(one, _ONE_DONE());
222
}
223
}
224
225
#ifdef __cplusplus
226
}
227
#endif
228
229
/* -------------------------------------------------------------------------- */
230
231
#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
232
namespace
stateos {
233
237
238
struct
OnceFlag
239
{
241
constexpr
242
OnceFlag
(): flg_{_ONE_INIT()} {}
243
244
/// move constructor
245
OnceFlag
(
OnceFlag
&& _src ): flg_{_src.flg_}
246
{
247
assert(_src.flg_ != _ONE_BUSY());
// the once function is not being executed
248
}
249
250
OnceFlag
(
const
OnceFlag
& ) =
delete
;
251
OnceFlag
& operator=(
OnceFlag
&& ) =
delete
;
252
OnceFlag
& operator=(
const
OnceFlag
& ) =
delete
;
253
254
#if __cplusplus >= 201402L
256
template
<
typename
Callable,
typename
... Args>
257
void
call
( Callable&& _fun, Args&&... _args )
258
{
259
if
(core_one_enter(&flg_))
260
{
261
#if __cpp_exceptions
262
try
263
{
264
_fun(std::forward<Args>(_args)...);
265
}
266
catch
(...)
267
{
268
core_one_leave(&flg_, _ONE_INIT());
// the once function can be executed again
269
throw
;
270
}
271
#else
272
_fun(std::forward<Args>(_args)...);
273
#endif
274
core_one_leave(&flg_, _ONE_DONE());
275
}
276
}
277
#else
278
void
call
(
fun_t
*_fun )
279
{
280
one_call
(&flg_, _fun);
281
}
282
#endif
283
284
private
:
285
one_t
flg_;
286
};
287
288
}
// namespace
289
#endif
//__cplusplus
290
291
/* -------------------------------------------------------------------------- */
292
294
295
#endif
//__STATEOS_ONE_H
fun_t
void fun_t(void)
timer/task procedure
Definition
osbase.h:137
one_init
void one_init(one_t *one)
initialize a once flag object
Definition
osonceflag.h:153
one_call
void one_call(one_t *one, fun_t *fun)
try to execute given function
Definition
osonceflag.h:212
one_t
uint_fast8_t one_t
once flag
Definition
osonceflag.h:48
stateos::OnceFlag
use a once flag object
Definition
osonceflag.h:238
stateos::OnceFlag::call
void call(Callable &&_fun, Args &&... _args)
call _fun(_args...) once, like std::call_once (one_call())
Definition
osonceflag.h:256
stateos::OnceFlag::OnceFlag
constexpr OnceFlag()
constructor, see the constructor parameters of the class
Definition
osonceflag.h:241
kernel
inc
osonceflag.h
Generated by
1.18.0