Skip to content

Commit 8c07902

Browse files
Mattemagikerncfriedt
authored andcommitted
tests: kernel: pipe: Add concurrency test for zero-length pipes
This commit adds a new test case to the pipe API concurrency tests to verify the behavior of zero-length pipes. The test ensures that writing to a zero-length pipe blocks until data is read, and reading from a zero-length pipe blocks until data is written—unless the pipe is closed, reset, or the operation times out. The test creates two pipes with zero-length buffers and spawns a helper thread that performs complementary read/write operations. The two threads use the pipe API as their only synchronization mechanism, validating the correct blocking and wake-up behavior of zero-length pipes under concurrent access. Signed-off-by: Måns Ansgariusson <[email protected]>
1 parent 14d26b2 commit 8c07902

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tests/kernel/pipe/pipe_api/src/concurrency.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,51 @@ ZTEST(k_pipe_concurrency, test_partial_write)
163163
"failed t read from pipe");
164164
k_thread_join(tid, K_FOREVER);
165165
}
166+
167+
static volatile bool zero_thread_read;
168+
static volatile bool zero_thread_write;
169+
static void zero_thread_read_write(void *arg1, void *arg2, void *arg3)
170+
{
171+
uint8_t tmp[DUMMY_DATA_SIZE];
172+
struct k_pipe *input = (struct k_pipe *)arg1;
173+
struct k_pipe *output = (struct k_pipe *)arg2;
174+
175+
memset(tmp, 0xBB, sizeof(tmp));
176+
177+
zero_thread_read = true;
178+
zassert_true(k_pipe_read(input, tmp, sizeof(tmp), K_FOREVER) == sizeof(tmp),
179+
"Failed to read from pipe");
180+
zero_thread_write = true;
181+
zassert_true(k_pipe_write(output, tmp, sizeof(tmp), K_FOREVER) == sizeof(tmp),
182+
"Failed to write to pipe");
183+
}
184+
185+
ZTEST(k_pipe_concurrency, test_zero_size_pipe_read_write)
186+
{
187+
k_tid_t tid;
188+
struct k_pipe input_pipe;
189+
struct k_pipe output_pipe;
190+
uint8_t input[DUMMY_DATA_SIZE];
191+
uint8_t output[DUMMY_DATA_SIZE];
192+
193+
memset(input, 0xAA, sizeof(input));
194+
memset(output, 0xCC, sizeof(output));
195+
k_pipe_init(&input_pipe, NULL, 0);
196+
k_pipe_init(&output_pipe, NULL, 0);
197+
198+
tid = k_thread_create(&thread, stack, K_THREAD_STACK_SIZEOF(stack),
199+
zero_thread_read_write, &input_pipe, &output_pipe, NULL, K_PRIO_COOP(0), 0,
200+
K_NO_WAIT);
201+
202+
zassert_true(sizeof(input) == k_pipe_write(&input_pipe, input, sizeof(input), K_FOREVER),
203+
"Failed to write to pipe");
204+
zassert_true(sizeof(output) == k_pipe_read(&output_pipe, output, sizeof(output), K_FOREVER),
205+
"Failed to read from pipe");
206+
zassert_true(memcmp(input, output, sizeof(input)) == 0,
207+
"Unexpected data received from pipe");
208+
209+
zassert_true(zero_thread_read && zero_thread_write,
210+
"Thread did not execute expected read/write operations");
211+
212+
k_thread_join(tid, K_FOREVER);
213+
}

0 commit comments

Comments
 (0)