Skip to content

Conversation

@yeungao
Copy link

@yeungao yeungao commented Feb 3, 2026

use cmocka framework to write unit tests for rwsem api

Summary

Add unit tests for rwsemaphore API using cmocka framework to improve code coverage and ensure correctness of reader-writer semaphore functionality. The tests verify basic synchronization primitives including initialization, lock acquisition (blocking/non-blocking), and lock release for both readers and writers.

Impact

Build: Minimal impact, only adds new test files
Testing: Adds new test case rwsemtest for schedtest (enabled with CONFIG_TESTING_SCHEDTEST=y)
Compatibility: No breaking changes, purely additive

Testing

Test Scenarios
Single Writer: Writer thread acquires write lock, updates shared resource (0 → 10), verifies exclusive access
Multiple Readers: Three reader threads with different behaviors:
Reader 1: Basic read lock/unlock with data verification
Reader 2: Tests down_read_trylock() non-blocking behavior
Reader 3: Tests recursive read lock acquisition (same thread acquires twice)
Reader-Writer Exclusion: Verifies readers are blocked while writer holds the lock

Tested on QEMU simultiaon
Config:CONFIG_TESTING_SCHEDTEST=y

use cmocka framework to write unit tests for rwsem api

Signed-off-by: yangao1 <yangao1@xiaomi.com>

typedef struct
{
shared_data_t *data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation alignment is incorrect, just compare with previous struct


static void *writer_thread(void *arg)
{
shared_data_t *data = (shared_data_t *)arg;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also incorrect, NuttX uses 2 spaces be default instead of 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants