StateOS
Loading...
Searching...
No Matches
osjobqueue.h
1/******************************************************************************
2
3 @file StateOS: osjobqueue.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_JOB_H
33#define __STATEOS_JOB_H
34
35#include "oskernel.h"
36#include "osclock.h"
37
43
47
48typedef struct __job job_t;
49
50struct __job
51{
52 obj_t obj;
53
54 unsigned count;
55 unsigned limit;
56
57 unsigned head;
58 unsigned tail;
59 fun_t ** data;
60};
61
62typedef struct __job job_id [];
63
64/******************************************************************************
65 *
66 * Name : _JOB_INIT
67 *
68 * Description : create and initialize a job queue object
69 *
70 * Parameters
71 * limit : size of a queue (max number of stored job procedures)
72 * data : job queue data buffer
73 *
74 * Return : job queue object
75 *
76 * Note : for internal use
77 *
78 ******************************************************************************/
79
80#define _JOB_INIT( _limit, _data ) { _OBJ_INIT(), 0, _limit, 0, 0, _data }
81
82/******************************************************************************
83 *
84 * Name : _JOB_DATA
85 *
86 * Description : create a job queue data buffer
87 *
88 * Parameters
89 * limit : size of a queue (max number of stored job procedures)
90 *
91 * Return : job queue data buffer
92 *
93 * Note : for internal use
94 *
95 ******************************************************************************/
96
97#ifndef __cplusplus
98#define _JOB_DATA( _limit ) (fun_t *[_limit]){ NULL }
99#endif
100
108#define OS_JOB_BUFFER( buf, size ) \
109 fun_t *buf[size]
110
118#define OS_JOB( job, limit ) \
119 static fun_t *job##__buf[limit]; \
120 job_t job[] = { _JOB_INIT( limit, job##__buf ) }
121
122/** static alias of OS_JOB() */
123#define static_JOB( job, limit ) \
124 static fun_t *job##__buf[limit]; \
125 static job_t job[] = { _JOB_INIT( limit, job##__buf ) }
126
136
137#ifndef __cplusplus
138#define JOB_INIT( limit ) \
139 _JOB_INIT( limit, _JOB_DATA( limit ) )
140#endif
141
151
152#ifndef __cplusplus
153#define JOB_CREATE( limit ) \
154 { JOB_INIT ( limit ) }
155/** alias of JOB_CREATE() */
156#define JOB_NEW \
157 JOB_CREATE
158#endif
159
160#ifdef __cplusplus
161extern "C" {
162#endif
163
174void job_init( job_t *job, fun_t **data, size_t bufsize );
175
187job_t *job_create( unsigned limit );
188
190__STATIC_INLINE
191job_t *job_new( unsigned limit ) { return job_create(limit); }
192
201void job_reset( job_t *job );
202
204__STATIC_INLINE
205void job_kill( job_t *job ) { job_reset(job); }
206
215void job_destroy( job_t *job );
216
218__STATIC_INLINE
219void job_delete( job_t *job ) { job_destroy(job); }
220
235int job_take( job_t *job );
236
238__STATIC_INLINE
239int job_tryWait( job_t *job ) { return job_take(job); }
240
242__STATIC_INLINE
243int job_takeISR( job_t *job ) { return job_take(job); }
244
245#if OS_ATOMICS
247int job_takeAsync( job_t *job );
248#endif
249
267int job_waitFor( job_t *job, cnt_t delay );
268
284int job_waitUntil( job_t *job, cnt_t time );
285
301
302__STATIC_INLINE
303int job_wait( job_t *job ) { return job_waitFor(job, INFINITE); }
304
305#if OS_ATOMICS
307int job_waitAsync( job_t *job );
308#endif
309
325int job_give( job_t *job, fun_t *fun );
326
328__STATIC_INLINE
329int job_giveISR( job_t *job, fun_t *fun ) { return job_give(job, fun); }
330
331#if OS_ATOMICS
333int job_giveAsync( job_t *job, fun_t *fun );
334#endif
335
354int job_sendFor( job_t *job, fun_t *fun, cnt_t delay );
355
372int job_sendUntil( job_t *job, fun_t *fun, cnt_t time );
373
390
391__STATIC_INLINE
392int job_send( job_t *job, fun_t *fun ) { return job_sendFor(job, fun, INFINITE); }
393
394#if OS_ATOMICS
396int job_sendAsync( job_t *job, fun_t *fun );
397#endif
398
410void job_push( job_t *job, fun_t *fun );
411
413__STATIC_INLINE
414void job_pushISR( job_t *job, fun_t *fun ) { job_push(job, fun); }
415
427unsigned job_count( job_t *job );
428
430__STATIC_INLINE
431unsigned job_countISR( job_t *job ) { return job_count(job); }
432
444unsigned job_space( job_t *job );
445
447__STATIC_INLINE
448unsigned job_spaceISR( job_t *job ) { return job_space(job); }
449
461unsigned job_limit( job_t *job );
462
464__STATIC_INLINE
465unsigned job_limitISR( job_t *job ) { return job_limit(job); }
466
467#ifdef __cplusplus
468}
469#endif
470
471/* -------------------------------------------------------------------------- */
472
473#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
474namespace stateos {
475
482
483template<unsigned limit_>
484struct JobQueueT : public __job
485{
487 constexpr
488 JobQueueT(): __job _JOB_INIT(limit_, data_) {}
489
490 ~JobQueueT() { assert(__job::obj.queue == nullptr); }
491
492 /// move constructor
493 JobQueueT( JobQueueT&& _src ): __job _JOB_INIT(limit_, data_)
494 {
495 assert(_src.__job::count == 0 && _src.obj.queue == nullptr);
496 (void) _src;
497 }
498
499 JobQueueT( const JobQueueT& ) = delete;
500 JobQueueT& operator=( JobQueueT&& ) = delete;
501 JobQueueT& operator=( const JobQueueT& ) = delete;
503 void reset () { job_reset (this); }
504 void kill () { job_kill (this); }
505 void destroy () { job_destroy (this); }
506 int take () { return job_take (this); }
507 int tryWait () { return job_tryWait (this); }
508 int takeISR () { return job_takeISR (this); }
509 template<typename T>
510 int waitFor ( const T& _delay ) { return job_waitFor (this, Clock::count(_delay)); }
511 template<typename T>
512 int waitUntil( const T& _time ) { return job_waitUntil(this, Clock::until(_time)); }
513 int wait () { return job_wait (this); }
514 int give ( fun_t *_fun ) { return job_give (this, _fun); }
515 int giveISR ( fun_t *_fun ) { return job_giveISR (this, _fun); }
516 template<typename T>
517 int sendFor ( fun_t *_fun, const T& _delay ) { return job_sendFor (this, _fun, Clock::count(_delay)); }
518 template<typename T>
519 int sendUntil( fun_t *_fun, const T& _time ) { return job_sendUntil(this, _fun, Clock::until(_time)); }
520 int send ( fun_t *_fun ) { return job_send (this, _fun); }
521 void push ( fun_t *_fun ) { job_push (this, _fun); }
522 void pushISR ( fun_t *_fun ) { job_pushISR (this, _fun); }
523 unsigned count () { return job_count (this); }
524 unsigned countISR () { return job_countISR (this); }
525 unsigned space () { return job_space (this); }
526 unsigned spaceISR () { return job_spaceISR (this); }
527 unsigned limit () { return job_limit (this); }
528 unsigned limitISR () { return job_limitISR (this); }
529#if OS_ATOMICS
530 int takeAsync() { return job_takeAsync(this); }
531 int waitAsync() { return job_waitAsync(this); }
532 int giveAsync( fun_t *_fun ) { return job_giveAsync(this, _fun); }
533 int sendAsync( fun_t *_fun ) { return job_sendAsync(this, _fun); }
534#endif
535
536#if __cplusplus >= 201402L
537 using Ptr = std::unique_ptr<JobQueueT<limit_>>;
538#else
539 using Ptr = JobQueueT<limit_> *;
540#endif
541
552
553 static
554 Ptr Create()
555 {
556 auto job = new (std::nothrow) JobQueueT<limit_>();
557 if (job != nullptr)
558 job->__job::obj.res = job;
559 return Ptr(job);
560 }
561
562 private:
563 fun_t *data_[limit_];
564};
565
566} // namespace
567#endif//__cplusplus
568
569/* -------------------------------------------------------------------------- */
570
572
573#endif//__STATEOS_JOB_H
#define INFINITE
infinite waiting
Definition osbase.h:261
void fun_t(void)
timer/task procedure
Definition osbase.h:137
void job_pushISR(job_t *job, fun_t *fun)
ISR alias of job_push().
Definition osjobqueue.h:413
unsigned job_countISR(job_t *job)
ISR alias of job_count().
Definition osjobqueue.h:430
int job_sendFor(job_t *job, fun_t *fun, cnt_t delay)
try to transfer job data to the job queue object, wait for given duration of time while the job queue...
int job_waitUntil(job_t *job, cnt_t time)
try to transfer job data from the job queue object and execute the job procedure, wait until given ti...
int job_tryWait(job_t *job)
alias of job_take()
Definition osjobqueue.h:238
unsigned job_count(job_t *job)
return the amount of data contained in the job queue
int job_wait(job_t *job)
try to transfer job data from the job queue object and execute the job procedure, wait indefinitely w...
Definition osjobqueue.h:302
int job_giveISR(job_t *job, fun_t *fun)
ISR alias of job_give().
Definition osjobqueue.h:328
job_t * job_new(unsigned limit)
alias of job_create()
Definition osjobqueue.h:190
int job_takeAsync(job_t *job)
Async alias of job_take().
unsigned job_spaceISR(job_t *job)
ISR alias of job_space().
Definition osjobqueue.h:447
int job_sendAsync(job_t *job, fun_t *fun)
Async alias of job_send().
unsigned job_limitISR(job_t *job)
ISR alias of job_limit().
Definition osjobqueue.h:464
void job_push(job_t *job, fun_t *fun)
transfer job data to the job queue object, remove the oldest job data if the job queue object is full
void job_kill(job_t *job)
alias of job_reset()
Definition osjobqueue.h:204
int job_giveAsync(job_t *job, fun_t *fun)
Async alias of job_give().
void job_init(job_t *job, fun_t **data, size_t bufsize)
initialize a job queue object
unsigned job_limit(job_t *job)
return the size of the job queue
unsigned job_space(job_t *job)
return the amount of free space in the job queue
int job_give(job_t *job, fun_t *fun)
try to transfer job data to the job queue object, don't wait if the job queue object is full
int job_waitFor(job_t *job, cnt_t delay)
try to transfer job data from the job queue object and execute the job procedure, wait for given dura...
int job_take(job_t *job)
try to transfer job data from the job queue object and execute the job procedure, don't wait if the j...
void job_destroy(job_t *job)
reset the job queue object, wake up all waiting tasks with 'E_DELETED' event value and free allocated...
int job_takeISR(job_t *job)
ISR alias of job_take().
Definition osjobqueue.h:242
job_t * job_create(unsigned limit)
create and initialize a new job queue object
int job_send(job_t *job, fun_t *fun)
try to transfer job data to the job queue object, wait indefinitely while the job queue object is ful...
Definition osjobqueue.h:391
void job_delete(job_t *job)
alias of job_destroy()
Definition osjobqueue.h:218
void job_reset(job_t *job)
reset the job queue object and wake up all waiting tasks with 'E_STOPPED' event
int job_waitAsync(job_t *job)
Async alias of job_wait().
int job_sendUntil(job_t *job, fun_t *fun, cnt_t time)
try to transfer job data to the job queue object, wait until given timepoint while the job queue obje...
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 job queue object
Definition osjobqueue.h:484
unsigned limit()
job_limit()
Definition osjobqueue.h:526
int tryWait()
job_tryWait()
Definition osjobqueue.h:506
unsigned limitISR()
job_limitISR()
Definition osjobqueue.h:527
void kill()
job_kill()
Definition osjobqueue.h:503
int waitAsync()
job_waitAsync()
Definition osjobqueue.h:530
int giveAsync(fun_t *_fun)
job_giveAsync()
Definition osjobqueue.h:531
unsigned spaceISR()
job_spaceISR()
Definition osjobqueue.h:525
std::unique_ptr< JobQueueT< limit_ > > Ptr
result type of Create()
Definition osjobqueue.h:536
int takeISR()
job_takeISR()
Definition osjobqueue.h:507
void reset()
job_reset()
Definition osjobqueue.h:502
int wait()
job_wait()
Definition osjobqueue.h:512
int sendAsync(fun_t *_fun)
job_sendAsync()
Definition osjobqueue.h:532
void pushISR(fun_t *_fun)
job_pushISR()
Definition osjobqueue.h:521
int takeAsync()
job_takeAsync()
Definition osjobqueue.h:529
unsigned count()
job_count()
Definition osjobqueue.h:522
static Ptr Create()
create dynamic object with manageable resources
Definition osjobqueue.h:553
constexpr JobQueueT()
constructor, see the constructor parameters of the class
Definition osjobqueue.h:487
unsigned countISR()
job_countISR()
Definition osjobqueue.h:523
void push(fun_t *_fun)
job_push()
Definition osjobqueue.h:520
int give(fun_t *_fun)
job_give()
Definition osjobqueue.h:513
void destroy()
job_destroy()
Definition osjobqueue.h:504
int sendFor(fun_t *_fun, const T &_delay)
job_sendFor()
Definition osjobqueue.h:516
int send(fun_t *_fun)
job_send()
Definition osjobqueue.h:519
int sendUntil(fun_t *_fun, const T &_time)
job_sendUntil()
Definition osjobqueue.h:518
int waitFor(const T &_delay)
job_waitFor()
Definition osjobqueue.h:509
unsigned space()
job_space()
Definition osjobqueue.h:524
int take()
job_take()
Definition osjobqueue.h:505
int waitUntil(const T &_time)
job_waitUntil()
Definition osjobqueue.h:511
int giveISR(fun_t *_fun)
job_giveISR()
Definition osjobqueue.h:514