Zephyr Project API  3.1.0
A Scalable Open Source RTOS
thread.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_KERNEL_THREAD_H_
8#define ZEPHYR_INCLUDE_KERNEL_THREAD_H_
9
10#ifdef CONFIG_DEMAND_PAGING_THREAD_STATS
12#endif
13
14#include <zephyr/kernel/stats.h>
15
33#ifdef CONFIG_THREAD_MONITOR
34struct __thread_entry {
35 k_thread_entry_t pEntry;
36 void *parameter1;
37 void *parameter2;
38 void *parameter3;
39};
40#endif
41
42/* can be used for creating 'dummy' threads, e.g. for pending on objects */
43struct _thread_base {
44
45 /* this thread's entry in a ready/wait queue */
46 union {
47 sys_dnode_t qnode_dlist;
48 struct rbnode qnode_rb;
49 };
50
51 /* wait queue on which the thread is pended (needed only for
52 * trees, not dumb lists)
53 */
54 _wait_q_t *pended_on;
55
56 /* user facing 'thread options'; values defined in include/kernel.h */
57 uint8_t user_options;
58
59 /* thread state */
60 uint8_t thread_state;
61
62 /*
63 * scheduler lock count and thread priority
64 *
65 * These two fields control the preemptibility of a thread.
66 *
67 * When the scheduler is locked, sched_locked is decremented, which
68 * means that the scheduler is locked for values from 0xff to 0x01. A
69 * thread is coop if its prio is negative, thus 0x80 to 0xff when
70 * looked at the value as unsigned.
71 *
72 * By putting them end-to-end, this means that a thread is
73 * non-preemptible if the bundled value is greater than or equal to
74 * 0x0080.
75 */
76 union {
77 struct {
78#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
79 uint8_t sched_locked;
80 int8_t prio;
81#else /* LITTLE and PDP */
82 int8_t prio;
83 uint8_t sched_locked;
84#endif
85 };
86 uint16_t preempt;
87 };
88
89#ifdef CONFIG_SCHED_DEADLINE
90 int prio_deadline;
91#endif
92
93 uint32_t order_key;
94
95#ifdef CONFIG_SMP
96 /* True for the per-CPU idle threads */
97 uint8_t is_idle;
98
99 /* CPU index on which thread was last run */
100 uint8_t cpu;
101
102 /* Recursive count of irq_lock() calls */
103 uint8_t global_lock_count;
104
105#endif
106
107#ifdef CONFIG_SCHED_CPU_MASK
108 /* "May run on" bits for each CPU */
109 uint8_t cpu_mask;
110#endif
111
112 /* data returned by APIs */
113 void *swap_data;
114
115#ifdef CONFIG_SYS_CLOCK_EXISTS
116 /* this thread's entry in a timeout queue */
117 struct _timeout timeout;
118#endif
119
120#ifdef CONFIG_TIMESLICE_PER_THREAD
121 int32_t slice_ticks;
122 k_thread_timeslice_fn_t slice_expired;
123 void *slice_data;
124#endif
125
126#ifdef CONFIG_SCHED_THREAD_USAGE
127 struct k_cycle_stats usage; /* Track thread usage statistics */
128#endif
129};
130
131typedef struct _thread_base _thread_base_t;
132
133#if defined(CONFIG_THREAD_STACK_INFO)
134/* Contains the stack information of a thread */
135struct _thread_stack_info {
136 /* Stack start - Represents the start address of the thread-writable
137 * stack area.
138 */
139 uintptr_t start;
140
141 /* Thread writable stack buffer size. Represents the size of the actual
142 * buffer, starting from the 'start' member, that should be writable by
143 * the thread. This comprises of the thread stack area, any area reserved
144 * for local thread data storage, as well as any area left-out due to
145 * random adjustments applied to the initial thread stack pointer during
146 * thread initialization.
147 */
148 size_t size;
149
150 /* Adjustment value to the size member, removing any storage
151 * used for TLS or random stack base offsets. (start + size - delta)
152 * is the initial stack pointer for a thread. May be 0.
153 */
154 size_t delta;
155};
156
157typedef struct _thread_stack_info _thread_stack_info_t;
158#endif /* CONFIG_THREAD_STACK_INFO */
159
160#if defined(CONFIG_USERSPACE)
161struct _mem_domain_info {
163 sys_dnode_t mem_domain_q_node;
165 struct k_mem_domain *mem_domain;
166};
167
168#endif /* CONFIG_USERSPACE */
169
170#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
171struct _thread_userspace_local_data {
172#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO)
173 int errno_var;
174#endif
175};
176#endif
177
179#ifdef CONFIG_SCHED_THREAD_USAGE
180 uint64_t execution_cycles;
181 uint64_t total_cycles; /* total # of non-idle cycles */
182 /*
183 * In the context of thread statistics, [execution_cycles] is the same
184 * as the total # of non-idle cycles. In the context of CPU statistics,
185 * it refers to the sum of non-idle + idle cycles.
186 */
187#endif
188
189#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
190 /*
191 * For threads, the following fields refer to the time spent executing
192 * as bounded by when the thread was scheduled in and scheduled out.
193 * For CPUs, the same fields refer to the time spent executing
194 * non-idle threads as bounded by the idle thread(s).
195 */
196
197 uint64_t current_cycles; /* current # of non-idle cycles */
198 uint64_t peak_cycles; /* peak # of non-idle cycles */
199 uint64_t average_cycles; /* average # of non-idle cycles */
200#endif
201
202#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
203 /*
204 * This field is always zero for individual threads. It only comes
205 * into play when gathering statistics for the CPU. In that case it
206 * represents the total number of cycles spent idling.
207 */
208
209 uint64_t idle_cycles;
210#endif
211
212#if __cplusplus && !defined(CONFIG_SCHED_THREAD_USAGE) && \
213 !defined(CONFIG_SCHED_THREAD_USAGE_ANALYSIS) && !defined(CONFIG_SCHED_THREAD_USAGE_ALL)
214 /* If none of the above Kconfig values are defined, this struct will have a size 0 in C
215 * which is not allowed in C++ (it'll have a size 1). To prevent this, we add a 1 byte dummy
216 * variable when the struct would otherwise be empty.
217 */
218 uint8_t dummy;
219#endif
221
222struct z_poller {
223 bool is_polling;
224 uint8_t mode;
225};
226
231struct k_thread {
232
233 struct _thread_base base;
234
236 struct _callee_saved callee_saved;
237
240
242 _wait_q_t join_queue;
243
244#if defined(CONFIG_POLL)
245 struct z_poller poller;
246#endif
247
248#if defined(CONFIG_EVENTS)
249 struct k_thread *next_event_link;
250
251 uint32_t events;
252 uint32_t event_options;
253#endif
254
255#if defined(CONFIG_THREAD_MONITOR)
257 struct __thread_entry entry;
258
261#endif
262
263#if defined(CONFIG_THREAD_NAME)
265 char name[CONFIG_THREAD_MAX_NAME_LEN];
266#endif
267
268#ifdef CONFIG_THREAD_CUSTOM_DATA
271#endif
272
273#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
274 struct _thread_userspace_local_data *userspace_local_data;
275#endif
276
277#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO)
278#ifndef CONFIG_USERSPACE
280 int errno_var;
281#endif
282#endif
283
284#if defined(CONFIG_THREAD_STACK_INFO)
286 struct _thread_stack_info stack_info;
287#endif /* CONFIG_THREAD_STACK_INFO */
288
289#if defined(CONFIG_USERSPACE)
291 struct _mem_domain_info mem_domain_info;
296#endif /* CONFIG_USERSPACE */
297
298
299#if defined(CONFIG_USE_SWITCH)
300 /* When using __switch() a few previously arch-specific items
301 * become part of the core OS
302 */
303
306
309#endif
312
313#if defined(CONFIG_THREAD_LOCAL_STORAGE)
314 /* Pointer to arch-specific TLS area */
315 uintptr_t tls;
316#endif /* CONFIG_THREAD_LOCAL_STORAGE */
317
318#ifdef CONFIG_DEMAND_PAGING_THREAD_STATS
320 struct k_mem_paging_stats_t paging_stats;
321#endif
322
324 struct _thread_arch arch;
325};
326
327typedef struct k_thread _thread_t;
328typedef struct k_thread *k_tid_t;
329
330void z_init_cpu(int id);
331void z_sched_ipi(void);
332void z_smp_start_cpu(int id);
333
334#endif
struct z_thread_stack_element k_thread_stack_t
Typedef of struct z_thread_stack_element.
Definition: arch_interface.h:44
void(* k_thread_entry_t)(void *p1, void *p2, void *p3)
Thread entry point function type.
Definition: arch_interface.h:46
ZTEST_BMEM int timeout
Definition: main.c:31
struct _dnode sys_dnode_t
Definition: dlist.h:49
struct k_thread * k_tid_t
Definition: thread.h:328
struct k_thread_runtime_stats k_thread_runtime_stats_t
void(* k_thread_timeslice_fn_t)(struct k_thread *thread, void *data)
Definition: kernel_structs.h:255
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__INT32_TYPE__ int32_t
Definition: stdint.h:74
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:105
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
__INT8_TYPE__ int8_t
Definition: stdint.h:72
Definition: stats.h:17
Definition: kernel.h:5144
Memory Domain.
Definition: mem_domain.h:80
Definition: mem_manage.h:91
Definition: thread.h:178
Definition: thread.h:231
struct _thread_base base
Definition: thread.h:233
struct k_thread * next_thread
Definition: thread.h:260
struct _thread_arch arch
Definition: thread.h:324
void * init_data
Definition: thread.h:239
void * switch_handle
Definition: thread.h:308
struct k_heap * resource_pool
Definition: thread.h:311
k_thread_stack_t * stack_obj
Definition: thread.h:293
void * custom_data
Definition: thread.h:270
struct __thread_entry entry
Definition: thread.h:257
void * syscall_frame
Definition: thread.h:295
struct _thread_stack_info stack_info
Definition: thread.h:286
_wait_q_t join_queue
Definition: thread.h:242
struct _mem_domain_info mem_domain_info
Definition: thread.h:291
int swap_retval
Definition: thread.h:305
struct _callee_saved callee_saved
Definition: thread.h:236
Definition: rb.h:49