StateOS
Loading...
Searching...
No Matches
osmailboxqueue.h
1/******************************************************************************
2
3 @file StateOS: osmailboxqueue.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_BOX_H
33#define __STATEOS_BOX_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __box box_t;
49
50struct __box
51{
52 obj_t obj;
53
54 size_t count;
55 size_t limit;
56 size_t size;
57
58 size_t head;
59 size_t tail;
60 char * data;
61};
62
63typedef struct __box box_id [];
64
65/******************************************************************************
66 *
67 * Name : _BOX_INIT
68 *
69 * Description : create and initialize a mailbox queue object
70 *
71 * Parameters
72 * limit : size of a queue (max number of stored mails)
73 * size : size of a single mail (in bytes)
74 * data : mailbox queue data buffer
75 *
76 * Return : mailbox queue object
77 *
78 * Note : for internal use
79 *
80 ******************************************************************************/
81
82#define _BOX_INIT( _limit, _size, _data ) { _OBJ_INIT(), 0, _limit * _size, _size, 0, 0, _data }
83
84/******************************************************************************
85 *
86 * Name : _BOX_DATA
87 *
88 * Description : create a mailbox queue data buffer
89 *
90 * Parameters
91 * limit : size of a queue (max number of stored mails)
92 * size : size of a single mail (in bytes)
93 *
94 * Return : mailbox queue data buffer
95 *
96 * Note : for internal use
97 *
98 ******************************************************************************/
99
100#ifndef __cplusplus
101#define _BOX_DATA( _limit, _size ) (char[_limit * _size]){ 0 }
102#endif
103
112#define OS_BOX_BUFFER( buf, limit, size ) \
113 char buf[limit * size]
114
123#define OS_BOX( box, limit, size ) \
124 static char box##__buf[limit * size]; \
125 box_t box[] = { _BOX_INIT( limit, size, box##__buf ) }
126
127/** static alias of OS_BOX() */
128#define static_BOX( box, limit, size ) \
129 static char box##__buf[limit * size]; \
130 static box_t box[] = { _BOX_INIT( limit, size, box##__buf ) }
131
142
143#ifndef __cplusplus
144#define BOX_INIT( limit, size ) \
145 _BOX_INIT( limit, size, _BOX_DATA( limit, size ) )
146#endif
147
158
159#ifndef __cplusplus
160#define BOX_CREATE( limit, size ) \
161 { BOX_INIT ( limit, size ) }
162/** alias of BOX_CREATE() */
163#define BOX_NEW \
164 BOX_CREATE
165#endif
166
167#ifdef __cplusplus
168extern "C" {
169#endif
170
182void box_init( box_t *box, size_t size, void *data, size_t bufsize );
183
196box_t *box_create( unsigned limit, size_t size );
197
199__STATIC_INLINE
200box_t *box_new( unsigned limit, size_t size ) { return box_create(limit, size); }
201
210void box_reset( box_t *box );
211
213__STATIC_INLINE
214void box_kill( box_t *box ) { box_reset(box); }
215
224void box_destroy( box_t *box );
225
227__STATIC_INLINE
228void box_delete( box_t *box ) { box_destroy(box); }
229
245int box_take( box_t *box, void *data );
246
248__STATIC_INLINE
249int box_tryWait( box_t *box, void *data ) { return box_take(box, data); }
250
252__STATIC_INLINE
253int box_takeISR( box_t *box, void *data ) { return box_take(box, data); }
254
255#if OS_ATOMICS
257int box_takeAsync( box_t *box, void *data );
258#endif
259
278int box_waitFor( box_t *box, void *data, cnt_t delay );
279
296int box_waitUntil( box_t *box, void *data, cnt_t time );
297
314
315__STATIC_INLINE
316int box_wait( box_t *box, void *data ) { return box_waitFor(box, data, INFINITE); }
317
318#if OS_ATOMICS
320int box_waitAsync( box_t *box, void *data );
321#endif
322
338int box_give( box_t *box, const void *data );
339
341__STATIC_INLINE
342int box_giveISR( box_t *box, const void *data ) { return box_give(box, data); }
343
344#if OS_ATOMICS
346int box_giveAsync( box_t *box, const void *data );
347#endif
348
367int box_sendFor( box_t *box, const void *data, cnt_t delay );
368
385int box_sendUntil( box_t *box, const void *data, cnt_t time );
386
403
404__STATIC_INLINE
405int box_send( box_t *box, const void *data ) { return box_sendFor(box, data, INFINITE); }
406
407#if OS_ATOMICS
409int box_sendAsync( box_t *box, const void *data );
410#endif
411
423void box_push( box_t *box, const void *data );
424
426__STATIC_INLINE
427void box_pushISR( box_t *box, const void *data ) { box_push(box, data); }
428
440unsigned box_count( box_t *box );
441
443__STATIC_INLINE
444unsigned box_countISR( box_t *box ) { return box_count(box); }
445
457unsigned box_space( box_t *box );
458
460__STATIC_INLINE
461unsigned box_spaceISR( box_t *box ) { return box_space(box); }
462
474unsigned box_limit( box_t *box );
475
477__STATIC_INLINE
478unsigned box_limitISR( box_t *box ) { return box_limit(box); }
479
490
491__STATIC_INLINE
492size_t box_size( box_t *box ) { return box->size; }
493
495__STATIC_INLINE
496size_t box_sizeISR( box_t *box ) { return box_size(box); }
497
498#ifdef __cplusplus
499}
500#endif
501
502/* -------------------------------------------------------------------------- */
503
504#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
505namespace stateos {
506
514
515template<unsigned limit_, size_t size_>
516struct MailBoxQueueT : public __box
517{
519 constexpr
520 MailBoxQueueT(): __box _BOX_INIT(limit_, size_, data_) {}
521
522 ~MailBoxQueueT() { assert(__box::obj.queue == nullptr); }
523
524 /// move constructor
525 MailBoxQueueT( MailBoxQueueT&& _src ): __box _BOX_INIT(limit_, size_, data_)
526 {
527 assert(_src.__box::count == 0 && _src.obj.queue == nullptr);
528 (void) _src;
529 }
530
531 MailBoxQueueT( const MailBoxQueueT& ) = delete;
532 MailBoxQueueT& operator=( MailBoxQueueT&& ) = delete;
533 MailBoxQueueT& operator=( const MailBoxQueueT& ) = delete;
535 void reset () { box_reset (this); }
536 void kill () { box_kill (this); }
537 void destroy () { box_destroy (this); }
538 int take ( void *_data ) { return box_take (this, _data); }
539 int tryWait ( void *_data ) { return box_tryWait (this, _data); }
540 int takeISR ( void *_data ) { return box_takeISR (this, _data); }
541 template<typename T>
542 int waitFor ( void *_data, const T& _delay ) { return box_waitFor (this, _data, Clock::count(_delay)); }
543 template<typename T>
544 int waitUntil( void *_data, const T& _time ) { return box_waitUntil(this, _data, Clock::until(_time)); }
545 int wait ( void *_data ) { return box_wait (this, _data); }
546 int give ( const void *_data ) { return box_give (this, _data); }
547 int giveISR ( const void *_data ) { return box_giveISR (this, _data); }
548 template<typename T>
549 int sendFor ( const void *_data, const T& _delay ) { return box_sendFor (this, _data, Clock::count(_delay)); }
550 template<typename T>
551 int sendUntil( const void *_data, const T& _time ) { return box_sendUntil(this, _data, Clock::until(_time)); }
552 int send ( const void *_data ) { return box_send (this, _data); }
553 void push ( const void *_data ) { box_push (this, _data); }
554 void pushISR ( const void *_data ) { box_pushISR (this, _data); }
555 unsigned count () { return box_count (this); }
556 unsigned countISR () { return box_countISR (this); }
557 unsigned space () { return box_space (this); }
558 unsigned spaceISR () { return box_spaceISR (this); }
559 unsigned limit () { return box_limit (this); }
560 unsigned limitISR () { return box_limitISR (this); }
561 size_t size () { return box_size (this); }
562 size_t sizeISR () { return box_sizeISR (this); }
563#if OS_ATOMICS
564 int takeAsync( void *_data ) { return box_takeAsync(this, _data); }
565 int waitAsync( void *_data ) { return box_waitAsync(this, _data); }
566 int giveAsync( const void *_data ) { return box_giveAsync(this, _data); }
567 int sendAsync( const void *_data ) { return box_sendAsync(this, _data); }
568#endif
569
570#if __cplusplus >= 201402L
571 using Ptr = std::unique_ptr<MailBoxQueueT<limit_, size_>>;
572#else
574#endif
575
587
588 static
589 Ptr Create()
590 {
591 auto box = new (std::nothrow) MailBoxQueueT<limit_, size_>();
592 if (box != nullptr)
593 box->__box::obj.res = box;
594 return Ptr(box);
595 }
596
597 private:
598 char data_[limit_ * size_];
599};
600
608
609template<unsigned limit_, class C>
610struct MailBoxQueueTT : public MailBoxQueueT<limit_, sizeof(C)>
611{
613 constexpr
614 MailBoxQueueTT(): MailBoxQueueT<limit_, sizeof(C)>() {}
615
616#if __cplusplus >= 201402L
617 using Ptr = std::unique_ptr<MailBoxQueueTT<limit_, C>>;
618#else
620#endif
621
633
634 static
635 Ptr Create()
636 {
637 auto box = new (std::nothrow) MailBoxQueueTT<limit_, C>();
638 if (box != nullptr)
639 box->__box::obj.res = box;
640 return Ptr(box);
641 }
642
643};
644
645} // namespace
646#endif//__cplusplus
647
648/* -------------------------------------------------------------------------- */
649
651
652#endif//__STATEOS_BOX_H
#define INFINITE
infinite waiting
Definition osbase.h:261
int box_giveAsync(box_t *box, const void *data)
Async alias of box_give().
int box_take(box_t *box, void *data)
try to transfer mailbox data from the mailbox queue object, don't wait if the mailbox queue object is...
void box_kill(box_t *box)
alias of box_reset()
Definition osmailboxqueue.h:213
int box_send(box_t *box, const void *data)
try to transfer mailbox data to the mailbox queue object, wait indefinitely while the mailbox queue o...
Definition osmailboxqueue.h:404
void box_destroy(box_t *box)
reset the mailbox queue object, wake up all waiting tasks with 'E_DELETED' event value and free alloc...
int box_takeISR(box_t *box, void *data)
ISR alias of box_take().
Definition osmailboxqueue.h:252
size_t box_size(box_t *box)
return size of a single mail
Definition osmailboxqueue.h:491
unsigned box_limit(box_t *box)
return the size of the mailbox queue
int box_takeAsync(box_t *box, void *data)
Async alias of box_take().
unsigned box_spaceISR(box_t *box)
ISR alias of box_space().
Definition osmailboxqueue.h:460
unsigned box_limitISR(box_t *box)
ISR alias of box_limit().
Definition osmailboxqueue.h:477
int box_sendFor(box_t *box, const void *data, cnt_t delay)
try to transfer mailbox data to the mailbox queue object, wait for given duration of time while the m...
void box_delete(box_t *box)
alias of box_destroy()
Definition osmailboxqueue.h:227
unsigned box_count(box_t *box)
return the amount of data contained in the mailbox queue
void box_init(box_t *box, size_t size, void *data, size_t bufsize)
initialize a mailbox queue object
int box_giveISR(box_t *box, const void *data)
ISR alias of box_give().
Definition osmailboxqueue.h:341
int box_sendUntil(box_t *box, const void *data, cnt_t time)
try to transfer mailbox data to the mailbox queue object, wait until given timepoint while the mailbo...
size_t box_sizeISR(box_t *box)
ISR alias of box_size().
Definition osmailboxqueue.h:495
int box_tryWait(box_t *box, void *data)
alias of box_take()
Definition osmailboxqueue.h:248
int box_give(box_t *box, const void *data)
try to transfer mailbox data to the mailbox queue object, don't wait if the mailbox queue object is f...
int box_wait(box_t *box, void *data)
try to transfer mailbox data from the mailbox queue object, wait indefinitely while the mailbox queue...
Definition osmailboxqueue.h:315
box_t * box_create(unsigned limit, size_t size)
create and initialize a new mailbox queue object
void box_pushISR(box_t *box, const void *data)
ISR alias of box_push().
Definition osmailboxqueue.h:426
unsigned box_countISR(box_t *box)
ISR alias of box_count().
Definition osmailboxqueue.h:443
unsigned box_space(box_t *box)
return the amount of free space in the mailbox queue
box_t * box_new(unsigned limit, size_t size)
alias of box_create()
Definition osmailboxqueue.h:199
void box_push(box_t *box, const void *data)
transfer mailbox data to the mailbox queue object, remove the oldest mailbox data if the mailbox queu...
int box_waitUntil(box_t *box, void *data, cnt_t time)
try to transfer mailbox data from the mailbox queue object, wait until given timepoint while the mail...
int box_sendAsync(box_t *box, const void *data)
Async alias of box_send().
void box_reset(box_t *box)
reset the mailbox queue object and wake up all waiting tasks with 'E_STOPPED' event value
int box_waitFor(box_t *box, void *data, cnt_t delay)
try to transfer mailbox data from the mailbox queue object, wait for given duration of time while the...
int box_waitAsync(box_t *box, void *data)
Async alias of box_wait().
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
static Ptr Create()
create dynamic object with manageable resources
Definition osmailboxqueue.h:634
constexpr MailBoxQueueTT()
constructor, see the constructor parameters of the class
Definition osmailboxqueue.h:613
std::unique_ptr< MailBoxQueueTT< limit_, C > > Ptr
result type of Create()
Definition osmailboxqueue.h:616
create and initialize a mailbox queue object
Definition osmailboxqueue.h:516
int waitFor(void *_data, const T &_delay)
box_waitFor()
Definition osmailboxqueue.h:541
int takeISR(void *_data)
box_takeISR()
Definition osmailboxqueue.h:539
unsigned count()
box_count()
Definition osmailboxqueue.h:554
unsigned limit()
box_limit()
Definition osmailboxqueue.h:558
void kill()
box_kill()
Definition osmailboxqueue.h:535
int take(void *_data)
box_take()
Definition osmailboxqueue.h:537
int giveAsync(const void *_data)
box_giveAsync()
Definition osmailboxqueue.h:565
unsigned limitISR()
box_limitISR()
Definition osmailboxqueue.h:559
void destroy()
box_destroy()
Definition osmailboxqueue.h:536
void push(const void *_data)
box_push()
Definition osmailboxqueue.h:552
int takeAsync(void *_data)
box_takeAsync()
Definition osmailboxqueue.h:563
int wait(void *_data)
box_wait()
Definition osmailboxqueue.h:544
unsigned countISR()
box_countISR()
Definition osmailboxqueue.h:555
size_t sizeISR()
box_sizeISR()
Definition osmailboxqueue.h:561
unsigned spaceISR()
box_spaceISR()
Definition osmailboxqueue.h:557
int sendAsync(const void *_data)
box_sendAsync()
Definition osmailboxqueue.h:566
int sendUntil(const void *_data, const T &_time)
box_sendUntil()
Definition osmailboxqueue.h:550
std::unique_ptr< MailBoxQueueT< limit_, size_ > > Ptr
result type of Create()
Definition osmailboxqueue.h:570
int waitAsync(void *_data)
box_waitAsync()
Definition osmailboxqueue.h:564
int waitUntil(void *_data, const T &_time)
box_waitUntil()
Definition osmailboxqueue.h:543
static Ptr Create()
create dynamic object with manageable resources
Definition osmailboxqueue.h:588
int send(const void *_data)
box_send()
Definition osmailboxqueue.h:551
int sendFor(const void *_data, const T &_delay)
box_sendFor()
Definition osmailboxqueue.h:548
constexpr MailBoxQueueT()
constructor, see the constructor parameters of the class
Definition osmailboxqueue.h:519
size_t size()
box_size()
Definition osmailboxqueue.h:560
int giveISR(const void *_data)
box_giveISR()
Definition osmailboxqueue.h:546
unsigned space()
box_space()
Definition osmailboxqueue.h:556
int give(const void *_data)
box_give()
Definition osmailboxqueue.h:545
void reset()
box_reset()
Definition osmailboxqueue.h:534
int tryWait(void *_data)
box_tryWait()
Definition osmailboxqueue.h:538
void pushISR(const void *_data)
box_pushISR()
Definition osmailboxqueue.h:553