Skip to content

Commit 81ca509

Browse files
povergoingcarlescufi
authored andcommitted
tests: kernel: spinlock: Fix test_trylock thread reusable issue
The test_trylock reuses the cpu1_thread, but there is no way for it to exit. This will cause the thread created twice, as a result, two cpu running the same thread simultaneously cause an unexpected crash. Fix this by adding initialization of resources and also the exit for the cpu1_thread. Signed-off-by: Jaxson Han <[email protected]>
1 parent cf71de4 commit 81ca509

File tree

1 file changed

+14
-3
lines changed
  • tests/kernel/spinlock/src

1 file changed

+14
-3
lines changed

tests/kernel/spinlock/src/main.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void cpu1_fn(void *p1, void *p2, void *p3)
111111
ARG_UNUSED(p2);
112112
ARG_UNUSED(p3);
113113

114-
while (1) {
114+
while (!bounce_done) {
115115
bounce_once(4321, false);
116116
}
117117
}
@@ -138,6 +138,8 @@ ZTEST(spinlock, test_spinlock_bounce)
138138
}
139139

140140
bounce_done = 1;
141+
142+
k_thread_join(&cpu1_thread, K_FOREVER);
141143
}
142144

143145
/**
@@ -191,7 +193,7 @@ void trylock_fn(void *p1, void *p2, void *p3)
191193
ARG_UNUSED(p2);
192194
ARG_UNUSED(p3);
193195

194-
while (1) {
196+
while (!bounce_done) {
195197
bounce_once(4321, true);
196198
}
197199
}
@@ -219,8 +221,17 @@ ZTEST(spinlock, test_trylock)
219221

220222
bounce_done = 1;
221223

224+
k_thread_join(&cpu1_thread, K_FOREVER);
225+
222226
zassert_true(trylock_failures > 0);
223227
zassert_true(trylock_successes > 0);
224228
}
225229

226-
ZTEST_SUITE(spinlock, NULL, NULL, NULL, NULL, NULL);
230+
static void before(void *ctx)
231+
{
232+
ARG_UNUSED(ctx);
233+
234+
bounce_done = 0;
235+
}
236+
237+
ZTEST_SUITE(spinlock, NULL, NULL, before, NULL, NULL);

0 commit comments

Comments
 (0)