/*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, 0, 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); }
static int test_timeout(void) { struct timeout_order *data; s32_t timeout; int rv; int i; /* test k_busy_wait() */ TC_PRINT("Testing k_busy_wait()\n"); timeout = 20; /* in ms */ k_thread_spawn(timeout_stacks[0], THREAD_STACKSIZE, test_busy_wait, (void *)(intptr_t) timeout, NULL, NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0); rv = k_sem_take(&reply_timeout, timeout * 2); if (rv) { TC_ERROR(" *** task timed out waiting for " "k_busy_wait()\n"); return TC_FAIL; } /* test k_sleep() */ TC_PRINT("Testing k_sleep()\n"); timeout = 50; k_thread_spawn(timeout_stacks[0], THREAD_STACKSIZE, test_thread_sleep, (void *)(intptr_t) timeout, NULL, NULL, K_PRIO_COOP(THREAD_PRIORITY), 0, 0); rv = k_sem_take(&reply_timeout, timeout * 2); if (rv) { TC_ERROR(" *** task timed out waiting for thread on " "k_sleep().\n"); return TC_FAIL; } /* test k_thread_spawn() without cancellation */ TC_PRINT("Testing k_thread_spawn() without cancellation\n"); for (i = 0; i < NUM_TIMEOUT_THREADS; i++) { k_thread_spawn(timeout_stacks[i], THREAD_STACKSIZE, delayed_thread, (void *)i, NULL, NULL, K_PRIO_COOP(5), 0, timeouts[i].timeout); } for (i = 0; i < NUM_TIMEOUT_THREADS; i++) { data = k_fifo_get(&timeout_order_fifo, 750); if (!data) { TC_ERROR (" *** timeout while waiting for delayed thread\n"); return TC_FAIL; } if (data->timeout_order != i) { TC_ERROR(" *** wrong delayed thread ran (got %d, " "expected %d)\n", data->timeout_order, i); return TC_FAIL; } TC_PRINT(" got thread (q order: %d, t/o: %d) as expected\n", data->q_order, data->timeout); } /* ensure no more thread fire */ data = k_fifo_get(&timeout_order_fifo, 750); if (data) { TC_ERROR(" *** got something unexpected in the fifo\n"); return TC_FAIL; } /* test k_thread_spawn() with cancellation */ TC_PRINT("Testing k_thread_spawn() with cancellations\n"); int cancellations[] = { 0, 3, 4, 6 }; int num_cancellations = ARRAY_SIZE(cancellations); int next_cancellation = 0; k_tid_t delayed_threads[NUM_TIMEOUT_THREADS]; for (i = 0; i < NUM_TIMEOUT_THREADS; i++) { k_tid_t id; id = k_thread_spawn(timeout_stacks[i], THREAD_STACKSIZE, delayed_thread, (void *)i, NULL, NULL, K_PRIO_COOP(5), 0, timeouts[i].timeout); delayed_threads[i] = id; } for (i = 0; i < NUM_TIMEOUT_THREADS; i++) { int j; if (i == cancellations[next_cancellation]) { TC_PRINT(" cancelling " "[q order: %d, t/o: %d, t/o order: %d]\n", timeouts[i].q_order, timeouts[i].timeout, i); for (j = 0; j < NUM_TIMEOUT_THREADS; j++) { if (timeouts[j].timeout_order == i) { break; } } if (j < NUM_TIMEOUT_THREADS) { k_thread_cancel(delayed_threads[j]); ++next_cancellation; continue; } } data = k_fifo_get(&timeout_order_fifo, 2750); if (!data) { TC_ERROR (" *** timeout while waiting for delayed thread\n"); return TC_FAIL; } if (data->timeout_order != i) { TC_ERROR(" *** wrong delayed thread ran (got %d, " "expected %d)\n", data->timeout_order, i); return TC_FAIL; } TC_PRINT(" got (q order: %d, t/o: %d, t/o order %d) " "as expected\n", data->q_order, data->timeout, data->timeout_order); } if (num_cancellations != next_cancellation) { TC_ERROR(" *** wrong number of cancellations (expected %d, " "got %d\n", num_cancellations, next_cancellation); return TC_FAIL; } /* ensure no more thread fire */ data = k_fifo_get(&timeout_order_fifo, 750); if (data) { TC_ERROR(" *** got something unexpected in the fifo\n"); return TC_FAIL; } return TC_PASS; }