Zephyr API Documentation  3.0.0
A Scalable Open Source RTOS
3.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
11#include <sys/mem_manage.h>
12#endif
13
14#include <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_SCHED_THREAD_USAGE
121 struct k_cycle_stats usage; /* Track thread usage statistics */
122#endif
123};
124
125typedef struct _thread_base _thread_base_t;
126
127#if defined(CONFIG_THREAD_STACK_INFO)
128/* Contains the stack information of a thread */
129struct _thread_stack_info {
130 /* Stack start - Represents the start address of the thread-writable
131 * stack area.
132 */
133 uintptr_t start;
134
135 /* Thread writable stack buffer size. Represents the size of the actual
136 * buffer, starting from the 'start' member, that should be writable by
137 * the thread. This comprises of the thread stack area, any area reserved
138 * for local thread data storage, as well as any area left-out due to
139 * random adjustments applied to the initial thread stack pointer during
140 * thread initialization.
141 */
142 size_t size;
143
144 /* Adjustment value to the size member, removing any storage
145 * used for TLS or random stack base offsets. (start + size - delta)
146 * is the initial stack pointer for a thread. May be 0.
147 */
148 size_t delta;
149};
150
151typedef struct _thread_stack_info _thread_stack_info_t;
152#endif /* CONFIG_THREAD_STACK_INFO */
153
154#if defined(CONFIG_USERSPACE)
155struct _mem_domain_info {
157 sys_dnode_t mem_domain_q_node;
159 struct k_mem_domain *mem_domain;
160};
161
162#endif /* CONFIG_USERSPACE */
163
164#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
165struct _thread_userspace_local_data {
166#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS)
167 int errno_var;
168#endif
169};
170#endif
171
173#ifdef CONFIG_SCHED_THREAD_USAGE
174 uint64_t execution_cycles;
175 uint64_t total_cycles; /* total # of non-idle cycles */
176 /*
177 * In the context of thread statistics, [execution_cycles] is the same
178 * as the total # of non-idle cycles. In the context of CPU statistics,
179 * it refers to the sum of non-idle + idle cycles.
180 */
181#endif
182
183#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
184 /*
185 * For threads, the following fields refer to the time spent executing
186 * as bounded by when the thread was scheduled in and scheduled out.
187 * For CPUs, the same fields refer to the time spent executing
188 * non-idle threads as bounded by the idle thread(s).
189 */
190
191 uint64_t current_cycles; /* current # of non-idle cycles */
192 uint64_t peak_cycles; /* peak # of non-idle cycles */
193 uint64_t average_cycles; /* average # of non-idle cycles */
194#endif
195
196#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
197 /*
198 * This field is always zero for individual threads. It only comes
199 * into play when gathering statistics for the CPU. In that case it
200 * represents the total number of cycles spent idling.
201 */
202
203 uint64_t idle_cycles;
204#endif
206
207struct z_poller {
208 bool is_polling;
209 uint8_t mode;
210};
211
216struct k_thread {
217
218 struct _thread_base base;
219
221 struct _callee_saved callee_saved;
222
225
227 _wait_q_t join_queue;
228
229#if defined(CONFIG_POLL)
230 struct z_poller poller;
231#endif
232
233#if defined(CONFIG_EVENTS)
234 struct k_thread *next_event_link;
235
236 uint32_t events;
237 uint32_t event_options;
238#endif
239
240#if defined(CONFIG_THREAD_MONITOR)
242 struct __thread_entry entry;
243
246#endif
247
248#if defined(CONFIG_THREAD_NAME)
250 char name[CONFIG_THREAD_MAX_NAME_LEN];
251#endif
252
253#ifdef CONFIG_THREAD_CUSTOM_DATA
256#endif
257
258#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
259 struct _thread_userspace_local_data *userspace_local_data;
260#endif
261
262#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS)
263#ifndef CONFIG_USERSPACE
265 int errno_var;
266#endif
267#endif
268
269#if defined(CONFIG_THREAD_STACK_INFO)
271 struct _thread_stack_info stack_info;
272#endif /* CONFIG_THREAD_STACK_INFO */
273
274#if defined(CONFIG_USERSPACE)
276 struct _mem_domain_info mem_domain_info;
281#endif /* CONFIG_USERSPACE */
282
283
284#if defined(CONFIG_USE_SWITCH)
285 /* When using __switch() a few previously arch-specific items
286 * become part of the core OS
287 */
288
291
294#endif
297
298#if defined(CONFIG_THREAD_LOCAL_STORAGE)
299 /* Pointer to arch-specific TLS area */
300 uintptr_t tls;
301#endif /* CONFIG_THREAD_LOCAL_STORAGE */
302
303#ifdef CONFIG_DEMAND_PAGING_THREAD_STATS
305 struct k_mem_paging_stats_t paging_stats;
306#endif
307
309 struct _thread_arch arch;
310};
311
312typedef struct k_thread _thread_t;
313typedef struct k_thread *k_tid_t;
314
315#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:313
struct k_thread_runtime_stats k_thread_runtime_stats_t
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:75
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
__INT8_TYPE__ int8_t
Definition: stdint.h:42
Definition: stats.h:17
Definition: kernel.h:5078
Memory Domain.
Definition: mem_domain.h:80
Definition: mem_manage.h:83
Definition: thread.h:172
Definition: thread.h:216
struct _thread_base base
Definition: thread.h:218
struct k_thread * next_thread
Definition: thread.h:245
struct _thread_arch arch
Definition: thread.h:309
void * init_data
Definition: thread.h:224
void * switch_handle
Definition: thread.h:293
struct k_heap * resource_pool
Definition: thread.h:296
k_thread_stack_t * stack_obj
Definition: thread.h:278
void * custom_data
Definition: thread.h:255
struct __thread_entry entry
Definition: thread.h:242
void * syscall_frame
Definition: thread.h:280
struct _thread_stack_info stack_info
Definition: thread.h:271
_wait_q_t join_queue
Definition: thread.h:227
struct _mem_domain_info mem_domain_info
Definition: thread.h:276
int swap_retval
Definition: thread.h:290
struct _callee_saved callee_saved
Definition: thread.h:221
Definition: rb.h:49