void test_priority_preemptible(void) { int old_prio = k_thread_priority_get(k_current_get()); /* set current thread to a non-negative priority */ last_prio = 2; k_thread_priority_set(k_current_get(), last_prio); int spawn_prio = last_prio - 1; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: thread is preempted by higher thread */ zassert_true(last_prio == spawn_prio, NULL); k_sleep(100); k_thread_abort(tid); spawn_prio = last_prio + 1; tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: thread is not preempted by lower thread */ zassert_false(last_prio == spawn_prio, NULL); k_thread_abort(tid); /* restore environment */ k_thread_priority_set(k_current_get(), old_prio); }
static void threads_suspend_resume(int prio) { int old_prio = k_thread_priority_get(k_current_get()); /* set current thread */ last_prio = prio; k_thread_priority_set(k_current_get(), last_prio); /* spawn thread with lower priority */ int spawn_prio = last_prio + 1; k_tid_t tid = k_thread_spawn(tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: suspend current thread */ k_thread_suspend(tid); k_sleep(100); /* checkpoint: spawned thread shouldn't be executed after suspend */ assert_false(last_prio == spawn_prio, NULL); k_thread_resume(tid); k_sleep(100); /* checkpoint: spawned thread should be executed after resume */ assert_true(last_prio == spawn_prio, NULL); k_thread_abort(tid); /* restore environment */ k_thread_priority_set(k_current_get(), old_prio); }
/*test cases*/ void test_priority_cooperative(void) { int old_prio = k_thread_priority_get(k_current_get()); /* set current thread to a negative priority */ last_prio = -1; k_thread_priority_set(k_current_get(), last_prio); /* spawn thread with higher priority */ int spawn_prio = last_prio - 1; k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, thread_entry, NULL, NULL, NULL, spawn_prio, 0, 0); /* checkpoint: current thread shouldn't preempted by higher thread */ zassert_true(last_prio == k_thread_priority_get(k_current_get()), NULL); k_sleep(100); /* checkpoint: spawned thread get executed */ zassert_true(last_prio == spawn_prio, NULL); k_thread_abort(tid); /* restore environment */ k_thread_priority_set(k_current_get(), old_prio); }
/*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); }
void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, size_t bytes_to_write, struct k_sem *sem) { struct k_pipe_async *async_desc; size_t dummy_bytes_written; /* For simplicity, always allocate an asynchronous descriptor */ _pipe_async_alloc(&async_desc); async_desc->desc.block = &async_desc->desc.copy_block; async_desc->desc.copy_block = *block; async_desc->desc.sem = sem; async_desc->thread.prio = k_thread_priority_get(_current); (void) _k_pipe_put_internal(pipe, async_desc, block->data, bytes_to_write, &dummy_bytes_written, bytes_to_write, K_FOREVER); }
static int shell_cmd_tasks(int argc, char *argv[]) { ARG_UNUSED(argc); ARG_UNUSED(argv); struct k_thread *thread_list = NULL; printk("tasks:\n"); thread_list = (struct k_thread *)SYS_THREAD_MONITOR_HEAD; while (thread_list != NULL) { printk("%s%p: options: 0x%x priority: %d\n", (thread_list == k_current_get()) ? "*" : " ", thread_list, thread_list->base.user_options, k_thread_priority_get(thread_list)); thread_list = (struct k_thread *)SYS_THREAD_MONITOR_NEXT(thread_list); } return 0; }
static void thread_entry(void *p1, void *p2, void *p3) { last_prio = k_thread_priority_get(k_current_get()); }