1 /*
2  * Copyright (c) 2013-2015 Wind River Systems, Inc.
3  * Copyright (c) 2016-2020 Intel Corporation.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file
10  * @brief Measure time
11  *
12  */
13 #include <zephyr/kernel.h>
14 #include <ksched.h>
15 
16 #include "footprint.h"
17 
18 #define STACK_SIZE	512
19 
20 K_SEM_DEFINE(yield_sem, 0, 1);
21 
test_thread_entry(void * p,void * p1,void * p2)22 void test_thread_entry(void *p, void *p1, void *p2)
23 {
24 	static int i;
25 
26 	i++;
27 }
28 
thread_swap(void * p1,void * p2,void * p3)29 void thread_swap(void *p1, void *p2, void *p3)
30 {
31 	k_thread_abort(arch_current_thread());
32 }
33 
thread_suspend(void * p1,void * p2,void * p3)34 void thread_suspend(void *p1, void *p2, void *p3)
35 {
36 	k_thread_suspend(arch_current_thread());
37 }
38 
thread_yield0(void * p1,void * p2,void * p3)39 void thread_yield0(void *p1, void *p2, void *p3)
40 {
41 	uint32_t count = 0;
42 
43 	k_sem_take(&yield_sem, K_MSEC(10));
44 
45 	while (count != 1000U) {
46 		count++;
47 		k_yield();
48 	}
49 }
50 
thread_yield1(void * p1,void * p2,void * p3)51 void thread_yield1(void *p1, void *p2, void *p3)
52 {
53 	k_sem_give(&yield_sem);
54 	while (1) {
55 		k_yield();
56 	}
57 }
58 
run_thread_system(void)59 void run_thread_system(void)
60 {
61 	int prio;
62 	k_tid_t yield0_tid;
63 	k_tid_t yield1_tid;
64 
65 	k_tid_t my_tid = k_thread_create(&my_thread, my_stack_area,
66 					 STACK_SIZE,
67 					 thread_swap,
68 					 NULL, NULL, NULL,
69 					 5 /*priority*/, 0, K_FOREVER);
70 
71 	k_thread_priority_set(my_tid, 5);
72 	prio = k_thread_priority_get(my_tid);
73 	if (prio != 5) {
74 		printk("thread priority is not set to 5!\n");
75 	}
76 
77 	k_thread_start(my_tid);
78 
79 	k_thread_abort(my_tid);
80 
81 	k_tid_t sus_res_tid = k_thread_create(&my_thread, my_stack_area,
82 					      STACK_SIZE,
83 					      thread_suspend,
84 					      NULL, NULL, NULL,
85 					      -1 /*priority*/, 0, K_NO_WAIT);
86 
87 	k_thread_resume(sus_res_tid);
88 
89 	k_thread_join(sus_res_tid, K_FOREVER);
90 
91 	k_sleep(K_MSEC(10));
92 
93 	yield0_tid = k_thread_create(&my_thread, my_stack_area,
94 				     STACK_SIZE,
95 				     thread_yield0,
96 				     NULL, NULL, NULL,
97 				     0 /*priority*/, 0, K_NO_WAIT);
98 
99 	yield1_tid = k_thread_create(&my_thread_0, my_stack_area_0,
100 				     STACK_SIZE,
101 				     thread_yield1,
102 				     NULL, NULL, NULL,
103 				     0 /*priority*/, 0, K_NO_WAIT);
104 
105 	k_sleep(K_MSEC(1000));
106 
107 	k_thread_abort(yield0_tid);
108 	k_thread_abort(yield1_tid);
109 }
110