IntrOS
Loading...
Searching...
No Matches
osmailboxqueue.h
1/******************************************************************************
2
3 @file IntrOS: osmailboxqueue.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_BOX_H
33#define __INTROS_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
180void box_init( box_t *box, size_t size, void *data, size_t bufsize );
181
190void box_reset( box_t *box );
191
193__STATIC_INLINE
194void box_kill( box_t *box ) { box_reset(box); }
195
209int box_take( box_t *box, void *data );
210
212__STATIC_INLINE
213int box_tryWait( box_t *box, void *data ) { return box_take(box, data); }
214
215#if OS_ATOMICS
217int box_takeAsync( box_t *box, void *data );
218#endif
219
235int box_waitFor( box_t *box, void *data, cnt_t delay );
236
250int box_waitUntil( box_t *box, void *data, cnt_t time );
251
264
265__STATIC_INLINE
266int box_wait( box_t *box, void *data ) { return box_waitFor(box, data, INFINITE); }
267
268#if OS_ATOMICS
270int box_waitAsync( box_t *box, void *data );
271#endif
272
286int box_give( box_t *box, const void *data );
287
288#if OS_ATOMICS
290int box_giveAsync( box_t *box, const void *data );
291#endif
292
308int box_sendFor( box_t *box, const void *data, cnt_t delay );
309
323int box_sendUntil( box_t *box, const void *data, cnt_t time );
324
337
338__STATIC_INLINE
339int box_send( box_t *box, const void *data ) { return box_sendFor(box, data, INFINITE); }
340
341#if OS_ATOMICS
343int box_sendAsync( box_t *box, const void *data );
344#endif
345
354void box_push( box_t *box, const void *data );
355
364unsigned box_count( box_t *box );
365
374unsigned box_space( box_t *box );
375
384unsigned box_limit( box_t *box );
385
393
394__STATIC_INLINE
395size_t box_size( box_t *box ) { return box->size; }
396
397#ifdef __cplusplus
398}
399#endif
400
401/* -------------------------------------------------------------------------- */
402
403#if defined(__cplusplus)
404namespace intros {
405
413
414template<unsigned limit_, size_t size_>
415struct MailBoxQueueT : public __box
416{
418 constexpr
419 MailBoxQueueT(): __box _BOX_INIT(limit_, size_, data_) {}
420
421 ~MailBoxQueueT() { assert(__box::obj.queue == nullptr); }
422
423 /// move constructor
424 MailBoxQueueT( MailBoxQueueT&& _src ): __box _BOX_INIT(limit_, size_, data_)
425 {
426 assert(_src.__box::count == 0 && _src.obj.queue == nullptr);
427 (void) _src;
428 }
429
430 MailBoxQueueT( const MailBoxQueueT& ) = delete;
431 MailBoxQueueT& operator=( MailBoxQueueT&& ) = delete;
432 MailBoxQueueT& operator=( const MailBoxQueueT& ) = delete;
434 void reset () { box_reset (this); }
435 void kill () { box_kill (this); }
436 int take ( void *_data ) { return box_take (this, _data); }
437 int tryWait ( void *_data ) { return box_tryWait (this, _data); }
438 template<typename T>
439 int waitFor ( void *_data, const T& _delay ) { return box_waitFor (this, _data, Clock::count(_delay)); }
440 template<typename T>
441 int waitUntil( void *_data, const T& _time ) { return box_waitUntil(this, _data, Clock::until(_time)); }
442 int wait ( void *_data ) { return box_wait (this, _data); }
443 int give ( const void *_data ) { return box_give (this, _data); }
444 template<typename T>
445 int sendFor ( const void *_data, const T& _delay ) { return box_sendFor (this, _data, Clock::count(_delay)); }
446 template<typename T>
447 int sendUntil( const void *_data, const T& _time ) { return box_sendUntil(this, _data, Clock::until(_time)); }
448 int send ( const void *_data ) { return box_send (this, _data); }
449 void push ( const void *_data ) { box_push (this, _data); }
450 unsigned count () { return box_count (this); }
451 unsigned space () { return box_space (this); }
452 unsigned limit () { return box_limit (this); }
453 size_t size () { return box_size (this); }
454#if OS_ATOMICS
455 int takeAsync( void *_data ) { return box_takeAsync(this, _data); }
456 int waitAsync( void *_data ) { return box_waitAsync(this, _data); }
457 int giveAsync( const void *_data ) { return box_giveAsync(this, _data); }
458 int sendAsync( const void *_data ) { return box_sendAsync(this, _data); }
459#endif
460
461 private:
462 char data_[limit_ * size_];
463};
464
472
473template<unsigned limit_, class C>
474struct MailBoxQueueTT : public MailBoxQueueT<limit_, sizeof(C)>
475{
477 constexpr
478 MailBoxQueueTT(): MailBoxQueueT<limit_, sizeof(C)>() {}
479};
480
481} // namespace
482#endif//__cplusplus
483
484/* -------------------------------------------------------------------------- */
485
487
488#endif//__INTROS_BOX_H
#define INFINITE
infinite waiting
Definition osbase.h:250
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:193
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:338
size_t box_size(box_t *box)
return size of a single mail
Definition osmailboxqueue.h:394
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().
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...
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_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...
int box_tryWait(box_t *box, void *data)
alias of box_take()
Definition osmailboxqueue.h:212
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:265
unsigned box_space(box_t *box)
return the amount of free space in the mailbox queue
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 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
constexpr MailBoxQueueTT()
constructor, see the constructor parameters of the class
Definition osmailboxqueue.h:477
create and initialize a mailbox queue object
Definition osmailboxqueue.h:415
int sendUntil(const void *_data, const T &_time)
box_sendUntil()
Definition osmailboxqueue.h:446
int wait(void *_data)
box_wait()
Definition osmailboxqueue.h:441
int give(const void *_data)
box_give()
Definition osmailboxqueue.h:442
unsigned space()
box_space()
Definition osmailboxqueue.h:450
int waitFor(void *_data, const T &_delay)
box_waitFor()
Definition osmailboxqueue.h:438
void kill()
box_kill()
Definition osmailboxqueue.h:434
void reset()
box_reset()
Definition osmailboxqueue.h:433
constexpr MailBoxQueueT()
constructor, see the constructor parameters of the class
Definition osmailboxqueue.h:418
int take(void *_data)
box_take()
Definition osmailboxqueue.h:435
int waitAsync(void *_data)
box_waitAsync()
Definition osmailboxqueue.h:455
void push(const void *_data)
box_push()
Definition osmailboxqueue.h:448
int send(const void *_data)
box_send()
Definition osmailboxqueue.h:447
unsigned limit()
box_limit()
Definition osmailboxqueue.h:451
int takeAsync(void *_data)
box_takeAsync()
Definition osmailboxqueue.h:454
unsigned count()
box_count()
Definition osmailboxqueue.h:449
int giveAsync(const void *_data)
box_giveAsync()
Definition osmailboxqueue.h:456
int tryWait(void *_data)
box_tryWait()
Definition osmailboxqueue.h:436
size_t size()
box_size()
Definition osmailboxqueue.h:452
int sendAsync(const void *_data)
box_sendAsync()
Definition osmailboxqueue.h:457
int sendFor(const void *_data, const T &_delay)
box_sendFor()
Definition osmailboxqueue.h:444
int waitUntil(void *_data, const T &_time)
box_waitUntil()
Definition osmailboxqueue.h:440