Zephyr Project API  3.2.0
A Scalable Open Source RTOS
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>
27#include <zephyr/sys/dlist.h>
28#include <zephyr/sys/util.h>
29#include <zephyr/sys/sys_heap.h>
30#include <zephyr/arch/structs.h>
31#include <zephyr/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#if defined(CONFIG_FPU_SHARING)
130 void *fp_ctx;
131#endif
132
133#ifdef CONFIG_SMP
134 /* True when _current is allowed to context switch */
135 uint8_t swap_ok;
136#endif
137
138#ifdef CONFIG_SCHED_THREAD_USAGE
139 /*
140 * [usage0] is used as a timestamp to mark the beginning of an
141 * execution window. [0] is a special value indicating that it
142 * has been stopped (but not disabled).
143 */
144
145 uint32_t usage0;
146
147#ifdef CONFIG_SCHED_THREAD_USAGE_ALL
148 struct k_cycle_stats usage;
149#endif
150#endif
151
152 /* Per CPU architecture specifics */
153 struct _cpu_arch arch;
154};
155
156typedef struct _cpu _cpu_t;
157
158struct z_kernel {
159 struct _cpu cpus[CONFIG_MP_NUM_CPUS];
160
161#ifdef CONFIG_PM
162 int32_t idle; /* Number of ticks for kernel idling */
163#endif
164
165 /*
166 * ready queue: can be big, keep after small fields, since some
167 * assembly (e.g. ARC) are limited in the encoding of the offset
168 */
169#ifndef CONFIG_SCHED_CPU_MASK_PIN_ONLY
170 struct _ready_q ready_q;
171#endif
172
173#ifdef CONFIG_FPU_SHARING
174 /*
175 * A 'current_sse' field does not exist in addition to the 'current_fp'
176 * field since it's not possible to divide the IA-32 non-integer
177 * registers into 2 distinct blocks owned by differing threads. In
178 * other words, given that the 'fxnsave/fxrstor' instructions
179 * save/restore both the X87 FPU and XMM registers, it's not possible
180 * for a thread to only "own" the XMM registers.
181 */
182
183 /* thread that owns the FP regs */
184 struct k_thread *current_fp;
185#endif
186
187#if defined(CONFIG_THREAD_MONITOR)
188 struct k_thread *threads; /* singly linked list of ALL threads */
189#endif
190
191#if defined(CONFIG_SMP) && defined(CONFIG_SCHED_IPI_SUPPORTED)
192 /* Need to signal an IPI at the next scheduling point */
193 bool pending_ipi;
194#endif
195};
196
197typedef struct z_kernel _kernel_t;
198
199extern struct z_kernel _kernel;
200
201#ifdef CONFIG_SMP
202
203/* True if the current context can be preempted and migrated to
204 * another SMP CPU.
205 */
206bool z_smp_cpu_mobile(void);
207
208#define _current_cpu ({ __ASSERT_NO_MSG(!z_smp_cpu_mobile()); \
209 arch_curr_cpu(); })
210#define _current z_current_get()
211
212#else
213#define _current_cpu (&_kernel.cpus[0])
214#define _current _kernel.cpus[0].current
215#endif
216
217/* kernel wait queue record */
218
219#ifdef CONFIG_WAITQ_SCALABLE
220
221typedef struct {
222 struct _priq_rb waitq;
223} _wait_q_t;
224
225extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
226
227#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
228
229#else
230
231typedef struct {
232 sys_dlist_t waitq;
233} _wait_q_t;
234
235#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
236
237#endif
238
239/* kernel timeout record */
240
241struct _timeout;
242typedef void (*_timeout_func_t)(struct _timeout *t);
243
244struct _timeout {
245 sys_dnode_t node;
246 _timeout_func_t fn;
247#ifdef CONFIG_TIMEOUT_64BIT
248 /* Can't use k_ticks_t for header dependency reasons */
249 int64_t dticks;
250#else
251 int32_t dticks;
252#endif
253};
254
255typedef void (*k_thread_timeslice_fn_t)(struct k_thread *thread, void *data);
256
257#ifdef __cplusplus
258}
259#endif
260
261#endif /* _ASMLANGUAGE */
262
263#endif /* ZEPHYR_KERNEL_INCLUDE_KERNEL_STRUCTS_H_ */
static struct k_thread thread[2]
Definition: atomic.c:26
Doubly-linked list implementation.
static struct k_thread threads[2]
Definition: errno.c:26
struct _dnode sys_dnode_t
Definition: dlist.h:49
struct _dnode sys_dlist_t
Definition: dlist.h:48
void(* k_thread_timeslice_fn_t)(struct k_thread *thread, void *data)
Definition: kernel_structs.h:255
struct k_thread t
Definition: kobject.c:1327
__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
Definition: stats.h:17
Definition: thread.h:245
Definition: rb.h:49
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
void idle(void *p1, void *p2, void *p3)
Misc utilities.