IntrOS
Loading...
Searching...
No Matches
oseventqueue.h
1/******************************************************************************
2
3 @file IntrOS: oseventqueue.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_EVQ_H
33#define __INTROS_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
172void evq_init( evq_t *evq, unsigned *data, size_t bufsize );
173
182void evq_reset( evq_t *evq );
183
185__STATIC_INLINE
186void evq_kill( evq_t *evq ) { evq_reset(evq); }
187
201int evq_take( evq_t *evq, unsigned *event );
202
204__STATIC_INLINE
205int evq_tryWait( evq_t *evq, unsigned *event ) { return evq_take(evq, event); }
206
207#if OS_ATOMICS
209int evq_takeAsync( evq_t *evq, unsigned *event );
210#endif
211
227int evq_waitFor( evq_t *evq, unsigned *event, cnt_t delay );
228
242int evq_waitUntil( evq_t *evq, unsigned *event, cnt_t time );
243
256
257__STATIC_INLINE
258int evq_wait( evq_t *evq, unsigned *event ) { return evq_waitFor(evq, event, INFINITE); }
259
260#if OS_ATOMICS
262int evq_waitAsync( evq_t *evq, unsigned *event );
263#endif
264
278int evq_give( evq_t *evq, unsigned event );
279
280#if OS_ATOMICS
282int evq_giveAsync( evq_t *evq, unsigned event );
283#endif
284
300int evq_sendFor( evq_t *evq, unsigned event, cnt_t delay );
301
315int evq_sendUntil( evq_t *evq, unsigned event, cnt_t time );
316
329
330__STATIC_INLINE
331int evq_send( evq_t *evq, unsigned event ) { return evq_sendFor(evq, event, INFINITE); }
332
333#if OS_ATOMICS
335int evq_sendAsync( evq_t *evq, unsigned event );
336#endif
337
346void evq_push( evq_t *evq, unsigned event );
347
356unsigned evq_count( evq_t *evq );
357
366unsigned evq_space( evq_t *evq );
367
376unsigned evq_limit( evq_t *evq );
377
378#ifdef __cplusplus
379}
380#endif
381
382/* -------------------------------------------------------------------------- */
383
384#if defined(__cplusplus)
385namespace intros {
386
393
394template<unsigned limit_>
395struct EventQueueT : public __evq
396{
398 constexpr
399 EventQueueT(): __evq _EVQ_INIT(limit_, data_) {}
400
401 ~EventQueueT() { assert(__evq::obj.queue == nullptr); }
402
403 /// move constructor
404 EventQueueT( EventQueueT&& _src ): __evq _EVQ_INIT(limit_, data_)
405 {
406 assert(_src.__evq::count == 0 && _src.obj.queue == nullptr);
407 (void) _src;
408 }
409
410 EventQueueT( const EventQueueT& ) = delete;
411 EventQueueT& operator=( EventQueueT&& ) = delete;
412 EventQueueT& operator=( const EventQueueT& ) = delete;
414 void reset () { evq_reset (this); }
415 void kill () { evq_kill (this); }
416 int take ( unsigned *_event = nullptr ) { return evq_take (this, _event); }
417 int take ( unsigned &_event ) { return evq_take (this, &_event); }
418 int tryWait ( unsigned *_event = nullptr ) { return evq_tryWait (this, _event); }
419 int tryWait ( unsigned &_event ) { return evq_tryWait (this, &_event); }
420 template<typename T>
421 int waitFor ( unsigned *_event, const T& _delay ) { return evq_waitFor (this, _event, Clock::count(_delay)); }
422 template<typename T>
423 int waitUntil( unsigned *_event, const T& _time ) { return evq_waitUntil(this, _event, Clock::until(_time)); }
424 int wait ( unsigned *_event = nullptr ) { return evq_wait (this, _event); }
425 int wait ( unsigned &_event ) { return evq_wait (this, &_event); }
426 int give ( unsigned _event ) { return evq_give (this, _event); }
427 template<typename T>
428 int sendFor ( unsigned _event, const T& _delay ) { return evq_sendFor (this, _event, Clock::count(_delay)); }
429 template<typename T>
430 int sendUntil( unsigned _event, const T& _time ) { return evq_sendUntil(this, _event, Clock::until(_time)); }
431 int send ( unsigned _event ) { return evq_send (this, _event); }
432 void push ( unsigned _event ) { evq_push (this, _event); }
433 unsigned count () { return evq_count (this); }
434 unsigned space () { return evq_space (this); }
435 unsigned limit () { return evq_limit (this); }
436#if OS_ATOMICS
437 int takeAsync( unsigned *_event = nullptr ) { return evq_takeAsync(this, _event); }
438 int takeAsync( unsigned &_event ) { return evq_takeAsync(this, &_event); }
439 int waitAsync( unsigned *_event = nullptr ) { return evq_waitAsync(this, _event); }
440 int waitAsync( unsigned &_event ) { return evq_waitAsync(this, &_event); }
441 int giveAsync( unsigned _event ) { return evq_giveAsync(this, _event); }
442 int sendAsync( unsigned _event ) { return evq_sendAsync(this, _event); }
443#endif
444
445 private:
446 unsigned data_[limit_];
447};
448
449} // namespace
450#endif//__cplusplus
451
452/* -------------------------------------------------------------------------- */
453
455
456#endif//__INTROS_EVQ_H
#define INFINITE
infinite waiting
Definition osbase.h:250
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
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:204
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...
void evq_kill(evq_t *evq)
alias of evq_reset()
Definition oseventqueue.h:185
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:257
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_reset(evq_t *evq)
reset the event queue object and wake up all waiting tasks with 'E_STOPPED' event value
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:330
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().
unsigned evq_limit(evq_t *evq)
return the size of the event queue
int evq_waitAsync(evq_t *evq, unsigned *event)
Async alias of evq_wait().
char data[]
message data
Definition osmessagequeue.h:54
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
create and initialize an event queue object
Definition oseventqueue.h:395
int waitFor(unsigned *_event, const T &_delay)
evq_waitFor()
Definition oseventqueue.h:420
constexpr EventQueueT()
constructor, see the constructor parameters of the class
Definition oseventqueue.h:398
int waitUntil(unsigned *_event, const T &_time)
evq_waitUntil()
Definition oseventqueue.h:422
int takeAsync(unsigned *_event=nullptr)
evq_takeAsync()
Definition oseventqueue.h:436
int send(unsigned _event)
evq_send()
Definition oseventqueue.h:430
int tryWait(unsigned &_event)
evq_tryWait()
Definition oseventqueue.h:418
int wait(unsigned *_event=nullptr)
evq_wait()
Definition oseventqueue.h:423
void push(unsigned _event)
evq_push()
Definition oseventqueue.h:431
void reset()
evq_reset()
Definition oseventqueue.h:413
void kill()
evq_kill()
Definition oseventqueue.h:414
int wait(unsigned &_event)
evq_wait()
Definition oseventqueue.h:424
unsigned count()
evq_count()
Definition oseventqueue.h:432
int tryWait(unsigned *_event=nullptr)
evq_tryWait()
Definition oseventqueue.h:417
int sendUntil(unsigned _event, const T &_time)
evq_sendUntil()
Definition oseventqueue.h:429
int give(unsigned _event)
evq_give()
Definition oseventqueue.h:425
int giveAsync(unsigned _event)
evq_giveAsync()
Definition oseventqueue.h:440
unsigned space()
evq_space()
Definition oseventqueue.h:433
int takeAsync(unsigned &_event)
evq_takeAsync()
Definition oseventqueue.h:437
int take(unsigned &_event)
evq_take()
Definition oseventqueue.h:416
int sendAsync(unsigned _event)
evq_sendAsync()
Definition oseventqueue.h:441
int take(unsigned *_event=nullptr)
evq_take()
Definition oseventqueue.h:415
int waitAsync(unsigned *_event=nullptr)
evq_waitAsync()
Definition oseventqueue.h:438
unsigned limit()
evq_limit()
Definition oseventqueue.h:434
int waitAsync(unsigned &_event)
evq_waitAsync()
Definition oseventqueue.h:439
int sendFor(unsigned _event, const T &_delay)
evq_sendFor()
Definition oseventqueue.h:427