StateOS
Loading...
Searching...
No Matches
oseventqueue.h
1/******************************************************************************
2
3 @file StateOS: oseventqueue.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_EVQ_H
33#define __STATEOS_EVQ_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __evq evq_t;
49
50struct __evq
51{
52 obj_t obj;
53
54 unsigned count;
55 unsigned limit;
56
57 unsigned head;
58 unsigned tail;
59 unsigned*data;
60};
61
62typedef struct __evq evq_id [];
63
64/******************************************************************************
65 *
66 * Name : _EVQ_INIT
67 *
68 * Description : create and initialize an event queue object
69 *
70 * Parameters
71 * limit : size of a queue (max number of stored events)
72 * data : event queue data buffer
73 *
74 * Return : event queue object
75 *
76 * Note : for internal use
77 *
78 ******************************************************************************/
79
80#define _EVQ_INIT( _limit, _data ) { _OBJ_INIT(), 0, _limit, 0, 0, _data }
81
82/******************************************************************************
83 *
84 * Name : _EVQ_DATA
85 *
86 * Description : create an event queue data buffer
87 *
88 * Parameters
89 * limit : size of a queue (max number of stored events)
90 *
91 * Return : event queue data buffer
92 *
93 * Note : for internal use
94 *
95 ******************************************************************************/
96
97#ifndef __cplusplus
98#define _EVQ_DATA( _limit ) (unsigned[_limit]){ 0 }
99#endif
100
108#define OS_EVQ_BUFFER( buf, limit ) \
109 unsigned buf[limit]
110
118#define OS_EVQ( evq, limit ) \
119 static unsigned evq##__buf[limit]; \
120 evq_t evq[] = { _EVQ_INIT( limit, evq##__buf ) }
121
122/** static alias of OS_EVQ() */
123#define static_EVQ( evq, limit ) \
124 static unsigned evq##__buf[limit]; \
125 static evq_t evq[] = { _EVQ_INIT( limit, evq##__buf ) }
126
136
137#ifndef __cplusplus
138#define EVQ_INIT( limit ) \
139 _EVQ_INIT( limit, _EVQ_DATA( limit ) )
140#endif
141
151
152#ifndef __cplusplus
153#define EVQ_CREATE( limit ) \
154 { EVQ_INIT ( limit ) }
155/** alias of EVQ_CREATE() */
156#define EVQ_NEW \
157 EVQ_CREATE
158#endif
159
160#ifdef __cplusplus
161extern "C" {
162#endif
163
174void evq_init( evq_t *evq, unsigned *data, size_t bufsize );
175
187evq_t *evq_create( unsigned limit );
188
190__STATIC_INLINE
191evq_t *evq_new( unsigned limit ) { return evq_create(limit); }
192
201void evq_reset( evq_t *evq );
202
204__STATIC_INLINE
205void evq_kill( evq_t *evq ) { evq_reset(evq); }
206
215void evq_destroy( evq_t *evq );
216
218__STATIC_INLINE
219void evq_delete( evq_t *evq ) { evq_destroy(evq); }
220
236int evq_take( evq_t *evq, unsigned *event );
237
239__STATIC_INLINE
240int evq_tryWait( evq_t *evq, unsigned *event ) { return evq_take(evq, event); }
241
243__STATIC_INLINE
244int evq_takeISR( evq_t *evq, unsigned *event ) { return evq_take(evq, event); }
245
246#if OS_ATOMICS
248int evq_takeAsync( evq_t *evq, unsigned *event );
249#endif
250
269int evq_waitFor( evq_t *evq, unsigned *event, cnt_t delay );
270
287int evq_waitUntil( evq_t *evq, unsigned *event, cnt_t time );
288
305
306__STATIC_INLINE
307int evq_wait( evq_t *evq, unsigned *event ) { return evq_waitFor(evq, event, INFINITE); }
308
309#if OS_ATOMICS
311int evq_waitAsync( evq_t *evq, unsigned *event );
312#endif
313
329int evq_give( evq_t *evq, unsigned event );
330
332__STATIC_INLINE
333int evq_giveISR( evq_t *evq, unsigned event ) { return evq_give(evq, event); }
334
335#if OS_ATOMICS
337int evq_giveAsync( evq_t *evq, unsigned event );
338#endif
339
358int evq_sendFor( evq_t *evq, unsigned event, cnt_t delay );
359
376int evq_sendUntil( evq_t *evq, unsigned event, cnt_t time );
377
394
395__STATIC_INLINE
396int evq_send( evq_t *evq, unsigned event ) { return evq_sendFor(evq, event, INFINITE); }
397
398#if OS_ATOMICS
400int evq_sendAsync( evq_t *evq, unsigned event );
401#endif
402
414void evq_push( evq_t *evq, unsigned event );
415
417__STATIC_INLINE
418void evq_pushISR( evq_t *evq, unsigned event ) { evq_push(evq, event); }
419
431unsigned evq_count( evq_t *evq );
432
434__STATIC_INLINE
435unsigned evq_countISR( evq_t *evq ) { return evq_count(evq); }
436
448unsigned evq_space( evq_t *evq );
449
451__STATIC_INLINE
452unsigned evq_spaceISR( evq_t *evq ) { return evq_space(evq); }
453
465unsigned evq_limit( evq_t *evq );
466
468__STATIC_INLINE
469unsigned evq_limitISR( evq_t *evq ) { return evq_limit(evq); }
470
471#ifdef __cplusplus
472}
473#endif
474
475/* -------------------------------------------------------------------------- */
476
477#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
478namespace stateos {
479
486
487template<unsigned limit_>
488struct EventQueueT : public __evq
489{
491 constexpr
492 EventQueueT(): __evq _EVQ_INIT(limit_, data_) {}
493
494 ~EventQueueT() { assert(__evq::obj.queue == nullptr); }
495
496 /// move constructor
497 EventQueueT( EventQueueT&& _src ): __evq _EVQ_INIT(limit_, data_)
498 {
499 assert(_src.__evq::count == 0 && _src.obj.queue == nullptr);
500 (void) _src;
501 }
502
503 EventQueueT( const EventQueueT& ) = delete;
504 EventQueueT& operator=( EventQueueT&& ) = delete;
505 EventQueueT& operator=( const EventQueueT& ) = delete;
507 void reset () { evq_reset (this); }
508 void kill () { evq_kill (this); }
509 void destroy () { evq_destroy (this); }
510 int take ( unsigned *_event = nullptr ) { return evq_take (this, _event); }
511 int take ( unsigned &_event ) { return evq_take (this, &_event); }
512 int tryWait ( unsigned *_event = nullptr ) { return evq_tryWait (this, _event); }
513 int tryWait ( unsigned &_event ) { return evq_tryWait (this, &_event); }
514 int takeISR ( unsigned *_event ) { return evq_takeISR (this, _event); }
515 template<typename T>
516 int waitFor ( unsigned *_event, const T& _delay ) { return evq_waitFor (this, _event, Clock::count(_delay)); }
517 template<typename T>
518 int waitUntil( unsigned *_event, const T& _time ) { return evq_waitUntil(this, _event, Clock::until(_time)); }
519 int wait ( unsigned *_event = nullptr ) { return evq_wait (this, _event); }
520 int wait ( unsigned &_event ) { return evq_wait (this, &_event); }
521 int give ( unsigned _event ) { return evq_give (this, _event); }
522 int giveISR ( unsigned _event ) { return evq_giveISR (this, _event); }
523 template<typename T>
524 int sendFor ( unsigned _event, const T& _delay ) { return evq_sendFor (this, _event, Clock::count(_delay)); }
525 template<typename T>
526 int sendUntil( unsigned _event, const T& _time ) { return evq_sendUntil(this, _event, Clock::until(_time)); }
527 int send ( unsigned _event ) { return evq_send (this, _event); }
528 void push ( unsigned _event ) { evq_push (this, _event); }
529 void pushISR ( unsigned _event ) { evq_pushISR (this, _event); }
530 unsigned count () { return evq_count (this); }
531 unsigned countISR () { return evq_countISR (this); }
532 unsigned space () { return evq_space (this); }
533 unsigned spaceISR () { return evq_spaceISR (this); }
534 unsigned limit () { return evq_limit (this); }
535 unsigned limitISR () { return evq_limitISR (this); }
536#if OS_ATOMICS
537 int takeAsync( unsigned *_event = nullptr ) { return evq_takeAsync(this, _event); }
538 int takeAsync( unsigned &_event ) { return evq_takeAsync(this, &_event); }
539 int waitAsync( unsigned *_event = nullptr ) { return evq_waitAsync(this, _event); }
540 int waitAsync( unsigned &_event ) { return evq_waitAsync(this, &_event); }
541 int giveAsync( unsigned _event ) { return evq_giveAsync(this, _event); }
542 int sendAsync( unsigned _event ) { return evq_sendAsync(this, _event); }
543#endif
544
545#if __cplusplus >= 201402L
546 using Ptr = std::unique_ptr<EventQueueT<limit_>>;
547#else
548 using Ptr = EventQueueT<limit_> *;
549#endif
550
561
562 static
563 Ptr Create()
564 {
565 auto evq = new (std::nothrow) EventQueueT<limit_>();
566 if (evq != nullptr)
567 evq->__evq::obj.res = evq;
568 return Ptr(evq);
569 }
570
571 private:
572 unsigned data_[limit_];
573};
574
575} // namespace
576#endif//__cplusplus
577
578/* -------------------------------------------------------------------------- */
579
581
582#endif//__STATEOS_EVQ_H
#define INFINITE
infinite waiting
Definition osbase.h:261
int evq_give(evq_t *evq, unsigned event)
try to transfer event value to the event queue object, don't wait if the event queue object is full
unsigned evq_limitISR(evq_t *evq)
ISR alias of evq_limit().
Definition oseventqueue.h:468
void evq_push(evq_t *evq, unsigned event)
transfer event value to the event queue object, remove the oldest event value if the event queue obje...
void evq_init(evq_t *evq, unsigned *data, size_t bufsize)
initialize an event queue object
int evq_tryWait(evq_t *evq, unsigned *event)
alias of evq_take()
Definition oseventqueue.h:239
int evq_waitUntil(evq_t *evq, unsigned *event, cnt_t time)
try to transfer event value from the event queue object, wait until given timepoint while the event q...
int evq_sendFor(evq_t *evq, unsigned event, cnt_t delay)
try to transfer event value to the event queue object, wait for given duration of time while the even...
evq_t * evq_new(unsigned limit)
alias of evq_create()
Definition oseventqueue.h:190
void evq_kill(evq_t *evq)
alias of evq_reset()
Definition oseventqueue.h:204
int evq_giveAsync(evq_t *evq, unsigned event)
Async alias of evq_give().
int evq_wait(evq_t *evq, unsigned *event)
try to transfer event value from the event queue object, wait indefinitely while the event queue obje...
Definition oseventqueue.h:306
unsigned evq_spaceISR(evq_t *evq)
ISR alias of evq_space().
Definition oseventqueue.h:451
unsigned evq_space(evq_t *evq)
return the amount of free space in the event queue
int evq_waitFor(evq_t *evq, unsigned *event, cnt_t delay)
try to transfer event value from the event queue object, wait for given duration of time while the ev...
unsigned evq_count(evq_t *evq)
return the amount of data contained in the event queue
void evq_delete(evq_t *evq)
alias of evq_destroy()
Definition oseventqueue.h:218
void evq_reset(evq_t *evq)
reset the event queue object and wake up all waiting tasks with 'E_STOPPED' event value
int evq_giveISR(evq_t *evq, unsigned event)
ISR alias of evq_give().
Definition oseventqueue.h:332
void evq_pushISR(evq_t *evq, unsigned event)
ISR alias of evq_push().
Definition oseventqueue.h:417
int evq_sendAsync(evq_t *evq, unsigned event)
Async alias of evq_send().
int evq_sendUntil(evq_t *evq, unsigned event, cnt_t time)
try to transfer event value to the event queue object, wait until given timepoint while the event que...
int evq_send(evq_t *evq, unsigned event)
try to transfer event value to the event queue object, wait indefinitely while the event queue object...
Definition oseventqueue.h:395
int evq_take(evq_t *evq, unsigned *event)
try to transfer event value from the event queue object, don't wait if the event queue object is empt...
int evq_takeAsync(evq_t *evq, unsigned *event)
Async alias of evq_take().
void evq_destroy(evq_t *evq)
reset the event queue object, wake up all waiting tasks with 'E_DELETED' event value and free allocat...
int evq_takeISR(evq_t *evq, unsigned *event)
ISR alias of evq_take().
Definition oseventqueue.h:243
unsigned evq_limit(evq_t *evq)
return the size of the event queue
evq_t * evq_create(unsigned limit)
create and initialize a new event queue object
int evq_waitAsync(evq_t *evq, unsigned *event)
Async alias of evq_wait().
unsigned evq_countISR(evq_t *evq)
ISR alias of evq_count().
Definition oseventqueue.h:434
char data[]
message data
Definition osmessagequeue.h:54
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 queue object
Definition oseventqueue.h:488
int send(unsigned _event)
evq_send()
Definition oseventqueue.h:526
int take(unsigned &_event)
evq_take()
Definition oseventqueue.h:510
unsigned space()
evq_space()
Definition oseventqueue.h:531
constexpr EventQueueT()
constructor, see the constructor parameters of the class
Definition oseventqueue.h:491
unsigned limitISR()
evq_limitISR()
Definition oseventqueue.h:534
int giveISR(unsigned _event)
evq_giveISR()
Definition oseventqueue.h:521
void reset()
evq_reset()
Definition oseventqueue.h:506
unsigned spaceISR()
evq_spaceISR()
Definition oseventqueue.h:532
void destroy()
evq_destroy()
Definition oseventqueue.h:508
int takeAsync(unsigned &_event)
evq_takeAsync()
Definition oseventqueue.h:537
int takeISR(unsigned *_event)
evq_takeISR()
Definition oseventqueue.h:513
void kill()
evq_kill()
Definition oseventqueue.h:507
unsigned count()
evq_count()
Definition oseventqueue.h:529
int sendAsync(unsigned _event)
evq_sendAsync()
Definition oseventqueue.h:541
std::unique_ptr< EventQueueT< limit_ > > Ptr
result type of Create()
Definition oseventqueue.h:545
int take(unsigned *_event=nullptr)
evq_take()
Definition oseventqueue.h:509
unsigned limit()
evq_limit()
Definition oseventqueue.h:533
int waitFor(unsigned *_event, const T &_delay)
evq_waitFor()
Definition oseventqueue.h:515
int wait(unsigned *_event=nullptr)
evq_wait()
Definition oseventqueue.h:518
int tryWait(unsigned *_event=nullptr)
evq_tryWait()
Definition oseventqueue.h:511
int wait(unsigned &_event)
evq_wait()
Definition oseventqueue.h:519
int tryWait(unsigned &_event)
evq_tryWait()
Definition oseventqueue.h:512
void pushISR(unsigned _event)
evq_pushISR()
Definition oseventqueue.h:528
int waitUntil(unsigned *_event, const T &_time)
evq_waitUntil()
Definition oseventqueue.h:517
int sendFor(unsigned _event, const T &_delay)
evq_sendFor()
Definition oseventqueue.h:523
static Ptr Create()
create dynamic object with manageable resources
Definition oseventqueue.h:562
void push(unsigned _event)
evq_push()
Definition oseventqueue.h:527
int takeAsync(unsigned *_event=nullptr)
evq_takeAsync()
Definition oseventqueue.h:536
int waitAsync(unsigned &_event)
evq_waitAsync()
Definition oseventqueue.h:539
int give(unsigned _event)
evq_give()
Definition oseventqueue.h:520
int giveAsync(unsigned _event)
evq_giveAsync()
Definition oseventqueue.h:540
int waitAsync(unsigned *_event=nullptr)
evq_waitAsync()
Definition oseventqueue.h:538
int sendUntil(unsigned _event, const T &_time)
evq_sendUntil()
Definition oseventqueue.h:525
unsigned countISR()
evq_countISR()
Definition oseventqueue.h:530