From fe9b356b8f432277227931508149a5362e6a46df Mon Sep 17 00:00:00 2001 From: Bowen Wang Date: Fri, 13 Nov 2026 21:06:21 +0800 Subject: [PATCH] testing/libc/ring: add cmocka tests for the lock-free ring Add a cmocka suite for include/nuttx/ring.h covering all four modes (SPSC/MPSC/SPMC/MPMC): basic and batch enqueue/dequeue, full and empty boundaries, partial batches, index wraparound, non-power-of-two capacities, ring_alloc(), overwrite, skip, init and warm-restart recovery (rewind, corrupted or resized blob, interrupted MP commit, SPSC slave, concurrent init), and pthread producer/consumer stress. Enable CONFIG_TESTING_RING and run "ring_test" from nsh. Signed-off-by: wangjianyu3 Signed-off-by: Bowen Wang --- testing/libc/ring/CMakeLists.txt | 39 + testing/libc/ring/Kconfig | 30 + testing/libc/ring/Make.defs | 25 + testing/libc/ring/Makefile | 37 + testing/libc/ring/ring_main.c | 42 + testing/libc/ring/ring_mpmc.c | 1074 ++++++++++++++++++++++ testing/libc/ring/ring_mpsc.c | 511 +++++++++++ testing/libc/ring/ring_spmc.c | 520 +++++++++++ testing/libc/ring/ring_spsc.c | 1462 ++++++++++++++++++++++++++++++ testing/libc/ring/ring_test.h | 176 ++++ 10 files changed, 3916 insertions(+) create mode 100644 testing/libc/ring/CMakeLists.txt create mode 100644 testing/libc/ring/Kconfig create mode 100644 testing/libc/ring/Make.defs create mode 100644 testing/libc/ring/Makefile create mode 100644 testing/libc/ring/ring_main.c create mode 100644 testing/libc/ring/ring_mpmc.c create mode 100644 testing/libc/ring/ring_mpsc.c create mode 100644 testing/libc/ring/ring_spmc.c create mode 100644 testing/libc/ring/ring_spsc.c create mode 100644 testing/libc/ring/ring_test.h diff --git a/testing/libc/ring/CMakeLists.txt b/testing/libc/ring/CMakeLists.txt new file mode 100644 index 00000000000..742491a7e12 --- /dev/null +++ b/testing/libc/ring/CMakeLists.txt @@ -0,0 +1,39 @@ +# ############################################################################## +# apps/testing/libc/ring/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_TESTING_RING) + nuttx_add_application( + NAME + ${CONFIG_TESTING_RING_PROGNAME} + PRIORITY + ${CONFIG_TESTING_RING_PRIORITY} + STACKSIZE + ${CONFIG_TESTING_RING_STACKSIZE} + MODULE + ${CONFIG_TESTING_RING} + SRCS + ring_main.c + ring_spsc.c + ring_mpsc.c + ring_spmc.c + ring_mpmc.c) +endif() diff --git a/testing/libc/ring/Kconfig b/testing/libc/ring/Kconfig new file mode 100644 index 00000000000..8ba96af9e19 --- /dev/null +++ b/testing/libc/ring/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_RING + tristate "cmocka ring queue test" + default n + depends on TESTING_CMOCKA + ---help--- + Enable the lock-free ring queue testing using cmocka framework. + +if TESTING_RING + +config TESTING_RING_PROGNAME + string "Program name" + default "ring_test" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config TESTING_RING_PRIORITY + int "Ring test task priority" + default 100 + +config TESTING_RING_STACKSIZE + int "Ring test stack size" + default 8192 + +endif diff --git a/testing/libc/ring/Make.defs b/testing/libc/ring/Make.defs new file mode 100644 index 00000000000..daf733d84b1 --- /dev/null +++ b/testing/libc/ring/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/testing/libc/ring/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_TESTING_RING),) +CONFIGURED_APPS += $(APPDIR)/testing/libc/ring +endif diff --git a/testing/libc/ring/Makefile b/testing/libc/ring/Makefile new file mode 100644 index 00000000000..7a0a1e6baf0 --- /dev/null +++ b/testing/libc/ring/Makefile @@ -0,0 +1,37 @@ +############################################################################ +# apps/testing/libc/ring/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# Ring test built-in application info + +PROGNAME = $(CONFIG_TESTING_RING_PROGNAME) +PRIORITY = $(CONFIG_TESTING_RING_PRIORITY) +STACKSIZE = $(CONFIG_TESTING_RING_STACKSIZE) +MODULE = $(CONFIG_TESTING_RING) + +# Ring test sources + +CSRCS = ring_spsc.c ring_mpsc.c ring_spmc.c ring_mpmc.c +MAINSRC = ring_main.c + +include $(APPDIR)/Application.mk diff --git a/testing/libc/ring/ring_main.c b/testing/libc/ring/ring_main.c new file mode 100644 index 00000000000..6a41ce96707 --- /dev/null +++ b/testing/libc/ring/ring_main.c @@ -0,0 +1,42 @@ +/**************************************************************************** + * apps/testing/libc/ring/ring_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "ring_test.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + int fail = 0; + + fail += ring_test_spsc_local(); + fail += ring_test_mpsc_local(); + fail += ring_test_spmc_local(); + fail += ring_test_mpmc_local(); + return fail; +} diff --git a/testing/libc/ring/ring_mpmc.c b/testing/libc/ring/ring_mpmc.c new file mode 100644 index 00000000000..7483a146346 --- /dev/null +++ b/testing/libc/ring/ring_mpmc.c @@ -0,0 +1,1074 @@ +/**************************************************************************** + * apps/testing/libc/ring/ring_mpmc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "ring_test.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Per-test static rings, kept off the test stack */ + +RING_TEST_DEFINE(g_mpmc_basic, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_batch, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_full, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_typed, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_boundary, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_partial, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_wrap, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_cap2, uint32_t, 2); +RING_TEST_DEFINE(g_mpmc_count, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_conc, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_ow, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_ow_wrap, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_ow_batch, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_ow_cap, uint32_t, 8); +RING_TEST_DEFINE(g_mpmc_ow_conc, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpmc_skip_conc, uint32_t, RING_TEST_CAP); + +/**************************************************************************** + * Private Functions - Single-core functional test + ****************************************************************************/ + +static void test_mpmc_basic(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_basic); + + for (i = 0; i < 16; i++) + { + val = i + 500; + n = rt_mpmc_enqueue(&g_mpmc_basic, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_mpmc_basic), 16); + + for (i = 0; i < 16; i++) + { + n = rt_mpmc_dequeue(&g_mpmc_basic, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 500); + } +} + +static void test_mpmc_batch(FAR void **state) +{ + uint32_t buf[RING_TEST_BATCH]; + uint32_t out[RING_TEST_BATCH]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_batch); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + buf[i] = i + 200; + } + + n = rt_mpmc_enqueue(&g_mpmc_batch, buf, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + n = rt_mpmc_dequeue(&g_mpmc_batch, out, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + assert_int_equal(out[i], i + 200); + } +} + +static void test_mpmc_full(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_full); + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_mpmc_enqueue(&g_mpmc_full, &val, 1); + assert_int_equal(n, 1); + } + + val = 999; + n = rt_mpmc_enqueue(&g_mpmc_full, &val, 1); + assert_int_equal(n, 0); + + for (i = 0; i < RING_TEST_CAP; i++) + { + n = rt_mpmc_dequeue(&g_mpmc_full, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i); + } + + assert_int_equal(rt_count(&g_mpmc_full), 0); +} + +static void test_mpmc_typed_api(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t batch[4]; + uint32_t obatch[4]; + uint32_t n; + uint32_t i; + + ring_test_init(&g_mpmc_typed); + + val = 42; + n = rt_mpmc_enqueue(&g_mpmc_typed, &val, 1); + assert_int_equal(n, 1); + n = rt_mpmc_dequeue(&g_mpmc_typed, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 42); + + for (i = 0; i < 4; i++) + { + batch[i] = i + 500; + } + + n = rt_mpmc_enqueue(&g_mpmc_typed, batch, 4); + assert_int_equal(n, 4); + n = rt_mpmc_dequeue(&g_mpmc_typed, obatch, 4); + assert_int_equal(n, 4); + + for (i = 0; i < 4; i++) + { + assert_int_equal(obatch[i], i + 500); + } +} + +static void test_mpmc_alloc_free(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + assert_non_null(ring_test_alloc(r, RING_TEST_CAP, sizeof(uint32_t))); + + val = 77; + n = rt_mpmc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_mpmc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 77); + + ring_free(r->r); +} + +/**************************************************************************** + * Private Functions - Boundary condition tests (MP enqueue, MC dequeue) + ****************************************************************************/ + +static void test_mpmc_enqueue_dequeue_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_boundary); + + /* Dequeue from empty ring (MC CAS path) */ + + n = rt_mpmc_dequeue(&g_mpmc_boundary, &out, 1); + assert_int_equal(n, 0); + + /* Enqueue count=0 (MP CAS path) */ + + val = 42; + n = rt_mpmc_enqueue(&g_mpmc_boundary, &val, 0); + assert_int_equal(n, 0); + assert_int_equal(rt_count(&g_mpmc_boundary), 0); + + /* Dequeue count=0 */ + + n = rt_mpmc_dequeue(&g_mpmc_boundary, &out, 0); + assert_int_equal(n, 0); + + /* Fill ring (MP CAS path), then try one more */ + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_mpmc_enqueue(&g_mpmc_boundary, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_mpmc_boundary), RING_TEST_CAP); + + val = 999; + n = rt_mpmc_enqueue(&g_mpmc_boundary, &val, 1); + assert_int_equal(n, 0); +} + +static void test_mpmc_partial_batch(FAR void **state) +{ + uint32_t buf[16]; + uint32_t out[16]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_partial); + + /* Batch enqueue larger than capacity (MP CAS path) */ + + for (i = 0; i < 16; i++) + { + buf[i] = i + 1; + } + + n = rt_mpmc_enqueue(&g_mpmc_partial, buf, 16); + assert_int_equal(n, 8); + + /* Batch dequeue larger than available (MC CAS path) */ + + n = rt_mpmc_dequeue(&g_mpmc_partial, out, 16); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1); + } + + /* Partial enqueue when partially full */ + + for (i = 0; i < 6; i++) + { + buf[i] = i + 100; + } + + n = rt_mpmc_enqueue(&g_mpmc_partial, buf, 6); + assert_int_equal(n, 6); + + for (i = 0; i < 4; i++) + { + buf[i] = i + 200; + } + + n = rt_mpmc_enqueue(&g_mpmc_partial, buf, 4); + assert_int_equal(n, 2); + assert_int_equal(rt_count(&g_mpmc_partial), 8); + + /* Partial dequeue (MC CAS path) */ + + n = rt_mpmc_dequeue(&g_mpmc_partial, out, 5); + assert_int_equal(n, 5); + assert_int_equal(rt_count(&g_mpmc_partial), 3); + + n = rt_mpmc_dequeue(&g_mpmc_partial, out, 3); + assert_int_equal(n, 3); + assert_int_equal(rt_count(&g_mpmc_partial), 0); +} + +static void test_mpmc_wraparound_batch(FAR void **state) +{ + uint32_t buf[8]; + uint32_t out[8]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_wrap); + + /* Advance indices to force wraparound */ + + for (i = 0; i < 6; i++) + { + buf[i] = i; + } + + n = rt_mpmc_enqueue(&g_mpmc_wrap, buf, 6); + assert_int_equal(n, 6); + n = rt_mpmc_dequeue(&g_mpmc_wrap, out, 6); + assert_int_equal(n, 6); + + /* Enqueue 8 items wrapping around (MP CAS path) */ + + for (i = 0; i < 8; i++) + { + buf[i] = i + 1000; + } + + n = rt_mpmc_enqueue(&g_mpmc_wrap, buf, 8); + assert_int_equal(n, 8); + + /* Dequeue wrapping around (MC CAS path) */ + + memset(out, 0, sizeof(out)); + n = rt_mpmc_dequeue(&g_mpmc_wrap, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1000); + } +} + +static void test_mpmc_capacity_two(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t n; + + ring_test_init(&g_mpmc_cap2); + + assert_int_equal(rt_capacity(&g_mpmc_cap2), 2); + + val = 10; + n = rt_mpmc_enqueue(&g_mpmc_cap2, &val, 1); + assert_int_equal(n, 1); + + val = 20; + n = rt_mpmc_enqueue(&g_mpmc_cap2, &val, 1); + assert_int_equal(n, 1); + + /* Full (MP CAS path) */ + + val = 30; + n = rt_mpmc_enqueue(&g_mpmc_cap2, &val, 1); + assert_int_equal(n, 0); + + /* MC dequeue */ + + n = rt_mpmc_dequeue(&g_mpmc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 10); + + n = rt_mpmc_dequeue(&g_mpmc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 20); + + /* Empty (MC CAS path) */ + + n = rt_mpmc_dequeue(&g_mpmc_cap2, &out, 1); + assert_int_equal(n, 0); +} + +static void test_mpmc_count_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out[8]; + uint32_t i; + + ring_test_init(&g_mpmc_count); + + assert_int_equal(rt_count(&g_mpmc_count), 0); + + for (i = 0; i < 3; i++) + { + val = i; + rt_mpmc_enqueue(&g_mpmc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_mpmc_count), 3); + + for (i = 3; i < 8; i++) + { + val = i; + rt_mpmc_enqueue(&g_mpmc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_mpmc_count), 8); + + rt_mpmc_dequeue(&g_mpmc_count, out, 1); + assert_int_equal(rt_count(&g_mpmc_count), 7); + + rt_mpmc_dequeue(&g_mpmc_count, out, 7); + assert_int_equal(rt_count(&g_mpmc_count), 0); +} + +/**************************************************************************** + * Private Functions - Overwrite enqueue tests + ****************************************************************************/ + +static void test_mpmc_overwrite_basic(FAR void **state) +{ + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow); + + for (i = 0; i < 8; i++) + { + val = i; + rt_mpmc_overwrite(&g_mpmc_ow, &val, 1); + } + + assert_int_equal(rt_count(&g_mpmc_ow), 8); + + /* Overwrite: push one more, oldest (0) discarded */ + + val = 100; + n = rt_mpmc_overwrite(&g_mpmc_ow, &val, 1); + assert_int_equal(n, 1); + assert_int_equal(rt_count(&g_mpmc_ow), 8); + + n = rt_mpmc_dequeue(&g_mpmc_ow, out, 8); + assert_int_equal(n, 8); + for (i = 0; i < 7; i++) + { + assert_int_equal(out[i], i + 1); + } + + assert_int_equal(out[7], 100); +} + +static void test_mpmc_overwrite_wrap(FAR void **state) +{ + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow_wrap); + + /* Advance indices to force wraparound */ + + for (i = 0; i < 6; i++) + { + val = i; + rt_mpmc_overwrite(&g_mpmc_ow_wrap, &val, 1); + } + + rt_mpmc_dequeue(&g_mpmc_ow_wrap, out, 6); + + /* Fill ring (indices start at 6, wraps at 8) */ + + for (i = 0; i < 8; i++) + { + val = i + 10; + rt_mpmc_overwrite(&g_mpmc_ow_wrap, &val, 1); + } + + /* Overwrite 3 more: discards 10,11,12 */ + + for (i = 0; i < 3; i++) + { + val = i + 200; + rt_mpmc_overwrite(&g_mpmc_ow_wrap, &val, 1); + } + + assert_int_equal(rt_count(&g_mpmc_ow_wrap), 8); + + n = rt_mpmc_dequeue(&g_mpmc_ow_wrap, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 5; i++) + { + assert_int_equal(out[i], i + 13); + } + + for (i = 0; i < 3; i++) + { + assert_int_equal(out[i + 5], i + 200); + } +} + +static void test_mpmc_overwrite_batch(FAR void **state) +{ + uint32_t in[4]; + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow_batch); + + for (i = 0; i < 8; i++) + { + val = i; + rt_mpmc_overwrite(&g_mpmc_ow_batch, &val, 1); + } + + /* Batch overwrite 4 items: discards 0,1,2,3 */ + + for (i = 0; i < 4; i++) + { + in[i] = i + 100; + } + + n = rt_mpmc_overwrite(&g_mpmc_ow_batch, in, 4); + assert_int_equal(n, 4); + assert_int_equal(rt_count(&g_mpmc_ow_batch), 8); + + n = rt_mpmc_dequeue(&g_mpmc_ow_batch, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 4; i++) + { + assert_int_equal(out[i], i + 4); + } + + for (i = 0; i < 4; i++) + { + assert_int_equal(out[i + 4], i + 100); + } +} + +static void test_mpmc_overwrite_exceed_cap(FAR void **state) +{ + uint32_t in[16]; + uint32_t out[8]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow_cap); + + for (i = 0; i < 16; i++) + { + in[i] = i; + } + + n = rt_mpmc_overwrite(&g_mpmc_ow_cap, in, 16); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_mpmc_ow_cap), 8); + + n = rt_mpmc_dequeue(&g_mpmc_ow_cap, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 8); + } +} + +static void test_mpmc_overwrite_not_full(FAR void **state) +{ + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow); + + for (i = 0; i < 4; i++) + { + val = i; + rt_mpmc_overwrite(&g_mpmc_ow, &val, 1); + } + + val = 99; + n = rt_mpmc_overwrite(&g_mpmc_ow, &val, 1); + assert_int_equal(n, 0); + assert_int_equal(rt_count(&g_mpmc_ow), 5); + + n = rt_mpmc_dequeue(&g_mpmc_ow, out, 5); + assert_int_equal(n, 5); + for (i = 0; i < 4; i++) + { + assert_int_equal(out[i], i); + } + + assert_int_equal(out[4], 99); +} + +/**************************************************************************** + * Private Functions - Dequeue skip tests + ****************************************************************************/ + +static void test_mpmc_dequeue_skip_basic(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow); + + for (i = 0; i < 8; i++) + { + val = i; + rt_mpmc_enqueue(&g_mpmc_ow, &val, 1); + } + + n = rt_mpmc_skip(&g_mpmc_ow, 3); + assert_int_equal(n, 3); + assert_int_equal(rt_count(&g_mpmc_ow), 5); + + n = rt_mpmc_dequeue(&g_mpmc_ow, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 3); +} + +static void test_mpmc_dequeue_skip_all(FAR void **state) +{ + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow); + + for (i = 0; i < 8; i++) + { + val = i; + rt_mpmc_enqueue(&g_mpmc_ow, &val, 1); + } + + n = rt_mpmc_skip(&g_mpmc_ow, 8); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_mpmc_ow), 0); +} + +static void test_mpmc_dequeue_skip_more_than_avail(FAR void **state) +{ + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpmc_ow); + + for (i = 0; i < 4; i++) + { + val = i; + rt_mpmc_enqueue(&g_mpmc_ow, &val, 1); + } + + n = rt_mpmc_skip(&g_mpmc_ow, 8); + assert_int_equal(n, 4); + assert_int_equal(rt_count(&g_mpmc_ow), 0); +} + +static void test_mpmc_dequeue_skip_empty(FAR void **state) +{ + uint32_t n; + + ring_test_init(&g_mpmc_ow); + + n = rt_mpmc_skip(&g_mpmc_ow, 5); + assert_int_equal(n, 0); +} + +/**************************************************************************** + * Private Functions - Single-core multi-thread concurrent test + * + * MPMC: N producer threads + N consumer threads. + * Uses pthread + sched_yield() for single-core safety. + ****************************************************************************/ + +struct mpmc_prod_ctx_s +{ + FAR struct ring_test_s *ring; + int items; + atomic_t *start; +}; + +struct mpmc_cons_ctx_s +{ + FAR struct ring_test_s *ring; + FAR atomic_t *consumed; + int total; + atomic_t *start; +}; + +static FAR void *mpmc_producer_thread(FAR void *arg) +{ + FAR struct mpmc_prod_ctx_s *ctx = (FAR struct mpmc_prod_ctx_s *)arg; + uint32_t val; + int sent = 0; + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + while (sent < ctx->items) + { + val = (uint32_t)sent; + if (rt_mpmc_enqueue(ctx->ring, &val, 1) == 1) + { + sent++; + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static FAR void *mpmc_consumer_thread(FAR void *arg) +{ + FAR struct mpmc_cons_ctx_s *ctx = (FAR struct mpmc_cons_ctx_s *)arg; + uint32_t out = 0; + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + while (atomic_read(ctx->consumed) < ctx->total) + { + if (rt_mpmc_dequeue(ctx->ring, &out, 1) == 1) + { + atomic_add(ctx->consumed, 1); + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static void test_mpmc_concurrent(FAR void **state) +{ + struct mpmc_prod_ctx_s pctx[RING_TEST_THREADS]; + struct mpmc_cons_ctx_s cctx[RING_TEST_THREADS]; + pthread_t prod_threads[RING_TEST_THREADS]; + pthread_t cons_threads[RING_TEST_THREADS]; + atomic_t consumed = 0; + atomic_t start = 0; + int total; + int i; + + ring_test_init(&g_mpmc_conc); + + total = RING_TEST_ITEMS * RING_TEST_THREADS; + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pctx[i].ring = &g_mpmc_conc; + pctx[i].items = RING_TEST_ITEMS; + pctx[i].start = &start; + pthread_create(&prod_threads[i], NULL, + mpmc_producer_thread, &pctx[i]); + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + cctx[i].ring = &g_mpmc_conc; + cctx[i].consumed = &consumed; + cctx[i].total = total; + cctx[i].start = &start; + pthread_create(&cons_threads[i], NULL, + mpmc_consumer_thread, &cctx[i]); + } + + /* Signal all threads to start */ + + atomic_set(&start, 1); + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(prod_threads[i], NULL); + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(cons_threads[i], NULL); + } + + assert_int_equal(atomic_read(&consumed), total); + assert_int_equal(rt_count(&g_mpmc_conc), 0); +} + +/**************************************************************************** + * Private Functions - Overwrite concurrent test + * + * MPMC overwrite: N overwrite-producer threads + N consumer threads. + * Producers continuously overwrite; consumers dequeue. + * Invariant: consumed <= produced, ring empty at end. + ****************************************************************************/ + +struct mpmc_ow_prod_ctx_s +{ + FAR struct ring_test_s *ring; + int items; + FAR atomic_t *start; + FAR atomic_t *produced; +}; + +struct mpmc_ow_cons_ctx_s +{ + FAR struct ring_test_s *ring; + FAR atomic_t *start; + FAR atomic_t *consumed; + FAR atomic_t *prod_done; +}; + +static FAR void *mpmc_ow_producer_thread(FAR void *arg) +{ + FAR struct mpmc_ow_prod_ctx_s *ctx = + (FAR struct mpmc_ow_prod_ctx_s *)arg; + uint32_t val; + int sent = 0; + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + while (sent < ctx->items) + { + val = (uint32_t)sent; + rt_mpmc_overwrite(ctx->ring, &val, 1); + atomic_add(ctx->produced, 1); + sent++; + } + + return NULL; +} + +static FAR void *mpmc_ow_consumer_thread(FAR void *arg) +{ + FAR struct mpmc_ow_cons_ctx_s *ctx = + (FAR struct mpmc_ow_cons_ctx_s *)arg; + uint32_t out = 0; + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + for (; ; ) + { + if (rt_mpmc_dequeue(ctx->ring, &out, 1) == 1) + { + atomic_add(ctx->consumed, 1); + } + else if (atomic_read(ctx->prod_done) == + RING_TEST_THREADS) + { + if (rt_mpmc_dequeue(ctx->ring, &out, 1) == 0) + { + break; + } + + atomic_add(ctx->consumed, 1); + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static void test_mpmc_overwrite_concurrent(FAR void **state) +{ + struct mpmc_ow_prod_ctx_s pctx[RING_TEST_THREADS]; + struct mpmc_ow_cons_ctx_s cctx[RING_TEST_THREADS]; + pthread_t prod_threads[RING_TEST_THREADS]; + pthread_t cons_threads[RING_TEST_THREADS]; + atomic_t produced = 0; + atomic_t consumed = 0; + atomic_t prod_done = 0; + atomic_t start = 0; + int i; + + ring_test_init(&g_mpmc_ow_conc); + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pctx[i].ring = &g_mpmc_ow_conc; + pctx[i].items = RING_TEST_ITEMS; + pctx[i].start = &start; + pctx[i].produced = &produced; + pthread_create(&prod_threads[i], NULL, + mpmc_ow_producer_thread, &pctx[i]); + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + cctx[i].ring = &g_mpmc_ow_conc; + cctx[i].start = &start; + cctx[i].consumed = &consumed; + cctx[i].prod_done = &prod_done; + pthread_create(&cons_threads[i], NULL, + mpmc_ow_consumer_thread, &cctx[i]); + } + + atomic_set(&start, 1); + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(prod_threads[i], NULL); + atomic_add(&prod_done, 1); + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(cons_threads[i], NULL); + } + + assert_true(atomic_read(&consumed) <= atomic_read(&produced)); + assert_int_equal(rt_count(&g_mpmc_ow_conc), 0); +} + +/**************************************************************************** + * Private Functions - Skip concurrent test + * + * MPMC skip: N producer threads + N skip-consumer threads. + * Producers enqueue normally; consumers use skip. + * Invariant: total_skipped == total_produced, ring empty at end. + ****************************************************************************/ + +struct mpmc_skip_cons_ctx_s +{ + FAR struct ring_test_s *ring; + FAR atomic_t *start; + FAR atomic_t *skipped; + FAR atomic_t *prod_done; +}; + +static FAR void *mpmc_skip_consumer_thread(FAR void *arg) +{ + FAR struct mpmc_skip_cons_ctx_s *ctx = + (FAR struct mpmc_skip_cons_ctx_s *)arg; + uint32_t n; + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + for (; ; ) + { + n = rt_mpmc_skip(ctx->ring, 1); + if (n > 0) + { + atomic_add(ctx->skipped, n); + } + else if (atomic_read(ctx->prod_done) == + RING_TEST_THREADS) + { + n = rt_mpmc_skip(ctx->ring, 1); + if (n == 0) + { + break; + } + + atomic_add(ctx->skipped, n); + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static void test_mpmc_skip_concurrent(FAR void **state) +{ + struct mpmc_prod_ctx_s pctx[RING_TEST_THREADS]; + struct mpmc_skip_cons_ctx_s cctx[RING_TEST_THREADS]; + pthread_t prod_threads[RING_TEST_THREADS]; + pthread_t cons_threads[RING_TEST_THREADS]; + atomic_t skipped = 0; + atomic_t prod_done = 0; + atomic_t start = 0; + int total; + int i; + + ring_test_init(&g_mpmc_skip_conc); + + total = RING_TEST_ITEMS * RING_TEST_THREADS; + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pctx[i].ring = &g_mpmc_skip_conc; + pctx[i].items = RING_TEST_ITEMS; + pctx[i].start = &start; + pthread_create(&prod_threads[i], NULL, + mpmc_producer_thread, &pctx[i]); + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + cctx[i].ring = &g_mpmc_skip_conc; + cctx[i].start = &start; + cctx[i].skipped = &skipped; + cctx[i].prod_done = &prod_done; + pthread_create(&cons_threads[i], NULL, + mpmc_skip_consumer_thread, &cctx[i]); + } + + atomic_set(&start, 1); + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(prod_threads[i], NULL); + atomic_add(&prod_done, 1); + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(cons_threads[i], NULL); + } + + assert_int_equal(atomic_read(&skipped), total); + assert_int_equal(rt_count(&g_mpmc_skip_conc), 0); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +static const struct CMUnitTest g_mpmc_local_tests[] = +{ + cmocka_unit_test(test_mpmc_basic), + cmocka_unit_test(test_mpmc_batch), + cmocka_unit_test(test_mpmc_full), + cmocka_unit_test(test_mpmc_typed_api), + cmocka_unit_test(test_mpmc_alloc_free), + cmocka_unit_test(test_mpmc_enqueue_dequeue_boundary), + cmocka_unit_test(test_mpmc_partial_batch), + cmocka_unit_test(test_mpmc_wraparound_batch), + cmocka_unit_test(test_mpmc_capacity_two), + cmocka_unit_test(test_mpmc_count_boundary), + cmocka_unit_test(test_mpmc_concurrent), + cmocka_unit_test(test_mpmc_overwrite_basic), + cmocka_unit_test(test_mpmc_overwrite_wrap), + cmocka_unit_test(test_mpmc_overwrite_batch), + cmocka_unit_test(test_mpmc_overwrite_exceed_cap), + cmocka_unit_test(test_mpmc_overwrite_not_full), + cmocka_unit_test(test_mpmc_dequeue_skip_basic), + cmocka_unit_test(test_mpmc_dequeue_skip_all), + cmocka_unit_test(test_mpmc_dequeue_skip_more_than_avail), + cmocka_unit_test(test_mpmc_dequeue_skip_empty), + cmocka_unit_test(test_mpmc_overwrite_concurrent), + cmocka_unit_test(test_mpmc_skip_concurrent), +}; + +int ring_test_mpmc_local(void) +{ + return cmocka_run_group_tests(g_mpmc_local_tests, NULL, NULL); +} diff --git a/testing/libc/ring/ring_mpsc.c b/testing/libc/ring/ring_mpsc.c new file mode 100644 index 00000000000..9c19e6b4c17 --- /dev/null +++ b/testing/libc/ring/ring_mpsc.c @@ -0,0 +1,511 @@ +/**************************************************************************** + * apps/testing/libc/ring/ring_mpsc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "ring_test.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Per-test static rings, kept off the test stack */ + +RING_TEST_DEFINE(g_mpsc_basic, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpsc_batch, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpsc_full, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpsc_typed, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpsc_boundary, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_mpsc_partial, uint32_t, 8); +RING_TEST_DEFINE(g_mpsc_wrap, uint32_t, 8); +RING_TEST_DEFINE(g_mpsc_cap2, uint32_t, 2); +RING_TEST_DEFINE(g_mpsc_count, uint32_t, 8); +RING_TEST_DEFINE(g_mpsc_concurrent, uint32_t, RING_TEST_CAP); + +/**************************************************************************** + * Private Functions - Single-core functional test + ****************************************************************************/ + +static void test_mpsc_basic(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpsc_basic); + + for (i = 0; i < 16; i++) + { + val = i + 300; + n = rt_mpsc_enqueue(&g_mpsc_basic, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_mpsc_basic), 16); + + for (i = 0; i < 16; i++) + { + n = rt_mpsc_dequeue(&g_mpsc_basic, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 300); + } +} + +static void test_mpsc_batch(FAR void **state) +{ + uint32_t buf[RING_TEST_BATCH]; + uint32_t out[RING_TEST_BATCH]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpsc_batch); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + buf[i] = i + 200; + } + + n = rt_mpsc_enqueue(&g_mpsc_batch, buf, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + n = rt_mpsc_dequeue(&g_mpsc_batch, out, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + assert_int_equal(out[i], i + 200); + } +} + +static void test_mpsc_full(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpsc_full); + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_mpsc_enqueue(&g_mpsc_full, &val, 1); + assert_int_equal(n, 1); + } + + val = 999; + n = rt_mpsc_enqueue(&g_mpsc_full, &val, 1); + assert_int_equal(n, 0); + + for (i = 0; i < RING_TEST_CAP; i++) + { + n = rt_mpsc_dequeue(&g_mpsc_full, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i); + } + + assert_int_equal(rt_count(&g_mpsc_full), 0); +} + +static void test_mpsc_typed_api(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t batch[4]; + uint32_t obatch[4]; + uint32_t n; + uint32_t i; + + ring_test_init(&g_mpsc_typed); + + val = 42; + n = rt_mpsc_enqueue(&g_mpsc_typed, &val, 1); + assert_int_equal(n, 1); + n = rt_mpsc_dequeue(&g_mpsc_typed, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 42); + + for (i = 0; i < 4; i++) + { + batch[i] = i + 500; + } + + n = rt_mpsc_enqueue(&g_mpsc_typed, batch, 4); + assert_int_equal(n, 4); + n = rt_mpsc_dequeue(&g_mpsc_typed, obatch, 4); + assert_int_equal(n, 4); + + for (i = 0; i < 4; i++) + { + assert_int_equal(obatch[i], i + 500); + } +} + +static void test_mpsc_alloc_free(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + assert_non_null(ring_test_alloc(r, RING_TEST_CAP, sizeof(uint32_t))); + + val = 77; + n = rt_mpsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_mpsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 77); + + ring_free(r->r); +} + +/**************************************************************************** + * Private Functions - Boundary condition tests (MP enqueue, SC dequeue) + ****************************************************************************/ + +static void test_mpsc_enqueue_dequeue_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpsc_boundary); + + /* Dequeue from empty ring */ + + n = rt_mpsc_dequeue(&g_mpsc_boundary, &out, 1); + assert_int_equal(n, 0); + + /* Enqueue count=0 */ + + val = 42; + n = rt_mpsc_enqueue(&g_mpsc_boundary, &val, 0); + assert_int_equal(n, 0); + assert_int_equal(rt_count(&g_mpsc_boundary), 0); + + /* Dequeue count=0 */ + + n = rt_mpsc_dequeue(&g_mpsc_boundary, &out, 0); + assert_int_equal(n, 0); + + /* Fill ring, then try one more (MP CAS path) */ + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_mpsc_enqueue(&g_mpsc_boundary, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_mpsc_boundary), RING_TEST_CAP); + + val = 999; + n = rt_mpsc_enqueue(&g_mpsc_boundary, &val, 1); + assert_int_equal(n, 0); +} + +static void test_mpsc_partial_batch(FAR void **state) +{ + uint32_t buf[16]; + uint32_t out[16]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpsc_partial); + + /* Batch enqueue larger than capacity */ + + for (i = 0; i < 16; i++) + { + buf[i] = i + 1; + } + + n = rt_mpsc_enqueue(&g_mpsc_partial, buf, 16); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_mpsc_partial), 8); + + /* Batch dequeue larger than available */ + + n = rt_mpsc_dequeue(&g_mpsc_partial, out, 16); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1); + } + + /* Partial enqueue when partially full */ + + for (i = 0; i < 6; i++) + { + buf[i] = i + 100; + } + + n = rt_mpsc_enqueue(&g_mpsc_partial, buf, 6); + assert_int_equal(n, 6); + + for (i = 0; i < 4; i++) + { + buf[i] = i + 200; + } + + n = rt_mpsc_enqueue(&g_mpsc_partial, buf, 4); + assert_int_equal(n, 2); + assert_int_equal(rt_count(&g_mpsc_partial), 8); + + n = rt_mpsc_dequeue(&g_mpsc_partial, out, 8); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_mpsc_partial), 0); +} + +static void test_mpsc_wraparound_batch(FAR void **state) +{ + uint32_t buf[8]; + uint32_t out[8]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_mpsc_wrap); + + /* Advance indices to force wraparound */ + + for (i = 0; i < 6; i++) + { + buf[i] = i; + } + + n = rt_mpsc_enqueue(&g_mpsc_wrap, buf, 6); + assert_int_equal(n, 6); + n = rt_mpsc_dequeue(&g_mpsc_wrap, out, 6); + assert_int_equal(n, 6); + + /* Enqueue 8 items wrapping around (MP CAS path) */ + + for (i = 0; i < 8; i++) + { + buf[i] = i + 1000; + } + + n = rt_mpsc_enqueue(&g_mpsc_wrap, buf, 8); + assert_int_equal(n, 8); + + memset(out, 0, sizeof(out)); + n = rt_mpsc_dequeue(&g_mpsc_wrap, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1000); + } +} + +static void test_mpsc_capacity_two(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t n; + + ring_test_init(&g_mpsc_cap2); + + assert_int_equal(rt_capacity(&g_mpsc_cap2), 2); + + val = 10; + n = rt_mpsc_enqueue(&g_mpsc_cap2, &val, 1); + assert_int_equal(n, 1); + + val = 20; + n = rt_mpsc_enqueue(&g_mpsc_cap2, &val, 1); + assert_int_equal(n, 1); + + /* Full */ + + val = 30; + n = rt_mpsc_enqueue(&g_mpsc_cap2, &val, 1); + assert_int_equal(n, 0); + + n = rt_mpsc_dequeue(&g_mpsc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 10); + + n = rt_mpsc_dequeue(&g_mpsc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 20); + + /* Empty */ + + n = rt_mpsc_dequeue(&g_mpsc_cap2, &out, 1); + assert_int_equal(n, 0); +} + +static void test_mpsc_count_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out[8]; + uint32_t i; + + ring_test_init(&g_mpsc_count); + + assert_int_equal(rt_count(&g_mpsc_count), 0); + + for (i = 0; i < 3; i++) + { + val = i; + rt_mpsc_enqueue(&g_mpsc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_mpsc_count), 3); + + for (i = 3; i < 8; i++) + { + val = i; + rt_mpsc_enqueue(&g_mpsc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_mpsc_count), 8); + + rt_mpsc_dequeue(&g_mpsc_count, out, 1); + assert_int_equal(rt_count(&g_mpsc_count), 7); + + rt_mpsc_dequeue(&g_mpsc_count, out, 7); + assert_int_equal(rt_count(&g_mpsc_count), 0); +} + +/**************************************************************************** + * Private Functions - Single-core multi-thread concurrent test + * + * MPSC: multiple producer threads + main thread as single consumer. + * Uses pthread + sched_yield() for single-core safety. + ****************************************************************************/ + +struct mpsc_thread_ctx_s +{ + FAR struct ring_test_s *ring; + int items; + atomic_t *start; +}; + +static FAR void *mpsc_producer_thread(FAR void *arg) +{ + FAR struct mpsc_thread_ctx_s *ctx = (FAR struct mpsc_thread_ctx_s *)arg; + uint32_t val; + int sent = 0; + + /* Wait for start signal */ + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + while (sent < ctx->items) + { + val = (uint32_t)sent; + if (rt_mpsc_enqueue(ctx->ring, &val, 1) == 1) + { + sent++; + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static void test_mpsc_concurrent(FAR void **state) +{ + struct mpsc_thread_ctx_s ctx[RING_TEST_THREADS]; + pthread_t threads[RING_TEST_THREADS]; + atomic_t start = 0; + uint32_t out = 0; + int total; + int received = 0; + int i; + + ring_test_init(&g_mpsc_concurrent); + + total = RING_TEST_ITEMS * RING_TEST_THREADS; + + for (i = 0; i < RING_TEST_THREADS; i++) + { + ctx[i].ring = &g_mpsc_concurrent; + ctx[i].items = RING_TEST_ITEMS; + ctx[i].start = &start; + pthread_create(&threads[i], NULL, mpsc_producer_thread, &ctx[i]); + } + + /* Signal all producers to start */ + + atomic_set(&start, 1); + + /* Main thread: single consumer */ + + while (received < total) + { + if (rt_mpsc_dequeue(&g_mpsc_concurrent, &out, 1) == 1) + { + received++; + } + else + { + sched_yield(); + } + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(threads[i], NULL); + } + + assert_int_equal(received, total); + assert_int_equal(rt_count(&g_mpsc_concurrent), 0); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +static const struct CMUnitTest g_mpsc_local_tests[] = +{ + cmocka_unit_test(test_mpsc_basic), + cmocka_unit_test(test_mpsc_batch), + cmocka_unit_test(test_mpsc_full), + cmocka_unit_test(test_mpsc_typed_api), + cmocka_unit_test(test_mpsc_alloc_free), + cmocka_unit_test(test_mpsc_enqueue_dequeue_boundary), + cmocka_unit_test(test_mpsc_partial_batch), + cmocka_unit_test(test_mpsc_wraparound_batch), + cmocka_unit_test(test_mpsc_capacity_two), + cmocka_unit_test(test_mpsc_count_boundary), + cmocka_unit_test(test_mpsc_concurrent), +}; + +int ring_test_mpsc_local(void) +{ + return cmocka_run_group_tests(g_mpsc_local_tests, NULL, NULL); +} diff --git a/testing/libc/ring/ring_spmc.c b/testing/libc/ring/ring_spmc.c new file mode 100644 index 00000000000..93d34e7d0f1 --- /dev/null +++ b/testing/libc/ring/ring_spmc.c @@ -0,0 +1,520 @@ +/**************************************************************************** + * apps/testing/libc/ring/ring_spmc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "ring_test.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +RING_TEST_DEFINE(g_spmc_basic, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spmc_batch, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spmc_full, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spmc_typed, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spmc_boundary, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spmc_partial, uint32_t, 8); +RING_TEST_DEFINE(g_spmc_wrap, uint32_t, 8); +RING_TEST_DEFINE(g_spmc_cap2, uint32_t, 2); +RING_TEST_DEFINE(g_spmc_count, uint32_t, 8); +RING_TEST_DEFINE(g_spmc_concurrent, uint32_t, RING_TEST_CAP); + +/**************************************************************************** + * Private Functions - Single-core functional test + ****************************************************************************/ + +static void test_spmc_basic(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spmc_basic); + + for (i = 0; i < 16; i++) + { + val = i + 400; + n = rt_spmc_enqueue(&g_spmc_basic, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_spmc_basic), 16); + + for (i = 0; i < 16; i++) + { + n = rt_spmc_dequeue(&g_spmc_basic, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 400); + } +} + +static void test_spmc_batch(FAR void **state) +{ + uint32_t buf[RING_TEST_BATCH]; + uint32_t out[RING_TEST_BATCH]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spmc_batch); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + buf[i] = i + 200; + } + + n = rt_spmc_enqueue(&g_spmc_batch, buf, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + n = rt_spmc_dequeue(&g_spmc_batch, out, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + assert_int_equal(out[i], i + 200); + } +} + +static void test_spmc_full(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spmc_full); + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_spmc_enqueue(&g_spmc_full, &val, 1); + assert_int_equal(n, 1); + } + + val = 999; + n = rt_spmc_enqueue(&g_spmc_full, &val, 1); + assert_int_equal(n, 0); + + for (i = 0; i < RING_TEST_CAP; i++) + { + n = rt_spmc_dequeue(&g_spmc_full, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i); + } + + assert_int_equal(rt_count(&g_spmc_full), 0); +} + +static void test_spmc_typed_api(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t batch[4]; + uint32_t obatch[4]; + uint32_t n; + uint32_t i; + + ring_test_init(&g_spmc_typed); + + val = 42; + n = rt_spmc_enqueue(&g_spmc_typed, &val, 1); + assert_int_equal(n, 1); + n = rt_spmc_dequeue(&g_spmc_typed, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 42); + + for (i = 0; i < 4; i++) + { + batch[i] = i + 500; + } + + n = rt_spmc_enqueue(&g_spmc_typed, batch, 4); + assert_int_equal(n, 4); + n = rt_spmc_dequeue(&g_spmc_typed, obatch, 4); + assert_int_equal(n, 4); + + for (i = 0; i < 4; i++) + { + assert_int_equal(obatch[i], i + 500); + } +} + +static void test_spmc_alloc_free(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + assert_non_null(ring_test_alloc(r, RING_TEST_CAP, sizeof(uint32_t))); + + val = 77; + n = rt_spmc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spmc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 77); + + ring_free(r->r); +} + +/**************************************************************************** + * Private Functions - Boundary condition tests (SP enqueue, MC dequeue) + ****************************************************************************/ + +static void test_spmc_enqueue_dequeue_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spmc_boundary); + + /* Dequeue from empty ring (MC CAS path) */ + + n = rt_spmc_dequeue(&g_spmc_boundary, &out, 1); + assert_int_equal(n, 0); + + /* Enqueue count=0 */ + + val = 42; + n = rt_spmc_enqueue(&g_spmc_boundary, &val, 0); + assert_int_equal(n, 0); + assert_int_equal(rt_count(&g_spmc_boundary), 0); + + /* Dequeue count=0 */ + + n = rt_spmc_dequeue(&g_spmc_boundary, &out, 0); + assert_int_equal(n, 0); + + /* Fill ring, then try one more */ + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_spmc_enqueue(&g_spmc_boundary, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_spmc_boundary), RING_TEST_CAP); + + val = 999; + n = rt_spmc_enqueue(&g_spmc_boundary, &val, 1); + assert_int_equal(n, 0); +} + +static void test_spmc_partial_batch(FAR void **state) +{ + uint32_t buf[16]; + uint32_t out[16]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spmc_partial); + + /* Batch enqueue larger than capacity */ + + for (i = 0; i < 16; i++) + { + buf[i] = i + 1; + } + + n = rt_spmc_enqueue(&g_spmc_partial, buf, 16); + assert_int_equal(n, 8); + + /* Batch dequeue larger than available (MC CAS path) */ + + n = rt_spmc_dequeue(&g_spmc_partial, out, 16); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1); + } + + /* Partial enqueue when partially full */ + + for (i = 0; i < 6; i++) + { + buf[i] = i + 100; + } + + n = rt_spmc_enqueue(&g_spmc_partial, buf, 6); + assert_int_equal(n, 6); + + for (i = 0; i < 4; i++) + { + buf[i] = i + 200; + } + + n = rt_spmc_enqueue(&g_spmc_partial, buf, 4); + assert_int_equal(n, 2); + assert_int_equal(rt_count(&g_spmc_partial), 8); + + /* Partial dequeue (MC CAS path) */ + + n = rt_spmc_dequeue(&g_spmc_partial, out, 5); + assert_int_equal(n, 5); + assert_int_equal(rt_count(&g_spmc_partial), 3); + + n = rt_spmc_dequeue(&g_spmc_partial, out, 3); + assert_int_equal(n, 3); + assert_int_equal(rt_count(&g_spmc_partial), 0); +} + +static void test_spmc_wraparound_batch(FAR void **state) +{ + uint32_t buf[8]; + uint32_t out[8]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spmc_wrap); + + /* Advance indices to force wraparound */ + + for (i = 0; i < 6; i++) + { + buf[i] = i; + } + + n = rt_spmc_enqueue(&g_spmc_wrap, buf, 6); + assert_int_equal(n, 6); + n = rt_spmc_dequeue(&g_spmc_wrap, out, 6); + assert_int_equal(n, 6); + + /* Enqueue 8 items wrapping around */ + + for (i = 0; i < 8; i++) + { + buf[i] = i + 1000; + } + + n = rt_spmc_enqueue(&g_spmc_wrap, buf, 8); + assert_int_equal(n, 8); + + /* Dequeue wrapping around (MC CAS path) */ + + memset(out, 0, sizeof(out)); + n = rt_spmc_dequeue(&g_spmc_wrap, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1000); + } +} + +static void test_spmc_capacity_two(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t n; + + ring_test_init(&g_spmc_cap2); + + assert_int_equal(rt_capacity(&g_spmc_cap2), 2); + + val = 10; + n = rt_spmc_enqueue(&g_spmc_cap2, &val, 1); + assert_int_equal(n, 1); + + val = 20; + n = rt_spmc_enqueue(&g_spmc_cap2, &val, 1); + assert_int_equal(n, 1); + + /* Full */ + + val = 30; + n = rt_spmc_enqueue(&g_spmc_cap2, &val, 1); + assert_int_equal(n, 0); + + /* MC dequeue */ + + n = rt_spmc_dequeue(&g_spmc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 10); + + n = rt_spmc_dequeue(&g_spmc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 20); + + /* Empty */ + + n = rt_spmc_dequeue(&g_spmc_cap2, &out, 1); + assert_int_equal(n, 0); +} + +static void test_spmc_count_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out[8]; + uint32_t i; + + ring_test_init(&g_spmc_count); + + assert_int_equal(rt_count(&g_spmc_count), 0); + + for (i = 0; i < 3; i++) + { + val = i; + rt_spmc_enqueue(&g_spmc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_spmc_count), 3); + + for (i = 3; i < 8; i++) + { + val = i; + rt_spmc_enqueue(&g_spmc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_spmc_count), 8); + + rt_spmc_dequeue(&g_spmc_count, out, 1); + assert_int_equal(rt_count(&g_spmc_count), 7); + + rt_spmc_dequeue(&g_spmc_count, out, 7); + assert_int_equal(rt_count(&g_spmc_count), 0); +} + +/**************************************************************************** + * Private Functions - Single-core multi-thread concurrent test + * + * SPMC: main thread as single producer + multiple consumer threads. + * Uses pthread + sched_yield() for single-core safety. + ****************************************************************************/ + +struct spmc_thread_ctx_s +{ + FAR struct ring_test_s *ring; + FAR atomic_t *consumed; + int total; + atomic_t *start; +}; + +static FAR void *spmc_consumer_thread(FAR void *arg) +{ + FAR struct spmc_thread_ctx_s *ctx = (FAR struct spmc_thread_ctx_s *)arg; + uint32_t out = 0; + + /* Wait for start signal */ + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + while (atomic_read(ctx->consumed) < ctx->total) + { + if (rt_spmc_dequeue(ctx->ring, &out, 1) == 1) + { + atomic_add(ctx->consumed, 1); + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static void test_spmc_concurrent(FAR void **state) +{ + struct spmc_thread_ctx_s ctx[RING_TEST_THREADS]; + pthread_t threads[RING_TEST_THREADS]; + atomic_t consumed = 0; + atomic_t start = 0; + uint32_t val; + int total; + int sent = 0; + int i; + + ring_test_init(&g_spmc_concurrent); + + total = RING_TEST_ITEMS; + + for (i = 0; i < RING_TEST_THREADS; i++) + { + ctx[i].ring = &g_spmc_concurrent; + ctx[i].consumed = &consumed; + ctx[i].total = total; + ctx[i].start = &start; + pthread_create(&threads[i], NULL, spmc_consumer_thread, &ctx[i]); + } + + /* Signal all consumers to start */ + + atomic_set(&start, 1); + + /* Main thread: single producer */ + + while (sent < total) + { + val = (uint32_t)sent; + if (rt_spmc_enqueue(&g_spmc_concurrent, &val, 1) == 1) + { + sent++; + } + else + { + sched_yield(); + } + } + + for (i = 0; i < RING_TEST_THREADS; i++) + { + pthread_join(threads[i], NULL); + } + + assert_int_equal(atomic_read(&consumed), total); + assert_int_equal(rt_count(&g_spmc_concurrent), 0); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +static const struct CMUnitTest g_spmc_local_tests[] = +{ + cmocka_unit_test(test_spmc_basic), + cmocka_unit_test(test_spmc_batch), + cmocka_unit_test(test_spmc_full), + cmocka_unit_test(test_spmc_typed_api), + cmocka_unit_test(test_spmc_alloc_free), + cmocka_unit_test(test_spmc_enqueue_dequeue_boundary), + cmocka_unit_test(test_spmc_partial_batch), + cmocka_unit_test(test_spmc_wraparound_batch), + cmocka_unit_test(test_spmc_capacity_two), + cmocka_unit_test(test_spmc_count_boundary), + cmocka_unit_test(test_spmc_concurrent), +}; + +int ring_test_spmc_local(void) +{ + return cmocka_run_group_tests(g_spmc_local_tests, NULL, NULL); +} diff --git a/testing/libc/ring/ring_spsc.c b/testing/libc/ring/ring_spsc.c new file mode 100644 index 00000000000..c1a9e5484dc --- /dev/null +++ b/testing/libc/ring/ring_spsc.c @@ -0,0 +1,1462 @@ +/**************************************************************************** + * apps/testing/libc/ring/ring_spsc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "ring_test.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct large_elem +{ + uint32_t a; + uint32_t b; + uint64_t c; + char name[16]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Per-test rings (moved from stack to avoid overflow on embedded) */ + +RING_TEST_DEFINE(g_spsc_basic, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_batch, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_full, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_typed, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_boundary, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_partial, uint32_t, 8); +RING_TEST_DEFINE(g_spsc_wrap, uint32_t, 8); +RING_TEST_DEFINE(g_spsc_cap2, uint32_t, 2); +RING_TEST_DEFINE(g_spsc_large, struct large_elem, 8); +RING_TEST_DEFINE(g_spsc_count, uint32_t, 8); +RING_TEST_DEFINE(g_spsc_concurrent, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_init, uint32_t, RING_TEST_CAP); +RING_TEST_DEFINE(g_spsc_ow, uint32_t, 8); +RING_TEST_DEFINE(g_spsc_ow_wrap, uint32_t, 8); +RING_TEST_DEFINE(g_spsc_ow_batch, uint32_t, 8); +RING_TEST_DEFINE(g_spsc_ow_cap, uint32_t, 8); + +/* Per-test large element arrays */ + +static struct large_elem g_large_batch_in[8]; +static struct large_elem g_large_batch_out[8]; + +/**************************************************************************** + * Private Functions - Single-core functional tests + ****************************************************************************/ + +static void test_spsc_basic(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_basic); + + assert_int_equal(rt_count(&g_spsc_basic), 0); + assert_int_equal(rt_capacity(&g_spsc_basic), RING_TEST_CAP); + n = rt_spsc_dequeue(&g_spsc_basic, &out, 1); + assert_int_equal(n, 0); + + for (i = 0; i < 32; i++) + { + val = i + 100; + n = rt_spsc_enqueue(&g_spsc_basic, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_spsc_basic), 32); + + for (i = 0; i < 32; i++) + { + n = rt_spsc_dequeue(&g_spsc_basic, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 100); + } + + assert_int_equal(rt_count(&g_spsc_basic), 0); +} + +static void test_spsc_batch(FAR void **state) +{ + uint32_t buf[RING_TEST_BATCH]; + uint32_t out[RING_TEST_BATCH]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_batch); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + buf[i] = i + 200; + } + + n = rt_spsc_enqueue(&g_spsc_batch, buf, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + n = rt_spsc_dequeue(&g_spsc_batch, out, RING_TEST_BATCH); + assert_int_equal(n, RING_TEST_BATCH); + + for (i = 0; i < RING_TEST_BATCH; i++) + { + assert_int_equal(out[i], i + 200); + } +} + +static void test_spsc_full(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_full); + + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_spsc_enqueue(&g_spsc_full, &val, 1); + assert_int_equal(n, 1); + } + + val = 999; + n = rt_spsc_enqueue(&g_spsc_full, &val, 1); + assert_int_equal(n, 0); + + for (i = 0; i < RING_TEST_CAP / 2; i++) + { + n = rt_spsc_dequeue(&g_spsc_full, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i); + } + + for (i = 0; i < RING_TEST_CAP / 2; i++) + { + val = i + 1000; + n = rt_spsc_enqueue(&g_spsc_full, &val, 1); + assert_int_equal(n, 1); + } + + for (i = 0; i < RING_TEST_CAP / 2; i++) + { + n = rt_spsc_dequeue(&g_spsc_full, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + RING_TEST_CAP / 2); + } + + for (i = 0; i < RING_TEST_CAP / 2; i++) + { + n = rt_spsc_dequeue(&g_spsc_full, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 1000); + } + + assert_int_equal(rt_count(&g_spsc_full), 0); +} + +static void test_spsc_typed_api(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t batch[4]; + uint32_t obatch[4]; + uint32_t n; + uint32_t i; + + ring_test_init(&g_spsc_typed); + + val = 42; + n = rt_spsc_enqueue(&g_spsc_typed, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(&g_spsc_typed, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 42); + + for (i = 0; i < 4; i++) + { + batch[i] = i + 500; + } + + n = rt_spsc_enqueue(&g_spsc_typed, batch, 4); + assert_int_equal(n, 4); + n = rt_spsc_dequeue(&g_spsc_typed, obatch, 4); + assert_int_equal(n, 4); + + for (i = 0; i < 4; i++) + { + assert_int_equal(obatch[i], i + 500); + } +} + +static void test_spsc_alloc_free(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + assert_non_null(ring_test_alloc(r, RING_TEST_CAP, sizeof(uint32_t))); + + val = 77; + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 77); + + ring_free(r->r); +} + +/**************************************************************************** + * Private Functions - ring_alloc tests + ****************************************************************************/ + +static void test_ring_alloc_valid(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + assert_non_null(ring_test_alloc(r, 16, sizeof(uint32_t))); + assert_int_equal(rt_capacity(r), 16); + assert_int_equal(rt_count(r), 0); + + val = 123; + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 123); + + ring_free(r->r); +} + +static void test_ring_alloc_invalid(FAR void **state) +{ + assert_null(ring_alloc(0)); +} + +static void test_ring_alloc_non_pow2(FAR void **state) +{ + static const uint32_t caps[] = + { + 3, 5, 6, 7, 9, 10 + }; + + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out; + uint32_t i; + uint32_t j; + + for (i = 0; i < nitems(caps); i++) + { + assert_non_null(ring_test_alloc(r, caps[i], sizeof(uint32_t))); + assert_int_equal(rt_capacity(r), caps[i]); + + /* Two full passes, so the indices wrap at least once */ + + for (j = 0; j < 2 * caps[i]; j++) + { + val = j; + assert_int_equal(rt_spsc_enqueue(r, &val, 1), 1); + assert_int_equal(rt_spsc_dequeue(r, &out, 1), 1); + assert_int_equal(out, j); + } + + ring_free(r->r); + } +} + +/**************************************************************************** + * Private Functions - Enqueue/dequeue boundary tests + ****************************************************************************/ + +static void test_enqueue_dequeue_boundary(FAR void **state) +{ + uint32_t out = 0; + uint32_t n; + + ring_test_init(&g_spsc_boundary); + + /* Dequeue from empty ring */ + + n = rt_spsc_dequeue(&g_spsc_boundary, &out, 1); + assert_int_equal(n, 0); + + /* Enqueue count=0 */ + + uint32_t val = 42; + n = rt_spsc_enqueue(&g_spsc_boundary, &val, 0); + assert_int_equal(n, 0); + assert_int_equal(rt_count(&g_spsc_boundary), 0); + + /* Dequeue count=0 */ + + n = rt_spsc_dequeue(&g_spsc_boundary, &out, 0); + assert_int_equal(n, 0); + + /* Enqueue to full ring, then try one more */ + + uint32_t i; + for (i = 0; i < RING_TEST_CAP; i++) + { + val = i; + n = rt_spsc_enqueue(&g_spsc_boundary, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(&g_spsc_boundary), RING_TEST_CAP); + + val = 999; + n = rt_spsc_enqueue(&g_spsc_boundary, &val, 1); + assert_int_equal(n, 0); +} + +static void test_partial_batch(FAR void **state) +{ + uint32_t buf[16]; + uint32_t out[16]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_partial); + + /* Batch enqueue larger than capacity: should partial enqueue */ + + for (i = 0; i < 16; i++) + { + buf[i] = i + 1; + } + + n = rt_spsc_enqueue(&g_spsc_partial, buf, 16); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_spsc_partial), 8); + + /* Batch dequeue larger than available: should partial dequeue */ + + n = rt_spsc_dequeue(&g_spsc_partial, out, 16); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1); + } + + assert_int_equal(rt_count(&g_spsc_partial), 0); + + /* Partial enqueue when partially full */ + + for (i = 0; i < 6; i++) + { + buf[i] = i + 100; + } + + n = rt_spsc_enqueue(&g_spsc_partial, buf, 6); + assert_int_equal(n, 6); + + /* Try to enqueue 4 more, only 2 should fit */ + + for (i = 0; i < 4; i++) + { + buf[i] = i + 200; + } + + n = rt_spsc_enqueue(&g_spsc_partial, buf, 4); + assert_int_equal(n, 2); + assert_int_equal(rt_count(&g_spsc_partial), 8); + + /* Partial dequeue: request 5, get 5 */ + + n = rt_spsc_dequeue(&g_spsc_partial, out, 5); + assert_int_equal(n, 5); + assert_int_equal(rt_count(&g_spsc_partial), 3); +} + +static void test_wraparound_batch(FAR void **state) +{ + uint32_t buf[8]; + uint32_t out[8]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_wrap); + + /* Fill ring to advance head/tail indices */ + + for (i = 0; i < 6; i++) + { + buf[i] = i; + } + + n = rt_spsc_enqueue(&g_spsc_wrap, buf, 6); + assert_int_equal(n, 6); + n = rt_spsc_dequeue(&g_spsc_wrap, out, 6); + assert_int_equal(n, 6); + + /* Now head/tail are at index 6. Enqueue 8 items wraps around. */ + + for (i = 0; i < 8; i++) + { + buf[i] = i + 1000; + } + + n = rt_spsc_enqueue(&g_spsc_wrap, buf, 8); + assert_int_equal(n, 8); + + memset(out, 0, sizeof(out)); + n = rt_spsc_dequeue(&g_spsc_wrap, out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 1000); + } +} + +static void test_capacity_two(FAR void **state) +{ + /* Smallest ring exercised: 2 items */ + + uint32_t val; + uint32_t out = 0; + uint32_t n; + + ring_test_init(&g_spsc_cap2); + + assert_int_equal(rt_capacity(&g_spsc_cap2), 2); + assert_int_equal(rt_count(&g_spsc_cap2), 0); + + val = 10; + n = rt_spsc_enqueue(&g_spsc_cap2, &val, 1); + assert_int_equal(n, 1); + + val = 20; + n = rt_spsc_enqueue(&g_spsc_cap2, &val, 1); + assert_int_equal(n, 1); + + /* Full */ + + val = 30; + n = rt_spsc_enqueue(&g_spsc_cap2, &val, 1); + assert_int_equal(n, 0); + + n = rt_spsc_dequeue(&g_spsc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 10); + + n = rt_spsc_dequeue(&g_spsc_cap2, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 20); + + /* Empty */ + + n = rt_spsc_dequeue(&g_spsc_cap2, &out, 1); + assert_int_equal(n, 0); +} + +static void test_large_elem(FAR void **state) +{ + struct large_elem in; + struct large_elem out; + uint32_t n; + uint32_t i; + + ring_test_init(&g_spsc_large); + + /* Single element */ + + memset(&in, 0, sizeof(in)); + in.a = 1; + in.b = 2; + in.c = 0xdeadbeefcafe0000ull; + strlcpy(in.name, "hello", sizeof(in.name)); + + n = rt_spsc_enqueue(&g_spsc_large, &in, 1); + assert_int_equal(n, 1); + + memset(&out, 0, sizeof(out)); + n = rt_spsc_dequeue(&g_spsc_large, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out.a, 1); + assert_int_equal(out.b, 2); + assert_true(out.c == 0xdeadbeefcafe0000ull); + assert_string_equal(out.name, "hello"); + + /* Batch with wraparound */ + + /* Advance indices to force wraparound */ + + for (i = 0; i < 5; i++) + { + memset(&in, 0, sizeof(in)); + in.a = i; + rt_spsc_enqueue(&g_spsc_large, &in, 1); + } + + rt_spsc_dequeue(&g_spsc_large, g_large_batch_out, 5); + + /* Now enqueue 8 items (wraps around) */ + + for (i = 0; i < 8; i++) + { + memset(&g_large_batch_in[i], 0, sizeof(g_large_batch_in[i])); + g_large_batch_in[i].a = i + 100; + g_large_batch_in[i].b = i + 200; + g_large_batch_in[i].c = (uint64_t)i * 1000; + } + + n = rt_spsc_enqueue(&g_spsc_large, g_large_batch_in, 8); + assert_int_equal(n, 8); + + memset(g_large_batch_out, 0, sizeof(g_large_batch_out)); + n = rt_spsc_dequeue(&g_spsc_large, g_large_batch_out, 8); + assert_int_equal(n, 8); + + for (i = 0; i < 8; i++) + { + assert_int_equal(g_large_batch_out[i].a, i + 100); + assert_int_equal(g_large_batch_out[i].b, i + 200); + assert_true(g_large_batch_out[i].c == (uint64_t)i * 1000); + } +} + +static void test_ring_count_boundary(FAR void **state) +{ + uint32_t val; + uint32_t out[8]; + uint32_t i; + + ring_test_init(&g_spsc_count); + + /* Empty */ + + assert_int_equal(rt_count(&g_spsc_count), 0); + + /* Partially filled */ + + for (i = 0; i < 3; i++) + { + val = i; + rt_spsc_enqueue(&g_spsc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_spsc_count), 3); + + /* Full */ + + for (i = 3; i < 8; i++) + { + val = i; + rt_spsc_enqueue(&g_spsc_count, &val, 1); + } + + assert_int_equal(rt_count(&g_spsc_count), 8); + + /* Dequeue one: count should be 7 */ + + rt_spsc_dequeue(&g_spsc_count, out, 1); + assert_int_equal(rt_count(&g_spsc_count), 7); + + /* Drain all */ + + rt_spsc_dequeue(&g_spsc_count, out, 7); + assert_int_equal(rt_count(&g_spsc_count), 0); +} + +static void test_ring_alloc_all_modes(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + /* Test ring_alloc with MPSC mode */ + + assert_non_null(ring_test_alloc(r, 8, sizeof(uint32_t))); + + val = 1; + n = rt_mpsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 1); + ring_free(r->r); + + /* Test ring_alloc with SPMC mode */ + + assert_non_null(ring_test_alloc(r, 8, sizeof(uint32_t))); + + val = 2; + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_mpmc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 2); + ring_free(r->r); + + /* Test ring_alloc with MPMC mode */ + + assert_non_null(ring_test_alloc(r, 8, sizeof(uint32_t))); + + val = 3; + n = rt_mpsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_mpmc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 3); + ring_free(r->r); +} + +/**************************************************************************** + * Private Functions - init magic / warm-restart tests + ****************************************************************************/ + +/* A reboot leaves the blob as it was and clears the handle, which lives + * in ordinary RAM: forget the handle, then init again on the same blob. + */ + +static void ring_test_reboot(FAR struct ring_test_s *t) +{ + memset(t->r, 0, sizeof(*t->r)); +} + +static void test_init_fresh(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + FAR struct ring_blob_s *blob = (FAR struct ring_blob_s *)r->blob; + uint32_t val; + uint32_t out = 0; + uint32_t n; + + ring_test_init(r); + + assert_true(ring_is_ready(r->r)); + assert_int_equal(blob->magic, RING_BLOB_MAGIC_READY); + assert_int_equal(rt_capacity(r), RING_TEST_CAP); + assert_int_equal(rt_count(r), 0); + + val = 42; + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 42); +} + +static void test_init_warm_restart_recovery(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + uint32_t val; + uint32_t out = 0; + uint32_t n; + uint32_t i; + + ring_test_init(r); + for (i = 0; i < 16; i++) + { + val = i + 500; + rt_spsc_enqueue(r, &val, 1); + } + + for (i = 0; i < 8; i++) + { + rt_spsc_dequeue(r, &out, 1); + } + + assert_int_equal(rt_count(r), 8); + + ring_test_reboot(r); + ring_mpmc_init(r->r, r->blob, r->size, 0); + + assert_true(ring_is_ready(r->r)); + assert_int_equal(rt_count(r), 8); + + for (i = 0; i < 8; i++) + { + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 508); + } + + assert_int_equal(rt_count(r), 0); +} + +static void test_init_warm_restart_rewind(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + uint32_t val; + uint32_t out = 0; + uint32_t i; + + ring_test_init(r); + for (i = 0; i < 16; i++) + { + val = i + 500; + rt_spsc_enqueue(r, &val, 1); + } + + for (i = 0; i < 8; i++) + { + rt_spsc_dequeue(r, &out, 1); + } + + /* Replay the last 3 items consumed before the reboot */ + + ring_test_reboot(r); + ring_mpmc_init(r->r, r->blob, r->size, RT_BYTES(r, 3)); + + assert_int_equal(rt_count(r), 11); + + for (i = 0; i < 11; i++) + { + assert_int_equal(rt_spsc_dequeue(r, &out, 1), 1); + assert_int_equal(out, i + 505); + } +} + +static void test_init_warm_restart_corrupt(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + FAR struct ring_blob_s *blob = (FAR struct ring_blob_s *)r->blob; + uint32_t val = 99; + uint32_t out = 0; + uint32_t n; + + /* READY blob whose indices claim more data than the ring can hold */ + + ring_test_init(r); + ring_test_reboot(r); + atomic_set(&blob->prod_tail, + RTS_PACK(0, RT_BYTES(r, RING_TEST_CAP + 10))); + atomic_set(&blob->cons_tail, RTS_PACK(0, 0)); + + ring_mpmc_init(r->r, r->blob, r->size, 0); + + assert_true(ring_is_ready(r->r)); + assert_int_equal(rt_count(r), 0); + + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 99); +} + +static void test_init_warm_restart_cnt_reset(FAR void **state) +{ + /* A reboot in the middle of an MPMC enqueue leaves head.cnt != + * tail.cnt; unless init zeroes the counters, tail can never catch head + * again and the next MP commit spins forever. + */ + + FAR struct ring_test_s *r = &g_spsc_init; + FAR struct ring_blob_s *blob = (FAR struct ring_blob_s *)r->blob; + uint32_t val; + uint32_t out = 0; + uint32_t n; + uint32_t i; + + ring_test_init(r); + ring_test_reboot(r); + atomic_set(&blob->prod_head, RTS_PACK(3, RT_BYTES(r, 5))); + atomic_set(&blob->prod_tail, RTS_PACK(1, RT_BYTES(r, 5))); + atomic_set(&blob->cons_head, RTS_PACK(2, 0)); + atomic_set(&blob->cons_tail, RTS_PACK(0, 0)); + + ring_mpmc_init(r->r, r->blob, r->size, 0); + + assert_int_equal(RTS_CNT(atomic_read(&blob->prod_head)), 0); + assert_int_equal(RTS_CNT(atomic_read(&blob->prod_tail)), 0); + assert_int_equal(RTS_CNT(atomic_read(&blob->cons_head)), 0); + assert_int_equal(RTS_CNT(atomic_read(&blob->cons_tail)), 0); + + assert_int_equal(RTS_IDX(atomic_read(&blob->prod_tail)), + RT_BYTES(r, 5)); + assert_int_equal(RTS_IDX(atomic_read(&blob->cons_tail)), 0); + assert_int_equal(rt_count(r), 5); + + for (i = 0; i < 4; i++) + { + val = i + 900; + n = rt_mpmc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + } + + assert_int_equal(rt_count(r), 9); + + rt_mpmc_skip(r, 5); + + for (i = 0; i < 4; i++) + { + n = rt_mpmc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 900); + } + + assert_int_equal(rt_count(r), 0); +} + +static void test_init_garbage_magic(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + FAR struct ring_blob_s *blob = (FAR struct ring_blob_s *)r->blob; + + ring_test_reboot(r); + atomic_set(&blob->magic, (int32_t)0xdeadbeef); + atomic_set(&blob->prod_tail, RTS_PACK(3, 100)); + atomic_set(&blob->cons_tail, RTS_PACK(1, 50)); + + ring_mpmc_init(r->r, r->blob, r->size, 0); + + assert_int_equal(atomic_read(&blob->magic), RING_BLOB_MAGIC_READY); + assert_int_equal(rt_count(r), 0); +} + +static void test_init_geometry_mismatch(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + uint32_t val = 7; + + /* A READY blob left by a build with a different size is not reused */ + + ring_test_init(r); + rt_spsc_enqueue(r, &val, 1); + + ring_test_reboot(r); + ring_mpmc_init(r->r, r->blob, r->size - RT_BYTES(r, 1), 0); + + assert_int_equal(rt_capacity(r), RING_TEST_CAP - 1); + assert_int_equal(rt_count(r), 0); +} + +static void test_init_double_call(FAR void **state) +{ + /* A second init in the same boot must not reset a ring in use */ + + FAR struct ring_test_s *r = &g_spsc_init; + uint32_t val = 77; + uint32_t out = 0; + uint32_t n; + + ring_test_init(r); + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + + assert_false(ring_mpmc_init(r->r, r->blob, r->size, 0)); + assert_int_equal(ring_spsc_init(r->r, r->blob, r->size, 0, false), OK); + assert_int_equal(rt_count(r), 1); + + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 77); +} + +static void test_init_spsc_slave(FAR void **state) +{ + FAR struct ring_test_s *r = &g_spsc_init; + struct ring_s slave; + uint32_t val = 55; + uint32_t out = 0; + + memset(&slave, 0, sizeof(slave)); + memset(r->blob, 0, sizeof(struct ring_blob_s)); + memset(r->r, 0, sizeof(*r->r)); + + /* The slave never initializes the blob, it waits for the master */ + + assert_int_equal(ring_spsc_init(&slave, r->blob, r->size, 0, true), + -EAGAIN); + assert_false(ring_is_ready(&slave)); + + assert_int_equal(ring_spsc_init(r->r, r->blob, r->size, 0, false), OK); + assert_int_equal(ring_spsc_init(&slave, r->blob, r->size - 1, 0, true), + -EINVAL); + assert_int_equal(ring_spsc_init(&slave, r->blob, r->size, 0, true), OK); + + assert_int_equal(ring_spsc_enqueue(r->r, &val, sizeof(val)), + sizeof(val)); + assert_int_equal(ring_spsc_dequeue(&slave, &out, sizeof(out)), + sizeof(out)); + assert_int_equal(out, 55); +} + +static void test_init_alloc_warm_restart(FAR void **state) +{ + struct ring_test_s t; + FAR struct ring_test_s *r = &t; + FAR struct ring_s *alloc; + uint32_t val; + uint32_t out = 0; + uint32_t n; + uint32_t i; + + alloc = ring_test_alloc(r, 16, sizeof(uint32_t)); + assert_non_null(alloc); + + for (i = 0; i < 10; i++) + { + val = i + 300; + rt_spsc_enqueue(r, &val, 1); + } + + for (i = 0; i < 4; i++) + { + rt_spsc_dequeue(r, &out, 1); + } + + assert_int_equal(rt_count(r), 6); + + /* Rebind a fresh handle to the allocated blob, as after a reboot */ + + memset(&r->handle, 0, sizeof(r->handle)); + r->r = &r->handle; + ring_mpmc_init(r->r, alloc->blob, r->size, 0); + + assert_int_equal(rt_count(r), 6); + + for (i = 0; i < 6; i++) + { + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, i + 304); + } + + ring_free(alloc); +} + +struct init_thread_ctx_s +{ + FAR struct ring_test_s *ring; + FAR atomic_t *go; + FAR atomic_t *first_count; + FAR atomic_t *done_count; +}; + +static FAR void *init_thread_func(FAR void *arg) +{ + FAR struct init_thread_ctx_s *ctx = + (FAR struct init_thread_ctx_s *)arg; + struct ring_s handle; + + memset(&handle, 0, sizeof(handle)); + + while (atomic_read(ctx->go) == 0) + { + sched_yield(); + } + + if (ring_mpmc_init(&handle, ctx->ring->blob, ctx->ring->size, 0)) + { + atomic_add(ctx->first_count, 1); + } + + if (ring_is_ready(&handle)) + { + atomic_add(ctx->done_count, 1); + } + + return NULL; +} + +static void test_init_concurrent(FAR void **state) +{ + /* Each thread binds its own handle to the same cold blob: exactly one + * of them may run the init body. + */ + + FAR struct ring_test_s *r = &g_spsc_init; + struct init_thread_ctx_s ctx; + pthread_t threads[4]; + atomic_t go = 0; + atomic_t first_count = 0; + atomic_t done_count = 0; + uint32_t val; + uint32_t out = 0; + uint32_t n; + int i; + + memset(r->blob, 0, sizeof(struct ring_blob_s)); + + ctx.ring = r; + ctx.go = &go; + ctx.first_count = &first_count; + ctx.done_count = &done_count; + + for (i = 0; i < 4; i++) + { + pthread_create(&threads[i], NULL, init_thread_func, &ctx); + } + + atomic_set(&go, 1); + + for (i = 0; i < 4; i++) + { + pthread_join(threads[i], NULL); + } + + assert_int_equal(atomic_read(&first_count), 1); + assert_int_equal(atomic_read(&done_count), 4); + + ring_test_reboot(r); + ring_mpmc_init(r->r, r->blob, r->size, 0); + assert_int_equal(rt_count(r), 0); + + val = 123; + n = rt_spsc_enqueue(r, &val, 1); + assert_int_equal(n, 1); + n = rt_spsc_dequeue(r, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 123); +} + +/**************************************************************************** + * Private Functions - Overwrite enqueue tests + ****************************************************************************/ + +static void test_spsc_overwrite_basic(FAR void **state) +{ + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow); + + /* Fill ring to capacity */ + + for (i = 0; i < 8; i++) + { + val = i; + rt_spmc_overwrite(&g_spsc_ow, &val, 1); + } + + assert_int_equal(rt_count(&g_spsc_ow), 8); + + /* Overwrite: push one more, oldest (0) should be discarded */ + + val = 100; + n = rt_spmc_overwrite(&g_spsc_ow, &val, 1); + assert_int_equal(n, 1); + assert_int_equal(rt_count(&g_spsc_ow), 8); + + /* Dequeue all: expect 1,2,3,4,5,6,7,100 */ + + n = rt_spmc_dequeue(&g_spsc_ow, out, 8); + assert_int_equal(n, 8); + for (i = 0; i < 7; i++) + { + assert_int_equal(out[i], i + 1); + } + + assert_int_equal(out[7], 100); +} + +static void test_spsc_overwrite_wrap(FAR void **state) +{ + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow_wrap); + + /* Advance indices to force wraparound */ + + for (i = 0; i < 6; i++) + { + val = i; + rt_spmc_overwrite(&g_spsc_ow_wrap, &val, 1); + } + + rt_spmc_dequeue(&g_spsc_ow_wrap, out, 6); + + /* Now fill ring (indices start at 6, wraps at 8) */ + + for (i = 0; i < 8; i++) + { + val = i + 10; + rt_spmc_overwrite(&g_spsc_ow_wrap, &val, 1); + } + + /* Overwrite 3 more: discards 10,11,12 */ + + for (i = 0; i < 3; i++) + { + val = i + 200; + rt_spmc_overwrite(&g_spsc_ow_wrap, &val, 1); + } + + assert_int_equal(rt_count(&g_spsc_ow_wrap), 8); + + n = rt_spmc_dequeue(&g_spsc_ow_wrap, out, 8); + assert_int_equal(n, 8); + + /* Expect: 13,14,15,16,17,200,201,202 */ + + for (i = 0; i < 5; i++) + { + assert_int_equal(out[i], i + 13); + } + + for (i = 0; i < 3; i++) + { + assert_int_equal(out[i + 5], i + 200); + } +} + +static void test_spsc_overwrite_batch(FAR void **state) +{ + uint32_t in[8]; + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow_batch); + + /* Fill with 0..7 */ + + for (i = 0; i < 8; i++) + { + val = i; + rt_spmc_overwrite(&g_spsc_ow_batch, &val, 1); + } + + /* Batch overwrite 4 items: discards 0,1,2,3 */ + + for (i = 0; i < 4; i++) + { + in[i] = i + 100; + } + + n = rt_spmc_overwrite(&g_spsc_ow_batch, in, 4); + assert_int_equal(n, 4); + assert_int_equal(rt_count(&g_spsc_ow_batch), 8); + + n = rt_spmc_dequeue(&g_spsc_ow_batch, out, 8); + assert_int_equal(n, 8); + + /* Expect: 4,5,6,7,100,101,102,103 */ + + for (i = 0; i < 4; i++) + { + assert_int_equal(out[i], i + 4); + } + + for (i = 0; i < 4; i++) + { + assert_int_equal(out[i + 4], i + 100); + } +} + +static void test_spsc_overwrite_exceed_cap(FAR void **state) +{ + uint32_t in[16]; + uint32_t out[8]; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow_cap); + + /* Enqueue 16 items into cap=8 ring: only last 8 should remain */ + + for (i = 0; i < 16; i++) + { + in[i] = i; + } + + n = rt_spmc_overwrite(&g_spsc_ow_cap, in, 16); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_spsc_ow_cap), 8); + + n = rt_spmc_dequeue(&g_spsc_ow_cap, out, 8); + assert_int_equal(n, 8); + + /* Expect: 8,9,10,11,12,13,14,15 */ + + for (i = 0; i < 8; i++) + { + assert_int_equal(out[i], i + 8); + } +} + +static void test_spsc_overwrite_not_full(FAR void **state) +{ + uint32_t out[8]; + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow); + + /* Partially fill (4 of 8) */ + + for (i = 0; i < 4; i++) + { + val = i; + rt_spmc_overwrite(&g_spsc_ow, &val, 1); + } + + /* Overwrite enqueue when not full: should behave like normal */ + + val = 99; + n = rt_spmc_overwrite(&g_spsc_ow, &val, 1); + assert_int_equal(n, 0); + assert_int_equal(rt_count(&g_spsc_ow), 5); + + n = rt_spmc_dequeue(&g_spsc_ow, out, 5); + assert_int_equal(n, 5); + for (i = 0; i < 4; i++) + { + assert_int_equal(out[i], i); + } + + assert_int_equal(out[4], 99); +} + +/**************************************************************************** + * Private Functions - Dequeue skip tests + ****************************************************************************/ + +static void test_spsc_dequeue_skip_basic(FAR void **state) +{ + uint32_t val; + uint32_t out = 0; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow); + + /* Fill with 0..7 */ + + for (i = 0; i < 8; i++) + { + val = i; + rt_spsc_enqueue(&g_spsc_ow, &val, 1); + } + + /* Skip first 3 */ + + n = rt_spsc_skip(&g_spsc_ow, 3); + assert_int_equal(n, 3); + assert_int_equal(rt_count(&g_spsc_ow), 5); + + /* Next dequeue should return 3 */ + + n = rt_spsc_dequeue(&g_spsc_ow, &out, 1); + assert_int_equal(n, 1); + assert_int_equal(out, 3); +} + +static void test_spsc_dequeue_skip_all(FAR void **state) +{ + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow); + + for (i = 0; i < 8; i++) + { + val = i; + rt_spsc_enqueue(&g_spsc_ow, &val, 1); + } + + /* Skip all */ + + n = rt_spsc_skip(&g_spsc_ow, 8); + assert_int_equal(n, 8); + assert_int_equal(rt_count(&g_spsc_ow), 0); +} + +static void test_spsc_dequeue_skip_more_than_avail(FAR void **state) +{ + uint32_t val; + uint32_t i; + uint32_t n; + + ring_test_init(&g_spsc_ow); + + for (i = 0; i < 4; i++) + { + val = i; + rt_spsc_enqueue(&g_spsc_ow, &val, 1); + } + + /* Try to skip 8, only 4 available */ + + n = rt_spsc_skip(&g_spsc_ow, 8); + assert_int_equal(n, 4); + assert_int_equal(rt_count(&g_spsc_ow), 0); +} + +static void test_spsc_dequeue_skip_empty(FAR void **state) +{ + uint32_t n; + + ring_test_init(&g_spsc_ow); + + n = rt_spsc_skip(&g_spsc_ow, 5); + assert_int_equal(n, 0); +} + +/**************************************************************************** + * Private Functions - Single-core multi-thread concurrent test + * + * SPSC: 1 producer thread + main thread as consumer. + * Uses pthread + sched_yield() for single-core safety. + ****************************************************************************/ + +struct spsc_thread_ctx_s +{ + FAR struct ring_test_s *ring; + int items; + atomic_t *start; +}; + +static FAR void *spsc_producer_thread(FAR void *arg) +{ + FAR struct spsc_thread_ctx_s *ctx = + (FAR struct spsc_thread_ctx_s *)arg; + uint32_t val; + int sent = 0; + + while (atomic_read(ctx->start) == 0) + { + sched_yield(); + } + + while (sent < ctx->items) + { + val = (uint32_t)sent; + if (rt_spsc_enqueue(ctx->ring, &val, 1) == 1) + { + sent++; + } + else + { + sched_yield(); + } + } + + return NULL; +} + +static void test_spsc_concurrent(FAR void **state) +{ + struct spsc_thread_ctx_s ctx; + pthread_t thread; + atomic_t start = 0; + uint32_t out = 0; + int received = 0; + + ring_test_init(&g_spsc_concurrent); + + ctx.ring = &g_spsc_concurrent; + ctx.items = RING_TEST_ITEMS; + ctx.start = &start; + pthread_create(&thread, NULL, spsc_producer_thread, &ctx); + + /* Signal producer to start */ + + atomic_set(&start, 1); + + /* Main thread: single consumer */ + + while (received < RING_TEST_ITEMS) + { + if (rt_spsc_dequeue(&g_spsc_concurrent, &out, 1) == 1) + { + assert_int_equal(out, (uint32_t)received); + received++; + } + else + { + sched_yield(); + } + } + + pthread_join(thread, NULL); + assert_int_equal(received, RING_TEST_ITEMS); + assert_int_equal(rt_count(&g_spsc_concurrent), 0); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +static const struct CMUnitTest g_spsc_local_tests[] = +{ + cmocka_unit_test(test_spsc_basic), + cmocka_unit_test(test_spsc_batch), + cmocka_unit_test(test_spsc_full), + cmocka_unit_test(test_spsc_typed_api), + cmocka_unit_test(test_spsc_alloc_free), + cmocka_unit_test(test_ring_alloc_valid), + cmocka_unit_test(test_ring_alloc_invalid), + cmocka_unit_test(test_ring_alloc_all_modes), + cmocka_unit_test(test_ring_alloc_non_pow2), + cmocka_unit_test(test_enqueue_dequeue_boundary), + cmocka_unit_test(test_partial_batch), + cmocka_unit_test(test_wraparound_batch), + cmocka_unit_test(test_capacity_two), + cmocka_unit_test(test_large_elem), + cmocka_unit_test(test_ring_count_boundary), + cmocka_unit_test(test_init_fresh), + cmocka_unit_test(test_init_warm_restart_recovery), + cmocka_unit_test(test_init_warm_restart_rewind), + cmocka_unit_test(test_init_warm_restart_corrupt), + cmocka_unit_test(test_init_warm_restart_cnt_reset), + cmocka_unit_test(test_init_garbage_magic), + cmocka_unit_test(test_init_geometry_mismatch), + cmocka_unit_test(test_init_double_call), + cmocka_unit_test(test_init_spsc_slave), + cmocka_unit_test(test_init_alloc_warm_restart), + cmocka_unit_test(test_init_concurrent), + cmocka_unit_test(test_spsc_concurrent), + cmocka_unit_test(test_spsc_overwrite_basic), + cmocka_unit_test(test_spsc_overwrite_wrap), + cmocka_unit_test(test_spsc_overwrite_batch), + cmocka_unit_test(test_spsc_overwrite_exceed_cap), + cmocka_unit_test(test_spsc_overwrite_not_full), + cmocka_unit_test(test_spsc_dequeue_skip_basic), + cmocka_unit_test(test_spsc_dequeue_skip_all), + cmocka_unit_test(test_spsc_dequeue_skip_more_than_avail), + cmocka_unit_test(test_spsc_dequeue_skip_empty), +}; + +int ring_test_spsc_local(void) +{ + return cmocka_run_group_tests(g_spsc_local_tests, NULL, NULL); +} diff --git a/testing/libc/ring/ring_test.h b/testing/libc/ring/ring_test.h new file mode 100644 index 00000000000..7653347415d --- /dev/null +++ b/testing/libc/ring/ring_test.h @@ -0,0 +1,176 @@ +/**************************************************************************** + * apps/testing/libc/ring/ring_test.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __APPS_TESTING_LIBC_RING_RING_TEST_H +#define __APPS_TESTING_LIBC_RING_RING_TEST_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define RING_TEST_CAP 64 +#define RING_TEST_ITEMS 1024 +#define RING_TEST_BATCH 8 +#define RING_TEST_THREADS 2 + +/* The ring counts bytes while the cases count items, so each test ring + * records its item size and the rt_ wrappers convert both ways. + */ + +#define RING_TEST_DEFINE(name, type, cap) \ + static char name##_blob[sizeof(struct ring_blob_s) + \ + (cap) * sizeof(type)] \ + aligned_data(RING_BLOB_ALIGN); \ + static struct ring_test_s name = \ + { \ + .r = &name.handle, \ + .blob = name##_blob, \ + .size = sizeof(name##_blob), \ + .elem = sizeof(type), \ + } + +#define RT_BYTES(t, n) ((uint32_t)(n) * (t)->elem) +#define RT_ITEMS(t, b) ((uint32_t)(b) / (t)->elem) + +#define RT_IO(name, op) \ + static inline uint32_t name(FAR struct ring_test_s *t, \ + FAR void *items, uint32_t n) \ + { \ + return RT_ITEMS(t, op(t->r, items, RT_BYTES(t, n))); \ + } + +#define RT_SKIP(name, op) \ + static inline uint32_t name(FAR struct ring_test_s *t, uint32_t n) \ + { \ + return RT_ITEMS(t, op(t->r, RT_BYTES(t, n))); \ + } + +#define rt_count(t) RT_ITEMS(t, ring_count((t)->r)) +#define rt_capacity(t) RT_ITEMS(t, ring_capacity((t)->r)) +#define rt_reset(t) ((void)ring_reset((t)->r)) + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct ring_test_s +{ + FAR struct ring_s *r; /* Handle in use: &handle or ring_alloc */ + struct ring_s handle; + FAR char *blob; + uint32_t size; /* Whole blob, header included */ + uint32_t elem; /* Item size in bytes */ +}; + +/**************************************************************************** + * Inline Functions - item-count wrappers + ****************************************************************************/ + +RT_IO(rt_spsc_enqueue, ring_spsc_enqueue) +RT_IO(rt_spsc_dequeue, ring_spsc_dequeue) +RT_IO(rt_mpsc_enqueue, ring_mpsc_enqueue) +RT_IO(rt_mpsc_dequeue, ring_mpsc_dequeue) +RT_IO(rt_spmc_enqueue, ring_spmc_enqueue) +RT_IO(rt_spmc_dequeue, ring_spmc_dequeue) +RT_IO(rt_mpmc_enqueue, ring_mpmc_enqueue) +RT_IO(rt_mpmc_dequeue, ring_mpmc_dequeue) + +/* Overwrite returns the items that did not stay in the ring */ + +RT_IO(rt_spmc_overwrite, ring_spmc_overwrite) +RT_IO(rt_mpmc_overwrite, ring_mpmc_overwrite) + +RT_SKIP(rt_spsc_skip, ring_spsc_skip) +RT_SKIP(rt_mpmc_skip, ring_mpmc_skip) + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int ring_test_spsc_local(void); +int ring_test_mpsc_local(void); +int ring_test_spmc_local(void); +int ring_test_mpmc_local(void); + +/**************************************************************************** + * Inline Functions - test ring helpers + ****************************************************************************/ + +/**************************************************************************** + * Name: ring_test_init + * + * Description: + * Cold-start a test ring. Both magics are cleared first: a READY blob + * would be recovered as a warm restart, and a READY handle makes init + * return without touching the blob. + * + ****************************************************************************/ + +static inline void ring_test_init(FAR struct ring_test_s *t) +{ + memset(t->r, 0, sizeof(*t->r)); + memset(t->blob, 0, sizeof(struct ring_blob_s)); + ring_spsc_init(t->r, t->blob, t->size, 0, false); +} + +/**************************************************************************** + * Name: ring_test_alloc + * + * Description: + * Wrap a ring_alloc() ring holding cap items of elem bytes. Free it + * with ring_free(t->r). + * + ****************************************************************************/ + +static inline FAR struct ring_s *ring_test_alloc(FAR struct ring_test_s *t, + uint32_t cap, + uint32_t elem) +{ + t->elem = elem; + t->size = sizeof(struct ring_blob_s) + cap * elem; + t->blob = NULL; + t->r = ring_alloc(t->size); + return t->r; +} + +#endif /* __APPS_TESTING_LIBC_RING_RING_TEST_H */