Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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 /* CONFIG_DEMAND_PAGING_THREAD_STATS */
13
14#include <zephyr/kernel/stats.h>
16
33
34#ifdef CONFIG_THREAD_MONITOR
35struct __thread_entry {
36 k_thread_entry_t pEntry;
37 void *parameter1;
38 void *parameter2;
39 void *parameter3;
40};
41#endif /* CONFIG_THREAD_MONITOR */
42
43struct k_thread;
44
45/* can be used for creating 'dummy' threads, e.g. for pending on objects */
46struct _thread_base {
47
48 /* this thread's entry in a ready/wait queue */
49 union {
50 sys_dnode_t qnode_dlist;
51 struct rbnode qnode_rb;
52 };
53
54 /* wait queue on which the thread is pended (needed only for
55 * trees, not simple lists)
56 */
57 _wait_q_t *pended_on;
58
59 /* user facing 'thread options'; values defined in include/zephyr/kernel.h */
60 uint16_t user_options;
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#ifdef CONFIG_BIG_ENDIAN
79 uint8_t sched_locked;
80 int8_t prio;
81#else /* Little Endian */
82 int8_t prio;
83 uint8_t sched_locked;
84#endif /* CONFIG_BIG_ENDIAN */
85 };
86 uint16_t preempt;
87 };
88
89#ifdef CONFIG_SCHED_DEADLINE
90 int prio_deadline;
91#endif /* CONFIG_SCHED_DEADLINE */
92
93#if defined(CONFIG_SCHED_SCALABLE) || defined(CONFIG_WAITQ_SCALABLE)
94 uint32_t order_key;
95#endif
96
97 /* thread state */
98 uint8_t thread_state;
99
100#ifdef CONFIG_SMP
101 /* True for the per-CPU idle threads */
102 uint8_t is_idle;
103
104 /* Identify CPU on which thread is (or was last) executing */
105 uint8_t cpu;
106
107 /* Recursive count of irq_lock() calls */
108 uint8_t global_lock_count;
109
110#endif /* CONFIG_SMP */
111
112#ifdef CONFIG_SCHED_CPU_MASK
113 /* "May run on" bits for each CPU */
114 uint32_t cpu_mask;
115#endif /* CONFIG_SCHED_CPU_MASK */
116
117 /* data returned by APIs */
118 void *swap_data;
119
120#ifdef CONFIG_SYS_CLOCK_EXISTS
121 /* this thread's entry in a timeout queue */
122 struct _timeout timeout;
123#endif /* CONFIG_SYS_CLOCK_EXISTS */
124
125#ifdef CONFIG_TIMESLICE_PER_THREAD
126 int32_t slice_ticks;
127 k_thread_timeslice_fn_t slice_expired;
128 void *slice_data;
129#endif /* CONFIG_TIMESLICE_PER_THREAD */
130
131#ifdef CONFIG_SCHED_THREAD_USAGE
132 struct k_cycle_stats usage; /* Track thread usage statistics */
133#endif /* CONFIG_SCHED_THREAD_USAGE */
134};
135
136typedef struct _thread_base _thread_base_t;
137
138#if defined(CONFIG_THREAD_STACK_INFO)
139
140#if defined(CONFIG_THREAD_RUNTIME_STACK_SAFETY)
141struct _thread_stack_usage {
142 size_t unused_threshold; /* Threshold below which to trigger hook */
143};
144#endif
145
146/* Contains the stack information of a thread */
147struct _thread_stack_info {
148 /* Stack start - Represents the start address of the thread-writable
149 * stack area.
150 */
151 uintptr_t start;
152
153 /* Thread writable stack buffer size. Represents the size of the actual
154 * buffer, starting from the 'start' member, that should be writable by
155 * the thread. This comprises of the thread stack area, any area reserved
156 * for local thread data storage, as well as any area left-out due to
157 * random adjustments applied to the initial thread stack pointer during
158 * thread initialization.
159 */
160 size_t size;
161
162 /* Adjustment value to the size member, removing any storage
163 * used for TLS or random stack base offsets. (start + size - delta)
164 * is the initial stack pointer for a thread. May be 0.
165 */
166 size_t delta;
167
168#if defined(CONFIG_THREAD_STACK_MEM_MAPPED)
169 struct {
171 k_thread_stack_t *addr;
172
174 size_t sz;
175 } mapped;
176#endif /* CONFIG_THREAD_STACK_MEM_MAPPED */
177
178#if defined(CONFIG_THREAD_RUNTIME_STACK_SAFETY)
179 struct _thread_stack_usage usage;
180#endif
181};
182
183typedef struct _thread_stack_info _thread_stack_info_t;
184#endif /* CONFIG_THREAD_STACK_INFO */
185
186#if defined(CONFIG_USERSPACE)
187struct _mem_domain_info {
188#ifdef CONFIG_MEM_DOMAIN_HAS_THREAD_LIST
190 sys_dnode_t thread_mem_domain_node;
191#endif /* CONFIG_MEM_DOMAIN_HAS_THREAD_LIST */
193 struct k_mem_domain *mem_domain;
194};
195
196typedef struct _mem_domain_info _mem_domain_info_t;
197#endif /* CONFIG_USERSPACE */
198
199#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
200struct _thread_userspace_local_data {
201#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO)
202 int errno_var;
203#endif /* CONFIG_ERRNO && !CONFIG_ERRNO_IN_TLS && !CONFIG_LIBC_ERRNO */
204};
205#endif /* CONFIG_THREAD_USERSPACE_LOCAL_DATA */
206
208#ifdef CONFIG_SCHED_THREAD_USAGE
209 /*
210 * For CPU stats, execution_cycles is the sum of non-idle + idle cycles.
211 * For thread stats, execution_cycles = total_cycles.
212 */
213 uint64_t execution_cycles; /* total # of cycles (cpu: non-idle + idle) */
214 uint64_t total_cycles; /* total # of non-idle cycles */
215#endif /* CONFIG_SCHED_THREAD_USAGE */
216
217#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
218 /*
219 * For threads, the following fields refer to the time spent executing
220 * as bounded by when the thread was scheduled in and scheduled out.
221 * For CPUs, the same fields refer to the time spent executing
222 * non-idle threads as bounded by the idle thread(s).
223 */
224
225 uint64_t current_cycles; /* current # of non-idle cycles */
226 uint64_t peak_cycles; /* peak # of non-idle cycles */
227 uint64_t average_cycles; /* average # of non-idle cycles */
228#endif /* CONFIG_SCHED_THREAD_USAGE_ANALYSIS */
229
230#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
231 /*
232 * This field is always zero for individual threads. It only comes
233 * into play when gathering statistics for the CPU. In that case it
234 * represents the total number of cycles spent idling.
235 */
236
237 uint64_t idle_cycles;
238#endif /* CONFIG_SCHED_THREAD_USAGE_ALL */
239
240#if defined(__cplusplus) && !defined(CONFIG_SCHED_THREAD_USAGE) && \
241 !defined(CONFIG_SCHED_THREAD_USAGE_ANALYSIS) && !defined(CONFIG_SCHED_THREAD_USAGE_ALL)
242 /* An empty struct is not valid C, and compilers that accept it give
243 * it size 0 while C++ gives 1. Keep a byte so both languages agree.
244 */
245 uint8_t dummy;
246#endif
248
249struct z_poller {
250 bool is_polling;
251 uint8_t mode;
252};
253
258struct k_thread {
259
260 struct _thread_base base;
261
263 struct _callee_saved callee_saved;
264
267
269 _wait_q_t join_queue;
270
271#if defined(CONFIG_USERSPACE)
277#endif /* CONFIG_USERSPACE */
278
279#if defined(CONFIG_POLL)
280 struct z_poller poller;
281#endif /* CONFIG_POLL */
282
283#if defined(CONFIG_WAITQ_SCALABLE) && (defined(CONFIG_EVENTS) || defined(CONFIG_USERSPACE))
292 struct k_thread *next_wake_link;
293#endif /* CONFIG_WAITQ_SCALABLE && (CONFIG_EVENTS || CONFIG_USERSPACE) */
294
295#if defined(CONFIG_EVENTS)
296 uint32_t events; /* dual purpose - wait on and then received */
297 uint32_t event_options;
298#endif /* CONFIG_EVENTS */
299
300#if defined(CONFIG_THREAD_MONITOR)
302 struct __thread_entry entry;
303
306#endif /* CONFIG_THREAD_MONITOR */
307
308#if defined(CONFIG_THREAD_NAME)
310 char name[CONFIG_THREAD_MAX_NAME_LEN];
311#endif /* CONFIG_THREAD_NAME */
312
313#ifdef CONFIG_THREAD_CUSTOM_DATA
316#endif /* CONFIG_THREAD_CUSTOM_DATA */
317
318#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
319 struct _thread_userspace_local_data *userspace_local_data;
320#endif /* CONFIG_THREAD_USERSPACE_LOCAL_DATA */
321
322#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO)
323#ifndef CONFIG_USERSPACE
325 int errno_var;
326#endif /* CONFIG_USERSPACE */
327#endif /* CONFIG_ERRNO && !CONFIG_ERRNO_IN_TLS && !CONFIG_LIBC_ERRNO */
328
329#if defined(CONFIG_THREAD_STACK_INFO)
331 struct _thread_stack_info stack_info;
332#endif /* CONFIG_THREAD_STACK_INFO */
333
334#if defined(CONFIG_USERSPACE)
336 struct _mem_domain_info mem_domain_info;
337
345
348#endif /* CONFIG_USERSPACE */
349
350
351#if defined(CONFIG_USE_SWITCH)
352 /* When using __switch() a few previously arch-specific items
353 * become part of the core OS
354 */
355
358
361#endif /* CONFIG_USE_SWITCH */
364
365#if defined(CONFIG_THREAD_LOCAL_STORAGE)
366 /* Pointer to arch-specific TLS area */
367 uintptr_t tls;
368#endif /* CONFIG_THREAD_LOCAL_STORAGE */
369
370#ifdef CONFIG_DEMAND_PAGING_THREAD_STATS
372 struct k_mem_paging_stats_t paging_stats;
373#endif /* CONFIG_DEMAND_PAGING_THREAD_STATS */
374
375#ifdef CONFIG_OBJ_CORE_THREAD
376 struct k_obj_core obj_core;
377#endif /* CONFIG_OBJ_CORE_THREAD */
378
379#ifdef CONFIG_SMP
381 _wait_q_t halt_queue;
382#endif /* CONFIG_SMP */
383
384/*
385 * True when the priority-inheritance fields below and in struct k_mutex
386 * (kernel.h) are compiled in. Defined here rather than in kernel.h because
387 * this header is included first.
388 */
389#define Z_MUTEX_PI_ENABLED (CONFIG_PRIORITY_CEILING < CONFIG_NUM_PREEMPT_PRIORITIES)
390
391#if Z_MUTEX_PI_ENABLED
397 sys_slist_t held_mutexes;
398
404 struct k_mutex *mutex_pended_on;
406 int8_t orig_prio;
407#endif /* Z_MUTEX_PI_ENABLED */
408
410 struct _thread_arch arch;
411};
412
413typedef struct k_thread _thread_t;
414typedef struct k_thread *k_tid_t;
415
416#endif /* ZEPHYR_INCLUDE_KERNEL_THREAD_H_ */
void(* k_thread_entry_t)(void *p1, void *p2, void *p3)
Thread entry point function type.
Definition arch_interface.h:48
struct z_thread_stack_element k_thread_stack_t
Typedef of struct z_thread_stack_element.
Definition arch_interface.h:46
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition dlist.h:59
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:54
struct k_thread * k_tid_t
Definition thread.h:414
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:357
__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
Kernel synchronized heap structure.
Definition kernel.h:6304
Paging Statistics.
Definition demand_paging.h:37
Kernel mutex structure.
Definition kernel.h:3504
Object core structure.
Definition obj_core.h:123
Definition thread.h:207
Thread Structure.
Definition thread.h:258
struct _thread_base base
Definition thread.h:260
struct k_thread * next_thread
next item in list of all threads
Definition thread.h:305
struct _thread_arch arch
arch-specifics: must always be at the end
Definition thread.h:410
void * init_data
static thread init data
Definition thread.h:266
void * switch_handle
Context handle returned via arch_switch().
Definition thread.h:360
struct k_heap * resource_pool
resource pool
Definition thread.h:363
k_thread_stack_t * stack_obj
Base address of thread stack.
Definition thread.h:344
void * custom_data
crude thread-local storage
Definition thread.h:315
struct __thread_entry entry
thread entry and parameters description
Definition thread.h:302
void * syscall_frame
current syscall frame pointer
Definition thread.h:347
struct _thread_stack_info stack_info
Stack Info.
Definition thread.h:331
_wait_q_t join_queue
threads waiting in k_thread_join()
Definition thread.h:269
struct _mem_domain_info mem_domain_info
memory domain info of the thread
Definition thread.h:336
_wait_q_t halt_queue
threads waiting in k_thread_suspend()
Definition thread.h:381
void * futex_pointer
The futex this thread is waiting on.
Definition thread.h:276
int swap_retval
z_swap() return value
Definition thread.h:357
struct _callee_saved callee_saved
defined by the architecture, but all archs need these
Definition thread.h:263