1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4 #include "cpp_thread.h"
5
6 using namespace AOS;
7
8 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
9
10 /**
11 * This function will initialize a task
12 * @param[in] name the name of task, which shall be unique
13 * @param[in] arg the parameter of task enter function
14 * @param[in] prio the prio of task
15 * @param[in] ticks the time slice if there are same prio task
16 * @param[in] stack_size the size of thread stack
17 * @param[in] entry the entry function of task
18 * @param[in] autorun the autorunning flag of task
19 * @return the operation status, RHINO_SUCCESS is OK, others is error
20 */
create(const name_t * name,void * arg,uint8_t prio,tick_t ticks,size_t stack_size,task_entry_t entry,uint8_t autorun)21 kstat_t thread::create(const name_t *name,
22 void *arg,
23 uint8_t prio,
24 tick_t ticks,
25 size_t stack_size,
26 task_entry_t entry,
27 uint8_t autorun)
28 {
29 return krhino_task_dyn_create(&p_thread_def,
30 name,
31 arg,
32 prio,
33 ticks,
34 (stack_size/4),
35 entry,
36 autorun);
37 }
38
thread(const name_t * name,void * arg,uint8_t prio,tick_t ticks,size_t stack_size,task_entry_t entry,uint8_t autorun)39 thread::thread(const name_t *name,
40 void *arg,
41 uint8_t prio,
42 tick_t ticks,
43 size_t stack_size,
44 task_entry_t entry,
45 uint8_t autorun)
46 {
47 krhino_task_dyn_create(&p_thread_def, name, arg, prio,
48 ticks, (stack_size/4), entry, autorun);
49 }
50
51
thread()52 thread::thread()
53 {
54 }
55
56
57 #if (RHINO_CONFIG_CPU_NUM > 1)
58
59 /**
60 * This function will initialize a task in SMP
61 * @param[in] name the name of task, which shall be unique
62 * @param[in] arg the parameter of task enter function
63 * @param[in] prio the prio of task
64 * @param[in] ticks the time slice if there are same prio task
65 * @param[in] stack_size the size of thread stack
66 * @param[in] entry the entry function of task
67 * @param[in] autorun the autorunning flag of task
68 * @param[in] cpu_num the cpu_num of task to run
69 * @return the operation status, RHINO_SUCCESS is OK, others is error
70 */
create_smp(const name_t * name,void * arg,uint8_t prio,tick_t ticks,size_t stack_size,task_entry_t entry,uint8_t cpu_num,uint8_t autorun)71 kstat_t thread::create_smp(const name_t *name,
72 void *arg,
73 uint8_t prio,
74 tick_t ticks,
75 size_t stack_size,
76 task_entry_t entry,
77 uint8_t cpu_num,
78 uint8_t autorun)
79 {
80 return krhino_task_cpu_dyn_create(&p_thread_def,
81 name,
82 arg,
83 prio,
84 ticks,
85 (stack_size/4),
86 entry,
87 cpu_num,
88 autorun);
89 }
90
91 #endif
92
93 /**
94 * This function will dynamic terminate current thread
95 * @param[in] NULL
96 * @return the operation status, RHINO_SUCCESS is OK, others is error
97 */
terminate(void)98 kstat_t thread::terminate(void)
99 {
100 return krhino_task_dyn_del(p_thread_def);
101 }
102
~thread(void)103 thread::~thread(void)
104 {
105 krhino_task_dyn_del(p_thread_def);
106 }
107
108 #endif
109
110 /**
111 * This function will start current thread
112 * @param[in] NULL
113 * @return the operation status, RHINO_SUCCESS is OK, others is error
114 */
start(void)115 kstat_t thread::start(void)
116 {
117 return krhino_task_resume(p_thread_def);
118 }
119
120 /**
121 * This function will stop current thread
122 * @param[in] NULL
123 * @return the operation status, RHINO_SUCCESS is OK, others is error
124 */
stop(void)125 kstat_t thread::stop(void)
126 {
127 return krhino_task_suspend(p_thread_def);
128 }
129
130 /**
131 * This function will cause a task to sleep for some millisec
132 * @param[in] millisec the time t0 sleep
133 * @return the operation status, RHINO_SUCCESS is OK, others is error
134 */
sleep(uint32_t millisec)135 kstat_t thread::sleep(uint32_t millisec)
136 {
137 tick_t ticks = 0;
138
139 if (millisec == 0) {
140 ticks = RHINO_NO_WAIT;
141 } else if (millisec == Thread_WAIT_FOREVER){
142 ticks = RHINO_WAIT_FOREVER;
143 } else {
144 ticks = krhino_ms_to_ticks(millisec);
145 }
146
147 return krhino_task_sleep(ticks ? ticks : 1); /* Minimum delay = 1 tick */
148 }
149
150 /**
151 * This function will yield current thread
152 * @param[in] NULL
153 * @return the operation status, RHINO_SUCCESS is OK, others is error
154 */
yield(void)155 kstat_t thread::yield(void)
156 {
157 return krhino_task_yield();
158 }
159
160 /**
161 * This function will return current thread
162 * @param[in] NULL
163 * @return the operation status, RHINO_SUCCESS is OK, others is error
164 */
self(void)165 ktask_t *thread::self(void)
166 {
167 return krhino_cur_task_get();
168 }
169
170 /**
171 * This function will change the prio of task
172 * @param[in] task the task to be changed prio
173 * @param[in] pri the prio to be changed.
174 * @param[out] old_pri the old task prio to be filled with
175 * @return the operation status, RHINO_SUCCESS is OK, others is error
176 */
prio_change(uint8_t pri)177 kstat_t thread::prio_change(uint8_t pri)
178 {
179 uint8_t old_pri = 0;
180
181 return krhino_task_pri_change(p_thread_def, pri, &old_pri);
182 }
183
184 #if (RHINO_CONFIG_CPU_NUM > 1)
185
186 /**
187 * This function will bind task to cpu
188 * @param[in] cpu_num the cpu_num of task to run
189 * @return the operation status, RHINO_SUCCESS is OK, others is error
190 */
cpu_bind(uint8_t cpu_num)191 kstat_t thread::cpu_bind(uint8_t cpu_num)
192 {
193 return krhino_task_cpu_bind(p_thread_def, cpu_num);
194 }
195
196 /**
197 * This function will bind task to cpu
198 * @param[in] NULL
199 * @return the operation status, RHINO_SUCCESS is OK, others is error
200 */
cpu_unbind(void)201 kstat_t thread::cpu_unbind(void)
202 {
203 return krhino_task_cpu_unbind(p_thread_def);
204 }
205
206 #endif
207