Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ /** * @addtogroup t_threads_lifecycle * @{ * @defgroup t_threads_cancel_abort test_threads_cancel_abort * @} */ #include <ztest.h> #define STACK_SIZE (256 + CONFIG_TEST_EXTRA_STACKSIZE) K_THREAD_STACK_EXTERN(tstack); extern struct k_thread tdata; static int execute_flag; K_SEM_DEFINE(sync_sema, 0, 1); #define BLOCK_SIZE 64 static void thread_entry(void *p1, void *p2, void *p3) { execute_flag = 1; k_sleep(100); execute_flag = 2; } static void thread_entry_abort(void *p1, void *p2, void *p3) { /**TESTPOINT: abort current thread*/ execute_flag = 1; k_thread_abort(k_current_get()); /*unreachable*/ execute_flag = 2; zassert_true(1 == 0, NULL); } /*test cases*/ void test_threads_cancel_undelayed(void) { int cur_prio = k_thread_priority_get(k_current_get()); /* spawn thread with lower priority */ int spawn_prio = cur_prio + 1; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, K_USER, 0); /**TESTPOINT: check cancel retcode when thread is not delayed*/ int cancel_ret = k_thread_cancel(tid); zassert_equal(cancel_ret, -EINVAL, NULL); k_thread_abort(tid); } void test_threads_cancel_started(void) { int cur_prio = k_thread_priority_get(k_current_get()); /* spawn thread with lower priority */ int spawn_prio = cur_prio + 1; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, K_USER, 0); k_sleep(50); /**TESTPOINT: check cancel retcode when thread is started*/ int cancel_ret = k_thread_cancel(tid); zassert_equal(cancel_ret, -EINVAL, NULL); k_thread_abort(tid); } void test_threads_cancel_delayed(void) { int cur_prio = k_thread_priority_get(k_current_get()); /* spawn thread with lower priority */ int spawn_prio = cur_prio + 1; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, K_USER, 100); k_sleep(50); /**TESTPOINT: check cancel retcode when thread is started*/ int cancel_ret = k_thread_cancel(tid); zassert_equal(cancel_ret, 0, NULL); k_thread_abort(tid); } void test_threads_abort_self(void) { execute_flag = 0; k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry_abort, NULL, NULL, NULL, 0, K_USER, 0); k_sleep(100); /**TESTPOINT: spawned thread executed but abort itself*/ zassert_true(execute_flag == 1, NULL); } void test_threads_abort_others(void) { execute_flag = 0; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, 0, K_USER, 0); k_thread_abort(tid); k_sleep(100); /**TESTPOINT: check not-started thread is aborted*/ zassert_true(execute_flag == 0, NULL); tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, 0, K_USER, 0); k_sleep(50); k_thread_abort(tid); /**TESTPOINT: check running thread is aborted*/ zassert_true(execute_flag == 1, NULL); k_sleep(1000); zassert_true(execute_flag == 1, NULL); } /* Test should not crash if repeated aborts are called on a dead thread. */ void test_threads_abort_repeat(void) { execute_flag = 0; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, 0, K_USER, 0); k_thread_abort(tid); k_sleep(100); k_thread_abort(tid); k_sleep(100); k_thread_abort(tid); /* If no fault occured till now. The test case passed. */ ztest_test_pass(); } /* Test to validate the call of abort handler specified by thread * when it is aborted */ bool abort_called; void *block; static void abort_function(void) { printk("Child thread's abort handler called\n"); abort_called = true; k_free(block); } static void uthread_entry(void) { block = k_malloc(BLOCK_SIZE); zassert_true(block != NULL, NULL); printk("Child thread is running\n"); k_sleep(K_MSEC(2)); } void test_abort_handler(void) { k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, (k_thread_entry_t)uthread_entry, NULL, NULL, NULL, 0, 0, 0); tdata.fn_abort = &abort_function; k_sleep(K_MSEC(1)); abort_called = false; printk("Calling abort of child from parent\n"); k_thread_abort(tid); zassert_true(abort_called == true, "Abort handler" " is not called"); } |