1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdint.h>
6 #include <k_api.h>
7 
8 /** @defgroup cpp_aos_thread
9  *  @ingroup cpp_aos_api
10  * @{
11  */
12 namespace AOS {
13 
14 #define Thread_WAIT_FOREVER 0xFFFFFFFFU
15 
16 /**
17  * @brief thread Class.
18  *
19  */
20 
21     class thread
22     {
23       public:
24 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
25 
26         /**
27          * This function will initialize a task
28          * @param[in]  name       the name of task, which shall be unique
29          * @param[in]  arg        the parameter of task enter function
30          * @param[in]  prio       the prio of task
31          * @param[in]  ticks      the time slice if there are same prio task
32          * @param[in]  stack_size the size of thread stack
33          * @param[in]  entry      the entry function of task
34          * @param[in]  autorun    the autorunning flag of task
35          * @return  the operation status, RHINO_SUCCESS is OK, others is error
36          */
37         kstat_t create(const name_t *name, void *arg, uint8_t prio,
38                        tick_t ticks, size_t stack_size, task_entry_t entry,
39                        uint8_t autorun);
40 
41         thread(const name_t *name, void *arg, uint8_t prio,
42                 tick_t ticks, size_t stack_size, task_entry_t entry, uint8_t autorun);
43 
44 
45         thread();
46 
47 
48 #if (RHINO_CONFIG_CPU_NUM > 1)
49 
50         /**
51          * This function will initialize a task in SMP
52          * @param[in]  name       the name of task, which shall be unique
53          * @param[in]  arg        the parameter of task enter function
54          * @param[in]  prio       the prio of task
55          * @param[in]  ticks      the time slice if there are same prio task
56          * @param[in]  stack_size the size of thread stack
57          * @param[in]  entry      the entry function of task
58          * @param[in]  autorun    the autorunning flag of task
59          * @param[in]  cpu_num    the cpu_num of task to run
60          * @return  the operation status, RHINO_SUCCESS is OK, others is error
61          */
62         kstat_t create_smp(const name_t *name, void *arg, uint8_t prio,
63                            tick_t ticks, size_t stack_size, task_entry_t entry,
64                            uint8_t cpu_num, uint8_t autorun);
65 
66 #endif
67 
68         /**
69          * This function will dynamic terminate current thread
70          * @param[in]  NULL
71          * @return  the operation status, RHINO_SUCCESS is OK, others is error
72          */
73         kstat_t terminate(void);
74         ~thread(void);
75 
76 #endif
77 
78         /**
79          * This function will start current thread
80          * @param[in]  NULL
81          * @return  the operation status, RHINO_SUCCESS is OK, others is error
82          */
83         kstat_t start(void);
84 
85         /**
86          * This function will stop current thread
87          * @param[in]  NULL
88          * @return  the operation status, RHINO_SUCCESS is OK, others is error
89          */
90         kstat_t stop(void);
91 
92         /**
93          * This function will cause a task to sleep for some millisec
94          * @param[in]  millisec the time t0 sleep
95          * @return  the operation status, RHINO_SUCCESS is OK, others is error
96          */
97         kstat_t sleep(uint32_t millisec);
98 
99         /**
100          * This function will yield current thread
101          * @param[in]  NULL
102          * @return  the operation status, RHINO_SUCCESS is OK, others is error
103          */
104         kstat_t yield(void);
105 
106         /**
107          * This function will return current thread
108          * @param[in]  NULL
109          * @return  the operation status, RHINO_SUCCESS is OK, others is error
110          */
111         ktask_t *self(void);
112 
113         /**
114          * This function will change the prio of task
115          * @param[in]   task     the task to be changed prio
116          * @param[in]   pri      the prio to be changed.
117          * @param[out]  old_pri  the old task prio to be filled with
118          * @return  the operation status, RHINO_SUCCESS is OK, others is error
119          */
120         kstat_t prio_change(uint8_t pri);
121 
122 #if (RHINO_CONFIG_CPU_NUM > 1)
123 
124         /**
125          * This function will bind task to cpu
126          * @param[in]  cpu_num    the cpu_num of task to run
127          * @return  the operation status, RHINO_SUCCESS is OK, others is error
128          */
129         kstat_t cpu_bind(uint8_t cpu_num);
130 
131         /**
132          * This function will bind task to cpu
133          * @param[in]  NULL
134          * @return  the operation status, RHINO_SUCCESS is OK, others is error
135          */
136         kstat_t cpu_unbind(void);
137 
138 #endif
139 
140       private:
141         /**
142          * @brief a Queue buffer
143          */
144         ktask_t *p_thread_def;
145     };
146 
147 }
148 /**
149  * @}
150  */
151