StateOS
Loading...
Searching...
No Matches
osevent.h
1/******************************************************************************
2
3 @file StateOS: osevent.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_EVT_H
33#define __STATEOS_EVT_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __evt evt_t;
49
50struct __evt
51{
52 obj_t obj;
53};
54
55typedef struct __evt evt_id [];
56
57/******************************************************************************
58 *
59 * Name : _EVT_INIT
60 *
61 * Description : create and initialize an event object
62 *
63 * Parameters : none
64 *
65 * Return : event object
66 *
67 * Note : for internal use
68 *
69 ******************************************************************************/
70
71#define _EVT_INIT() { _OBJ_INIT() }
72
79#define OS_EVT( evt ) \
80 evt_t evt[] = { _EVT_INIT() }
81
82/** static alias of OS_EVT() */
83#define static_EVT( evt ) \
84 static evt_t evt[] = { _EVT_INIT() }
85
93
94#ifndef __cplusplus
95#define EVT_INIT() \
96 _EVT_INIT()
97#endif
98
106
107#ifndef __cplusplus
108#define EVT_CREATE() \
109 { EVT_INIT () }
110/** alias of EVT_CREATE() */
111#define EVT_NEW \
112 EVT_CREATE
113#endif
114
115#ifdef __cplusplus
116extern "C" {
117#endif
118
127void evt_init( evt_t *evt );
128
138evt_t *evt_create( void );
139
141__STATIC_INLINE
142evt_t *evt_new( void ) { return evt_create(); }
143
152void evt_reset( evt_t *evt );
153
155__STATIC_INLINE
156void evt_kill( evt_t *evt ) { evt_reset(evt); }
157
166void evt_destroy( evt_t *evt );
167
169__STATIC_INLINE
170void evt_delete( evt_t *evt ) { evt_destroy(evt); }
171
189int evt_waitFor( evt_t *evt, unsigned *event, cnt_t delay );
190
206int evt_waitUntil( evt_t *evt, unsigned *event, cnt_t time );
207
220
221__STATIC_INLINE
222int evt_wait( evt_t *evt, unsigned *event ) { return evt_waitFor(evt, event, INFINITE); }
223
234void evt_give( evt_t *evt, unsigned event );
235
237__STATIC_INLINE
238void evt_giveISR( evt_t *evt, unsigned event ) { evt_give(evt, event); }
239
240#ifdef __cplusplus
241}
242#endif
243
244/* -------------------------------------------------------------------------- */
245
246#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
247namespace stateos {
248
253struct Event : public __evt
254{
256 constexpr
257 Event(): __evt _EVT_INIT() {}
258
259 ~Event() { assert(__evt::obj.queue == nullptr); }
260
261 /// move constructor
262 Event( Event&& _src ): __evt(_src)
263 {
264 assert(_src.obj.queue == nullptr);
265 }
266
267 Event( const Event& ) = delete;
268 Event& operator=( Event&& ) = delete;
269 Event& operator=( const Event& ) = delete;
271 void reset () { evt_reset (this); }
272 void kill () { evt_kill (this); }
273 void destroy () { evt_destroy (this); }
274 template<typename T>
275 int waitFor ( unsigned *_event, const T& _delay ) { return evt_waitFor (this, _event, Clock::count(_delay)); }
276 template<typename T>
277 int waitFor ( unsigned &_event, const T& _delay ) { return evt_waitFor (this, &_event, Clock::count(_delay)); }
278 template<typename T>
279 int waitUntil( unsigned *_event, const T& _time ) { return evt_waitUntil(this, _event, Clock::until(_time)); }
280 template<typename T>
281 int waitUntil( unsigned &_event, const T& _time ) { return evt_waitUntil(this, &_event, Clock::until(_time)); }
282 int wait ( unsigned *_event = nullptr ) { return evt_wait (this, _event); }
283 int wait ( unsigned &_event ) { return evt_wait (this, &_event); }
284 void give ( unsigned _event ) { evt_give (this, _event); }
285 void giveISR ( unsigned _event ) { evt_giveISR (this, _event); }
286
287#if __cplusplus >= 201402L
288 using Ptr = std::unique_ptr<Event>;
289#else
290 using Ptr = Event *;
291#endif
292
300
301 static
302 Ptr Create()
303 {
304 auto evt = new (std::nothrow) Event();
305 if (evt != nullptr)
306 evt->__evt::obj.res = evt;
307 return Ptr(evt);
308 }
309
310};
311
312} // namespace
313#endif//__cplusplus
314
315/* -------------------------------------------------------------------------- */
316
318
319#endif//__STATEOS_EVT_H
#define INFINITE
infinite waiting
Definition osbase.h:261
evt_t * evt_new(void)
alias of evt_create()
Definition osevent.h:141
int evt_waitFor(evt_t *evt, unsigned *event, cnt_t delay)
wait for release the event object for given duration of time
void evt_init(evt_t *evt)
initialize an event object
void evt_give(evt_t *evt, unsigned event)
resume all tasks that are waiting on the event object
int evt_waitUntil(evt_t *evt, unsigned *event, cnt_t time)
wait for release the event object until given timepoint
evt_t * evt_create(void)
create and initialize a new event object
void evt_delete(evt_t *evt)
alias of evt_destroy()
Definition osevent.h:169
void evt_giveISR(evt_t *evt, unsigned event)
ISR alias of evt_give().
Definition osevent.h:237
void evt_kill(evt_t *evt)
alias of evt_reset()
Definition osevent.h:155
int evt_wait(evt_t *evt, unsigned *event)
wait indefinitely until the event object has been released
Definition osevent.h:221
void evt_destroy(evt_t *evt)
reset the event object, wake up all waiting tasks with 'E_DELETED' event value and free allocated res...
void evt_reset(evt_t *evt)
reset the event object and wake up all waiting tasks with 'E_STOPPED' event value
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 an event object
Definition osevent.h:253
static Ptr Create()
create dynamic object with manageable resources
Definition osevent.h:301
void give(unsigned _event)
evt_give()
Definition osevent.h:283
void destroy()
evt_destroy()
Definition osevent.h:272
int waitFor(unsigned *_event, const T &_delay)
evt_waitFor()
Definition osevent.h:274
int wait(unsigned *_event=nullptr)
evt_wait()
Definition osevent.h:281
int waitUntil(unsigned *_event, const T &_time)
evt_waitUntil()
Definition osevent.h:278
void reset()
evt_reset()
Definition osevent.h:270
int wait(unsigned &_event)
evt_wait()
Definition osevent.h:282
void kill()
evt_kill()
Definition osevent.h:271
void giveISR(unsigned _event)
evt_giveISR()
Definition osevent.h:284
constexpr Event()
constructor, see the constructor parameters of the class
Definition osevent.h:256
std::unique_ptr< Event > Ptr
result type of Create()
Definition osevent.h:287