IntrOS
Loading...
Searching...
No Matches
osbarrier.h
1/******************************************************************************
2
3 @file IntrOS: osbarrier.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_BAR_H
33#define __INTROS_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
134void bar_init( bar_t *bar, unsigned limit );
135
144void bar_reset( bar_t *bar );
145
147__STATIC_INLINE
148void bar_kill( bar_t *bar ) { bar_reset(bar); }
149
163int bar_waitFor( bar_t *bar, cnt_t delay );
164
176int bar_waitUntil( bar_t *bar, cnt_t time );
177
186
187__STATIC_INLINE
188int bar_wait( bar_t *bar ) { return bar_waitFor(bar, INFINITE); }
189
190#ifdef __cplusplus
191}
192#endif
193
194/* -------------------------------------------------------------------------- */
195
196#if defined(__cplusplus)
197namespace intros {
198
206struct Barrier : public __bar
207{
209 constexpr
210 Barrier( const unsigned _limit ): __bar _BAR_INIT(_limit) {}
211
212 ~Barrier() { assert(__bar::obj.queue == nullptr); }
213
214 /// move constructor
215 Barrier( Barrier&& _src ): __bar(_src)
216 {
217 assert(_src.obj.queue == nullptr);
218 }
219
220 Barrier( const Barrier& ) = delete;
221 Barrier& operator=( Barrier&& ) = delete;
222 Barrier& operator=( const Barrier& ) = delete;
224 void reset () { bar_reset (this); }
225 void kill () { bar_kill (this); }
226 template<typename T>
227 int waitFor ( const T& _delay ) { return bar_waitFor (this, Clock::count(_delay)); }
228 template<typename T>
229 int waitUntil( const T& _time ) { return bar_waitUntil(this, Clock::until(_time)); }
230 int wait () { return bar_wait (this); }
231};
232
233} // namespace
234#endif//__cplusplus
235
236/* -------------------------------------------------------------------------- */
237
239
240#endif//__INTROS_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:147
void bar_init(bar_t *bar, unsigned limit)
initialize a barrier object
int bar_wait(bar_t *bar)
wait indefinitely until the barrier object has been released
Definition osbarrier.h:187
void bar_reset(bar_t *bar)
reset the barrier object and wake up all waiting tasks with 'E_STOPPED' event value
#define INFINITE
infinite waiting
Definition osbase.h:250
create and initialize a barrier object
Definition osbarrier.h:206
void reset()
bar_reset()
Definition osbarrier.h:223
int waitFor(const T &_delay)
bar_waitFor()
Definition osbarrier.h:226
constexpr Barrier(const unsigned _limit)
constructor, see the constructor parameters of the class
Definition osbarrier.h:209
int wait()
bar_wait()
Definition osbarrier.h:229
int waitUntil(const T &_time)
bar_waitUntil()
Definition osbarrier.h:228
void kill()
bar_kill()
Definition osbarrier.h:224
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