Skip to content

Commit e89fe33

Browse files
Enjia Mainashif
authored andcommitted
tests: thread: refine the thread abort test case
Refine the thread abort test case and add one extra condition. Signed-off-by: Enjia Mai <[email protected]>
1 parent 30bf585 commit e89fe33

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

tests/kernel/threads/thread_apis/src/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern void test_threads_cpu_mask(void);
3838
extern void test_threads_suspend_timeout(void);
3939
extern void test_threads_suspend(void);
4040
extern void test_abort_from_isr(void);
41+
extern void test_abort_from_isr_not_self(void);
4142
extern void test_essential_thread_abort(void);
4243

4344
struct k_thread tdata;
@@ -516,7 +517,8 @@ void test_main(void)
516517
ztest_user_unit_test(test_thread_join),
517518
ztest_unit_test(test_thread_join_isr),
518519
ztest_user_unit_test(test_thread_join_deadlock),
519-
ztest_unit_test(test_abort_from_isr)
520+
ztest_unit_test(test_abort_from_isr),
521+
ztest_unit_test(test_abort_from_isr_not_self)
520522
);
521523

522524
ztest_run_test_suite(threads_lifecycle);

tests/kernel/threads/thread_apis/src/test_threads_cancel_abort.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,15 @@ static void entry_abort_isr(void *p1, void *p2, void *p3)
218218
ztest_test_fail();
219219
}
220220

221+
extern struct k_sem offload_sem;
222+
221223
/**
222224
* @ingroup kernel_thread_tests
223-
* @brief Show that threads can be aborted from interrupt context
225+
*
226+
* @brief Show that threads can be aborted from interrupt context by itself
227+
*
228+
* @details Spwan a thread, then enter ISR context in child thread and abort
229+
* the child thread. Check if ISR completed and target thread was aborted.
224230
*
225231
* @see k_thread_abort()
226232
*/
@@ -231,6 +237,56 @@ void test_abort_from_isr(void)
231237
NULL, NULL, NULL, 0, 0, K_NO_WAIT);
232238

233239

240+
k_thread_join(&tdata, K_FOREVER);
241+
zassert_true(isr_finished, "ISR did not complete");
242+
243+
/* Notice: Recover back the offload_sem: This is use for releasing
244+
* offload_sem which might be held when thread aborts itself in ISR
245+
* context, it will cause irq_offload cannot be used again.
246+
*/
247+
k_sem_give(&offload_sem);
248+
}
249+
250+
/* use for sync thread start */
251+
static struct k_sem sem_abort;
252+
253+
static void entry_aborted_thread(void *p1, void *p2, void *p3)
254+
{
255+
k_sem_give(&sem_abort);
256+
257+
/* wait for being aborted */
258+
while (1) {
259+
k_sleep(K_MSEC(1));
260+
}
261+
zassert_unreachable("should not reach here");
262+
}
263+
264+
/**
265+
* @ingroup kernel_thread_tests
266+
*
267+
* @brief Show that threads can be aborted from interrupt context
268+
*
269+
* @details Spwan a thread, then enter ISR context in main thread and abort
270+
* the child thread. Check if ISR completed and target thread was aborted.
271+
*
272+
* @see k_thread_abort()
273+
*/
274+
void test_abort_from_isr_not_self(void)
275+
{
276+
k_tid_t tid;
277+
278+
isr_finished = false;
279+
k_sem_init(&sem_abort, 0, 1);
280+
281+
tid = k_thread_create(&tdata, tstack, STACK_SIZE, entry_aborted_thread,
282+
NULL, NULL, NULL, 0, 0, K_NO_WAIT);
283+
284+
/* wait for thread started */
285+
k_sem_take(&sem_abort, K_FOREVER);
286+
287+
/* Simulate taking an interrupt which kills spwan thread */
288+
irq_offload(offload_func, (void *)tid);
289+
234290
k_thread_join(&tdata, K_FOREVER);
235291
zassert_true(isr_finished, "ISR did not complete");
236292
}

0 commit comments

Comments
 (0)