Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
kernel_structs.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/*
8 * The purpose of this file is to provide essential/minimal kernel structure
9 * definitions, so that they can be used without including kernel.h.
10 *
11 * The following rules must be observed:
12 * 1. kernel_structs.h shall not depend on kernel.h both directly and
13 * indirectly (i.e. it shall not include any header files that include
14 * kernel.h in their dependency chain).
15 * 2. kernel.h shall imply kernel_structs.h, such that it shall not be
16 * necessary to include kernel_structs.h explicitly when kernel.h is
17 * included.
18 */
19
20#ifndef ZEPHYR_INCLUDE_KERNEL_STRUCTS_H_
21#define ZEPHYR_INCLUDE_KERNEL_STRUCTS_H_
22
23#if !defined(_ASMLANGUAGE)
24#include <zephyr/sys/atomic.h>
25#include <zephyr/types.h>
26#include <zephyr/sys/dlist.h>
27#include <zephyr/sys/util.h>
28#include <zephyr/sys/sys_heap.h>
29#include <zephyr/arch/structs.h>
30#include <zephyr/kernel/stats.h>
32#include <zephyr/sys/rb.h>
33#if defined(CONFIG_TIMEOUT_BACKEND_MINHEAP)
35#endif
36#endif
37
38#define K_NUM_THREAD_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + CONFIG_NUM_COOP_PRIORITIES + 1)
39#define PRIQ_BITMAP_SIZE (DIV_ROUND_UP(K_NUM_THREAD_PRIO, BITS_PER_LONG))
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/*
46 * Bitmask definitions for the struct k_thread.thread_state field.
47 *
48 * Must be before kernel_arch_data.h because it might need them to be already
49 * defined.
50 */
51
52/* states: common uses low bits, arch-specific use high bits */
53
54/* Not a real thread */
55#define _THREAD_DUMMY (BIT(0))
56
57/* Thread is waiting on an object */
58#define _THREAD_PENDING (BIT(1))
59
60/* Thread is sleeping */
61#define _THREAD_SLEEPING (BIT(2))
62
63/* Thread has terminated */
64#define _THREAD_DEAD (BIT(3))
65
66/* Thread is suspended */
67#define _THREAD_SUSPENDED (BIT(4))
68
69/* Thread is in the process of aborting */
70#define _THREAD_ABORTING (BIT(5))
71
72/* Thread is in the process of suspending */
73#define _THREAD_SUSPENDING (BIT(6))
74
75/* Thread is present in the ready queue */
76#define _THREAD_QUEUED (BIT(7))
77
78/* end - states */
79
80#ifdef CONFIG_STACK_SENTINEL
81/* Magic value in lowest bytes of the stack */
82#define STACK_SENTINEL 0xF0F0F0F0
83#endif
84
85/* lowest value of _thread_base.preempt at which a thread is non-preemptible */
86#define _NON_PREEMPT_THRESHOLD 0x0080U
87
88/* highest value of _thread_base.preempt at which a thread is preemptible */
89#define _PREEMPT_THRESHOLD (_NON_PREEMPT_THRESHOLD - 1U)
90
91#if !defined(_ASMLANGUAGE)
92
93/* There are three abstractions defined for "thread priority queues".
94 *
95 * The first is a simple doubly linked list (sys_dlist_t) appropriate for
96 * systems with small numbers of threads and sensitive to code size. It is
97 * stored in sorted order, taking an O(N) cost every time a thread is added
98 * to the list. This corresponds to the way the original _wait_q_t abstraction
99 * worked and is very fast as long as the number of threads is small.
100 *
101 * The second is a scalable balanced tree. It has a rather larger code size
102 * (due to the data structure itself, the code here is just stubs) and higher
103 * constant-factor performance overhead, with O(logN) scaling in the presence
104 * of large number of threads.
105 *
106 * The third is a traditional/textbook "multi-queue". It has separate lists
107 * for each priority. This corresponds to the original Zephyr scheduler. RAM
108 * requirements are comparatively high, but performance is very fast. It won't
109 * work with features like deadline scheduling which need large priority spaces
110 * to represent their requirements.
111 *
112 * Either the simple or balanced tree abstractions may be used for the wait_q.
113 * Any of the three may be used for the system ready queue. The choices are
114 * configurable at build time.
115 */
116
117struct _priq_rb {
118 struct rbtree tree;
119 int next_order_key;
120};
121
122struct _priq_mq {
124 unsigned long bitmask[PRIQ_BITMAP_SIZE];
125#ifndef CONFIG_SMP
126 unsigned int cached_queue_index;
127#endif
128};
129
130struct _ready_q {
131#ifndef CONFIG_SMP
132 /* always contains next thread to run: cannot be NULL */
133 struct k_thread *cache;
134#endif
135
136#if defined(CONFIG_SCHED_SIMPLE)
137 sys_dlist_t runq;
138#elif defined(CONFIG_SCHED_SCALABLE)
139 struct _priq_rb runq;
140#elif defined(CONFIG_SCHED_MULTIQ)
141 struct _priq_mq runq;
142#endif
143};
144
145typedef struct _ready_q _ready_q_t;
146
147struct _cpu {
148 /* nested interrupt count */
149 uint32_t nested;
150
151 /* interrupt stack pointer base */
152 char *irq_stack;
153
154 /* currently scheduled thread */
155 struct k_thread *current;
156
157 /* one assigned idle thread per CPU */
158 struct k_thread *idle_thread;
159
160#ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY
161 struct _ready_q ready_q;
162#endif
163
164#if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
165 /* Coop thread preempted by current metairq, or NULL */
166 struct k_thread *metairq_preempted;
167#endif
168
169 uint8_t id;
170
171#if defined(CONFIG_FPU_SHARING)
172 void *fp_ctx;
173#endif
174
175#ifdef CONFIG_SMP
176 /* True when _current is allowed to context switch */
177 uint8_t swap_ok;
178#endif
179
180#ifdef CONFIG_SCHED_THREAD_USAGE
181 /*
182 * [usage0] is used as a timestamp to mark the beginning of an
183 * execution window. [0] is a special value indicating that it
184 * has been stopped (but not disabled).
185 */
186
187 uint32_t usage0;
188
189#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
190 struct k_cycle_stats *usage;
191#endif
192#endif
193
194#ifdef CONFIG_OBJ_CORE_SYSTEM
195 struct k_obj_core obj_core;
196#endif
197
198#ifdef CONFIG_SCHED_IPI_SUPPORTED
199 sys_dlist_t ipi_workq;
200#endif
201
202 /* Per CPU architecture specifics */
203 struct _cpu_arch arch;
204};
205
206typedef struct _cpu _cpu_t;
207
208struct z_kernel {
209 struct _cpu cpus[CONFIG_MP_MAX_NUM_CPUS];
210
211#ifdef CONFIG_PM
212 int32_t idle; /* Number of ticks for kernel idling */
213#endif
214
215 /*
216 * ready queue: can be big, keep after small fields, since some
217 * assembly (e.g. ARC) are limited in the encoding of the offset
218 */
219#ifndef CONFIG_SCHED_CPU_MASK_PIN_ONLY
220 struct _ready_q ready_q;
221#endif
222
223#if defined(CONFIG_THREAD_MONITOR)
224 struct k_thread *threads; /* singly linked list of ALL threads */
225#endif
226#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
227 struct k_cycle_stats usage[CONFIG_MP_MAX_NUM_CPUS];
228#endif
229
230#ifdef CONFIG_OBJ_CORE_SYSTEM
231 struct k_obj_core obj_core;
232#endif
233
234#if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED)
235 /* Identify CPUs to send IPIs to at the next scheduling point */
236 atomic_t pending_ipi;
237#endif
238};
239
240typedef struct z_kernel _kernel_t;
241
242extern struct z_kernel _kernel;
243
244extern atomic_t _cpus_active;
245
246#ifdef CONFIG_SMP
247
248/* True if the current context can be preempted and migrated to
249 * another SMP CPU.
250 */
251bool z_smp_cpu_mobile(void);
252#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
253 arch_curr_cpu(); })
254
255__attribute_const__ struct k_thread *z_smp_current_get(void);
256#define _current z_smp_current_get()
257
258#else
259#define _current_cpu (&_kernel.cpus[0])
260#define _current _kernel.cpus[0].current
261#endif
262
263#define CPU_ID ((CONFIG_MP_MAX_NUM_CPUS == 1) ? 0 : _current_cpu->id)
264
265/* This is always invoked from a context where preemption is disabled */
266#define z_current_thread_set(thread) ({ _current_cpu->current = (thread); })
267
268#ifdef CONFIG_ARCH_HAS_CUSTOM_CURRENT_IMPL
269#undef _current
270#define _current arch_current_thread()
271#undef z_current_thread_set
272#define z_current_thread_set(thread) \
273 arch_current_thread_set(({ _current_cpu->current = (thread); }))
274#endif
275
276/* kernel wait queue record */
277#ifdef CONFIG_WAITQ_SCALABLE
278
279typedef struct {
280 struct _priq_rb waitq;
281} _wait_q_t;
282
283/* defined in kernel/priority_queues.c */
284bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
285
286#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
287
288#else
289
290typedef struct {
291 sys_dlist_t waitq;
292} _wait_q_t;
293
294#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
295
296#endif /* CONFIG_WAITQ_SCALABLE */
297
298/* kernel timeout record */
299struct _timeout;
300typedef void (*_timeout_func_t)(struct _timeout *t);
301
302struct _timeout {
303 /*
304 * Backend-specific queue representation. The handler pointer (fn) is
305 * common to all backends and kept as the trailing member; everything
306 * above it is owned by the selected timeout backend (see
307 * kernel/include/timeout_q.h).
308 */
309#if defined(CONFIG_TIMEOUT_BACKEND_MINHEAP)
310 /*
311 * Min-heap backend: absolute expiry tick plus the heap position
312 * handle. heap_handle.idx == 0 means the timeout is not queued
313 * (idle, popped for announcing, or aborted).
314 */
315 int64_t abs_ticks;
316 struct min_heap_handle heap_handle;
317#else
318 /*
319 * Delta-list and timer-wheel backends: a list node plus dticks (a
320 * delta to the predecessor for the delta list; an encoded slot
321 * position for the wheel). The wheel adds a flags field recording
322 * which wheel tier the timeout currently occupies.
323 */
324 sys_dnode_t node;
325#if defined(CONFIG_TIMEOUT_BACKEND_WHEEL)
327#endif
328#ifdef CONFIG_TIMEOUT_64BIT
329 /* Can't use k_ticks_t for header dependency reasons */
330 int64_t dticks;
331#else
332 int32_t dticks;
333#endif
334#endif /* CONFIG_TIMEOUT_BACKEND_MINHEAP */
335 _timeout_func_t fn;
336};
337
338typedef void (*k_thread_timeslice_fn_t)(struct k_thread *thread, void *data);
339
340#ifdef __cplusplus
341}
342#endif
343
344#endif /* _ASMLANGUAGE */
345
346#endif /* ZEPHYR_INCLUDE_KERNEL_STRUCTS_H_ */
Header file for the Atomic operations API.
Header file for the doubly-linked list API.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition dlist.h:57
struct _dnode sys_dlist_t
Doubly-linked list structure.
Definition dlist.h:53
#define PRIQ_BITMAP_SIZE
Definition kernel_structs.h:39
void(* k_thread_timeslice_fn_t)(struct k_thread *thread, void *data)
Definition kernel_structs.h:338
#define K_NUM_THREAD_PRIO
Definition kernel_structs.h:38
Header-file for the reference (handle-based) min-heap data structure.
flags
Definition parser.h:97
Header file for the balanced red/black tree API.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__INT64_TYPE__ int64_t
Definition stdint.h:75
Structure used to track internal statistics about both thread and CPU usage.
Definition stats.h:18
Object core structure.
Definition obj_core.h:123
Thread Structure.
Definition thread.h:259
Balanced red/black tree node structure.
Definition rb.h:61
Balanced red/black tree structure.
Definition rb.h:94
Misc utilities.