StateOS
Loading...
Searching...
No Matches
osbarrier.h
1/******************************************************************************
2
3 @file StateOS: osbarrier.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_BAR_H
33#define __STATEOS_BAR_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __bar bar_t;
49
50struct __bar
51{
52 obj_t obj;
53
54 unsigned limit;
55};
56
57typedef struct __bar bar_id [];
58
59/******************************************************************************
60 *
61 * Name : _BAR_INIT
62 *
63 * Description : create and initialize a barrier object
64 *
65 * Parameters
66 * limit : number of tasks that must call bar_wait[Until|For] function to release the barrier object
67 *
68 * Return : barrier object
69 *
70 * Note : for internal use
71 *
72 ******************************************************************************/
73
74#define _BAR_INIT( _limit ) { _OBJ_INIT(), _limit }
75
83#define OS_BAR( bar, limit ) \
84 bar_t bar[] = { _BAR_INIT( limit ) }
85
86/** static alias of OS_BAR() */
87#define static_BAR( bar, limit ) \
88 static bar_t bar[] = { _BAR_INIT( limit ) }
89
99
100#ifndef __cplusplus
101#define BAR_INIT( limit ) \
102 _BAR_INIT( limit )
103#endif
104
114
115#ifndef __cplusplus
116#define BAR_CREATE( limit ) \
117 { BAR_INIT ( limit ) }
118/** alias of BAR_CREATE() */
119#define BAR_NEW \
120 BAR_CREATE
121#endif
122
123#ifdef __cplusplus
124extern "C" {
125#endif
126
136void bar_init( bar_t *bar, unsigned limit );
137
149bar_t *bar_create( unsigned limit );
150
152__STATIC_INLINE
153bar_t *bar_new( unsigned limit ) { return bar_create(limit); }
154
163void bar_reset( bar_t *bar );
164
166__STATIC_INLINE
167void bar_kill( bar_t *bar ) { bar_reset(bar); }
168
177void bar_destroy( bar_t *bar );
178
180__STATIC_INLINE
181void bar_delete( bar_t *bar ) { bar_destroy(bar); }
182
199int bar_waitFor( bar_t *bar, cnt_t delay );
200
215int bar_waitUntil( bar_t *bar, cnt_t time );
216
228
229__STATIC_INLINE
230int bar_wait( bar_t *bar ) { return bar_waitFor(bar, INFINITE); }
231
232#ifdef __cplusplus
233}
234#endif
235
236/* -------------------------------------------------------------------------- */
237
238#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
239namespace stateos {
240
248struct Barrier : public __bar
249{
251 constexpr
252 Barrier( const unsigned _limit ): __bar _BAR_INIT(_limit) {}
253
254 ~Barrier() { assert(__bar::obj.queue == nullptr); }
255
256 /// move constructor
257 Barrier( Barrier&& _src ): __bar(_src)
258 {
259 assert(_src.obj.queue == nullptr);
260 }
261
262 Barrier( const Barrier& ) = delete;
263 Barrier& operator=( Barrier&& ) = delete;
264 Barrier& operator=( const Barrier& ) = delete;
266 void reset () { bar_reset (this); }
267 void kill () { bar_kill (this); }
268 void destroy () { bar_destroy (this); }
269 template<typename T>
270 int waitFor ( const T& _delay ) { return bar_waitFor (this, Clock::count(_delay)); }
271 template<typename T>
272 int waitUntil( const T& _time ) { return bar_waitUntil(this, Clock::until(_time)); }
273 int wait () { return bar_wait (this); }
274
275#if __cplusplus >= 201402L
276 using Ptr = std::unique_ptr<Barrier>;
277#else
278 using Ptr = Barrier *;
279#endif
280
290
291 static
292 Ptr Create( const unsigned _limit )
293 {
294 auto bar = new (std::nothrow) Barrier(_limit);
295 if (bar != nullptr)
296 bar->__bar::obj.res = bar;
297 return Ptr(bar);
298 }
299
300};
301
302} // namespace
303#endif//__cplusplus
304
305/* -------------------------------------------------------------------------- */
306
308
309#endif//__STATEOS_BAR_H
int bar_waitUntil(bar_t *bar, cnt_t time)
wait for release the barrier object until given timepoint
int bar_waitFor(bar_t *bar, cnt_t delay)
wait for release the barrier object for given duration of time
void bar_kill(bar_t *bar)
alias of bar_reset()
Definition osbarrier.h:166
void bar_init(bar_t *bar, unsigned limit)
initialize a barrier object
void bar_destroy(bar_t *bar)
reset the barrier object, wake up all waiting tasks with 'E_DELETED' event value and free allocated r...
bar_t * bar_new(unsigned limit)
alias of bar_create()
Definition osbarrier.h:152
int bar_wait(bar_t *bar)
wait indefinitely until the barrier object has been released
Definition osbarrier.h:229
void bar_delete(bar_t *bar)
alias of bar_destroy()
Definition osbarrier.h:180
void bar_reset(bar_t *bar)
reset the barrier object and wake up all waiting tasks with 'E_STOPPED' event value
bar_t * bar_create(unsigned limit)
create and initialize a new barrier object
#define INFINITE
infinite waiting
Definition osbase.h:261
create and initialize a barrier object
Definition osbarrier.h:248
constexpr Barrier(const unsigned _limit)
constructor, see the constructor parameters of the class
Definition osbarrier.h:251
static Ptr Create(const unsigned _limit)
create and initialize dynamic object with manageable resources
Definition osbarrier.h:291
int waitFor(const T &_delay)
bar_waitFor()
Definition osbarrier.h:269
std::unique_ptr< Barrier > Ptr
result type of Create()
Definition osbarrier.h:275
int wait()
bar_wait()
Definition osbarrier.h:272
int waitUntil(const T &_time)
bar_waitUntil()
Definition osbarrier.h:271
void kill()
bar_kill()
Definition osbarrier.h:266
void reset()
bar_reset()
Definition osbarrier.h:265
void destroy()
bar_destroy()
Definition osbarrier.h:267
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