Skip to content

Commit d922e6c

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 userdata is now const by default making it easier to use the device pointer as the userdata which is very common. Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
1 parent d84348f commit d922e6c

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

include/zephyr/rtio/rtio.h

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,28 @@ 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
286297
* @brief Callback signature for RTIO_OP_AWAIT signaled
287298
* @param iodev_sqe IODEV submission for the await op
288299
* @param userdata Userdata
289300
*/
290-
typedef void (*rtio_signaled_t)(struct rtio_iodev_sqe *iodev_sqe, void *userdata);
301+
typedef void (*rtio_signaled_t)(struct rtio_iodev_sqe *iodev_sqe, const void *userdata);
291302

292303
/**
293304
* @brief A submission queue event
@@ -310,7 +321,7 @@ struct rtio_sqe {
310321
* If unique identification of completions is desired this should be
311322
* unique as well.
312323
*/
313-
void *userdata;
324+
const void *userdata;
314325

315326
union {
316327

@@ -556,11 +567,14 @@ struct rtio_iodev {
556567
/** An operation that transmits tiny writes by copying the data to write */
557568
#define RTIO_OP_TINY_TX (RTIO_OP_TX+1)
558569

559-
/** An operation that calls a given function (callback) */
570+
/** An operation that calls a given function (callback) with immutable data */
560571
#define RTIO_OP_CALLBACK (RTIO_OP_TINY_TX+1)
561572

573+
/** An operation that calls a given function (callback) with mutable data */
574+
#define RTIO_OP_CALLBACK_MUT (RTIO_OP_CALLBACK+1)
575+
562576
/** An operation that transceives (reads and writes simultaneously) */
563-
#define RTIO_OP_TXRX (RTIO_OP_CALLBACK+1)
577+
#define RTIO_OP_TXRX (RTIO_OP_CALLBACK_MUT+1)
564578

565579
/** An operation that takes a specified amount of time (asynchronously) before completing */
566580
#define RTIO_OP_DELAY (RTIO_OP_TXRX+1)
@@ -588,7 +602,7 @@ struct rtio_iodev {
588602
*/
589603
static inline void rtio_sqe_prep_nop(struct rtio_sqe *sqe,
590604
const struct rtio_iodev *iodev,
591-
void *userdata)
605+
const void *userdata)
592606
{
593607
memset(sqe, 0, sizeof(struct rtio_sqe));
594608
sqe->op = RTIO_OP_NOP;
@@ -604,7 +618,7 @@ static inline void rtio_sqe_prep_read(struct rtio_sqe *sqe,
604618
int8_t prio,
605619
uint8_t *buf,
606620
uint32_t len,
607-
void *userdata)
621+
const void *userdata)
608622
{
609623
memset(sqe, 0, sizeof(struct rtio_sqe));
610624
sqe->op = RTIO_OP_RX;
@@ -622,15 +636,15 @@ static inline void rtio_sqe_prep_read(struct rtio_sqe *sqe,
622636
*/
623637
static inline void rtio_sqe_prep_read_with_pool(struct rtio_sqe *sqe,
624638
const struct rtio_iodev *iodev, int8_t prio,
625-
void *userdata)
639+
const void *userdata)
626640
{
627641
rtio_sqe_prep_read(sqe, iodev, prio, NULL, 0, userdata);
628642
sqe->flags = RTIO_SQE_MEMPOOL_BUFFER;
629643
}
630644

631645
static inline void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe,
632646
const struct rtio_iodev *iodev, int8_t prio,
633-
void *userdata)
647+
const void *userdata)
634648
{
635649
rtio_sqe_prep_read_with_pool(sqe, iodev, prio, userdata);
636650
sqe->flags |= RTIO_SQE_MULTISHOT;
@@ -644,7 +658,7 @@ static inline void rtio_sqe_prep_write(struct rtio_sqe *sqe,
644658
int8_t prio,
645659
const uint8_t *buf,
646660
uint32_t len,
647-
void *userdata)
661+
const void *userdata)
648662
{
649663
memset(sqe, 0, sizeof(struct rtio_sqe));
650664
sqe->op = RTIO_OP_TX;
@@ -670,7 +684,7 @@ static inline void rtio_sqe_prep_tiny_write(struct rtio_sqe *sqe,
670684
int8_t prio,
671685
const uint8_t *tiny_write_data,
672686
uint8_t tiny_write_len,
673-
void *userdata)
687+
const void *userdata)
674688
{
675689
__ASSERT_NO_MSG(tiny_write_len <= sizeof(sqe->tiny_tx.buf));
676690

@@ -694,7 +708,7 @@ static inline void rtio_sqe_prep_tiny_write(struct rtio_sqe *sqe,
694708
static inline void rtio_sqe_prep_callback(struct rtio_sqe *sqe,
695709
rtio_callback_t callback,
696710
void *arg0,
697-
void *userdata)
711+
const void *userdata)
698712
{
699713
memset(sqe, 0, sizeof(struct rtio_sqe));
700714
sqe->op = RTIO_OP_CALLBACK;
@@ -718,7 +732,7 @@ static inline void rtio_sqe_prep_callback(struct rtio_sqe *sqe,
718732
static inline void rtio_sqe_prep_callback_no_cqe(struct rtio_sqe *sqe,
719733
rtio_callback_t callback,
720734
void *arg0,
721-
void *userdata)
735+
const void *userdata)
722736
{
723737
rtio_sqe_prep_callback(sqe, callback, arg0, userdata);
724738
sqe->flags |= RTIO_SQE_NO_RESPONSE;
@@ -733,7 +747,7 @@ static inline void rtio_sqe_prep_transceive(struct rtio_sqe *sqe,
733747
const uint8_t *tx_buf,
734748
uint8_t *rx_buf,
735749
uint32_t buf_len,
736-
void *userdata)
750+
const void *userdata)
737751
{
738752
memset(sqe, 0, sizeof(struct rtio_sqe));
739753
sqe->op = RTIO_OP_TXRX;
@@ -748,7 +762,7 @@ static inline void rtio_sqe_prep_transceive(struct rtio_sqe *sqe,
748762
static inline void rtio_sqe_prep_await(struct rtio_sqe *sqe,
749763
const struct rtio_iodev *iodev,
750764
int8_t prio,
751-
void *userdata)
765+
const void *userdata)
752766
{
753767
memset(sqe, 0, sizeof(struct rtio_sqe));
754768
sqe->op = RTIO_OP_AWAIT;
@@ -759,7 +773,7 @@ static inline void rtio_sqe_prep_await(struct rtio_sqe *sqe,
759773

760774
static inline void rtio_sqe_prep_delay(struct rtio_sqe *sqe,
761775
k_timeout_t timeout,
762-
void *userdata)
776+
const void *userdata)
763777
{
764778
memset(sqe, 0, sizeof(struct rtio_sqe));
765779
sqe->op = RTIO_OP_DELAY;

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)