Zephyr API Documentation  2.7.0-rc2
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
11#include <sys/mem_manage.h>
12#endif
13
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
121typedef struct _thread_base _thread_base_t;
122
123#if defined(CONFIG_THREAD_STACK_INFO)
124/* Contains the stack information of a thread */
125struct _thread_stack_info {
126 /* Stack start - Represents the start address of the thread-writable
127 * stack area.
128 */
129 uintptr_t start;
130
131 /* Thread writable stack buffer size. Represents the size of the actual
132 * buffer, starting from the 'start' member, that should be writable by
133 * the thread. This comprises of the thread stack area, any area reserved
134 * for local thread data storage, as well as any area left-out due to
135 * random adjustments applied to the initial thread stack pointer during
136 * thread initialization.
137 */
138 size_t size;
139
140 /* Adjustment value to the size member, removing any storage
141 * used for TLS or random stack base offsets. (start + size - delta)
142 * is the initial stack pointer for a thread. May be 0.
143 */
144 size_t delta;
145};
146
147typedef struct _thread_stack_info _thread_stack_info_t;
148#endif /* CONFIG_THREAD_STACK_INFO */
149
150#if defined(CONFIG_USERSPACE)
151struct _mem_domain_info {
153 sys_dnode_t mem_domain_q_node;
155 struct k_mem_domain *mem_domain;
156};
157
158#endif /* CONFIG_USERSPACE */
159
160#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
161struct _thread_userspace_local_data {
162#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS)
163 int errno_var;
164#endif
165};
166#endif
167
168#ifdef CONFIG_THREAD_RUNTIME_STATS
169struct k_thread_runtime_stats {
170 /* Thread execution cycles */
171#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS
172 timing_t execution_cycles;
173#else
174 uint64_t execution_cycles;
175#endif
176};
177
178typedef struct k_thread_runtime_stats k_thread_runtime_stats_t;
179
180struct _thread_runtime_stats {
181 /* Timestamp when last switched in */
182#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS
183 timing_t last_switched_in;
184#else
185 uint32_t last_switched_in;
186#endif
187
188 k_thread_runtime_stats_t stats;
189};
190#endif
191
192struct z_poller {
193 bool is_polling;
194 uint8_t mode;
195};
196
201struct k_thread {
202
203 struct _thread_base base;
204
206 struct _callee_saved callee_saved;
207
210
212 _wait_q_t join_queue;
213
214#if defined(CONFIG_POLL)
215 struct z_poller poller;
216#endif
217
218#if defined(CONFIG_EVENTS)
219 struct k_thread *next_event_link;
220
221 uint32_t events;
222 uint32_t event_options;
223#endif
224
225#if defined(CONFIG_THREAD_MONITOR)
227 struct __thread_entry entry;
228
231#endif
232
233#if defined(CONFIG_THREAD_NAME)
235 char name[CONFIG_THREAD_MAX_NAME_LEN];
236#endif
237
238#ifdef CONFIG_THREAD_CUSTOM_DATA
241#endif
242
243#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
244 struct _thread_userspace_local_data *userspace_local_data;
245#endif
246
247#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS)
248#ifndef CONFIG_USERSPACE
250 int errno_var;
251#endif
252#endif
253
254#if defined(CONFIG_THREAD_STACK_INFO)
256 struct _thread_stack_info stack_info;
257#endif /* CONFIG_THREAD_STACK_INFO */
258
259#if defined(CONFIG_USERSPACE)
261 struct _mem_domain_info mem_domain_info;
266#endif /* CONFIG_USERSPACE */
267
268
269#if defined(CONFIG_USE_SWITCH)
270 /* When using __switch() a few previously arch-specific items
271 * become part of the core OS
272 */
273
276
279#endif
282
283#if defined(CONFIG_THREAD_LOCAL_STORAGE)
284 /* Pointer to arch-specific TLS area */
285 uintptr_t tls;
286#endif /* CONFIG_THREAD_LOCAL_STORAGE */
287
288#ifdef CONFIG_THREAD_RUNTIME_STATS
290 struct _thread_runtime_stats rt_stats;
291#endif
292
293#ifdef CONFIG_DEMAND_PAGING_THREAD_STATS
295 struct k_mem_paging_stats_t paging_stats;
296#endif
297
299 struct _thread_arch arch;
300};
301
302typedef struct k_thread _thread_t;
303typedef struct k_thread *k_tid_t;
304
305#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
uint64_t timing_t
Definition: types.h:10
struct k_thread * k_tid_t
Definition: thread.h:303
__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: kernel.h:5088
Memory Domain.
Definition: mem_domain.h:80
Definition: mem_manage.h:83
Definition: thread.h:201
struct _thread_base base
Definition: thread.h:203
struct k_thread * next_thread
Definition: thread.h:230
struct _thread_arch arch
Definition: thread.h:299
void * init_data
Definition: thread.h:209
void * switch_handle
Definition: thread.h:278
struct k_heap * resource_pool
Definition: thread.h:281
k_thread_stack_t * stack_obj
Definition: thread.h:263
void * custom_data
Definition: thread.h:240
struct __thread_entry entry
Definition: thread.h:227
void * syscall_frame
Definition: thread.h:265
struct _thread_stack_info stack_info
Definition: thread.h:256
_wait_q_t join_queue
Definition: thread.h:212
struct _mem_domain_info mem_domain_info
Definition: thread.h:261
int swap_retval
Definition: thread.h:275
struct _callee_saved callee_saved
Definition: thread.h:206
Definition: rb.h:49