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); }
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 thread_helper(void *arg1, void *arg2, void *arg3) { k_tid_t self_thread_id; ARG_UNUSED(arg1); ARG_UNUSED(arg2); ARG_UNUSED(arg3); /* * This thread starts off at a higher priority than thread_entry(). * Thus, it should execute immediately. */ thread_evidence++; /* Test that helper will yield to a thread of equal priority */ self_thread_id = k_current_get(); /* Lower priority to that of thread_entry() */ k_thread_priority_set(self_thread_id, self_thread_id->base.prio + 1); k_yield(); /* Yield to thread of equal priority */ thread_evidence++; /* <thread_evidence> should now be 2 */ }
void thread_sem0_give_test(void *p1, void *p2, void *p3) { k_sem_take(&sem_bench, 10);/* To sync threads */ /* test_time2 = OS_GET_TIME(); */ /* To make sure that the sem give will cause a swap to occur */ k_thread_priority_set(sem1_tid, 1); __read_swap_end_tsc_value = 1; sem_give_start_time = OS_GET_TIME(); k_sem_give(&sem_bench_1); }
/*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); }
static void tcoop_ctx(void *p1, void *p2, void *p3) { /** TESTPOINT: The thread's priority is in the cooperative range.*/ zassert_false(k_is_preempt_thread(), NULL); k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(1)); /** TESTPOINT: The thread's priority is in the preemptible range.*/ zassert_true(k_is_preempt_thread(), NULL); k_sched_lock(); /** TESTPOINT: The thread has locked the scheduler.*/ zassert_false(k_is_preempt_thread(), NULL); k_sched_unlock(); /** TESTPOINT: The thread has not locked the scheduler.*/ zassert_true(k_is_preempt_thread(), NULL); k_sem_give(&end_sema); }
/** * * @brief Test the k_yield() routine * * This routine tests the k_yield() routine. It starts another thread * (thus also testing k_thread_spawn() and checks that behaviour of * k_yield() against the cases of there being a higher priority thread, * a lower priority thread, and another thread of equal priority. * * On error, it may set <thread_detected_error> to one of the following values: * 10 - helper thread ran prematurely * 11 - k_yield() did not yield to a higher priority thread * 12 - k_yield() did not yield to an equal prioirty thread * 13 - k_yield() yielded to a lower priority thread * * @return TC_PASS on success * @return TC_FAIL on failure */ static int test_k_yield(void) { k_tid_t self_thread_id; /* * Start a thread of higher priority. Note that since the new thread is * being started from a thread, it will not automatically switch to the * thread as it would if done from a task. */ self_thread_id = k_current_get(); thread_evidence = 0; k_thread_spawn(thread_stack2, THREAD_STACKSIZE, thread_helper, NULL, NULL, NULL, K_PRIO_COOP(THREAD_PRIORITY - 1), 0, 0); if (thread_evidence != 0) { /* ERROR! Helper spawned at higher */ thread_detected_error = 10; /* priority ran prematurely. */ return TC_FAIL; } /* * Test that the thread will yield to the higher priority helper. * <thread_evidence> is still 0. */ k_yield(); if (thread_evidence == 0) { /* ERROR! Did not yield to higher */ thread_detected_error = 11; /* priority thread. */ return TC_FAIL; } if (thread_evidence > 1) { /* ERROR! Helper did not yield to */ thread_detected_error = 12; /* equal priority thread. */ return TC_FAIL; } /* * Raise the priority of thread_entry(). Calling k_yield() should * not result in switching to the helper. */ k_thread_priority_set(self_thread_id, self_thread_id->base.prio - 1); k_yield(); if (thread_evidence != 1) { /* ERROR! Context switched to a lower */ thread_detected_error = 13; /* priority thread! */ return TC_FAIL; } /* * Block on <sem_thread>. This will allow the helper thread to * complete. The main task will wake this thread. */ k_sem_take(&sem_thread, K_FOREVER); return TC_PASS; }
void run_tests(void) { k_thread_priority_set(k_current_get(), K_PRIO_COOP(7)); test_failed = false; struct net_conn_handle *handlers[CONFIG_NET_MAX_CONN]; struct net_if *iface = net_if_get_default(); struct net_if_addr *ifaddr; struct ud *ud; int ret, i = 0; bool st; struct sockaddr_in6 any_addr6; const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; struct sockaddr_in6 my_addr6; struct in6_addr in6addr_my = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; struct sockaddr_in6 peer_addr6; struct in6_addr in6addr_peer = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0x4e, 0x11, 0, 0, 0x2 } } }; struct sockaddr_in any_addr4; const struct in_addr in4addr_any = { { { 0 } } }; struct sockaddr_in my_addr4; struct in_addr in4addr_my = { { { 192, 0, 2, 1 } } }; struct sockaddr_in peer_addr4; struct in_addr in4addr_peer = { { { 192, 0, 2, 9 } } }; net_ipaddr_copy(&any_addr6.sin6_addr, &in6addr_any); any_addr6.sin6_family = AF_INET6; net_ipaddr_copy(&my_addr6.sin6_addr, &in6addr_my); my_addr6.sin6_family = AF_INET6; net_ipaddr_copy(&peer_addr6.sin6_addr, &in6addr_peer); peer_addr6.sin6_family = AF_INET6; net_ipaddr_copy(&any_addr4.sin_addr, &in4addr_any); any_addr4.sin_family = AF_INET; net_ipaddr_copy(&my_addr4.sin_addr, &in4addr_my); my_addr4.sin_family = AF_INET; net_ipaddr_copy(&peer_addr4.sin_addr, &in4addr_peer); peer_addr4.sin_family = AF_INET; k_sem_init(&recv_lock, 0, UINT_MAX); ifaddr = net_if_ipv6_addr_add(iface, &in6addr_my, NET_ADDR_MANUAL, 0); if (!ifaddr) { printk("Cannot add %s to interface %p\n", net_sprint_ipv6_addr(&in6addr_my), iface); zassert_true(0, "exiting"); } ifaddr = net_if_ipv4_addr_add(iface, &in4addr_my, NET_ADDR_MANUAL, 0); if (!ifaddr) { printk("Cannot add %s to interface %p\n", net_sprint_ipv4_addr(&in4addr_my), iface); zassert_true(0, "exiting"); } #define REGISTER(family, raddr, laddr, rport, lport) \ ({ \ static struct ud user_data; \ \ user_data.remote_addr = (struct sockaddr *)raddr; \ user_data.local_addr = (struct sockaddr *)laddr; \ user_data.remote_port = rport; \ user_data.local_port = lport; \ user_data.test = "DST="#raddr"-SRC="#laddr"-RP="#rport \ "-LP="#lport; \ \ set_port(family, (struct sockaddr *)raddr, \ (struct sockaddr *)laddr, rport, lport); \ \ ret = net_udp_register((struct sockaddr *)raddr, \ (struct sockaddr *)laddr, \ rport, lport, \ test_ok, &user_data, \ &handlers[i]); \ if (ret) { \ printk("UDP register %s failed (%d)\n", \ user_data.test, ret); \ zassert_true(0, "exiting"); \ } \ user_data.handle = handlers[i++]; \ &user_data; \ }) #define REGISTER_FAIL(raddr, laddr, rport, lport) \ ret = net_udp_register((struct sockaddr *)raddr, \ (struct sockaddr *)laddr, \ rport, lport, \ test_fail, INT_TO_POINTER(0), NULL); \ if (!ret) { \ printk("UDP register invalid match %s failed\n", \ "DST="#raddr"-SRC="#laddr"-RP="#rport"-LP="#lport); \ zassert_true(0, "exiting"); \ } #define UNREGISTER(ud) \ ret = net_udp_unregister(ud->handle); \ if (ret) { \ printk("UDP unregister %p failed (%d)\n", ud->handle, \ ret); \ zassert_true(0, "exiting"); \ } #define TEST_IPV6_OK(ud, raddr, laddr, rport, lport) \ st = send_ipv6_udp_msg(iface, raddr, laddr, rport, lport, ud, \ false); \ if (!st) { \ printk("%d: UDP test \"%s\" fail\n", __LINE__, \ ud->test); \ zassert_true(0, "exiting"); \ } #define TEST_IPV6_LONG_OK(ud, raddr, laddr, rport, lport) \ st = send_ipv6_udp_long_msg(iface, raddr, laddr, rport, lport, ud, \ false); \ if (!st) { \ printk("%d: UDP long test \"%s\" fail\n", __LINE__, \ ud->test); \ zassert_true(0, "exiting"); \ } #define TEST_IPV4_OK(ud, raddr, laddr, rport, lport) \ st = send_ipv4_udp_msg(iface, raddr, laddr, rport, lport, ud, \ false); \ if (!st) { \ printk("%d: UDP test \"%s\" fail\n", __LINE__, \ ud->test); \ zassert_true(0, "exiting"); \ } #define TEST_IPV6_FAIL(ud, raddr, laddr, rport, lport) \ st = send_ipv6_udp_msg(iface, raddr, laddr, rport, lport, ud, \ true); \ if (!st) { \ printk("%d: UDP neg test \"%s\" fail\n", __LINE__, \ ud->test); \ zassert_true(0, "exiting"); \ } #define TEST_IPV4_FAIL(ud, raddr, laddr, rport, lport) \ st = send_ipv4_udp_msg(iface, raddr, laddr, rport, lport, ud, \ true); \ if (!st) { \ printk("%d: UDP neg test \"%s\" fail\n", __LINE__, \ ud->test); \ zassert_true(0, "exiting"); \ } ud = REGISTER(AF_INET6, &any_addr6, &any_addr6, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400); UNREGISTER(ud); ud = REGISTER(AF_INET, &any_addr4, &any_addr4, 1234, 4242); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 4242); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 4242); TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 1234, 4325); TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 1234, 4325); UNREGISTER(ud); ud = REGISTER(AF_INET6, &any_addr6, NULL, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400); UNREGISTER(ud); ud = REGISTER(AF_INET6, NULL, &any_addr6, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 61400); UNREGISTER(ud); ud = REGISTER(AF_INET6, &peer_addr6, &my_addr6, 1234, 4242); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 4242); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 1234, 4243); ud = REGISTER(AF_INET, &peer_addr4, &my_addr4, 1234, 4242); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 4242); TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 1234, 4243); ud = REGISTER(AF_UNSPEC, NULL, NULL, 1234, 42423); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 42423); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 42423); ud = REGISTER(AF_UNSPEC, NULL, NULL, 1234, 0); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 42422); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 42422); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 1234, 42422); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 1234, 42422); TEST_IPV4_FAIL(ud, &in4addr_peer, &in4addr_my, 12345, 42421); TEST_IPV6_FAIL(ud, &in6addr_peer, &in6addr_my, 12345, 42421); ud = REGISTER(AF_UNSPEC, NULL, NULL, 0, 0); TEST_IPV4_OK(ud, &in4addr_peer, &in4addr_my, 12345, 42421); TEST_IPV6_OK(ud, &in6addr_peer, &in6addr_my, 12345, 42421); TEST_IPV6_LONG_OK(ud, &in6addr_peer, &in6addr_my, 12345, 42421); /* Remote addr same as local addr, these two will never match */ REGISTER(AF_INET6, &my_addr6, NULL, 1234, 4242); REGISTER(AF_INET, &my_addr4, NULL, 1234, 4242); /* IPv4 remote addr and IPv6 remote addr, impossible combination */ REGISTER_FAIL(&my_addr4, &my_addr6, 1234, 4242); /**TESTPOINT: Check if tests passed*/ zassert_false(fail, "Tests failed"); i--; while (i) { ret = net_udp_unregister(handlers[i]); if (ret < 0 && ret != -ENOENT) { printk("Cannot unregister udp %d\n", i); zassert_true(0, "exiting"); } i--; } zassert_true((net_udp_unregister(NULL) < 0), "Unregister udp failed"); zassert_false(test_failed, "udp tests failed"); }