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
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 <sys/atomic.h>
25#include <zephyr/types.h>
26#include <kernel/sched_priq.h>
27#include <sys/dlist.h>
28#include <sys/util.h>
29#include <sys/sys_heap.h>
30#include <arch/structs.h>
31#include <kernel/stats.h>
32#endif
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/*
39 * Bitmask definitions for the struct k_thread.thread_state field.
40 *
41 * Must be before kerneL_arch_data.h because it might need them to be already
42 * defined.
43 */
44
45/* states: common uses low bits, arch-specific use high bits */
46
47/* Not a real thread */
48#define _THREAD_DUMMY (BIT(0))
49
50/* Thread is waiting on an object */
51#define _THREAD_PENDING (BIT(1))
52
53/* Thread has not yet started */
54#define _THREAD_PRESTART (BIT(2))
55
56/* Thread has terminated */
57#define _THREAD_DEAD (BIT(3))
58
59/* Thread is suspended */
60#define _THREAD_SUSPENDED (BIT(4))
61
62/* Thread is being aborted */
63#define _THREAD_ABORTING (BIT(5))
64
65/* Thread is present in the ready queue */
66#define _THREAD_QUEUED (BIT(7))
67
68/* end - states */
69
70#ifdef CONFIG_STACK_SENTINEL
71/* Magic value in lowest bytes of the stack */
72#define STACK_SENTINEL 0xF0F0F0F0
73#endif
74
75/* lowest value of _thread_base.preempt at which a thread is non-preemptible */
76#define _NON_PREEMPT_THRESHOLD 0x0080U
77
78/* highest value of _thread_base.preempt at which a thread is preemptible */
79#define _PREEMPT_THRESHOLD (_NON_PREEMPT_THRESHOLD - 1U)
80
81#if !defined(_ASMLANGUAGE)
82
83struct _ready_q {
84#ifndef CONFIG_SMP
85 /* always contains next thread to run: cannot be NULL */
86 struct k_thread *cache;
87#endif
88
89#if defined(CONFIG_SCHED_DUMB)
90 sys_dlist_t runq;
91#elif defined(CONFIG_SCHED_SCALABLE)
92 struct _priq_rb runq;
93#elif defined(CONFIG_SCHED_MULTIQ)
94 struct _priq_mq runq;
95#endif
96};
97
98typedef struct _ready_q _ready_q_t;
99
100struct _cpu {
101 /* nested interrupt count */
102 uint32_t nested;
103
104 /* interrupt stack pointer base */
105 char *irq_stack;
106
107 /* currently scheduled thread */
108 struct k_thread *current;
109
110 /* one assigned idle thread per CPU */
111 struct k_thread *idle_thread;
112
113#ifdef CONFIG_SCHED_CPU_MASK_PIN_ONLY
114 struct _ready_q ready_q;
115#endif
116
117#if (CONFIG_NUM_METAIRQ_PRIORITIES > 0) && (CONFIG_NUM_COOP_PRIORITIES > 0)
118 /* Coop thread preempted by current metairq, or NULL */
119 struct k_thread *metairq_preempted;
120#endif
121
122#ifdef CONFIG_TIMESLICING
123 /* number of ticks remaining in current time slice */
124 int slice_ticks;
125#endif
126
127 uint8_t id;
128
129#ifdef CONFIG_SMP
130 /* True when _current is allowed to context switch */
131 uint8_t swap_ok;
132#endif
133
134#ifdef CONFIG_SCHED_THREAD_USAGE
135 /*
136 * [usage0] is used as a timestamp to mark the beginning of an
137 * execution window. [0] is a special value indicating that it
138 * has been stopped (but not disabled).
139 */
140
141 uint32_t usage0;
142
143#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
144 struct k_cycle_stats usage;
145#endif
146#endif
147
148 /* Per CPU architecture specifics */
149 struct _cpu_arch arch;
150};
151
152typedef struct _cpu _cpu_t;
153
154struct z_kernel {
155 struct _cpu cpus[CONFIG_MP_NUM_CPUS];
156
157#ifdef CONFIG_PM
158 int32_t idle; /* Number of ticks for kernel idling */
159#endif
160
161 /*
162 * ready queue: can be big, keep after small fields, since some
163 * assembly (e.g. ARC) are limited in the encoding of the offset
164 */
165#ifndef CONFIG_SCHED_CPU_MASK_PIN_ONLY
166 struct _ready_q ready_q;
167#endif
168
169#ifdef CONFIG_FPU_SHARING
170 /*
171 * A 'current_sse' field does not exist in addition to the 'current_fp'
172 * field since it's not possible to divide the IA-32 non-integer
173 * registers into 2 distinct blocks owned by differing threads. In
174 * other words, given that the 'fxnsave/fxrstor' instructions
175 * save/restore both the X87 FPU and XMM registers, it's not possible
176 * for a thread to only "own" the XMM registers.
177 */
178
179 /* thread that owns the FP regs */
180 struct k_thread *current_fp;
181#endif
182
183#if defined(CONFIG_THREAD_MONITOR)
184 struct k_thread *threads; /* singly linked list of ALL threads */
185#endif
186};
187
188typedef struct z_kernel _kernel_t;
189
190extern struct z_kernel _kernel;
191
192#ifdef CONFIG_SMP
193
194/* True if the current context can be preempted and migrated to
195 * another SMP CPU.
196 */
197bool z_smp_cpu_mobile(void);
198
199#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
200 arch_curr_cpu(); })
201#define _current z_current_get()
202
203#else
204#define _current_cpu (&_kernel.cpus[0])
205#define _current _kernel.cpus[0].current
206#endif
207
208/* kernel wait queue record */
209
210#ifdef CONFIG_WAITQ_SCALABLE
211
212typedef struct {
213 struct _priq_rb waitq;
214} _wait_q_t;
215
216extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
217
218#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
219
220#else
221
222typedef struct {
223 sys_dlist_t waitq;
224} _wait_q_t;
225
226#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
227
228#endif
229
230/* kernel timeout record */
231
232struct _timeout;
233typedef void (*_timeout_func_t)(struct _timeout *t);
234
235struct _timeout {
236 sys_dnode_t node;
237 _timeout_func_t fn;
238#ifdef CONFIG_TIMEOUT_64BIT
239 /* Can't use k_ticks_t for header dependency reasons */
240 int64_t dticks;
241#else
242 int32_t dticks;
243#endif
244};
245
246#ifdef __cplusplus
247}
248#endif
249
250#endif /* _ASMLANGUAGE */
251
252#endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ */
Doubly-linked list implementation.
static struct k_thread threads[2]
Definition: errno.c:25
struct _dnode sys_dnode_t
Definition: dlist.h:49
struct _dnode sys_dlist_t
Definition: dlist.h:48
struct k_thread t
Definition: kobject.c:1321
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__INT32_TYPE__ int32_t
Definition: stdint.h:44
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__INT64_TYPE__ int64_t
Definition: stdint.h:45
Definition: stats.h:17
Definition: thread.h:216
Definition: rb.h:49
void idle(void *p1, void *p2, void *p3)
Misc utilities.
#define CONFIG_MP_NUM_CPUS
Definition: ztest.h:38