Skip to content

Commit 2d1375f

Browse files
teburdMaureenHelm
authored andcommitted
rtio: Fix consume semaphore usage
When the consume semaphore is enabled always give and take from the semaphore. It's expected that rtio_cqe_consume and rtio_cqe_consume_block will be used exclusively rather than directly poking at the SPSC queues. Signed-off-by: Tom Burdick <[email protected]>
1 parent debbb69 commit 2d1375f

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

include/zephyr/rtio/rtio.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static inline void rtio_sqe_prep_write(struct rtio_sqe *sqe,
421421
IF_ENABLED(CONFIG_RTIO_SUBMIT_SEM, \
422422
(static K_SEM_DEFINE(_submit_sem_##name, 0, K_SEM_MAX_LIMIT))) \
423423
IF_ENABLED(CONFIG_RTIO_CONSUME_SEM, \
424-
(static K_SEM_DEFINE(_consume_sem_##name, 0, 1))) \
424+
(static K_SEM_DEFINE(_consume_sem_##name, 0, K_SEM_MAX_LIMIT))) \
425425
static RTIO_SQ_DEFINE(_sq_##name, sq_sz); \
426426
static RTIO_CQ_DEFINE(_cq_##name, cq_sz); \
427427
STRUCT_SECTION_ITERABLE(rtio, name) = { \
@@ -515,7 +515,15 @@ static inline void rtio_sqe_drop_all(struct rtio *r)
515515
*/
516516
static inline struct rtio_cqe *rtio_cqe_consume(struct rtio *r)
517517
{
518+
#ifdef CONFIG_RTIO_CONSUME_SEM
519+
if (k_sem_take(r->consume_sem, K_NO_WAIT) == 0) {
520+
return rtio_spsc_consume(r->cq);
521+
} else {
522+
return NULL;
523+
}
524+
#else
518525
return rtio_spsc_consume(r->cq);
526+
#endif
519527
}
520528

521529
/**
@@ -532,21 +540,18 @@ static inline struct rtio_cqe *rtio_cqe_consume_block(struct rtio *r)
532540
{
533541
struct rtio_cqe *cqe;
534542

535-
/* TODO is there a better way? reset this in submit? */
536543
#ifdef CONFIG_RTIO_CONSUME_SEM
537-
k_sem_reset(r->consume_sem);
538-
#endif
544+
k_sem_take(r->consume_sem, K_FOREVER);
545+
546+
cqe = rtio_spsc_consume(r->cq);
547+
#else
539548
cqe = rtio_spsc_consume(r->cq);
540549

541550
while (cqe == NULL) {
542551
cqe = rtio_spsc_consume(r->cq);
543552

544-
#ifdef CONFIG_RTIO_CONSUME_SEM
545-
k_sem_take(r->consume_sem, K_FOREVER);
546-
#else
547-
k_yield();
548-
#endif
549553
}
554+
#endif
550555

551556
return cqe;
552557
}

0 commit comments

Comments
 (0)