Skip to content

Commit aae53b3

Browse files
committed
rtio: More useful callback OPs
Callbacks now take a result parameter which may, if the callback was linkd to by a previous submissions, have the result code from the last submission. Additionally the callback op is now two, OP_CALLBACK and OP_CALLBACK_MUT where the primary difference is the arg0 parameter is mutable or immutable signaled by the const keyword. Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
1 parent d84348f commit aae53b3

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

include/zephyr/rtio/rtio.h

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,20 @@ struct rtio_iodev_sqe;
277277
* @brief Callback signature for RTIO_OP_CALLBACK
278278
* @param r RTIO context being used with the callback
279279
* @param sqe Submission for the callback op
280-
* @param arg0 Argument option as part of the sqe
280+
* @param res Result of the previously linked submission.
281+
* @param arg0 Pointer to immutable argument option as part of the sqe
281282
*/
282-
typedef void (*rtio_callback_t)(struct rtio *r, const struct rtio_sqe *sqe, void *arg0);
283+
typedef void (*rtio_callback_t)(struct rtio *r, const struct rtio_sqe *sqe, int res, const void *arg0);
284+
285+
/**
286+
* @typedef rtio_callback_mut_t
287+
* @brief Callback signature for RTIO_OP_CALLBACK
288+
* @param r RTIO context being used with the callback
289+
* @param sqe Submission for the callback op
290+
* @param res Result of the previously linked submission.
291+
* @param arg0 Pointer to mutable argument option as part of the sqe
292+
*/
293+
typedef void (*rtio_callback_mut_t)(struct rtio *r, const struct rtio_sqe *sqe, int res, void *arg0);
283294

284295
/**
285296
* @typedef rtio_signaled_t
@@ -335,9 +346,15 @@ struct rtio_sqe {
335346
/** OP_CALLBACK */
336347
struct {
337348
rtio_callback_t callback;
338-
void *arg0; /**< Last argument given to callback */
349+
const void *arg0; /**< Last argument given to callback */
339350
} callback;
340351

