1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
This file is part of ChibiOS.
ChibiOS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file oslib/include/chjobs.h
* @brief Jobs Queues structures and macros.
* @details This module implements queues of generic jobs to be delegated
* asynchronously to a pool of dedicated threads.
* Operations defined for Jobs Queues
* - <b>Get</b>: An job object is taken from the pool of the
* available jobs.
* - <b>Post</b>: A job is posted to the queue, it will be
* returned to the pool after execution.
* .
*
* @addtogroup oslib_jobs_queues
* @{
*/
#ifndef CHJOBS_H
#define CHJOBS_H
#if (CH_CFG_USE_JOBS == TRUE) || defined(__DOXYGEN__)
/*===========================================================================*/
/* Module constants. */
/*===========================================================================*/
/**
* @brief Dispatcher return code in case of a @p JOB_NUL has been received.
*/
#define MSG_JOB_NULL ((msg_t)-2)
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
#if CH_CFG_USE_MEMPOOLS == FALSE
#error "CH_CFG_USE_JOBS requires CH_CFG_USE_MEMPOOLS"
#endif
#if CH_CFG_USE_SEMAPHORES == FALSE
#error "CH_CFG_USE_JOBS requires CH_CFG_USE_SEMAPHORES"
#endif
#if CH_CFG_USE_MAILBOXES == FALSE
#error "CH_CFG_USE_JOBS requires CH_CFG_USE_MAILBOXES"
#endif
/*===========================================================================*/
/* Module data structures and types. */
/*===========================================================================*/
/**
* @brief Type of a jobs queue.
*/
typedef struct ch_jobs_queue {
/**
* @brief Pool of the free jobs.
*/
guarded_memory_pool_t free;
/**
* @brief Mailbox of the sent jobs.
*/
mailbox_t mbx;
} jobs_queue_t;
/**
* @brief Type of a job function.
*/
typedef void (*job_function_t)(void *arg);
/**
* @brief Type of a job descriptor.
*/
typedef struct ch_job_descriptor {
/**
* @brief Job function.
*/
job_function_t jobfunc;
/**
* @brief Argument to be passed to the job function.
*/
void *jobarg;
} job_descriptor_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
/*===========================================================================*/
/* Module inline functions. */
/*===========================================================================*/
/**
* @brief Initializes a jobs queue object.
*
* @param[out] jqp pointer to a @p jobs_queue_t structure
* @param[in] jobsn number of jobs available
* @param[in] jobsbuf pointer to the buffer of jobs, it must be able
* to hold @p jobsn @p job_descriptor_t structures
* @param[in] msgbuf pointer to the buffer of messages, it must be able
* to hold @p jobsn @p msg_t messages
*
* @init
*/
static inline void chJobObjectInit(jobs_queue_t *jqp,
size_t jobsn,
job_descriptor_t *jobsbuf,
msg_t *msgbuf) {
chDbgCheck((jobsn > 0U) && (jobsbuf != NULL) && (msgbuf != NULL));
chGuardedPoolObjectInit(&jqp->free, sizeof (job_descriptor_t));
chGuardedPoolLoadArray(&jqp->free, (void *)jobsbuf, jobsn);
chMBObjectInit(&jqp->mbx, msgbuf, jobsn);
}
/**
* @brief Allocates a free job object.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @return The pointer to the allocated job object.
*
* @api
*/
static inline job_descriptor_t *chJobGet(jobs_queue_t *jqp) {
return (job_descriptor_t *)chGuardedPoolAllocTimeout(&jqp->free, TIME_INFINITE);
}
/**
* @brief Allocates a free job object.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @return The pointer to the allocated job object.
* @retval NULL if a job object is not immediately available.
*
* @iclass
*/
static inline job_descriptor_t *chJobGetI(jobs_queue_t *jqp) {
return (job_descriptor_t *)chGuardedPoolAllocI(&jqp->free);
}
/**
* @brief Allocates a free job object.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return The pointer to the allocated job object.
* @retval NULL if a job object is not available within the specified
* timeout.
*
* @sclass
*/
static inline job_descriptor_t *chJobGetTimeoutS(jobs_queue_t *jqp,
sysinterval_t timeout) {
return (job_descriptor_t *)chGuardedPoolAllocTimeoutS(&jqp->free, timeout);
}
/**
* @brief Allocates a free job object.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return The pointer to the allocated job object.
* @retval NULL if a job object is not available within the specified
* timeout.
*
* @api
*/
static inline job_descriptor_t *chJobGetTimeout(jobs_queue_t *jqp,
sysinterval_t timeout) {
return (job_descriptor_t *)chGuardedPoolAllocTimeout(&jqp->free, timeout);
}
/**
* @brief Posts a job object.
* @note By design the object can be always immediately posted.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] jp pointer to the job object to be posted
*
* @iclass
*/
static inline void chJobPostI(jobs_queue_t *jqp, job_descriptor_t *jp) {
msg_t msg;
msg = chMBPostI(&jqp->mbx, (msg_t)jp);
chDbgAssert(msg == MSG_OK, "post failed");
}
/**
* @brief Posts a job object.
* @note By design the object can be always immediately posted.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] jp pointer to the job object to be posted
*
* @sclass
*/
static inline void chJobPostS(jobs_queue_t *jqp, job_descriptor_t *jp) {
msg_t msg;
msg = chMBPostTimeoutS(&jqp->mbx, (msg_t)jp, TIME_IMMEDIATE);
chDbgAssert(msg == MSG_OK, "post failed");
}
/**
* @brief Posts a job object.
* @note By design the object can be always immediately posted.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] jp pointer to the job object to be posted
*
* @api
*/
static inline void chJobPost(jobs_queue_t *jqp, job_descriptor_t *jp) {
msg_t msg;
msg = chMBPostTimeout(&jqp->mbx, (msg_t)jp, TIME_IMMEDIATE);
chDbgAssert(msg == MSG_OK, "post failed");
}
/**
* @brief Posts an high priority job object.
* @note By design the object can be always immediately posted.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] jp pointer to the job object to be posted
*
* @iclass
*/
static inline void chJobPostAheadI(jobs_queue_t *jqp, job_descriptor_t *jp) {
msg_t msg;
msg = chMBPostAheadI(&jqp->mbx, (msg_t)jp);
chDbgAssert(msg == MSG_OK, "post failed");
}
/**
* @brief Posts an high priority job object.
* @note By design the object can be always immediately posted.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] jp pointer to the job object to be posted
*
* @sclass
*/
static inline void chJobPostAheadS(jobs_queue_t *jqp, job_descriptor_t *jp) {
msg_t msg;
msg = chMBPostAheadTimeoutS(&jqp->mbx, (msg_t)jp, TIME_IMMEDIATE);
chDbgAssert(msg == MSG_OK, "post failed");
}
/**
* @brief Posts an high priority job object.
* @note By design the object can be always immediately posted.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] jp pointer to the job object to be posted
*
* @api
*/
static inline void chJobPostAhead(jobs_queue_t *jqp, job_descriptor_t *jp) {
msg_t msg;
msg = chMBPostAheadTimeout(&jqp->mbx, (msg_t)jp, TIME_IMMEDIATE);
chDbgAssert(msg == MSG_OK, "post failed");
}
/**
* @brief Waits for a job then executes it.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @return The function outcome.
* @retval MSG_OK if a job has been executed.
* @retval MSG_RESET if the internal mailbox has been reset.
* @retval MSG_JOB_NULL if a @p JOB_NULL has been received.
*/
static inline msg_t chJobDispatch(jobs_queue_t *jqp) {
msg_t msg, jmsg;
/* Waiting for a job.*/
msg = chMBFetchTimeout(&jqp->mbx, &jmsg, TIME_INFINITE);
if (msg == MSG_OK) {
job_descriptor_t *jp = (job_descriptor_t *)jmsg;
chDbgAssert(jp != NULL, "is NULL");
if (jp->jobfunc != NULL) {
/* Invoking the job function.*/
jp->jobfunc(jp->jobarg);
/* Returning the job descriptor object.*/
chGuardedPoolFree(&jqp->free, (void *)jp);
}
else {
msg = MSG_JOB_NULL;
}
}
return msg;
}
/**
* @brief Waits for a job then executes it.
*
* @param[in] jqp pointer to a @p jobs_queue_t structure
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return The function outcome.
* @retval MSG_OK if a job has been executed.
* @retval MSG_TIMEOUT if a timeout occurred.
* @retval MSG_RESET if the internal mailbox has been reset.
* @retval MSG_JOB_NULL if a @p JOB_NULL has been received.
*/
static inline msg_t chJobDispatchTimeout(jobs_queue_t *jqp,
sysinterval_t timeout) {
msg_t msg, jmsg;
/* Waiting for a job or a timeout.*/
msg = chMBFetchTimeout(&jqp->mbx, &jmsg, timeout);
if (msg == MSG_OK) {
job_descriptor_t *jp = (job_descriptor_t *)jmsg;
chDbgAssert(jp != NULL, "is NULL");
if (jp->jobfunc != NULL) {
/* Invoking the job function.*/
jp->jobfunc(jp->jobarg);
/* Returning the job descriptor object.*/
chGuardedPoolFree(&jqp->free, (void *)jp);
}
else {
msg = MSG_JOB_NULL;
}
}
return msg;
}
#endif /* CH_CFG_USE_JOBS == TRUE */
#endif /* CHJOBS_H */
/** @} */
|