Zephyr Project API 4.5.0-rc2
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
147/* CPU records in coherent memory do not need padding to avoid false sharing. */
148#ifndef CONFIG_KERNEL_COHERENCE
149#define Z_DCACHE_ALIGN_SMP_CPU Z_DCACHE_ALIGN_SMP
150#else
151#define Z_DCACHE_ALIGN_SMP_CPU
152#endif
153
154struct _cpu {
155 /* nested interrupt count */
156 uint32_t nested;
157
158 /* interrupt stack pointer base */
159 char *irq_stack;
160
161 /* currently scheduled thread */
162 struct k_thread *current;
163
164 /* one assigned idle thread per CPU */
165 struct k_thread *idle_thread;
166
167#ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY
168 struct _ready_q ready_q;
169#endif
170
171#if (CONFIG_NUM_METAIRQ_PRIORITIES > 0)
172 /* Coop thread preempted by current metairq, or NULL */
173 struct k_thread *metairq_preempted;
174#endif
175
176 uint8_t id;
177
178#if defined(CONFIG_FPU_SHARING)
179 void *fp_ctx;
180#endif
181
182#ifdef CONFIG_SMP
183 /* True when _current is allowed to context switch */
184 uint8_t swap_ok;
185#endif
186
187#ifdef CONFIG_SCHED_THREAD_USAGE
188 /*
189 * [usage0] is used as a timestamp to mark the beginning of an
190 * execution window. [0] is a special value indicating that it
191 * has been stopped (but not disabled).
192 */
193
194 uint32_t usage0;
195
196#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
197 struct k_cycle_stats *usage;
198#endif
199#endif
200
201#ifdef CONFIG_OBJ_CORE_SYSTEM
202 struct k_obj_core obj_core;
203#endif
204
205#ifdef CONFIG_SCHED_IPI_SUPPORTED
206 sys_dlist_t ipi_workq;
207#endif
208
209 /* Per CPU architecture specifics */
210 struct _cpu_arch arch;
211} Z_DCACHE_ALIGN_SMP_CPU;
212
213typedef struct _cpu _cpu_t;
214
215struct z_kernel {
216 struct _cpu cpus[CONFIG_MP_MAX_NUM_CPUS];
217
218#ifdef CONFIG_PM
219 int32_t idle; /* Number of ticks for kernel idling */
220#endif
221
222 /*
223 * ready queue: can be big, keep after small fields, since some
224 * assembly (e.g. ARC) are limited in the encoding of the offset
225 */
226#ifndef CONFIG_SCHED_CPU_MASK_PIN_ONLY
227 struct _ready_q ready_q;
228#endif
229
230#if defined(CONFIG_THREAD_MONITOR)
231 struct k_thread *threads; /* singly linked list of ALL threads */
232#endif
233#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
234 struct k_cycle_stats usage[CONFIG_MP_MAX_NUM_CPUS];
235#endif
236
237#ifdef CONFIG_OBJ_CORE_SYSTEM
238 struct k_obj_core obj_core;
239#endif
240
241#if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED)
242 /* Identify CPUs to send IPIs to at the next scheduling point */
243 atomic_t pending_ipi;
244#endif
245
246#if defined(CONFIG_IPI_OPTIMIZE_IDLE)
247 /* Idle CPU reservations. sched_ipi_reserved marks valid entries in
248 * sched_ipi_target. Each entry represents logical coverage by an
249 * outstanding IPI, not a binding between that CPU and thread.
250 * Protected by _sched_spinlock.
251 */
252 uint32_t sched_ipi_reserved;
253 struct k_thread *sched_ipi_target[CONFIG_MP_MAX_NUM_CPUS];
254#endif
255};
256
257typedef struct z_kernel _kernel_t;
258
259extern struct z_kernel _kernel;
260
261extern atomic_t _cpus_active;
262
263#ifdef CONFIG_SMP
264
265/* True if the current context can be preempted and migrated to
266 * another SMP CPU.
267 */
268bool z_smp_cpu_mobile(void);
269#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
270 arch_curr_cpu(); })
271#else
272#define _current_cpu (&_kernel.cpus[0])
273#endif
274
275/* The current thread, in the per-CPU structure where the kernel records it.
276 *
277 * This is the definitive location: z_current_thread_set() always writes here
278 * and nothing caches it, so it reports the incoming thread as soon as that
279 * call has been made. Reading it requires a context that cannot migrate,
280 * which is what _current_cpu asserts: an ISR, or interrupts locked.
281 *
282 * Use _current instead unless the value must be up to date and the caller
283 * already meets that requirement.
284 */
285#define _raw_current (_current_cpu->current)
286
287#ifdef CONFIG_SMP
288__attribute_const__ struct k_thread *z_smp_current_get(void);
289#define _current z_smp_current_get()
290#else
291#define _current _raw_current
292#endif
293
294#define CPU_ID ((CONFIG_MP_MAX_NUM_CPUS == 1) ? 0 : _current_cpu->id)
295
296/* This is always invoked from a context where preemption is disabled.
297 *
298 * Callers must not assume that _current reflects the new thread before the
299 * switch actually happens. An architecture may cache the current thread in
300 * a register (CONFIG_ARCH_HAS_CUSTOM_CURRENT_IMPL), and the compiler is then
301 * free to reuse a value it read earlier in the same function. Code running
302 * between this call and the switch must therefore use the new thread pointer
303 * it already has, or _raw_current where it has none.
304 */
305#define z_current_thread_set(thread) ({ _raw_current = (thread); })
306
307#ifdef CONFIG_ARCH_HAS_CUSTOM_CURRENT_IMPL
308#undef _current
309#define _current arch_current_thread()
310#undef z_current_thread_set
311#define z_current_thread_set(thread) \
312 arch_current_thread_set(({ _raw_current = (thread); }))
313#endif
314
315/* kernel wait queue record */
316#ifdef CONFIG_WAITQ_SCALABLE
317
318typedef struct {
319 struct _priq_rb waitq;
320} _wait_q_t;
321
322/* defined in kernel/priority_queues.c */
323bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
324
325#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
326
327#else
328
329typedef struct {
330 sys_dlist_t waitq;
331} _wait_q_t;
332
333#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
334
335#endif /* CONFIG_WAITQ_SCALABLE */
336
337/* kernel timeout record */
338struct _timeout;
339typedef void (*_timeout_func_t)(struct _timeout *t);
340
341struct _timeout {
342 /*
343 * Backend-specific queue representation. The handler pointer (fn) is
344 * common to all backends and kept as the trailing member; everything
345 * above it is owned by the selected timeout backend (see
346 * kernel/include/timeout_q.h).
347 */
348#if defined(CONFIG_TIMEOUT_BACKEND_MINHEAP)
349 /*
350 * Min-heap backend: absolute expiry tick plus the heap position
351 * handle. heap_handle.idx == 0 means the timeout is not queued
352 * (idle, popped for announcing, or aborted).
353 */
354 int64_t abs_ticks;
355 struct min_heap_handle heap_handle;
356#elif defined(CONFIG_TIMEOUT_BACKEND_SKIPLIST)
357 /*
358 * Skip-list backend: absolute expiry tick plus a geometric-height
359 * tower of forward pointers. height == 0 means the timeout is not
360 * queued (idle, popped for announcing, or aborted).
361 */
362 int64_t abs_ticks;
363 uint8_t height;
364 struct _timeout *forward[CONFIG_TIMEOUT_SKIPLIST_MAX_LEVEL];
365#else
366 /*
367 * Delta-list, bucket, and timer-wheel backends: a list node plus
368 * dticks (a delta to the predecessor for the delta list; an encoded
369 * slot position for the wheel or bucket). The wheel adds a flags
370 * field recording which wheel tier the timeout currently occupies.
371 */
372 sys_dnode_t node;
373#if defined(CONFIG_TIMEOUT_BACKEND_WHEEL)
375#endif
376#ifdef CONFIG_TIMEOUT_64BIT
377 /* Can't use k_ticks_t for header dependency reasons */
378 int64_t dticks;
379#else
380 int32_t dticks;
381#endif
382#endif /* CONFIG_TIMEOUT_BACKEND_MINHEAP */
383 _timeout_func_t fn;
384};
385
386typedef void (*k_thread_timeslice_fn_t)(struct k_thread *thread, void *data);
387
388#ifdef __cplusplus
389}
390#endif
391
392#endif /* _ASMLANGUAGE */
393
394#endif /* ZEPHYR_INCLUDE_KERNEL_STRUCTS_H_ */
Header file for the doubly-linked list API.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:34
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition dlist.h:59
struct _dnode sys_dlist_t
Doubly-linked list structure.
Definition dlist.h:55
#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:386
#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:86
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:160
Thread Structure.
Definition thread.h:258
Balanced red/black tree node structure.
Definition rb.h:61
Balanced red/black tree structure.
Definition rb.h:94
Header file for the Atomic operations API.
Misc utilities.