Skip to content

Commit e37b3ca

Browse files
Tom Burdickkartben
authored andcommitted
rtio: Add an sqe_acquire_n helper
Makes it safer/easier to obtain n many submissions for use in one go. Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
1 parent aea7108 commit e37b3ca

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

include/zephyr/rtio/rtio.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,51 @@ static inline struct rtio_sqe *rtio_sqe_acquire(struct rtio *r)
11211121
return &iodev_sqe->sqe;
11221122
}
11231123

1124+
/**
1125+
* @brief Acquire a number of submission queue events if available
1126+
*
1127+
* @warning The sqe array is in an undefined state if the return value is -ENOMEM
1128+
*
1129+
* @param r RTIO context
1130+
* @param n Number of submission queue events to acquire
1131+
* @param sqes A pointer to an array of rtio_sqe struct pointers of size n
1132+
*
1133+
* @retval 0 success
1134+
* @retval -ENOMEM out of memory
1135+
*/
1136+
static inline int rtio_sqe_acquire_array(struct rtio *r, size_t n, struct rtio_sqe **sqes)
1137+
{
1138+
struct rtio_iodev_sqe *iodev_sqe;
1139+
size_t i;
1140+
1141+
for (i = 0; i < n; i++) {
1142+
iodev_sqe = rtio_sqe_pool_alloc(r->sqe_pool);
1143+
if (iodev_sqe == NULL) {
1144+
break;
1145+
}
1146+
sqes[i] = &iodev_sqe->sqe;
1147+
}
1148+
1149+
/* Not enough SQEs in the pool */
1150+
if (i < n) {
1151+
while (i > 0) {
1152+
i--;
1153+
iodev_sqe = CONTAINER_OF(sqes[i], struct rtio_iodev_sqe, sqe);
1154+
rtio_sqe_pool_free(r->sqe_pool, iodev_sqe);
1155+
sqes[i] = NULL;
1156+
}
1157+
1158+
return -ENOMEM;
1159+
}
1160+
1161+
for (i = 0; i < n; i++) {
1162+
iodev_sqe = CONTAINER_OF(sqes[i], struct rtio_iodev_sqe, sqe);
1163+
mpsc_push(&r->sq, &iodev_sqe->q);
1164+
}
1165+
1166+
return 0;
1167+
}
1168+
11241169
/**
11251170
* @brief Drop all previously acquired sqe
11261171
*

tests/subsys/rtio/rtio_api/src/test_rtio_api.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,6 @@ ZTEST(rtio_api, test_rtio_await)
10791079
}
10801080

10811081

1082-
10831082
RTIO_DEFINE(r_callback_result, SQE_POOL_SIZE, CQE_POOL_SIZE);
10841083
RTIO_IODEV_TEST_DEFINE(iodev_test_callback_result);
10851084
static int callback_count;
@@ -1140,6 +1139,37 @@ ZTEST(rtio_api, test_rtio_callbacks)
11401139
}
11411140

11421141

1142+
RTIO_DEFINE(r_acquire_array, SQE_POOL_SIZE, CQE_POOL_SIZE);
1143+
1144+
ZTEST(rtio_api, test_rtio_acquire_array)
1145+
{
1146+
TC_PRINT("rtio acquire array\n");
1147+
1148+
struct rtio_sqe *sqes[SQE_POOL_SIZE];
1149+
1150+
int res = rtio_sqe_acquire_array(&r_acquire_array, SQE_POOL_SIZE, sqes);
1151+
1152+
zassert_ok(res, "Expected to acquire sqes");
1153+
1154+
struct rtio_sqe *last_sqe;
1155+
1156+
res = rtio_sqe_acquire_array(&r_acquire_array, 1, &last_sqe);
1157+
zassert_equal(res, -ENOMEM, "Expected to have no more sqes available");
1158+
1159+
rtio_sqe_drop_all(&r_acquire_array);
1160+
1161+
res = rtio_sqe_acquire_array(&r_acquire_array, SQE_POOL_SIZE - 1, sqes);
1162+
zassert_ok(res, "Expected to acquire sqes");
1163+
res = rtio_sqe_acquire_array(&r_acquire_array, 2, &last_sqe);
1164+
zassert_equal(res, -ENOMEM, "Expected to have only have a single sqe available");
1165+
res = rtio_sqe_acquire_array(&r_acquire_array, 1, &last_sqe);
1166+
zassert_equal(res, 0, "Expected a single sqe available");
1167+
res = rtio_sqe_acquire_array(&r_acquire_array, 1, &last_sqe);
1168+
zassert_equal(res, -ENOMEM, "Expected to have no more sqes available");
1169+
1170+
rtio_sqe_drop_all(&r_acquire_array);
1171+
}
1172+
11431173
static void *rtio_api_setup(void)
11441174
{
11451175
#ifdef CONFIG_USERSPACE

0 commit comments

Comments
 (0)