IntrOS
Loading...
Searching...
No Matches
osjobqueue.h
1/******************************************************************************
2
3 @file IntrOS: osjobqueue.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_JOB_H
33#define __INTROS_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
172void job_init( job_t *job, fun_t **data, size_t bufsize );
173
182void job_reset( job_t *job );
183
185__STATIC_INLINE
186void job_kill( job_t *job ) { job_reset(job); }
187
200int job_take( job_t *job );
201
203__STATIC_INLINE
204int job_tryWait( job_t *job ) { return job_take(job); }
205
206#if OS_ATOMICS
208int job_takeAsync( job_t *job );
209#endif
210
225int job_waitFor( job_t *job, cnt_t delay );
226
239int job_waitUntil( job_t *job, cnt_t time );
240
252
253__STATIC_INLINE
254int job_wait( job_t *job ) { return job_waitFor(job, INFINITE); }
255
256#if OS_ATOMICS
258int job_waitAsync( job_t *job );
259#endif
260
274int job_give( job_t *job, fun_t *fun );
275
276#if OS_ATOMICS
278int job_giveAsync( job_t *job, fun_t *fun );
279#endif
280
296int job_sendFor( job_t *job, fun_t *fun, cnt_t delay );
297
311int job_sendUntil( job_t *job, fun_t *fun, cnt_t time );
312
325
326__STATIC_INLINE
327int job_send( job_t *job, fun_t *fun ) { return job_sendFor(job, fun, INFINITE); }
328
329#if OS_ATOMICS
331int job_sendAsync( job_t *job, fun_t *fun );
332#endif
333
342void job_push( job_t *job, fun_t *fun );
343
352unsigned job_count( job_t *job );
353
362unsigned job_space( job_t *job );
363
372unsigned job_limit( job_t *job );
373
374#ifdef __cplusplus
375}
376#endif
377
378/* -------------------------------------------------------------------------- */
379
380#if defined(__cplusplus)
381namespace intros {
382
389
390template<unsigned limit_>
391struct JobQueueT : public __job
392{
394 constexpr
395 JobQueueT(): __job _JOB_INIT(limit_, data_) {}
396
397 ~JobQueueT() { assert(__job::obj.queue == nullptr); }
398
399 /// move constructor
400 JobQueueT( JobQueueT&& _src ): __job _JOB_INIT(limit_, data_)
401 {
402 assert(_src.__job::count == 0 && _src.obj.queue == nullptr);
403 (void) _src;
404 }
405
406 JobQueueT( const JobQueueT& ) = delete;
407 JobQueueT& operator=( JobQueueT&& ) = delete;
408 JobQueueT& operator=( const JobQueueT& ) = delete;
410 void reset () { job_reset (this); }
411 void kill () { job_kill (this); }
412 int take () { return job_take (this); }
413 int tryWait () { return job_tryWait (this); }
414 template<typename T>
415 int waitFor ( const T& _delay ) { return job_waitFor (this, Clock::count(_delay)); }
416 template<typename T>
417 int waitUntil( const T& _time ) { return job_waitUntil(this, Clock::until(_time)); }
418 int wait () { return job_wait (this); }
419 int give ( fun_t *_fun ) { return job_give (this, _fun); }
420 template<typename T>
421 int sendFor ( fun_t *_fun, const T& _delay ) { return job_sendFor (this, _fun, Clock::count(_delay)); }
422 template<typename T>
423 int sendUntil( fun_t *_fun, const T& _time ) { return job_sendUntil(this, _fun, Clock::until(_time)); }
424 int send ( fun_t *_fun ) { return job_send (this, _fun); }
425 void push ( fun_t *_fun ) { job_push (this, _fun); }
426 unsigned count () { return job_count (this); }
427 unsigned space () { return job_space (this); }
428 unsigned limit () { return job_limit (this); }
429#if OS_ATOMICS
430 int takeAsync() { return job_takeAsync(this); }
431 int waitAsync() { return job_waitAsync(this); }
432 int giveAsync( fun_t *_fun ) { return job_giveAsync(this, _fun); }
433 int sendAsync( fun_t *_fun ) { return job_sendAsync(this, _fun); }
434#endif
435
436 private:
437 fun_t *data_[limit_];
438};
439
440} // namespace
441#endif//__cplusplus
442
443/* -------------------------------------------------------------------------- */
444
446
447#endif//__INTROS_JOB_H
#define INFINITE
infinite waiting
Definition osbase.h:250
void fun_t(void)
timer/task procedure
Definition osbase.h:142
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:203
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:253
int job_takeAsync(job_t *job)
Async alias of job_take().
int job_sendAsync(job_t *job, fun_t *fun)
Async alias of job_send().
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:185
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...
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:326
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 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 a job queue object
Definition osjobqueue.h:391
void kill()
job_kill()
Definition osjobqueue.h:410
int waitFor(const T &_delay)
job_waitFor()
Definition osjobqueue.h:414
constexpr JobQueueT()
constructor, see the constructor parameters of the class
Definition osjobqueue.h:394
int sendUntil(fun_t *_fun, const T &_time)
job_sendUntil()
Definition osjobqueue.h:422
int give(fun_t *_fun)
job_give()
Definition osjobqueue.h:418
int send(fun_t *_fun)
job_send()
Definition osjobqueue.h:423
int wait()
job_wait()
Definition osjobqueue.h:417
void push(fun_t *_fun)
job_push()
Definition osjobqueue.h:424
int tryWait()
job_tryWait()
Definition osjobqueue.h:412
unsigned limit()
job_limit()
Definition osjobqueue.h:427
int sendAsync(fun_t *_fun)
job_sendAsync()
Definition osjobqueue.h:432
void reset()
job_reset()
Definition osjobqueue.h:409
unsigned space()
job_space()
Definition osjobqueue.h:426
int sendFor(fun_t *_fun, const T &_delay)
job_sendFor()
Definition osjobqueue.h:420
int waitUntil(const T &_time)
job_waitUntil()
Definition osjobqueue.h:416
int take()
job_take()
Definition osjobqueue.h:411
unsigned count()
job_count()
Definition osjobqueue.h:425
int waitAsync()
job_waitAsync()
Definition osjobqueue.h:430
int takeAsync()
job_takeAsync()
Definition osjobqueue.h:429
int giveAsync(fun_t *_fun)
job_giveAsync()
Definition osjobqueue.h:431