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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | /* * Copyright (c) 2018 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <ztest.h> #include <kernel.h> #include <cmsis_os2.h> #define STACKSZ CONFIG_CMSIS_V2_THREAD_MAX_STACK_SIZE /* This is used to check the thread yield functionality between 2 threads */ static int thread_yield_check; static int thread_yield_check_dynamic; static K_THREAD_STACK_DEFINE(test_stack1, STACKSZ); static osThreadAttr_t thread1_attr = { .name = "Thread1", .stack_mem = &test_stack1, .stack_size = STACKSZ, .priority = osPriorityHigh, }; static K_THREAD_STACK_DEFINE(test_stack2, STACKSZ); static osThreadAttr_t thread2_attr = { .name = "Thread2", .stack_mem = &test_stack2, .stack_size = STACKSZ, .priority = osPriorityHigh, }; struct thread1_args { int *yield_check; const char *name; }; static void thread1(void *argument) { osStatus_t status; osThreadId_t thread_id; const char *name; struct thread1_args *args = (struct thread1_args *)argument; thread_id = osThreadGetId(); zassert_true(thread_id != NULL, "Failed getting Thread ID"); name = osThreadGetName(thread_id); zassert_true(strcmp(args->name, name) == 0, "Failed getting Thread name"); /* This thread starts off at a high priority (same as thread2) */ (*args->yield_check)++; zassert_equal(*args->yield_check, 1, NULL); /* Yield to thread2 which is of same priority */ status = osThreadYield(); zassert_true(status == osOK, "Error doing thread yield"); /* thread_yield_check should now be 2 as it was incremented * in thread2. */ zassert_equal(*args->yield_check, 2, NULL); osThreadExit(); } static void thread2(void *argument) { u32_t i, num_threads, max_num_threads = 5U; osThreadId_t *thread_array; int *yield_check = (int *)argument; /* By now thread1 would have set thread_yield_check to 1 and would * have yielded the CPU. Incrementing it over here would essentially * confirm that the yield was indeed executed. */ (*yield_check)++; thread_array = k_calloc(max_num_threads, sizeof(osThreadId_t)); num_threads = osThreadEnumerate(thread_array, max_num_threads); zassert_equal(num_threads, 2, "Incorrect number of cmsis rtos v2 threads"); for (i = 0U; i < num_threads; i++) { u32_t size = osThreadGetStackSize(thread_array[i]); u32_t space = osThreadGetStackSpace(thread_array[i]); zassert_true(space < size, "stack size remaining is not what is expected"); } zassert_equal(osThreadGetState(thread_array[1]), osThreadReady, "Thread not in ready state"); zassert_equal(osThreadGetState(thread_array[0]), osThreadRunning, "Thread not in running state"); zassert_equal(osThreadSuspend(thread_array[1]), osOK, ""); zassert_equal(osThreadGetState(thread_array[1]), osThreadBlocked, "Thread not in blocked state"); zassert_equal(osThreadResume(thread_array[1]), osOK, ""); zassert_equal(osThreadGetState(thread_array[1]), osThreadReady, "Thread not in ready state"); k_free(thread_array); /* Yield back to thread1 which is of same priority */ osThreadYield(); } static void thread_apis_common(int *yield_check, const char *thread1_name, osThreadAttr_t *thread1_attr, osThreadAttr_t *thread2_attr) { osThreadId_t id1; osThreadId_t id2; struct thread1_args args = { .yield_check = yield_check, .name = thread1_name }; id1 = osThreadNew(thread1, &args, thread1_attr); zassert_true(id1 != NULL, "Failed creating thread1"); id2 = osThreadNew(thread2, yield_check, thread2_attr); zassert_true(id2 != NULL, "Failed creating thread2"); zassert_equal(osThreadGetCount(), 2, "Incorrect number of cmsis rtos v2 threads"); do { osDelay(100); } while (*yield_check != 2); } void test_thread_apis_dynamic(void) { thread_apis_common(&thread_yield_check_dynamic, "ZephyrThread", NULL, NULL); } void test_thread_apis(void) { thread_apis_common(&thread_yield_check, thread1_attr.name, &thread1_attr, &thread2_attr); } static osPriority_t OsPriorityInvalid = 60; /* This is used to indicate the completion of processing for thread3 */ static int thread3_state; static int thread3_state_dynamic; static K_THREAD_STACK_DEFINE(test_stack3, STACKSZ); static osThreadAttr_t thread3_attr = { .name = "Thread3", .stack_mem = &test_stack3, .stack_size = STACKSZ, .priority = osPriorityNormal, }; static void thread3(void *argument) { osStatus_t status; osPriority_t rv; osThreadId_t id = osThreadGetId(); osPriority_t prio = osThreadGetPriority(id); int *state = (int *)argument; /* Lower the priority of the current thread */ osThreadSetPriority(id, osPriorityBelowNormal); rv = osThreadGetPriority(id); zassert_equal(rv, osPriorityBelowNormal, "Expected priority to be changed to %d, not %d", (int)osPriorityBelowNormal, (int)rv); /* Increase the priority of the current thread */ osThreadSetPriority(id, osPriorityAboveNormal); rv = osThreadGetPriority(id); zassert_equal(rv, osPriorityAboveNormal, "Expected priority to be changed to %d, not %d", (int)osPriorityAboveNormal, (int)rv); /* Restore the priority of the current thread */ osThreadSetPriority(id, prio); rv = osThreadGetPriority(id); zassert_equal(rv, prio, "Expected priority to be changed to %d, not %d", (int)prio, (int)rv); /* Try to set unsupported priority and assert failure */ status = osThreadSetPriority(id, OsPriorityInvalid); zassert_true(status == osErrorParameter, "Something's wrong with osThreadSetPriority!"); /* Indication that thread3 is done with its processing */ *state = 1; /* Keep looping till it gets killed */ do { osDelay(100); } while (1); } static void thread_prior_common(int *state, osThreadAttr_t *attr) { osStatus_t status; osThreadId_t id3; id3 = osThreadNew(thread3, state, attr); zassert_true(id3 != NULL, "Failed creating thread3"); /* Keep delaying 10 milliseconds to ensure thread3 is done with * its execution. It loops at the end and is terminated here. */ do { osDelay(10); } while (*state == 0); status = osThreadTerminate(id3); zassert_true(status == osOK, "Error terminating thread3"); /* Try to set priority to inactive thread and assert failure */ status = osThreadSetPriority(id3, osPriorityNormal); zassert_true(status == osErrorResource, "Something's wrong with osThreadSetPriority!"); /* Try to terminate inactive thread and assert failure */ status = osThreadTerminate(id3); zassert_true(status == osErrorResource, "Something's wrong with osThreadTerminate!"); *state = 0; } void test_thread_prio_dynamic(void) { thread_prior_common(&thread3_state_dynamic, NULL); } void test_thread_prio(void) { thread_prior_common(&thread3_state, &thread3_attr); } #define DELAY_MS 1000 #define DELTA_MS 500 static void thread5(void *argument) { printk(" * Thread B started.\n"); osDelay(z_ms_to_ticks(DELAY_MS)); printk(" * Thread B joining...\n"); } static void thread4(void *argument) { osThreadId_t tB = argument; osStatus_t status; printk(" + Thread A started.\n"); status = osThreadJoin(tB); zassert_equal(status, osOK, "osThreadJoin thread B failed!"); printk(" + Thread A joining...\n"); } void test_thread_join(void) { osThreadAttr_t attr = { 0 }; s64_t time_stamp; s64_t milliseconds_spent; osThreadId_t tA, tB; osStatus_t status; attr.attr_bits = osThreadJoinable; time_stamp = k_uptime_get(); printk(" - Creating thread B...\n"); tB = osThreadNew(thread5, NULL, &attr); zassert_not_null(tB, "Failed to create thread B with osThreadNew!"); printk(" - Creating thread A...\n"); tA = osThreadNew(thread4, tB, &attr); zassert_not_null(tA, "Failed to create thread A with osThreadNew!"); printk(" - Waiting for thread B to join...\n"); status = osThreadJoin(tB); zassert_equal(status, osOK, "osThreadJoin thread B failed!"); if (status == osOK) { printk(" - Thread B joined.\n"); } milliseconds_spent = k_uptime_delta(&time_stamp); zassert_true((milliseconds_spent >= DELAY_MS - DELTA_MS) && (milliseconds_spent <= DELAY_MS + DELTA_MS), "Join completed but was too fast or too slow."); printk(" - Waiting for thread A to join...\n"); status = osThreadJoin(tA); zassert_equal(status, osOK, "osThreadJoin thread A failed!"); if (status == osOK) { printk(" - Thread A joined.\n"); } } void test_thread_detached(void) { osThreadId_t thread; osStatus_t status; thread = osThreadNew(thread5, NULL, NULL); /* osThreadDetached */ zassert_not_null(thread, "Failed to create thread with osThreadNew!"); osDelay(z_ms_to_ticks(DELAY_MS - DELTA_MS)); status = osThreadJoin(thread); zassert_equal(status, osErrorResource, "Incorrect status returned from osThreadJoin!"); osDelay(z_ms_to_ticks(DELTA_MS)); } void thread6(void *argument) { osThreadId_t thread = argument; osStatus_t status; status = osThreadJoin(thread); zassert_equal(status, osErrorResource, "Incorrect status returned from osThreadJoin!"); } void test_thread_joinable_detach(void) { osThreadAttr_t attr = { 0 }; osThreadId_t tA, tB; osStatus_t status; attr.attr_bits = osThreadJoinable; tA = osThreadNew(thread5, NULL, &attr); zassert_not_null(tA, "Failed to create thread with osThreadNew!"); tB = osThreadNew(thread6, tA, &attr); zassert_not_null(tB, "Failed to create thread with osThreadNew!"); osDelay(z_ms_to_ticks(DELAY_MS - DELTA_MS)); status = osThreadDetach(tA); zassert_equal(status, osOK, "osThreadDetach failed."); osDelay(z_ms_to_ticks(DELTA_MS)); } void test_thread_joinable_terminate(void) { osThreadAttr_t attr = { 0 }; osThreadId_t tA, tB; osStatus_t status; attr.attr_bits = osThreadJoinable; tA = osThreadNew(thread5, NULL, &attr); zassert_not_null(tA, "Failed to create thread with osThreadNew!"); tB = osThreadNew(thread6, tA, &attr); zassert_not_null(tB, "Failed to create thread with osThreadNew!"); osDelay(z_ms_to_ticks(DELAY_MS - DELTA_MS)); status = osThreadTerminate(tA); zassert_equal(status, osOK, "osThreadTerminate failed."); osDelay(z_ms_to_ticks(DELTA_MS)); } |