StateOS
Loading...
Searching...
No Matches
osconditionvariable.h
1/******************************************************************************
2
3 @file StateOS: osconditionvariable.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_CND_H
33#define __STATEOS_CND_H
34
35#include "oskernel.h"
36#include "osclock.h"
37#include "osmutex.h"
38
44
45/* -------------------------------------------------------------------------- */
46
47#define cndOne ( false )
48#define cndAll ( true )
49
53
54typedef struct __cnd cnd_t;
55
56struct __cnd
57{
58 obj_t obj;
59};
60
61typedef struct __cnd cnd_id [];
62
63/******************************************************************************
64 *
65 * Name : _CND_INIT
66 *
67 * Description : create and initialize a condition variable object
68 *
69 * Parameters : none
70 *
71 * Return : condition variable object
72 *
73 * Note : for internal use
74 *
75 ******************************************************************************/
76
77#define _CND_INIT() { _OBJ_INIT() }
78
85#define OS_CND( cnd ) \
86 cnd_t cnd[] = { _CND_INIT() }
87
88/** static alias of OS_CND() */
89#define static_CND( cnd ) \
90 static cnd_t cnd[] = { _CND_INIT() }
91
99
100#ifndef __cplusplus
101#define CND_INIT() \
102 _CND_INIT()
103#endif
104
112
113#ifndef __cplusplus
114#define CND_CREATE() \
115 { CND_INIT () }
116/** alias of CND_CREATE() */
117#define CND_NEW \
118 CND_CREATE
119#endif
120
121#ifdef __cplusplus
122extern "C" {
123#endif
124
133void cnd_init( cnd_t *cnd );
134
144cnd_t *cnd_create( void );
145
147__STATIC_INLINE
148cnd_t *cnd_new( void ) { return cnd_create(); }
149
158void cnd_reset( cnd_t *cnd );
159
161__STATIC_INLINE
162void cnd_kill( cnd_t *cnd ) { cnd_reset(cnd); }
163
172void cnd_destroy( cnd_t *cnd );
173
175__STATIC_INLINE
176void cnd_delete( cnd_t *cnd ) { cnd_destroy(cnd); }
177
198int cnd_waitFor( cnd_t *cnd, mtx_t *mtx, cnt_t delay );
199
218int cnd_waitUntil( cnd_t *cnd, mtx_t *mtx, cnt_t time );
219
235
236__STATIC_INLINE
237int cnd_wait( cnd_t *cnd, mtx_t *mtx ) { return cnd_waitFor(cnd, mtx, INFINITE); }
238
251void cnd_give( cnd_t *cnd, bool all );
252
254__STATIC_INLINE
255void cnd_giveISR( cnd_t *cnd, bool all ) { cnd_give(cnd, all); }
256
264
265__STATIC_INLINE
266void cnd_notifyOne( cnd_t *cnd ) { cnd_give(cnd, cndOne); }
267
275
276__STATIC_INLINE
277void cnd_notifyAll( cnd_t *cnd ) { cnd_give(cnd, cndAll); }
278
279#ifdef __cplusplus
280}
281#endif
282
283/* -------------------------------------------------------------------------- */
284
285#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
286namespace stateos {
287
292struct ConditionVariable : public __cnd
293{
295 constexpr
296 ConditionVariable(): __cnd _CND_INIT() {}
297
298 ~ConditionVariable() { assert(__cnd::obj.queue == nullptr); }
299
300 /// move constructor
301 ConditionVariable( ConditionVariable&& _src ): __cnd(_src)
302 {
303 assert(_src.obj.queue == nullptr);
304 }
305
306 ConditionVariable( const ConditionVariable& ) = delete;
307 ConditionVariable& operator=( ConditionVariable&& ) = delete;
308 ConditionVariable& operator=( const ConditionVariable& ) = delete;
310 void reset () { cnd_reset (this); }
311 void kill () { cnd_kill (this); }
312 void destroy () { cnd_destroy (this); }
313 template<typename T>
314 int waitFor ( mtx_t& _mtx, const T& _delay ) { return cnd_waitFor (this, &_mtx, Clock::count(_delay)); }
315 template<typename T>
316 int waitUntil( mtx_t& _mtx, const T& _time ) { return cnd_waitUntil(this, &_mtx, Clock::until(_time)); }
317 int wait ( mtx_t& _mtx ) { return cnd_wait (this, &_mtx); }
318 void give ( bool _all = cndAll ) { cnd_give (this, _all); }
319 void giveISR ( bool _all = cndAll ) { cnd_giveISR (this, _all); }
320 void notifyOne() { cnd_notifyOne(this); }
321 void notifyAll() { cnd_notifyAll(this); }
323 template<typename T, typename F>
324 int waitFor ( mtx_t& _mtx, const T& _delay, F _stopWaiting )
325 {
326 auto _time = Clock::count(_delay);
327 return (_time == INFINITE) ? wait(_mtx, _stopWaiting)
328 : waitUntil(_mtx, sys_time() + _time, _stopWaiting);
329 }
331 template<typename T, typename F>
332 int waitUntil( mtx_t& _mtx, const T& _time, F _stopWaiting )
333 {
334 int result = E_SUCCESS;
335 while (!_stopWaiting() && (result = waitUntil(_mtx, _time)) == E_SUCCESS);
336 if (result == E_TIMEOUT && _stopWaiting()) // the mutex is held again, the condition may have been satisfied during the timeout
337 result = E_SUCCESS;
338 return result;
339 }
341 template<typename F>
342 int wait ( mtx_t& _mtx, F _stopWaiting )
343 {
344 int result = E_SUCCESS;
345 while (!_stopWaiting() && (result = wait(_mtx)) == E_SUCCESS);
346 return result;
347 }
348
349#if __cplusplus >= 201402L
350 using Ptr = std::unique_ptr<ConditionVariable>;
351#else
352 using Ptr = ConditionVariable *;
353#endif
354
362
363 static
364 Ptr Create()
365 {
366 auto cnd = new (std::nothrow) ConditionVariable();
367 if (cnd != nullptr)
368 cnd->__cnd::obj.res = cnd;
369 return Ptr(cnd);
370 }
371
372};
373
374} // namespace
375#endif//__cplusplus
376
377/* -------------------------------------------------------------------------- */
378
380
381#endif//__STATEOS_CND_H
#define INFINITE
infinite waiting
Definition osbase.h:261
@ E_TIMEOUT
process was released as a result of the end of the timer countdown
Definition osbase.h:150
@ E_SUCCESS
process was released as a result of taking the supervising object
Definition osbase.h:146
cnt_t sys_time(void)
return current value of system counter
void cnd_notifyAll(cnd_t *cnd)
signal all tasks waiting on the condition variable
Definition osconditionvariable.h:276
void cnd_delete(cnd_t *cnd)
alias of cnd_destroy()
Definition osconditionvariable.h:175
int cnd_wait(cnd_t *cnd, mtx_t *mtx)
wait indefinitely on the condition variable releasing the currently owned mutex, and finally lock the...
Definition osconditionvariable.h:236
void cnd_init(cnd_t *cnd)
initialize a condition variable object
void cnd_reset(cnd_t *cnd)
reset the condition variable object and wake up all waiting tasks with 'E_STOPPED' event value
#define cndAll
notify all tasks (broadcast)
Definition osconditionvariable.h:48
void cnd_kill(cnd_t *cnd)
alias of cnd_reset()
Definition osconditionvariable.h:161
void cnd_destroy(cnd_t *cnd)
reset the condition variable object, wake up all waiting tasks with 'E_DELETED' event value and free ...
cnd_t * cnd_new(void)
alias of cnd_create()
Definition osconditionvariable.h:147
void cnd_notifyOne(cnd_t *cnd)
signal one task waiting on the condition variable
Definition osconditionvariable.h:265
cnd_t * cnd_create(void)
create and initialize a new condition variable object
int cnd_waitUntil(cnd_t *cnd, mtx_t *mtx, cnt_t time)
wait until given timepoint on the condition variable releasing the currently owned mutex,...
int cnd_waitFor(cnd_t *cnd, mtx_t *mtx, cnt_t delay)
wait for given duration of time on the condition variable releasing the currently owned mutex,...
void cnd_giveISR(cnd_t *cnd, bool all)
ISR alias of cnd_give().
Definition osconditionvariable.h:254
void cnd_give(cnd_t *cnd, bool all)
signal one or all tasks that are waiting on the condition variable
#define cndOne
notify one task
Definition osconditionvariable.h:47
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 condition variable object
Definition osconditionvariable.h:292
void notifyOne()
cnd_notifyOne()
Definition osconditionvariable.h:319
int wait(mtx_t &_mtx)
cnd_wait()
Definition osconditionvariable.h:316
static Ptr Create()
create and initialize dynamic object with manageable resources
Definition osconditionvariable.h:363
void notifyAll()
cnd_notifyAll()
Definition osconditionvariable.h:320
std::unique_ptr< ConditionVariable > Ptr
result type of Create()
Definition osconditionvariable.h:349
void reset()
cnd_reset()
Definition osconditionvariable.h:309
void kill()
cnd_kill()
Definition osconditionvariable.h:310
void giveISR(bool _all=cndAll)
cnd_giveISR()
Definition osconditionvariable.h:318
int waitUntil(mtx_t &_mtx, const T &_time)
cnd_waitUntil()
Definition osconditionvariable.h:315
void destroy()
cnd_destroy()
Definition osconditionvariable.h:311
void give(bool _all=cndAll)
cnd_give()
Definition osconditionvariable.h:317
int waitFor(mtx_t &_mtx, const T &_delay)
cnd_waitFor()
Definition osconditionvariable.h:313
constexpr ConditionVariable()
constructor, see the constructor parameters of the class
Definition osconditionvariable.h:295