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