Zephyr API Documentation  3.6.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_KERNEL_INCLUDE_KERNEL_STRUCTS_H_
21#define ZEPHYR_KERNEL_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#endif
34
35#define K_NUM_THREAD_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + CONFIG_NUM_COOP_PRIORITIES + 1)
36
37#if defined(CONFIG_64BIT)
38#define PRIQ_BITMAP_SIZE (DIV_ROUND_UP(K_NUM_THREAD_PRIO, 8 * sizeof(uint64_t)))
39#else
40#define PRIQ_BITMAP_SIZE (DIV_ROUND_UP(K_NUM_THREAD_PRIO, 8 * sizeof(uint32_t)))
41#endif
42
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/*
49 * Bitmask definitions for the struct k_thread.thread_state field.
50 *
51 * Must be before kernel_arch_data.h because it might need them to be already
52 * defined.
53 */
54
55/* states: common uses low bits, arch-specific use high bits */
56
57/* Not a real thread */
58#define _THREAD_DUMMY (BIT(0))
59
60/* Thread is waiting on an object */
61#define _THREAD_PENDING (BIT(1))
62
63/* Thread has not yet started */
64#define _THREAD_PRESTART (BIT(2))
65
66/* Thread has terminated */
67#define _THREAD_DEAD (BIT(3))
68
69/* Thread is suspended */
70#define _THREAD_SUSPENDED (BIT(4))
71
72/* Thread is in the process of aborting */
73#define _THREAD_ABORTING (BIT(5))
74
75/* Thread is in the process of suspending */
76#define _THREAD_SUSPENDING (BIT(6))
77
78/* Thread is present in the ready queue */
79#define _THREAD_QUEUED (BIT(7))
80
81/* end - states */
82
83#ifdef CONFIG_STACK_SENTINEL
84/* Magic value in lowest bytes of the stack */
85#define STACK_SENTINEL 0xF0F0F0F0
86#endif
87
88/* lowest value of _thread_base.preempt at which a thread is non-preemptible */
89#define _NON_PREEMPT_THRESHOLD 0x0080U
90
91/* highest value of _thread_base.preempt at which a thread is preemptible */
92#define _PREEMPT_THRESHOLD (_NON_PREEMPT_THRESHOLD - 1U)
93
94#if !defined(_ASMLANGUAGE)
95
96/* Two abstractions are defined here for "thread priority queues".
97 *
98 * One is a "dumb" list implementation appropriate for systems with
99 * small numbers of threads and sensitive to code size. It is stored
100 * in sorted order, taking an O(N) cost every time a thread is added
101 * to the list. This corresponds to the way the original _wait_q_t
102 * abstraction worked and is very fast as long as the number of
103 * threads is small.
104 *
105 * The other is a balanced tree "fast" implementation with rather
106 * larger code size (due to the data structure itself, the code here
107 * is just stubs) and higher constant-factor performance overhead, but
108 * much better O(logN) scaling in the presence of large number of
109 * threads.
110 *
111 * Each can be used for either the wait_q or system ready queue,
112 * configurable at build time.
113 */
114
115struct _priq_rb {
116 struct rbtree tree;
117 int next_order_key;
118};
119
120
121/* Traditional/textbook "multi-queue" structure. Separate lists for a
122 * small number (max 32 here) of fixed priorities. This corresponds
123 * to the original Zephyr scheduler. RAM requirements are
124 * comparatively high, but performance is very fast. Won't work with
125 * features like deadline scheduling which need large priority spaces
126 * to represent their requirements.
127 */
128struct _priq_mq {
130#ifdef CONFIG_64BIT
131 uint64_t bitmask[PRIQ_BITMAP_SIZE];
132#else
133 uint32_t bitmask[PRIQ_BITMAP_SIZE];
134#endif
135};
136
137struct _ready_q {
138#ifndef CONFIG_SMP
139 /* always contains next thread to run: cannot be NULL */
140 struct k_thread *cache;
141#endif
142
143#if defined(CONFIG_SCHED_DUMB)
144 sys_dlist_t runq;
145#elif defined(CONFIG_SCHED_SCALABLE)
146 struct _priq_rb runq;
147#elif defined(CONFIG_SCHED_MULTIQ)
148 struct _priq_mq runq;
149#endif
150};
151
152typedef struct _ready_q _ready_q_t;
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 (CONFIG_NUM_COOP_PRIORITIES > CONFIG_NUM_METAIRQ_PRIORITIES)
173 /* Coop thread preempted by current metairq, or NULL */
174 struct k_thread *metairq_preempted;
175#endif
176
177 uint8_t id;
178
179#if defined(CONFIG_FPU_SHARING)
180 void *fp_ctx;
181#endif
182
183#ifdef CONFIG_SMP
184 /* True when _current is allowed to context switch */
185 uint8_t swap_ok;
186#endif
187
188#ifdef CONFIG_SCHED_THREAD_USAGE
189 /*
190 * [usage0] is used as a timestamp to mark the beginning of an
191 * execution window. [0] is a special value indicating that it
192 * has been stopped (but not disabled).
193 */
194
195 uint32_t usage0;
196
197#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
198 struct k_cycle_stats *usage;
199#endif
200#endif
201
202#ifdef CONFIG_OBJ_CORE_SYSTEM
203 struct k_obj_core obj_core;
204#endif
205
206 /* Per CPU architecture specifics */
207 struct _cpu_arch arch;
208};
209
210typedef struct _cpu _cpu_t;
211
212struct z_kernel {
213 struct _cpu cpus[CONFIG_MP_MAX_NUM_CPUS];
214
215#ifdef CONFIG_PM
216 int32_t idle; /* Number of ticks for kernel idling */
217#endif
218
219 /*
220 * ready queue: can be big, keep after small fields, since some
221 * assembly (e.g. ARC) are limited in the encoding of the offset
222 */
223#ifndef CONFIG_SCHED_CPU_MASK_PIN_ONLY
224 struct _ready_q ready_q;
225#endif
226
227#ifdef CONFIG_FPU_SHARING
228 /*
229 * A 'current_sse' field does not exist in addition to the 'current_fp'
230 * field since it's not possible to divide the IA-32 non-integer
231 * registers into 2 distinct blocks owned by differing threads. In
232 * other words, given that the 'fxnsave/fxrstor' instructions
233 * save/restore both the X87 FPU and XMM registers, it's not possible
234 * for a thread to only "own" the XMM registers.
235 */
236
237 /* thread that owns the FP regs */
238 struct k_thread *current_fp;
239#endif
240
241#if defined(CONFIG_THREAD_MONITOR)
242 struct k_thread *threads; /* singly linked list of ALL threads */
243#endif
244#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
245 struct k_cycle_stats usage[CONFIG_MP_MAX_NUM_CPUS];
246#endif
247
248#ifdef CONFIG_OBJ_CORE_SYSTEM
249 struct k_obj_core obj_core;
250#endif
251
252#if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED)
253 /* Need to signal an IPI at the next scheduling point */
254 bool pending_ipi;
255#endif
256};
257
258typedef struct z_kernel _kernel_t;
259
260extern struct z_kernel _kernel;
261
262extern atomic_t _cpus_active;
263
264#ifdef CONFIG_SMP
265
266/* True if the current context can be preempted and migrated to
267 * another SMP CPU.
268 */
269bool z_smp_cpu_mobile(void);
270
271#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
272 arch_curr_cpu(); })
273#define _current k_sched_current_thread_query()
274
275#else
276#define _current_cpu (&_kernel.cpus[0])
277#define _current _kernel.cpus[0].current
278#endif
279
280/* kernel wait queue record */
281#ifdef CONFIG_WAITQ_SCALABLE
282
283typedef struct {
284 struct _priq_rb waitq;
285} _wait_q_t;
286
287/* defined in kernel/priority_queues.c */
288bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
289
290#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
291
292#else
293
294typedef struct {
295 sys_dlist_t waitq;
296} _wait_q_t;
297
298#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
299
300#endif /* CONFIG_WAITQ_SCALABLE */
301
302/* kernel timeout record */
303struct _timeout;
304typedef void (*_timeout_func_t)(struct _timeout *t);
305
306struct _timeout {
307 sys_dnode_t node;
308 _timeout_func_t fn;
309#ifdef CONFIG_TIMEOUT_64BIT
310 /* Can't use k_ticks_t for header dependency reasons */
311 int64_t dticks;
312#else
313 int32_t dticks;
314#endif
315};
316
317typedef void (*k_thread_timeslice_fn_t)(struct k_thread *thread, void *data);
318
319#ifdef __cplusplus
320}
321#endif
322
323#endif /* _ASMLANGUAGE */
324
325#endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ */
long atomic_t
Definition: atomic_types.h:15
struct _dnode sys_dnode_t
Doubly-linked list node structure.
Definition: dlist.h:54
struct _dnode sys_dlist_t
Doubly-linked list structure.
Definition: dlist.h:50
#define PRIQ_BITMAP_SIZE
Definition: kernel_structs.h:40
void(* k_thread_timeslice_fn_t)(struct k_thread *thread, void *data)
Definition: kernel_structs.h:317
#define K_NUM_THREAD_PRIO
Definition: kernel_structs.h:35
__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
__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:121
Thread Structure.
Definition: thread.h:260
Balanced red/black tree node structure.
Definition: rb.h:58
Balanced red/black tree structure.
Definition: rb.h:91
Misc utilities.