352+
/** OP_CALLBACK_MUT */
353+
struct {
354+
rtio_callback_t callback;
355+
void *arg0; /**< Last argument given to callback */
356+
} callback_mut;
357+
341358
/** OP_TXRX */
342359
struct {
343360
uint32_t buf_len; /**< Length of tx and rx buffers */
@@ -556,11 +573,14 @@ struct rtio_iodev {
556573
/** An operation that transmits tiny writes by copying the data to write */
557574
#define RTIO_OP_TINY_TX (RTIO_OP_TX+1)
558575

559-
/** An operation that calls a given function (callback) */
576+
/** An operation that calls a given function (callback) with immutable data */
560577
#define RTIO_OP_CALLBACK (RTIO_OP_TINY_TX+1)
561578

579+
/** An operation that calls a given function (callback) with mutable data */
580+
#define RTIO_OP_CALLBACK_MUT (RTIO_OP_CALLBACK+1)
581+
562582
/** An operation that transceives (reads and writes simultaneously) */
563-
#define RTIO_OP_TXRX (RTIO_OP_CALLBACK+1)
583+
#define RTIO_OP_TXRX (RTIO_OP_CALLBACK_MUT+1)
564584

565585
/** An operation that takes a specified amount of time (asynchronously) before completing */
566586
#define RTIO_OP_DELAY (RTIO_OP_TXRX+1)
@@ -693,7 +713,7 @@ static inline void rtio_sqe_prep_tiny_write(struct rtio_sqe *sqe,
693713
*/
694714
static inline void rtio_sqe_prep_callback(struct rtio_sqe *sqe,
695715
rtio_callback_t callback,
696-
void *arg0,
716+
const void *arg0,
697717
void *userdata)
698718
{
699719
memset(sqe, 0, sizeof(struct rtio_sqe));
@@ -724,6 +744,43 @@ static inline void rtio_sqe_prep_callback_no_cqe(struct rtio_sqe *sqe,
724744
sqe->flags |= RTIO_SQE_NO_RESPONSE;
725745
}
726746

747+
748+
/**
749+
* @brief Prepare a callback mut op submission
750+
*
751+
* A somewhat special operation in that it may only be done in kernel mode.
752+
*
753+
* Used where general purpose logic is required in a queue of io operations to do
754+
* transforms or logic with a mutable argument passed to the function
755+
*/
756+
static inline void rtio_sqe_prep_callback_mut(struct rtio_sqe *sqe,
757+
rtio_callback_t callback,
758+
void *arg0,
759+
void *userdata)
760+
{
761+
memset(sqe, 0, sizeof(struct rtio_sqe));
762+
sqe->op = RTIO_OP_CALLBACK;
763+
sqe->prio = 0;
764+
sqe->iodev = NULL;
765+
sqe->callback.callback = callback;
766+
sqe->callback.arg0 = arg0;
767+
sqe->userdata = userdata;
768+
}
769+
770+
/**
771+
* @brief Prepare a callback op submission that does not create a CQE
772+
*
773+
* Similar to @ref rtio_sqe_prep_callback_no_cqe, but the arg0 is mutable.
774+
*/
775+
static inline void rtio_sqe_prep_callback_mut_no_cqe(struct rtio_sqe *sqe,
776+
rtio_callback_t callback,
777+
void *arg0,
778+
void *userdata)
779+
{
780+
rtio_sqe_prep_callback_mut(sqe, callback, arg0, userdata);
781+
sqe->flags |= RTIO_SQE_NO_RESPONSE;
782+
}
783+
727784
/**
728785
* @brief Prepare a transceive op submission
729786
*/

subsys/rtio/rtio_executor.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ LOG_MODULE_REGISTER(rtio_executor, CONFIG_RTIO_LOG_LEVEL);
1515
/**
1616
* @brief Executor handled submissions
1717
*/
18-
static void rtio_executor_op(struct rtio_iodev_sqe *iodev_sqe)
18+
static void rtio_executor_op(struct rtio_iodev_sqe *iodev_sqe, int last_result)
1919
{
2020
const struct rtio_sqe *sqe = &iodev_sqe->sqe;
2121

2222
switch (sqe->op) {
2323
case RTIO_OP_CALLBACK:
24-
sqe->callback.callback(iodev_sqe->r, sqe, sqe->callback.arg0);
24+
sqe->callback.callback(iodev_sqe->r, sqe, last_result, sqe->callback.arg0);
25+
rtio_iodev_sqe_ok(iodev_sqe, 0);
26+
break;
27+
case RTIO_OP_CALLBACK_MUT:
28+
sqe->callback_mut.callback(iodev_sqe->r, sqe, last_result, sqe->callback.arg0);
2529
rtio_iodev_sqe_ok(iodev_sqe, 0);
2630
break;
2731
case RTIO_OP_DELAY:
@@ -40,7 +44,7 @@ static void rtio_executor_op(struct rtio_iodev_sqe *iodev_sqe)
4044
*
4145
* @param iodev_sqe Submission to work on
4246
*/
43-
static inline void rtio_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
47+
static inline void rtio_iodev_submit(struct rtio_iodev_sqe *iodev_sqe, int last_result)
4448
{
4549
if (FIELD_GET(RTIO_SQE_CANCELED, iodev_sqe->sqe.flags)) {
4650
rtio_iodev_sqe_err(iodev_sqe, -ECANCELED);
@@ -49,7 +53,7 @@ static inline void rtio_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
4953

5054
/* No iodev means its an executor specific operation */
5155
if (iodev_sqe->sqe.iodev == NULL) {
52-
rtio_executor_op(iodev_sqe);
56+
rtio_executor_op(iodev_sqe, result);
5357
return;
5458
}
5559

@@ -113,7 +117,7 @@ void rtio_executor_submit(struct rtio *r)
113117
curr->next = NULL;
114118
curr->r = r;
115119

116-
rtio_iodev_submit(iodev_sqe);
120+
rtio_iodev_submit(iodev_sqe, 0);
117121

118122
node = mpsc_pop(&r->sq);
119123
}
@@ -183,7 +187,7 @@ static inline void rtio_executor_done(struct rtio_iodev_sqe *iodev_sqe, int resu
183187

184188
/* curr should now be the last sqe in the transaction if that is what completed */
185189
if (sqe_flags & RTIO_SQE_CHAINED) {
186-
rtio_iodev_submit(curr);
190+
rtio_iodev_submit(curr, result);
187191
}
188192
}
189193

0 commit comments

Comments
 (0)