StateOS
Loading...
Searching...
No Matches
oslist.h
1/******************************************************************************
2
3 @file StateOS: oslist.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_LST_H
33#define __STATEOS_LST_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __que que_t;
49
50struct __que
51{
52 que_t * next;
53};
54
55/******************************************************************************
56 *
57 * Name : _QUE_INIT
58 *
59 * Description : create and initialize a queue object
60 *
61 * Parameters : none
62 *
63 * Return : queue object
64 *
65 * Note : for internal use
66 *
67 ******************************************************************************/
68
69#define _QUE_INIT() { NULL }
70
74
75typedef struct __lst lst_t;
76
77struct __lst
78{
79 obj_t obj;
80
81 que_t head;
82};
83
84typedef struct __lst lst_id [];
85
86/******************************************************************************
87 *
88 * Name : _LST_INIT
89 *
90 * Description : create and initialize a list object
91 *
92 * Parameters : none
93 *
94 * Return : list object
95 *
96 * Note : for internal use
97 *
98 ******************************************************************************/
99
100#define _LST_INIT() { _OBJ_INIT(), _QUE_INIT() }
101
108#define OS_LST( lst ) \
109 lst_t lst[] = { _LST_INIT() }
110
111/** static alias of OS_LST() */
112#define static_LST( lst ) \
113 static lst_t lst[] = { _LST_INIT() }
114
122
123#ifndef __cplusplus
124#define LST_INIT() \
125 _LST_INIT()
126#endif
127
135
136#ifndef __cplusplus
137#define LST_CREATE() \
138 { LST_INIT () }
139/** alias of LST_CREATE() */
140#define LST_NEW \
141 LST_CREATE
142#endif
143
144#ifdef __cplusplus
145extern "C" {
146#endif
147
156void lst_init( lst_t *lst );
157
167lst_t *lst_create( void );
168
170__STATIC_INLINE
171lst_t *lst_new( void ) { return lst_create(); }
172
181void lst_reset( lst_t *lst );
182
184__STATIC_INLINE
185void lst_kill( lst_t *lst ) { lst_reset(lst); }
186
195void lst_destroy( lst_t *lst );
196
198__STATIC_INLINE
199void lst_delete( lst_t *lst ) { lst_destroy(lst); }
200
214int lst_take( lst_t *lst, void **data );
215
217__STATIC_INLINE
218int lst_tryWait( lst_t *lst, void **data ) { return lst_take(lst, data); }
219
221__STATIC_INLINE
222int lst_takeISR( lst_t *lst, void **data ) { return lst_take(lst, data); }
223
242int lst_waitFor( lst_t *lst, void **data, cnt_t delay );
243
260int lst_waitUntil( lst_t *lst, void **data, cnt_t time );
261
275
276__STATIC_INLINE
277int lst_wait( lst_t *lst, void **data ) { return lst_waitFor(lst, data, INFINITE); }
278
289void lst_give( lst_t *lst, void *data );
290
292__STATIC_INLINE
293void lst_giveISR( lst_t *lst, void *data ) { lst_give(lst, data); }
294
295#ifdef __cplusplus
296}
297#endif
298
299/* -------------------------------------------------------------------------- */
300
301#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
302namespace stateos {
303
310
311template<class C>
312struct ListTT : public __lst
313{
315 constexpr
316 ListTT(): __lst _LST_INIT() {}
317
318 ~ListTT() { assert(__lst::obj.queue == nullptr); }
319
320 /// move constructor
321 ListTT( ListTT&& _src ): __lst(_src)
322 {
323 assert(_src.obj.queue == nullptr && _src.head.next == nullptr);
324 }
325
326 ListTT( const ListTT& ) = delete;
327 ListTT& operator=( ListTT&& ) = delete;
328 ListTT& operator=( const ListTT& ) = delete;
330 void reset () { lst_reset (this); }
331 void kill () { lst_kill (this); }
332 void destroy () { lst_destroy (this); }
333 int take ( C **_data ) { return lst_take (this, reinterpret_cast<void **>(_data)); }
334 int tryWait ( C **_data ) { return lst_tryWait (this, reinterpret_cast<void **>(_data)); }
335 int takeISR ( C **_data ) { return lst_takeISR (this, reinterpret_cast<void **>(_data)); }
336 template<typename T>
337 int waitFor ( C **_data, const T& _delay ) { return lst_waitFor (this, reinterpret_cast<void **>(_data), Clock::count(_delay)); }
338 template<typename T>
339 int waitUntil( C **_data, const T& _time ) { return lst_waitUntil(this, reinterpret_cast<void **>(_data), Clock::until(_time)); }
340 int wait ( C **_data ) { return lst_wait (this, reinterpret_cast<void **>(_data)); }
341 void give ( void *_data ) { lst_give (this, _data); }
342 void giveISR ( void *_data ) { lst_giveISR (this, _data); }
343
344#if __cplusplus >= 201402L
345 using Ptr = std::unique_ptr<ListTT<C>>;
346#else
347 using Ptr = ListTT<C> *;
348#endif
349
360
361 static
362 Ptr Create()
363 {
364 auto lst = new (std::nothrow) ListTT<C>();
365 if (lst != nullptr)
366 lst->__lst::obj.res = lst;
367 return Ptr(lst);
368 }
369
370};
371
375
376using List = ListTT<void>;
377
378} // namespace
379#endif//__cplusplus
380
381/* -------------------------------------------------------------------------- */
382
384
385#endif//__STATEOS_LST_H
#define INFINITE
infinite waiting
Definition osbase.h:261
void lst_init(lst_t *lst)
initialize a list object
void lst_reset(lst_t *lst)
wake up all waiting tasks with 'E_STOPPED' event value
lst_t * lst_create(void)
create and initialize a new list object
int lst_waitUntil(lst_t *lst, void **data, cnt_t time)
try to get memory object from the list object, wait until given timepoint while the list object is em...
lst_t * lst_new(void)
alias of lst_create()
Definition oslist.h:170
void lst_give(lst_t *lst, void *data)
transfer memory object to the list object,
int lst_take(lst_t *lst, void **data)
try to get memory object from the list object, don't wait if the list object is empty
int lst_wait(lst_t *lst, void **data)
try to get memory object from the list object, wait indefinitely while the list object is empty
Definition oslist.h:276
void lst_kill(lst_t *lst)
alias of lst_reset()
Definition oslist.h:184
void lst_giveISR(lst_t *lst, void *data)
ISR alias of lst_give().
Definition oslist.h:292
int lst_takeISR(lst_t *lst, void **data)
ISR alias of lst_take().
Definition oslist.h:221
int lst_waitFor(lst_t *lst, void **data, cnt_t delay)
try to get memory object from the list object, wait for given duration of time while the list object ...
void lst_delete(lst_t *lst)
alias of lst_destroy()
Definition oslist.h:198
int lst_tryWait(lst_t *lst, void **data)
alias of lst_take()
Definition oslist.h:217
void lst_destroy(lst_t *lst)
reset the list object, wake up all waiting tasks with 'E_DELETED' event value and free allocated reso...
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 a list object
Definition oslist.h:312
void kill()
Definition oslist.h:330
void give(void *_data)
Definition oslist.h:340
int waitUntil(void **_data, const T &_time)
Definition oslist.h:338
int takeISR(C **_data)
lst_takeISR()
Definition oslist.h:334
int tryWait(void **_data)
Definition oslist.h:333
void reset()
Definition oslist.h:329
constexpr ListTT()
constructor, see the constructor parameters of the class
Definition oslist.h:315
int take(void **_data)
Definition oslist.h:332
void giveISR(void *_data)
lst_giveISR()
Definition oslist.h:341
std::unique_ptr< ListTT< C > > Ptr
result type of Create()
Definition oslist.h:344
void destroy()
Definition oslist.h:331
int waitFor(void **_data, const T &_delay)
Definition oslist.h:336
static Ptr Create()
Definition oslist.h:361
int wait(void **_data)
Definition oslist.h:339