Skip to content

Commit 28a8b15

Browse files
committed
tests: kernel: spinlock: add test for k_spin_trylock()
Add a testcase to exercise two cases: * when `k_spin_trylock()` fails (lock is busy) * when `k_spin_trylock()` succeeds (lock is acquired) We use the same machinery for checking for a recursive mutex as `k_spin_lock()`. Signed-off-by: Christopher Friedt <[email protected]>
1 parent ac2b992 commit 28a8b15

File tree

1 file changed

+54
-4
lines changed
  • tests/kernel/spinlock/src

1 file changed

+54
-4
lines changed

tests/kernel/spinlock/src/main.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct k_thread cpu1_thread;
1818
static struct k_spinlock bounce_lock;
1919

2020
volatile int bounce_owner, bounce_done;
21+
volatile int trylock_failures;
22+
volatile int trylock_successes;
2123

2224
/**
2325
* @brief Tests for spinlock
@@ -53,8 +55,9 @@ ZTEST(spinlock, test_spinlock_basic)
5355
zassert_true(!l.locked, "Spinlock failed to unlock");
5456
}
5557

56-
void bounce_once(int id)
58+
void bounce_once(int id, bool trylock)
5759
{
60+
int ret;
5861
int i, locked;
5962
k_spinlock_key_t key;
6063

@@ -63,7 +66,16 @@ void bounce_once(int id)
6366
*/
6467
locked = 0;
6568
for (i = 0; i < 10000; i++) {
66-
key = k_spin_lock(&bounce_lock);
69+
if (trylock) {
70+
ret = k_spin_trylock(&bounce_lock, &key);
71+
if (ret == -EBUSY) {
72+
trylock_failures++;
73+
continue;
74+
}
75+
trylock_successes++;
76+
} else {
77+
key = k_spin_lock(&bounce_lock);
78+
}
6779

6880
if (bounce_owner != id) {
6981
locked = 1;
@@ -100,7 +112,7 @@ void cpu1_fn(void *p1, void *p2, void *p3)
100112
ARG_UNUSED(p3);
101113

102114
while (1) {
103-
bounce_once(4321);
115+
bounce_once(4321, false);
104116
}
105117
}
106118

@@ -122,7 +134,7 @@ ZTEST(spinlock, test_spinlock_bounce)
122134
k_busy_wait(10);
123135

124136
for (i = 0; i < 10000; i++) {
125-
bounce_once(1234);
137+
bounce_once(1234, false);
126138
}
127139

128140
bounce_done = 1;
@@ -173,4 +185,42 @@ ZTEST(spinlock, test_spinlock_mutual_exclusion)
173185
zassert_true(!lock_runtime.locked, "Spinlock failed to unlock");
174186
}
175187

188+
void trylock_fn(void *p1, void *p2, void *p3)
189+
{
190+
ARG_UNUSED(p1);
191+
ARG_UNUSED(p2);
192+
ARG_UNUSED(p3);
193+
194+
while (1) {
195+
bounce_once(4321, true);
196+
}
197+
}
198+
199+
/**
200+
* @brief Test k_spin_trylock()
201+
*
202+
* @ingroup kernel_spinlock_tests
203+
*
204+
* @see k_spin_trylock()
205+
*/
206+
ZTEST(spinlock, test_trylock)
207+
{
208+
int i;
209+
210+
k_thread_create(&cpu1_thread, cpu1_stack, CPU1_STACK_SIZE,
211+
trylock_fn, NULL, NULL, NULL,
212+
0, 0, K_NO_WAIT);
213+
214+
k_busy_wait(10);
215+
216+
for (i = 0; i < 10000; i++) {
217+
bounce_once(1234, true);
218+
}
219+
220+
bounce_done = 1;
221+
222+
zassert_true(trylock_failures > 0);
223+
zassert_true(trylock_successes > 0);
224+
}
225+
176226
ZTEST_SUITE(spinlock, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)