Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
kernel.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
12
13#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
15
16#if !defined(_ASMLANGUAGE)
18#include <errno.h>
19#include <limits.h>
20#include <stdbool.h>
21#include <zephyr/toolchain.h>
23#include <zephyr/sleep.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/*
33 * Zephyr currently assumes the size of a couple standard types to simplify
34 * print string formats. Let's make sure this doesn't change without notice.
35 */
36BUILD_ASSERT(sizeof(int32_t) == sizeof(int));
37BUILD_ASSERT(sizeof(int64_t) == sizeof(long long));
38BUILD_ASSERT(sizeof(intptr_t) == sizeof(long));
39
48
57#define K_ANY NULL
58
59#if (CONFIG_NUM_COOP_PRIORITIES + CONFIG_NUM_PREEMPT_PRIORITIES) == 0
60#error Zero available thread priorities defined!
61#endif
62
67
78#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
79
90#define K_PRIO_PREEMPT(x) (x)
91
93#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
95#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
97#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
99#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
101#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
102
104
105#ifdef CONFIG_POLL
106#define Z_POLL_EVENT_OBJ_INIT(obj) \
107 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
108#define Z_DECL_POLL_EVENT sys_dlist_t poll_events;
109#else
110#define Z_POLL_EVENT_OBJ_INIT(obj)
111#define Z_DECL_POLL_EVENT
112#endif
113
114struct k_thread;
115struct k_mutex;
116struct k_sem;
117struct k_msgq;
118struct k_mbox;
119struct k_pipe;
120struct k_queue;
121struct k_fifo;
122struct k_lifo;
123struct k_stack;
124struct k_mem_slab;
125struct k_timer;
126struct k_poll_event;
127struct k_poll_signal;
128struct k_mem_domain;
129struct k_mem_partition;
130struct k_futex;
131struct k_event;
132
143
144/* private, used by k_poll and k_work_poll */
145struct k_work_poll;
146typedef int (*_poller_cb_t)(struct k_poll_event *event, uint32_t state);
147
152
166static inline void
168{
169#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
170 thread->base.usage.longest = 0ULL;
171#endif
172}
173
180typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
181 void *user_data);
182
198void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
199
218#ifdef CONFIG_SMP
219void k_thread_foreach_filter_by_cpu(unsigned int cpu,
220 k_thread_user_cb_t user_cb, void *user_data);
221#else
222static inline
223void k_thread_foreach_filter_by_cpu(unsigned int cpu,
224 k_thread_user_cb_t user_cb, void *user_data)
225{
226 __ASSERT(cpu == 0, "cpu filter out of bounds");
227 ARG_UNUSED(cpu);
228 k_thread_foreach(user_cb, user_data);
229}
230#endif
231
260 k_thread_user_cb_t user_cb, void *user_data);
261
293#ifdef CONFIG_SMP
295 k_thread_user_cb_t user_cb, void *user_data);
296#else
297static inline
298void k_thread_foreach_unlocked_filter_by_cpu(unsigned int cpu,
299 k_thread_user_cb_t user_cb, void *user_data)
300{
301 __ASSERT(cpu == 0, "cpu filter out of bounds");
302 ARG_UNUSED(cpu);
303 k_thread_foreach_unlocked(user_cb, user_data);
304}
305#endif
306
308
314
315#endif /* !_ASMLANGUAGE */
316
317
318/*
319 * Thread user options. May be needed by assembly code. Common part uses low
320 * bits, arch-specific use high bits.
321 */
322
326#define K_ESSENTIAL (BIT(0))
327
331#define K_FP_IDX 1
344#define K_FP_REGS (BIT(K_FP_IDX))
345
352#define K_USER (BIT(2))
353
362#define K_INHERIT_PERMS (BIT(3))
363
373#define K_CALLBACK_STATE (BIT(4))
374
378#define K_DSP_IDX 13
391#define K_DSP_REGS (BIT(K_DSP_IDX))
392
396#define K_AGU_IDX 14
408#define K_AGU_REGS (BIT(K_AGU_IDX))
409
419#define K_SSE_REGS (BIT(15))
420
421/* end - thread options */
422
423#if !defined(_ASMLANGUAGE)
448__syscall k_thread_stack_t *k_thread_stack_alloc(size_t size, int flags);
449
463
515__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
516 k_thread_stack_t *stack,
517 size_t stack_size,
519 void *p1, void *p2, void *p3,
520 int prio, uint32_t options, k_timeout_t delay);
521
544 void *p1, void *p2,
545 void *p3);
546
560#define k_thread_access_grant(thread, ...) \
561 FOR_EACH_FIXED_ARG(k_object_access_grant, (;), (thread), __VA_ARGS__)
562
577static inline void k_thread_heap_assign(struct k_thread *thread,
578 struct k_heap *heap)
579{
580 thread->resource_pool = heap;
581}
582
583#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
605__syscall int k_thread_stack_space_get(const struct k_thread *thread,
606 size_t *unused_ptr);
607
623__syscall int k_thread_runtime_stack_unused_threshold_pct_set(struct k_thread *thread,
624 uint32_t pct);
625
641__syscall int k_thread_runtime_stack_unused_threshold_set(struct k_thread *thread,
642 size_t threshold);
643
656__syscall size_t k_thread_runtime_stack_unused_threshold_get(struct k_thread *thread);
657
669typedef void (*k_thread_stack_safety_handler_t)(const struct k_thread *thread,
670 size_t unused_space, void *arg);
671
686int k_thread_runtime_stack_safety_full_check(const struct k_thread *thread,
687 size_t *unused_ptr,
688 k_thread_stack_safety_handler_t handler,
689 void *arg);
690
705int k_thread_runtime_stack_safety_threshold_check(const struct k_thread *thread,
706 size_t *unused_ptr,
707 k_thread_stack_safety_handler_t handler,
708 void *arg);
709#endif
710
711#if (K_HEAP_MEM_POOL_SIZE > 0)
724void k_thread_system_pool_assign(struct k_thread *thread);
725#endif /* (K_HEAP_MEM_POOL_SIZE > 0) */
726
746__syscall int k_thread_join(struct k_thread *thread, k_timeout_t timeout);
747
764__syscall void k_busy_wait(uint32_t usec_to_wait);
765
777bool k_can_yield(void);
778
786__syscall void k_yield(void);
787
797__syscall void k_wakeup(k_tid_t thread);
798
812__attribute_const__
814
826static inline bool k_is_pre_kernel(void)
827{
828 extern bool z_sys_post_kernel; /* in init.c */
829
830 /*
831 * If called from userspace, it must be post kernel.
832 * This guard is necessary because z_sys_post_kernel memory
833 * is not accessible to user threads.
834 */
835 if (k_is_user_context()) {
836 return false;
837 }
838
839 /*
840 * Some compilers might optimize by pre-reading
841 * z_sys_post_kernel. This is absolutely not desirable.
842 * We are trying to avoid reading it if we are in user
843 * context as reading z_sys_post_kernel in user context
844 * will result in access fault. So add a compiler barrier
845 * here to stop that kind of optimizations.
846 */
847 compiler_barrier();
848
849 return !z_sys_post_kernel;
850}
851
858__attribute_const__
859static inline k_tid_t k_current_get(void)
860{
861 __ASSERT(!k_is_pre_kernel(), "k_current_get called pre-kernel");
862
863#ifdef CONFIG_CURRENT_THREAD_USE_TLS
864
865 /* Thread-local cache of current thread ID, set in z_thread_entry() */
866 extern Z_THREAD_LOCAL k_tid_t z_tls_current;
867
868 return z_tls_current;
869#else
871#endif
872}
873
894__syscall void k_thread_abort(k_tid_t thread);
895
896k_ticks_t z_timeout_expires(const struct _timeout *timeout);
897k_ticks_t z_timeout_remaining(const struct _timeout *timeout);
898
899#ifdef CONFIG_SYS_CLOCK_EXISTS
900
908__syscall k_ticks_t k_thread_timeout_expires_ticks(const struct k_thread *thread);
909
910static inline k_ticks_t z_impl_k_thread_timeout_expires_ticks(
911 const struct k_thread *thread)
912{
913 return z_timeout_expires(&thread->base.timeout);
914}
915
924
925static inline k_ticks_t z_impl_k_thread_timeout_remaining_ticks(
926 const struct k_thread *thread)
927{
928 return z_timeout_remaining(&thread->base.timeout);
929}
930
931#endif /* CONFIG_SYS_CLOCK_EXISTS */
932
936struct _static_thread_data {
937 struct k_thread *init_thread;
938 k_thread_stack_t *init_stack;
939 unsigned int init_stack_size;
940 k_thread_entry_t init_entry;
941 void *init_p1;
942 void *init_p2;
943 void *init_p3;
944 int init_prio;
945 uint32_t init_options;
946 const char *init_name;
947#ifdef CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME
948 int32_t init_delay_ms;
949#else
950 k_timeout_t init_delay;
951#endif
952};
953
954#ifdef CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME
955#define Z_THREAD_INIT_DELAY_INITIALIZER(ms) .init_delay_ms = (ms)
956#define Z_THREAD_INIT_DELAY(thread) SYS_TIMEOUT_MS((thread)->init_delay_ms)
957#else
958#define Z_THREAD_INIT_DELAY_INITIALIZER(ms) .init_delay = SYS_TIMEOUT_MS_INIT(ms)
959#define Z_THREAD_INIT_DELAY(thread) (thread)->init_delay
960#endif
961
962#define Z_THREAD_INITIALIZER(thread, stack, stack_size, \
963 entry, p1, p2, p3, \
964 prio, options, delay, tname) \
965 { \
966 .init_thread = (thread), \
967 .init_stack = (stack), \
968 .init_stack_size = (stack_size), \
969 .init_entry = (k_thread_entry_t)entry, \
970 .init_p1 = (void *)p1, \
971 .init_p2 = (void *)p2, \
972 .init_p3 = (void *)p3, \
973 .init_prio = (prio), \
974 .init_options = (options), \
975 .init_name = STRINGIFY(tname), \
976 Z_THREAD_INIT_DELAY_INITIALIZER(delay) \
977 }
978
979/*
980 * Refer to K_THREAD_DEFINE() and K_KERNEL_THREAD_DEFINE() for
981 * information on arguments.
982 */
983#define Z_THREAD_COMMON_DEFINE(name, stack_size, \
984 entry, p1, p2, p3, \
985 prio, options, delay) \
986 struct k_thread _k_thread_obj_##name; \
987 const STRUCT_SECTION_ITERABLE(_static_thread_data, \
988 _k_thread_data_##name) = \
989 Z_THREAD_INITIALIZER(&_k_thread_obj_##name, \
990 _k_thread_stack_##name, stack_size,\
991 entry, p1, p2, p3, prio, options, \
992 delay, name); \
993 __maybe_unused const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
997
1029#define K_THREAD_DEFINE(name, stack_size, \
1030 entry, p1, p2, p3, \
1031 prio, options, delay) \
1032 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
1033 Z_THREAD_COMMON_DEFINE(name, stack_size, entry, p1, p2, p3, \
1034 prio, options, delay)
1035
1066#define K_KERNEL_THREAD_DEFINE(name, stack_size, \
1067 entry, p1, p2, p3, \
1068 prio, options, delay) \
1069 K_KERNEL_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
1070 Z_THREAD_COMMON_DEFINE(name, stack_size, entry, p1, p2, p3, \
1071 prio, options, delay)
1072
1082__syscall int k_thread_priority_get(k_tid_t thread);
1083
1109__syscall void k_thread_priority_set(k_tid_t thread, int prio);
1110
1111
1112#ifdef CONFIG_SCHED_DEADLINE
1148__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1149
1190__syscall void k_thread_absolute_deadline_set(k_tid_t thread, int deadline);
1191#endif
1192
1211__syscall void k_reschedule(void);
1212
1213#ifdef CONFIG_SCHED_CPU_MASK
1231
1250
1266
1285
1296int k_thread_cpu_pin(k_tid_t thread, int cpu);
1297#endif
1298
1320__syscall void k_thread_suspend(k_tid_t thread);
1321
1333__syscall void k_thread_resume(k_tid_t thread);
1334
1348static inline void k_thread_start(k_tid_t thread)
1349{
1350 k_wakeup(thread);
1351}
1352
1379void k_sched_time_slice_set(int32_t slice, int prio);
1380
1419void k_thread_time_slice_set(struct k_thread *th, int32_t slice_ticks,
1420 k_thread_timeslice_fn_t expired, void *data);
1421
1423
1428
1440bool k_is_in_isr(void);
1441
1458__syscall int k_is_preempt_thread(void);
1459
1463
1468
1494void k_sched_lock(void);
1495
1504
1517__syscall void k_thread_custom_data_set(void *value);
1518
1526__syscall void *k_thread_custom_data_get(void);
1527
1544__syscall int k_thread_name_set(k_tid_t thread, const char *str);
1545
1554const char *k_thread_name_get(k_tid_t thread);
1555
1568__syscall int k_thread_name_copy(k_tid_t thread, char *buf,
1569 size_t size);
1570
1583const char *k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size);
1584
1588
1593
1602#define K_NO_WAIT Z_TIMEOUT_NO_WAIT
1603
1616#define K_NSEC(t) Z_TIMEOUT_NS(t)
1617
1630#define K_USEC(t) Z_TIMEOUT_US(t)
1631
1642#define K_CYC(t) Z_TIMEOUT_CYC(t)
1643
1654#define K_TICKS(t) Z_TIMEOUT_TICKS(t)
1655
1666#define K_MSEC(ms) Z_TIMEOUT_MS(ms)
1667
1678#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
1679
1690#define K_MINUTES(m) K_SECONDS((m) * 60)
1691
1702#define K_HOURS(h) K_MINUTES((h) * 60)
1703
1712#define K_FOREVER Z_FOREVER
1713
1728#define K_TIMEOUT_SUM(timeout1, timeout2) K_TICKS(z_timeout_sum(timeout1, timeout2))
1729
1730#ifdef CONFIG_TIMEOUT_64BIT
1731
1743#define K_TIMEOUT_ABS_TICKS(t) \
1744 Z_TIMEOUT_TICKS(Z_TICK_ABS((k_ticks_t)CLAMP(t, 0, (INT64_MAX - 1))))
1745
1757#define K_TIMEOUT_ABS_SEC(t) K_TIMEOUT_ABS_TICKS(k_sec_to_ticks_ceil64(t))
1758
1770#define K_TIMEOUT_ABS_MS(t) K_TIMEOUT_ABS_TICKS(k_ms_to_ticks_ceil64(t))
1771
1784#define K_TIMEOUT_ABS_US(t) K_TIMEOUT_ABS_TICKS(k_us_to_ticks_ceil64(t))
1785
1798#define K_TIMEOUT_ABS_NS(t) K_TIMEOUT_ABS_TICKS(k_ns_to_ticks_ceil64(t))
1799
1812#define K_TIMEOUT_ABS_CYC(t) K_TIMEOUT_ABS_TICKS(k_cyc_to_ticks_ceil64(t))
1813#endif
1814
1818
1825struct k_timer {
1829 /*
1830 * _timeout structure must be first here if we want to use
1831 * dynamic timer allocation. timeout.node is used in the double-linked
1832 * list of free timers
1833 */
1834 struct _timeout timeout;
1835
1836 /* wait queue for the (single) thread waiting on this timer */
1837 _wait_q_t wait_q;
1838
1839 /* runs in ISR context */
1840 void (*expiry_fn)(struct k_timer *timer);
1841
1842 /* runs in the context of the thread that calls k_timer_stop() */
1843 void (*stop_fn)(struct k_timer *timer);
1844
1845 /* timer period */
1846 k_timeout_t period;
1847
1848 /* timer status */
1849 uint32_t status;
1850
1851 /* user-specific data, also used to support legacy features */
1852 void *user_data;
1853
1855
1856#ifdef CONFIG_OBJ_CORE_TIMER
1857 struct k_obj_core obj_core;
1858#endif
1862};
1863
1864#ifdef CONFIG_TIMER_OBSERVER
1865struct k_timer_observer {
1866 /* Invoked upon completion of k_timer initialization */
1867 void (*on_init)(struct k_timer *timer);
1868
1869 /* Invoked after the timer transitions to the running state */
1870 void (*on_start)(struct k_timer *timer, k_timeout_t duration,
1871 k_timeout_t period);
1872
1873 /* Invoked when the active timer is explicitly stopped */
1874 void (*on_stop)(struct k_timer *timer);
1875
1876 /* Executes in ISR context, keep minimal and non-blocking */
1877 void (*on_expiry)(struct k_timer *timer);
1878};
1879#endif /* CONFIG_TIMER_OBSERVER */
1880
1884#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
1885 { \
1886 .timeout = { \
1887 .fn = z_timer_expiration_handler, \
1888 }, \
1889 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1890 .expiry_fn = expiry, \
1891 .stop_fn = stop, \
1892 .period = {}, \
1893 .status = 0, \
1894 .user_data = 0, \
1895 }
1899
1905
1916typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1917
1932typedef void (*k_timer_stop_t)(struct k_timer *timer);
1933
1945#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
1946 STRUCT_SECTION_ITERABLE(k_timer, name) = \
1947 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
1948
1949
1950#ifdef CONFIG_TIMER_OBSERVER
1951
1955#define Z_TIMER_OBSERVER_INITIALIZER(name, init, start, stop, expiry) \
1956 { \
1957 .on_init = init, \
1958 .on_start = start, \
1959 .on_stop = stop, \
1960 .on_expiry = expiry \
1961 }
1965
1979#define K_TIMER_OBSERVER_DEFINE(name, init, start, stop, expiry) \
1980 static const STRUCT_SECTION_ITERABLE(k_timer_observer, name) = \
1981 Z_TIMER_OBSERVER_INITIALIZER(name, init, start, stop, expiry)
1982
1983#endif /* CONFIG_TIMER_OBSERVER */
1984
1994void k_timer_init(struct k_timer *timer,
1995 k_timer_expiry_t expiry_fn,
1996 k_timer_stop_t stop_fn);
1997
2015__syscall void k_timer_start(struct k_timer *timer,
2016 k_timeout_t duration, k_timeout_t period);
2017
2034__syscall void k_timer_stop(struct k_timer *timer);
2035
2048__syscall uint32_t k_timer_status_get(struct k_timer *timer);
2049
2067__syscall uint32_t k_timer_status_sync(struct k_timer *timer);
2068
2069#ifdef CONFIG_SYS_CLOCK_EXISTS
2070
2082__syscall k_ticks_t k_timer_expires_ticks(const struct k_timer *timer);
2083
2084static inline k_ticks_t z_impl_k_timer_expires_ticks(
2085 const struct k_timer *timer)
2086{
2087 return z_timeout_expires(&timer->timeout);
2088}
2089
2100__syscall k_ticks_t k_timer_remaining_ticks(const struct k_timer *timer);
2101
2102static inline k_ticks_t z_impl_k_timer_remaining_ticks(
2103 const struct k_timer *timer)
2104{
2105 return z_timeout_remaining(&timer->timeout);
2106}
2107
2118static inline uint32_t k_timer_remaining_get(struct k_timer *timer)
2119{
2121}
2122
2123#endif /* CONFIG_SYS_CLOCK_EXISTS */
2124
2137__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
2138
2142static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
2143 void *user_data)
2144{
2145 timer->user_data = user_data;
2146}
2147
2155__syscall void *k_timer_user_data_get(const struct k_timer *timer);
2156
2157static inline void *z_impl_k_timer_user_data_get(const struct k_timer *timer)
2158{
2159 return timer->user_data;
2160}
2161
2182int k_timer_cleanup(struct k_timer *timer);
2183
2185
2191
2201__syscall int64_t k_uptime_ticks(void);
2202
2216static inline int64_t k_uptime_get(void)
2217{
2219}
2220
2240static inline uint32_t k_uptime_get_32(void)
2241{
2242 return (uint32_t)k_uptime_get();
2243}
2244
2253static inline uint32_t k_uptime_seconds(void)
2254{
2256}
2257
2269static inline int64_t k_uptime_delta(int64_t *reftime)
2270{
2271 int64_t uptime, delta;
2272
2273 uptime = k_uptime_get();
2274 delta = uptime - *reftime;
2275 *reftime = uptime;
2276
2277 return delta;
2278}
2279
2288static inline uint32_t k_cycle_get_32(void)
2289{
2290 return arch_k_cycle_get_32();
2291}
2292
2306static inline uint64_t k_cycle_get_64(void)
2307{
2308 if (!IS_ENABLED(CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER)) {
2309 __ASSERT(0, "64-bit cycle counter not enabled on this platform. "
2310 "See CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER");
2311 return 0;
2312 }
2313
2314 return arch_k_cycle_get_64();
2315}
2316
2320
2327struct k_queue {
2331 sys_sflist_t data_q;
2332 struct k_spinlock lock;
2333 _wait_q_t wait_q;
2334
2335 Z_DECL_POLL_EVENT
2336
2341};
2342
2346#define Z_QUEUE_INITIALIZER(obj) \
2347 { \
2348 .data_q = SYS_SFLIST_STATIC_INIT(&obj.data_q), \
2349 .lock = { }, \
2350 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
2351 Z_POLL_EVENT_OBJ_INIT(obj) \
2352 }
2356
2362
2370__syscall void k_queue_init(struct k_queue *queue);
2371
2385__syscall void k_queue_cancel_wait(struct k_queue *queue);
2386
2399void k_queue_append(struct k_queue *queue, void *data);
2400
2417__syscall int32_t k_queue_alloc_append(struct k_queue *queue, void *data);
2418
2431void k_queue_prepend(struct k_queue *queue, void *data);
2432
2449__syscall int32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
2450
2464void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2465
2484int k_queue_append_list(struct k_queue *queue, void *head, void *tail);
2485
2503int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2504
2522__syscall void *k_queue_get(struct k_queue *queue, k_timeout_t timeout);
2523
2538bool k_queue_remove(struct k_queue *queue, void *data);
2539
2554bool k_queue_unique_append(struct k_queue *queue, void *data);
2555
2569__syscall int k_queue_is_empty(struct k_queue *queue);
2570
2571static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
2572{
2573 return sys_sflist_is_empty(&queue->data_q) ? 1 : 0;
2574}
2575
2585__syscall void *k_queue_peek_head(struct k_queue *queue);
2586
2596__syscall void *k_queue_peek_tail(struct k_queue *queue);
2597
2607#define K_QUEUE_DEFINE(name) \
2608 STRUCT_SECTION_ITERABLE(k_queue, name) = \
2609 Z_QUEUE_INITIALIZER(name)
2610
2612
2613#ifdef CONFIG_USERSPACE
2623struct k_futex {
2630};
2631
2637
2656__syscall int k_futex_wait(struct k_futex *futex, int expected,
2657 k_timeout_t timeout);
2658
2672__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2673
2675#endif
2676
2682
2687
2694
2695struct k_event {
2699 _wait_q_t wait_q;
2700 uint32_t events;
2701 struct k_spinlock lock;
2702
2704
2705#ifdef CONFIG_OBJ_CORE_EVENT
2706 struct k_obj_core obj_core;
2707#endif
2711};
2712
2716#define Z_EVENT_INITIALIZER(obj) \
2717 { \
2718 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
2719 .events = 0, \
2720 .lock = {}, \
2721 }
2725
2733__syscall void k_event_init(struct k_event *event);
2734
2753__syscall uint32_t k_event_post(struct k_event *event, uint32_t events);
2754
2772__syscall uint32_t k_event_set(struct k_event *event, uint32_t events);
2773
2790__syscall uint32_t k_event_set_masked(struct k_event *event, uint32_t events,
2791 uint32_t events_mask);
2792
2806__syscall uint32_t k_event_clear(struct k_event *event, uint32_t events);
2807
2835__syscall uint32_t k_event_wait(struct k_event *event, uint32_t events,
2836 bool reset, k_timeout_t timeout);
2837
2865__syscall uint32_t k_event_wait_all(struct k_event *event, uint32_t events,
2866 bool reset, k_timeout_t timeout);
2867
2890__syscall uint32_t k_event_wait_safe(struct k_event *event, uint32_t events,
2891 bool reset, k_timeout_t timeout);
2892
2915__syscall uint32_t k_event_wait_all_safe(struct k_event *event, uint32_t events,
2916 bool reset, k_timeout_t timeout);
2917
2928static inline uint32_t k_event_test(struct k_event *event, uint32_t events_mask)
2929{
2930 return k_event_wait(event, events_mask, false, K_NO_WAIT);
2931}
2932
2942#define K_EVENT_DEFINE(name) \
2943 STRUCT_SECTION_ITERABLE(k_event, name) = \
2944 Z_EVENT_INITIALIZER(name);
2945
2947
2953struct k_fifo {
2957 struct k_queue _queue;
2958#ifdef CONFIG_OBJ_CORE_FIFO
2959 struct k_obj_core obj_core;
2960#endif
2964};
2965
2969#define Z_FIFO_INITIALIZER(obj) \
2970 { \
2971 ._queue = Z_QUEUE_INITIALIZER(obj._queue) \
2972 }
2976
2982
2990#define k_fifo_init(fifo) \
2991 ({ \
2992 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, init, fifo); \
2993 k_queue_init(&(fifo)->_queue); \
2994 K_OBJ_CORE_INIT(K_OBJ_CORE(fifo), _obj_type_fifo); \
2995 K_OBJ_CORE_LINK(K_OBJ_CORE(fifo)); \
2996 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, init, fifo); \
2997 })
2998
3010#define k_fifo_cancel_wait(fifo) \
3011 ({ \
3012 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, cancel_wait, fifo); \
3013 k_queue_cancel_wait(&(fifo)->_queue); \
3014 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, cancel_wait, fifo); \
3015 })
3016
3029#define k_fifo_put(fifo, data) \
3030 ({ \
3031 void *_data = data; \
3032 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, put, fifo, _data); \
3033 k_queue_append(&(fifo)->_queue, _data); \
3034 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, put, fifo, _data); \
3035 })
3036
3053#define k_fifo_alloc_put(fifo, data) \
3054 ({ \
3055 void *_data = data; \
3056 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, alloc_put, fifo, _data); \
3057 int fap_ret = k_queue_alloc_append(&(fifo)->_queue, _data); \
3058 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, alloc_put, fifo, _data, fap_ret); \
3059 fap_ret; \
3060 })
3061
3079#define k_fifo_put_list(fifo, head, tail) \
3080 ({ \
3081 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, put_list, fifo, head, tail); \
3082 k_queue_append_list(&(fifo)->_queue, head, tail); \
3083 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, put_list, fifo, head, tail); \
3084 })
3085
3102#define k_fifo_put_slist(fifo, list) \
3103 ({ \
3104 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, put_slist, fifo, list); \
3105 k_queue_merge_slist(&(fifo)->_queue, list); \
3106 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, put_slist, fifo, list); \
3107 })
3108
3127#define k_fifo_get(fifo, timeout) \
3128 ({ \
3129 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, get, fifo, timeout); \
3130 void *fg_ret = k_queue_get(&(fifo)->_queue, timeout); \
3131 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, get, fifo, timeout, fg_ret); \
3132 fg_ret; \
3133 })
3134
3148#define k_fifo_is_empty(fifo) \
3149 k_queue_is_empty(&(fifo)->_queue)
3150
3164#define k_fifo_peek_head(fifo) \
3165 ({ \
3166 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, peek_head, fifo); \
3167 void *fph_ret = k_queue_peek_head(&(fifo)->_queue); \
3168 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, peek_head, fifo, fph_ret); \
3169 fph_ret; \
3170 })
3171
3183#define k_fifo_peek_tail(fifo) \
3184 ({ \
3185 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, peek_tail, fifo); \
3186 void *fpt_ret = k_queue_peek_tail(&(fifo)->_queue); \
3187 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, peek_tail, fifo, fpt_ret); \
3188 fpt_ret; \
3189 })
3190
3200#define K_FIFO_DEFINE(name) \
3201 STRUCT_SECTION_ITERABLE(k_fifo, name) = \
3202 Z_FIFO_INITIALIZER(name)
3203
3205
3211struct k_lifo {
3215 struct k_queue _queue;
3216#ifdef CONFIG_OBJ_CORE_LIFO
3217 struct k_obj_core obj_core;
3218#endif
3222};
3223
3227#define Z_LIFO_INITIALIZER(obj) \
3228 { \
3229 ._queue = Z_QUEUE_INITIALIZER(obj._queue) \
3230 }
3234
3240
3248#define k_lifo_init(lifo) \
3249 ({ \
3250 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_lifo, init, lifo); \
3251 k_queue_init(&(lifo)->_queue); \
3252 K_OBJ_CORE_INIT(K_OBJ_CORE(lifo), _obj_type_lifo); \
3253 K_OBJ_CORE_LINK(K_OBJ_CORE(lifo)); \
3254 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_lifo, init, lifo); \
3255 })
3256
3269#define k_lifo_put(lifo, data) \
3270 ({ \
3271 void *_data = data; \
3272 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_lifo, put, lifo, _data); \
3273 k_queue_prepend(&(lifo)->_queue, _data); \
3274 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_lifo, put, lifo, _data); \
3275 })
3276
3293#define k_lifo_alloc_put(lifo, data) \
3294 ({ \
3295 void *_data = data; \
3296 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_lifo, alloc_put, lifo, _data); \
3297 int lap_ret = k_queue_alloc_prepend(&(lifo)->_queue, _data); \
3298 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_lifo, alloc_put, lifo, _data, lap_ret); \
3299 lap_ret; \
3300 })
3301
3320#define k_lifo_get(lifo, timeout) \
3321 ({ \
3322 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_lifo, get, lifo, timeout); \
3323 void *lg_ret = k_queue_get(&(lifo)->_queue, timeout); \
3324 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_lifo, get, lifo, timeout, lg_ret); \
3325 lg_ret; \
3326 })
3327
3337#define K_LIFO_DEFINE(name) \
3338 STRUCT_SECTION_ITERABLE(k_lifo, name) = \
3339 Z_LIFO_INITIALIZER(name)
3340
3342
3346#define K_STACK_FLAG_ALLOC ((uint8_t)1) /* Buffer was allocated */
3347
3348typedef uintptr_t stack_data_t;
3349
3350struct k_stack {
3351 _wait_q_t wait_q;
3352 struct k_spinlock lock;
3353 stack_data_t *base, *next, *top;
3354
3355 uint8_t flags;
3356
3358
3359#ifdef CONFIG_OBJ_CORE_STACK
3360 struct k_obj_core obj_core;
3361#endif
3362};
3363
3364#define Z_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
3365 { \
3366 .wait_q = Z_WAIT_Q_INIT(&(obj).wait_q), \
3367 .base = (stack_buffer), \
3368 .next = (stack_buffer), \
3369 .top = (stack_buffer) + (stack_num_entries), \
3370 }
3374
3380
3390void k_stack_init(struct k_stack *stack,
3391 stack_data_t *buffer, uint32_t num_entries);
3392
3393
3408
3409__syscall int32_t k_stack_alloc_init(struct k_stack *stack,
3410 uint32_t num_entries);
3411
3423int k_stack_cleanup(struct k_stack *stack);
3424
3438__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
3439
3460__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
3461 k_timeout_t timeout);
3462
3473#define K_STACK_DEFINE(name, stack_num_entries) \
3474 stack_data_t __noinit \
3475 _k_stack_buf_##name[stack_num_entries]; \
3476 STRUCT_SECTION_ITERABLE(k_stack, name) = \
3477 Z_STACK_INITIALIZER(name, _k_stack_buf_##name, \
3478 stack_num_entries)
3479
3481
3485struct k_work;
3486struct k_work_q;
3487struct k_work_queue_config;
3488extern struct k_work_q k_sys_work_q;
3492
3498
3504struct k_mutex {
3509 _wait_q_t wait_q;
3511 struct k_thread *owner;
3512
3514 uint32_t lock_count;
3515
3516#if Z_MUTEX_PI_ENABLED
3518 sys_snode_t held_node;
3519#endif /* Z_MUTEX_PI_ENABLED */
3520
3522
3523#ifdef CONFIG_OBJ_CORE_MUTEX
3524 struct k_obj_core obj_core;
3525#endif
3529};
3530
3534#if Z_MUTEX_PI_ENABLED
3535#define Z_MUTEX_HELD_NODE_INIT .held_node = {NULL},
3536#else
3537#define Z_MUTEX_HELD_NODE_INIT
3538#endif
3539
3540#define Z_MUTEX_INITIALIZER(obj) \
3541 { \
3542 .wait_q = Z_WAIT_Q_INIT(&(obj).wait_q), \
3543 .owner = NULL, \
3544 .lock_count = 0, \
3545 Z_MUTEX_HELD_NODE_INIT \
3546 }
3550
3560#define K_MUTEX_DEFINE(name) \
3561 STRUCT_SECTION_ITERABLE(k_mutex, name) = \
3562 Z_MUTEX_INITIALIZER(name)
3563
3576__syscall int k_mutex_init(struct k_mutex *mutex);
3577
3578
3600__syscall int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout);
3601
3622__syscall int k_mutex_unlock(struct k_mutex *mutex);
3623
3627
3637 _wait_q_t wait_q;
3638
3639#ifdef CONFIG_OBJ_CORE_CONDVAR
3640 struct k_obj_core obj_core;
3641#endif
3645};
3646
3650#define Z_CONDVAR_INITIALIZER(obj) \
3651 { \
3652 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
3653 }
3657
3663
3670__syscall int k_condvar_init(struct k_condvar *condvar);
3671
3678__syscall int k_condvar_signal(struct k_condvar *condvar);
3679
3687__syscall int k_condvar_broadcast(struct k_condvar *condvar);
3688
3706__syscall int k_condvar_wait(struct k_condvar *condvar, struct k_mutex *mutex,
3707 k_timeout_t timeout);
3708
3719#define K_CONDVAR_DEFINE(name) \
3720 STRUCT_SECTION_ITERABLE(k_condvar, name) = \
3721 Z_CONDVAR_INITIALIZER(name)
3722
3725
3731
3738struct k_sem {
3742 _wait_q_t wait_q;
3743 unsigned int count;
3744 unsigned int limit;
3745
3746 Z_DECL_POLL_EVENT
3747
3749
3750#ifdef CONFIG_OBJ_CORE_SEM
3751 struct k_obj_core obj_core;
3752#endif
3756};
3757
3761#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
3762 { \
3763 .wait_q = Z_WAIT_Q_INIT(&(obj).wait_q), \
3764 .count = (initial_count), \
3765 .limit = (count_limit), \
3766 Z_POLL_EVENT_OBJ_INIT(obj) \
3767 }
3771
3780#define K_SEM_MAX_LIMIT UINT_MAX
3781
3797__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
3798 unsigned int limit);
3799
3818__syscall int k_sem_take(struct k_sem *sem, k_timeout_t timeout);
3819
3830__syscall void k_sem_give(struct k_sem *sem);
3831
3844__syscall void k_sem_reset(struct k_sem *sem);
3845
3855__syscall unsigned int k_sem_count_get(struct k_sem *sem);
3856
3860static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
3861{
3862 return sem->count;
3863}
3864
3876#define K_SEM_DEFINE(name, initial_count, count_limit) \
3877 STRUCT_SECTION_ITERABLE(k_sem, name) = \
3878 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
3879 BUILD_ASSERT(((count_limit) != 0) && \
3880 (((initial_count) < (count_limit)) || ((initial_count) == (count_limit))) && \
3881 ((count_limit) <= K_SEM_MAX_LIMIT));
3882
3884
3885#if defined(CONFIG_SCHED_IPI_SUPPORTED) || defined(__DOXYGEN__)
3886struct k_ipi_work;
3887
3888
3896typedef void (*k_ipi_func_t)(struct k_ipi_work *work);
3897
3908 sys_dnode_t node[CONFIG_MP_MAX_NUM_CPUS]; /* Node in IPI work queue */
3909 k_ipi_func_t func; /* Function to execute on target CPU */
3910 struct k_event event; /* Event to signal when processed */
3911 uint32_t bitmask; /* Bitmask of targeted CPUs */
3915};
3916
3917
3925static inline void k_ipi_work_init(struct k_ipi_work *work)
3926{
3927 k_event_init(&work->event);
3928 for (unsigned int i = 0; i < CONFIG_MP_MAX_NUM_CPUS; i++) {
3929 sys_dnode_init(&work->node[i]);
3930 }
3931 work->bitmask = 0;
3932}
3933
3952int k_ipi_work_add(struct k_ipi_work *work, uint32_t cpu_bitmask,
3953 k_ipi_func_t func);
3954
3977int k_ipi_work_wait(struct k_ipi_work *work, k_timeout_t timeout);
3978
3988
3989#endif /* CONFIG_SCHED_IPI_SUPPORTED */
3990
3994struct k_work_delayable;
3995struct k_work_sync;
3999
4005
4012typedef void (*k_work_handler_t)(struct k_work *work);
4013
4027void k_work_init(struct k_work *work,
4028 k_work_handler_t handler);
4029
4044int k_work_busy_get(const struct k_work *work);
4045
4059static inline bool k_work_is_pending(const struct k_work *work);
4060
4082 struct k_work *work);
4083
4092int k_work_submit(struct k_work *work);
4093
4118bool k_work_flush(struct k_work *work,
4119 struct k_work_sync *sync);
4120
4140int k_work_cancel(struct k_work *work);
4141
4172bool k_work_cancel_sync(struct k_work *work, struct k_work_sync *sync);
4173
4183void k_work_queue_init(struct k_work_q *queue);
4184
4204void k_work_queue_start(struct k_work_q *queue,
4205 k_thread_stack_t *stack, size_t stack_size,
4206 int prio, const struct k_work_queue_config *cfg);
4207
4218void k_work_queue_run(struct k_work_q *queue, const struct k_work_queue_config *cfg);
4219
4229static inline k_tid_t k_work_queue_thread_get(struct k_work_q *queue);
4230
4254int k_work_queue_drain(struct k_work_q *queue, bool plug);
4255
4270
4290int k_work_queue_stop(struct k_work_q *queue, k_timeout_t timeout);
4291
4306 k_work_handler_t handler);
4307
4319static inline struct k_work_delayable *
4321
4336
4351static inline bool k_work_delayable_is_pending(
4352 const struct k_work_delayable *dwork);
4353
4368 const struct k_work_delayable *dwork);
4369
4384 const struct k_work_delayable *dwork);
4385
4414 struct k_work_delayable *dwork,
4415 k_timeout_t delay);
4416
4431 k_timeout_t delay);
4432
4469 struct k_work_delayable *dwork,
4470 k_timeout_t delay);
4471
4485 k_timeout_t delay);
4486
4512 struct k_work_sync *sync);
4513
4535
4565 struct k_work_sync *sync);
4566
4568enum {
4572 /* The atomic API is used for all work and queue flags fields to
4573 * enforce sequential consistency in SMP environments.
4574 */
4575
4576 /* Bits that represent the work item states. At least nine of the
4577 * combinations are distinct valid stable states.
4578 */
4579 K_WORK_RUNNING_BIT = 0,
4580 K_WORK_CANCELING_BIT = 1,
4581 K_WORK_QUEUED_BIT = 2,
4582 K_WORK_DELAYED_BIT = 3,
4583 K_WORK_FLUSHING_BIT = 4,
4584
4585 K_WORK_MASK = BIT(K_WORK_DELAYED_BIT) | BIT(K_WORK_QUEUED_BIT)
4586 | BIT(K_WORK_RUNNING_BIT) | BIT(K_WORK_CANCELING_BIT) | BIT(K_WORK_FLUSHING_BIT),
4587
4588 /* Static work flags */
4589 K_WORK_DELAYABLE_BIT = 8,
4590 K_WORK_DELAYABLE = BIT(K_WORK_DELAYABLE_BIT),
4591
4592 /* Dynamic work queue flags */
4593 K_WORK_QUEUE_STARTED_BIT = 0,
4594 K_WORK_QUEUE_STARTED = BIT(K_WORK_QUEUE_STARTED_BIT),
4595 K_WORK_QUEUE_BUSY_BIT = 1,
4596 K_WORK_QUEUE_BUSY = BIT(K_WORK_QUEUE_BUSY_BIT),
4597 K_WORK_QUEUE_DRAIN_BIT = 2,
4598 K_WORK_QUEUE_DRAIN = BIT(K_WORK_QUEUE_DRAIN_BIT),
4599 K_WORK_QUEUE_PLUGGED_BIT = 3,
4600 K_WORK_QUEUE_PLUGGED = BIT(K_WORK_QUEUE_PLUGGED_BIT),
4601 K_WORK_QUEUE_STOP_BIT = 4,
4602 K_WORK_QUEUE_STOP = BIT(K_WORK_QUEUE_STOP_BIT),
4603
4604 /* Static work queue flags */
4605 K_WORK_QUEUE_NO_YIELD_BIT = 8,
4606 K_WORK_QUEUE_NO_YIELD = BIT(K_WORK_QUEUE_NO_YIELD_BIT),
4610 /* Transient work flags */
4611
4617 K_WORK_RUNNING = BIT(K_WORK_RUNNING_BIT),
4618
4623 K_WORK_CANCELING = BIT(K_WORK_CANCELING_BIT),
4624
4630 K_WORK_QUEUED = BIT(K_WORK_QUEUED_BIT),
4631
4637 K_WORK_DELAYED = BIT(K_WORK_DELAYED_BIT),
4638
4643 K_WORK_FLUSHING = BIT(K_WORK_FLUSHING_BIT),
4644};
4645
4651struct k_work {
4655 /* All fields are protected by the work module spinlock. */
4656
4657 /* Node to link into k_work_q pending list. */
4658 sys_snode_t node;
4659
4660 /* The function to be invoked by the work queue thread. */
4661 k_work_handler_t handler;
4662
4663 /* The queue on which the work item was last submitted. */
4664 struct k_work_q *queue;
4665
4666 /* State of the work item.
4667 *
4668 * The item can be DELAYED, QUEUED, and RUNNING simultaneously.
4669 *
4670 * It can be RUNNING and CANCELING simultaneously.
4671 */
4676};
4677
4681#define Z_WORK_INITIALIZER(work_handler) { \
4682 .handler = (work_handler), \
4683}
4687
4697 /* The work item. */
4698 struct k_work work;
4699
4700 /* Timeout used to submit work after a delay. */
4701 struct _timeout timeout;
4702
4703 /* The queue to which the work should be submitted. */
4704 struct k_work_q *queue;
4708};
4709
4713#define Z_WORK_DELAYABLE_INITIALIZER(work_handler) { \
4714 .work = { \
4715 .handler = (work_handler), \
4716 .flags = K_WORK_DELAYABLE, \
4717 }, \
4718}
4722
4739#define K_WORK_DELAYABLE_DEFINE(work, work_handler) \
4740 struct k_work_delayable work \
4741 = Z_WORK_DELAYABLE_INITIALIZER(work_handler)
4742
4746/* Record used to wait for work to flush.
4747 *
4748 * The work item is inserted into the queue that will process (or is
4749 * processing) the item, and will be processed as soon as the item
4750 * completes. When the flusher is processed the semaphore will be
4751 * signaled, releasing the thread waiting for the flush.
4752 */
4753struct z_work_flusher {
4754 struct k_work work;
4755 struct k_sem sem;
4756};
4757
4758/* Record used to wait for work to complete a cancellation.
4759 *
4760 * The work item is inserted into a global queue of pending cancels.
4761 * When a cancelling work item goes idle any matching waiters are
4762 * removed from pending_cancels and are woken.
4763 */
4764struct z_work_canceller {
4765 sys_snode_t node;
4766 struct k_work *work;
4767 struct k_sem sem;
4768};
4772
4792 union {
4793 struct z_work_flusher flusher;
4794 struct z_work_canceller canceller;
4795 };
4799};
4800
4812 const char *name;
4813
4827
4832
4842};
4843
4849struct k_work_q {
4853 /* The thread that animates the work. */
4854 __deprecated struct k_thread thread;
4855
4856 /* The thread ID that animates the work. This may be an external thread
4857 * if k_work_queue_run() is used.
4858 */
4859 k_tid_t thread_id;
4860
4861 /* All the following fields must be accessed only while the
4862 * work module spinlock is held.
4863 */
4864
4865 /* List of k_work items to be worked. */
4866 sys_slist_t pending;
4867
4868 /* Wait queue for idle work thread. */
4869 _wait_q_t notifyq;
4870
4871 /* Wait queue for threads waiting for the queue to drain. */
4872 _wait_q_t drainq;
4873
4874 /* Flags describing queue state. */
4876
4877#if defined(CONFIG_WORKQUEUE_WORK_TIMEOUT)
4878 struct _timeout work_timeout_record;
4879 struct k_work *work;
4880 k_timeout_t work_timeout;
4881 bool finished;
4882#endif /* defined(CONFIG_WORKQUEUE_WORK_TIMEOUT) */
4886};
4887
4888/* Provide the implementation for inline functions declared above */
4889
4890static inline bool k_work_is_pending(const struct k_work *work)
4891{
4892 return k_work_busy_get(work) != 0;
4893}
4894
4895static inline struct k_work_delayable *
4897{
4898 return CONTAINER_OF(work, struct k_work_delayable, work);
4899}
4900
4902 const struct k_work_delayable *dwork)
4903{
4904 return k_work_delayable_busy_get(dwork) != 0;
4905}
4906
4908 const struct k_work_delayable *dwork)
4909{
4910 return z_timeout_expires(&dwork->timeout);
4911}
4912
4914 const struct k_work_delayable *dwork)
4915{
4916 return z_timeout_remaining(&dwork->timeout);
4917}
4918
4919static inline k_tid_t k_work_queue_thread_get(struct k_work_q *queue)
4920{
4921 return queue->thread_id;
4922}
4923
4925
4926struct k_work_user;
4927
4932
4942typedef void (*k_work_user_handler_t)(struct k_work_user *work);
4943
4947struct k_work_user_q {
4948 struct k_queue queue;
4949 struct k_thread thread;
4950};
4951
4952enum {
4953 K_WORK_USER_STATE_PENDING, /* Work item pending state */
4954};
4955
4956struct k_work_user {
4957 void *_reserved; /* Used by k_queue implementation. */
4958 k_work_user_handler_t handler;
4960};
4961
4962#if defined(__cplusplus) && ((__cplusplus - 0) < 202002L)
4963#define Z_WORK_USER_INITIALIZER(work_handler) { NULL, work_handler, 0 }
4964#else
4965#define Z_WORK_USER_INITIALIZER(work_handler) \
4966 { \
4967 ._reserved = NULL, \
4968 .handler = (work_handler), \
4969 .flags = 0 \
4970 }
4971#endif
4975
4987#define K_WORK_USER_DEFINE(work, work_handler) \
4988 struct k_work_user work = Z_WORK_USER_INITIALIZER(work_handler)
4989
4999static inline void k_work_user_init(struct k_work_user *work,
5000 k_work_user_handler_t handler)
5001{
5002 *work = (struct k_work_user)Z_WORK_USER_INITIALIZER(handler);
5003}
5004
5021static inline bool k_work_user_is_pending(struct k_work_user *work)
5022{
5023 return atomic_test_bit(&work->flags, K_WORK_USER_STATE_PENDING);
5024}
5025
5044static inline int k_work_user_submit_to_queue(struct k_work_user_q *work_q,
5045 struct k_work_user *work)
5046{
5047 int ret = -EBUSY;
5048
5049 if (!atomic_test_and_set_bit(&work->flags,
5050 K_WORK_USER_STATE_PENDING)) {
5051 ret = k_queue_alloc_append(&work_q->queue, work);
5052
5053 /* Couldn't insert into the queue. Clear the pending bit
5054 * so the work item can be submitted again
5055 */
5056 if (ret != 0) {
5057 atomic_clear_bit(&work->flags,
5058 K_WORK_USER_STATE_PENDING);
5059 }
5060 }
5061
5062 return ret;
5063}
5064
5084void k_work_user_queue_start(struct k_work_user_q *work_q,
5085 k_thread_stack_t *stack,
5086 size_t stack_size, int prio,
5087 const char *name);
5088
5099static inline k_tid_t k_work_user_queue_thread_get(struct k_work_user_q *work_q)
5100{
5101 return &work_q->thread;
5102}
5103
5105
5109struct k_work_poll {
5110 struct k_work work;
5111 struct k_work_q *workq;
5112 struct z_poller poller;
5113 struct k_poll_event *events;
5114 int num_events;
5115 k_work_handler_t real_handler;
5116 struct _timeout timeout;
5117 int poll_result;
5118};
5122
5127
5139#define K_WORK_DEFINE(work, work_handler) \
5140 struct k_work work = Z_WORK_INITIALIZER(work_handler)
5141
5151void k_work_poll_init(struct k_work_poll *work,
5152 k_work_handler_t handler);
5153
5189 struct k_work_poll *work,
5190 struct k_poll_event *events,
5191 int num_events,
5192 k_timeout_t timeout);
5193
5225int k_work_poll_submit(struct k_work_poll *work,
5226 struct k_poll_event *events,
5227 int num_events,
5228 k_timeout_t timeout);
5229
5244int k_work_poll_cancel(struct k_work_poll *work);
5245
5247
5253
5259struct k_msgq {
5264 _wait_q_t wait_q;
5266 struct k_spinlock lock;
5268 size_t msg_size;
5270 uint32_t max_msgs;
5272 char *buffer_start;
5274 char *buffer_end;
5276 char *read_ptr;
5278 char *write_ptr;
5280 uint32_t used_msgs;
5281
5282 Z_DECL_POLL_EVENT
5283
5285 uint8_t flags;
5286
5288
5289#ifdef CONFIG_OBJ_CORE_MSGQ
5290 struct k_obj_core obj_core;
5291#endif
5295};
5296
5300#define Z_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
5301 { \
5302 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
5303 .lock = {}, \
5304 .msg_size = q_msg_size, \
5305 .max_msgs = q_max_msgs, \
5306 .buffer_start = q_buffer, \
5307 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
5308 .read_ptr = q_buffer, \
5309 .write_ptr = q_buffer, \
5310 .used_msgs = 0, \
5311 Z_POLL_EVENT_OBJ_INIT(obj) \
5312 .flags = 0, \
5313 }
5314
5315#define K_MSGQ_FLAG_ALLOC BIT(0)
5319
5331
5332
5355#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
5356 static char __noinit __aligned(q_align) \
5357 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
5358 STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
5359 Z_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
5360 (q_msg_size), (q_max_msgs))
5361
5375#define K_MSGQ_DEFINE_STATIC(q_name, q_msg_size, q_max_msgs, q_align) \
5376 static char __noinit __aligned(q_align) \
5377 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
5378 static STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
5379 Z_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
5380 (q_msg_size), (q_max_msgs))
5381
5401#define K_MSGQ_DEFINE_TYPE(q_name, q_msg_type, q_max_msgs) \
5402 K_MSGQ_DEFINE(q_name, sizeof(q_msg_type), q_max_msgs, __alignof(q_msg_type))
5403
5415#define K_MSGQ_DEFINE_STATIC_TYPE(q_name, q_msg_type, q_max_msgs) \
5416 K_MSGQ_DEFINE_STATIC(q_name, sizeof(q_msg_type), q_max_msgs, __alignof(q_msg_type))
5417
5432void k_msgq_init(struct k_msgq *msgq, char *buffer, size_t msg_size,
5433 uint32_t max_msgs);
5434
5454__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
5455 uint32_t max_msgs);
5456
5470int k_msgq_cleanup(struct k_msgq *msgq);
5471
5492__syscall int k_msgq_put(struct k_msgq *msgq, const void *data, k_timeout_t timeout);
5493
5518__syscall int k_msgq_put_front(struct k_msgq *msgq, const void *data);
5519
5540__syscall int k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout);
5541
5556__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
5557
5574__syscall int k_msgq_peek_at(struct k_msgq *msgq, void *data, uint32_t idx);
5575
5585__syscall void k_msgq_purge(struct k_msgq *msgq);
5586
5597__syscall uint32_t k_msgq_num_free_get(struct k_msgq *msgq);
5598
5607__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
5608 struct k_msgq_attrs *attrs);
5609
5610
5611static inline uint32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
5612{
5613 return msgq->max_msgs - msgq->used_msgs;
5614}
5615
5625__syscall uint32_t k_msgq_num_used_get(struct k_msgq *msgq);
5626
5627static inline uint32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
5628{
5629 return msgq->used_msgs;
5630}
5631
5633
5639
5646 size_t size;
5650 void *tx_data;
5659 k_tid_t _syncing_thread;
5660#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
5662 struct k_sem *_async_sem;
5663#endif
5667};
5668
5673struct k_mbox {
5678 _wait_q_t tx_msg_queue;
5680 _wait_q_t rx_msg_queue;
5681 struct k_spinlock lock;
5682
5684
5685#ifdef CONFIG_OBJ_CORE_MAILBOX
5686 struct k_obj_core obj_core;
5687#endif
5691};
5692
5696#define Z_MBOX_INITIALIZER(obj) \
5697 { \
5698 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
5699 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
5700 }
5704
5714#define K_MBOX_DEFINE(name) \
5715 STRUCT_SECTION_ITERABLE(k_mbox, name) = \
5716 Z_MBOX_INITIALIZER(name) \
5717
5718
5725void k_mbox_init(struct k_mbox *mbox);
5726
5746int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
5747 k_timeout_t timeout);
5748
5762void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
5763 struct k_sem *sem);
5764
5782int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
5783 void *buffer, k_timeout_t timeout);
5784
5798void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
5799
5801
5807
5817__syscall void k_pipe_init(struct k_pipe *pipe, uint8_t *buffer, size_t buffer_size);
5818
5822enum pipe_flags {
5823 PIPE_FLAG_OPEN = BIT(0),
5824 PIPE_FLAG_RESET = BIT(1),
5825};
5829
5835struct k_pipe {
5839 size_t waiting;
5840 struct ring_buf buf;
5841 struct k_spinlock lock;
5842 _wait_q_t data;
5843 _wait_q_t space;
5844 uint8_t flags;
5845
5846 Z_DECL_POLL_EVENT
5847#ifdef CONFIG_OBJ_CORE_PIPE
5848 struct k_obj_core obj_core;
5849#endif
5854};
5855
5859#define Z_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
5860{ \
5861 .waiting = 0, \
5862 .buf = RING_BUF_INIT(pipe_buffer, pipe_buffer_size), \
5863 .data = Z_WAIT_Q_INIT(&obj.data), \
5864 .space = Z_WAIT_Q_INIT(&obj.space), \
5865 .flags = PIPE_FLAG_OPEN, \
5866 Z_POLL_EVENT_OBJ_INIT(obj) \
5867}
5871
5885#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
5886 static unsigned char __noinit __aligned(pipe_align) \
5887 _k_pipe_buf_##name[pipe_buffer_size]; \
5888 STRUCT_SECTION_ITERABLE(k_pipe, name) = \
5889 Z_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
5890
5891
5909__syscall int k_pipe_write(struct k_pipe *pipe, const uint8_t *data, size_t len,
5910 k_timeout_t timeout);
5911
5928__syscall int k_pipe_read(struct k_pipe *pipe, uint8_t *data, size_t len,
5929 k_timeout_t timeout);
5930
5940__syscall void k_pipe_reset(struct k_pipe *pipe);
5941
5950__syscall void k_pipe_close(struct k_pipe *pipe);
5952
5956struct k_mem_slab_info {
5957 uint32_t num_blocks;
5958 size_t block_size;
5959 uint32_t num_used;
5960#ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
5961 uint32_t max_used;
5962#endif
5963};
5964
5965struct k_mem_slab {
5966 _wait_q_t wait_q;
5967 struct k_spinlock lock;
5968 char *buffer;
5969 char *free_list;
5970 struct k_mem_slab_info info;
5971
5973
5974#ifdef CONFIG_OBJ_CORE_MEM_SLAB
5975 struct k_obj_core obj_core;
5976#endif
5977};
5978
5979#define Z_MEM_SLAB_INITIALIZER(_slab, _slab_buffer, _slab_block_size, \
5980 _slab_num_blocks) \
5981 { \
5982 .wait_q = Z_WAIT_Q_INIT(&(_slab).wait_q), \
5983 .lock = {}, \
5984 .buffer = _slab_buffer, \
5985 .free_list = NULL, \
5986 .info = {_slab_num_blocks, _slab_block_size, 0} \
5987 }
5991
5997
6023#define K_MEM_SLAB_DEFINE_IN_SECT(name, in_section, slab_block_size, slab_num_blocks, slab_align) \
6024 BUILD_ASSERT(((slab_block_size) % (slab_align)) == 0, \
6025 "slab_block_size must be a multiple of slab_align"); \
6026 BUILD_ASSERT((((slab_align) & ((slab_align) - 1)) == 0), \
6027 "slab_align must be a power of 2"); \
6028 char in_section __aligned(WB_UP( \
6029 slab_align)) _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
6030 STRUCT_SECTION_ITERABLE(k_mem_slab, name) = Z_MEM_SLAB_INITIALIZER( \
6031 name, _k_mem_slab_buf_##name, WB_UP(slab_block_size), slab_num_blocks)
6032
6056#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
6057 K_MEM_SLAB_DEFINE_IN_SECT(name, __noinit_named(k_mem_slab_buf_##name), slab_block_size, \
6058 slab_num_blocks, slab_align)
6059
6081#define K_MEM_SLAB_DEFINE_TYPE(name, type, slab_num_blocks) \
6082 K_MEM_SLAB_DEFINE(name, sizeof(type), slab_num_blocks, __alignof(type))
6083
6100#define K_MEM_SLAB_DEFINE_IN_SECT_STATIC(name, in_section, slab_block_size, slab_num_blocks, \
6101 slab_align) \
6102 BUILD_ASSERT(((slab_block_size) % (slab_align)) == 0, \
6103 "slab_block_size must be a multiple of slab_align"); \
6104 BUILD_ASSERT((((slab_align) & ((slab_align) - 1)) == 0), \
6105 "slab_align must be a power of 2"); \
6106 static char in_section __aligned(WB_UP( \
6107 slab_align)) _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
6108 static STRUCT_SECTION_ITERABLE(k_mem_slab, name) = Z_MEM_SLAB_INITIALIZER( \
6109 name, _k_mem_slab_buf_##name, WB_UP(slab_block_size), slab_num_blocks)
6110
6125#define K_MEM_SLAB_DEFINE_STATIC(name, slab_block_size, slab_num_blocks, slab_align) \
6126 K_MEM_SLAB_DEFINE_IN_SECT_STATIC(name, __noinit_named(k_mem_slab_buf_##name), \
6127 slab_block_size, slab_num_blocks, slab_align)
6128
6141#define K_MEM_SLAB_DEFINE_STATIC_TYPE(name, type, slab_num_blocks) \
6142 K_MEM_SLAB_DEFINE_STATIC(name, sizeof(type), slab_num_blocks, __alignof(type))
6143
6165int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
6166 size_t block_size, uint32_t num_blocks);
6167
6189int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
6190 k_timeout_t timeout);
6191
6203void k_mem_slab_free(struct k_mem_slab *slab, void *mem);
6204
6217static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
6218{
6219 return slab->info.num_used;
6220}
6221
6234static inline uint32_t k_mem_slab_max_used_get(struct k_mem_slab *slab)
6235{
6236#ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
6237 return slab->info.max_used;
6238#else
6239 ARG_UNUSED(slab);
6240 return 0;
6241#endif
6242}
6243
6256static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
6257{
6258 return slab->info.num_blocks - slab->info.num_used;
6259}
6260
6274
6275int k_mem_slab_runtime_stats_get(struct k_mem_slab *slab, struct sys_memory_stats *stats);
6276
6290int k_mem_slab_runtime_stats_reset_max(struct k_mem_slab *slab);
6291
6293
6298
6304struct k_heap {
6308 struct sys_heap heap;
6309 _wait_q_t wait_q;
6310 struct k_spinlock lock;
6314};
6315
6329void k_heap_init(struct k_heap *h, void *mem,
6330 size_t bytes) __attribute_nonnull(1);
6331
6353void *k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes,
6354 k_timeout_t timeout) __attribute_nonnull(1);
6355
6377void *k_heap_alloc(struct k_heap *h, size_t bytes,
6378 k_timeout_t timeout) __attribute_nonnull(1);
6379
6402void *k_heap_calloc(struct k_heap *h, size_t num, size_t size, k_timeout_t timeout)
6403 __attribute_nonnull(1);
6404
6428void *k_heap_realloc(struct k_heap *h, void *ptr, size_t bytes, k_timeout_t timeout)
6429 __attribute_nonnull(1);
6430
6441void k_heap_free(struct k_heap *h, void *mem) __attribute_nonnull(1);
6442
6443/*
6444 * Heap sizing constants computed at build time from actual struct layouts
6445 * in lib/heap/heap_constants.c via the gen_offset mechanism.
6446 */
6447#include <zephyr/heap_constants.h>
6448
6449/* chunk0 size in bytes for nb buckets (includes trailer metadata) */
6450#define _Z_HEAP_C0(nb) \
6451 (ROUND_UP(___z_heap_struct_SIZEOF + \
6452 (nb) * ___z_heap_bucket_SIZEOF, ___z_heap_chunk_unit_SIZEOF) + \
6453 ___z_heap_trailer_SIZEOF)
6454
6455/* Allocation chunk size in bytes (header + data rounded up, plus trailer) */
6456#define _Z_HEAP_AC(ab) \
6457 (ROUND_UP(___z_heap_hdr_SIZEOF + (ab), ___z_heap_chunk_unit_SIZEOF) + \
6458 ___z_heap_trailer_SIZEOF)
6459
6460/* Total heap size in chunk units */
6461#define _Z_HEAP_SZ(nb, ab) \
6462 ((_Z_HEAP_C0(nb) + _Z_HEAP_AC(ab)) / ___z_heap_chunk_unit_SIZEOF)
6463
6464/* Bucket count from heap size in chunk units (mirrors bucket_idx() + 1) */
6465#define _Z_HEAP_NB(sz) \
6466 (32 - __builtin_clz((unsigned int)((sz) - \
6467 ___z_heap_min_chunk_SIZEOF + 1)))
6468
6469/* 3-round convergent iteration starting from 1 bucket */
6470#define _Z_HEAP_NB1(ab) _Z_HEAP_NB(_Z_HEAP_SZ(1, ab))
6471#define _Z_HEAP_NB2(ab) _Z_HEAP_NB(_Z_HEAP_SZ(_Z_HEAP_NB1(ab), ab))
6472#define _Z_HEAP_NB3(ab) _Z_HEAP_NB(_Z_HEAP_SZ(_Z_HEAP_NB2(ab), ab))
6473
6487#define Z_HEAP_MIN_SIZE_FOR(alloc_bytes) \
6488 (_Z_HEAP_C0(_Z_HEAP_NB3(alloc_bytes)) + \
6489 _Z_HEAP_AC(alloc_bytes) + ___z_heap_ftr_SIZEOF)
6490
6491#define Z_HEAP_MIN_SIZE Z_HEAP_MIN_SIZE_FOR(1)
6492
6509#define Z_HEAP_DEFINE_IN_SECT(name, bytes, in_section) \
6510 char in_section \
6511 __aligned(8) /* CHUNK_UNIT */ \
6512 kheap_##name[MAX(bytes, Z_HEAP_MIN_SIZE)]; \
6513 STRUCT_SECTION_ITERABLE(k_heap, name) = { \
6514 .heap = { \
6515 .init_mem = kheap_##name, \
6516 .init_bytes = MAX(bytes, Z_HEAP_MIN_SIZE), \
6517 }, \
6518 }
6519
6534#define K_HEAP_DEFINE(name, bytes) \
6535 Z_HEAP_DEFINE_IN_SECT(name, bytes, \
6536 __noinit_named(kheap_buf_##name))
6537
6552#define K_HEAP_DEFINE_NOCACHE(name, bytes) \
6553 Z_HEAP_DEFINE_IN_SECT(name, bytes, __nocache)
6554
6564int k_heap_array_get(struct k_heap **heap);
6565
6569
6576
6595void *k_aligned_alloc(size_t align, size_t size);
6596
6608void *k_malloc(size_t size);
6609
6620void k_free(void *ptr);
6621
6633void *k_calloc(size_t nmemb, size_t size);
6634
6652void *k_realloc(void *ptr, size_t size);
6653
6655
6656/* polling API - PRIVATE */
6657
6658#ifdef CONFIG_POLL
6659#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
6660#else
6661#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
6662#endif
6663
6664/* private - types bit positions */
6665enum _poll_types_bits {
6666 /* can be used to ignore an event */
6667 _POLL_TYPE_IGNORE,
6668
6669 /* to be signaled by k_poll_signal_raise() */
6670 _POLL_TYPE_SIGNAL,
6671
6672 /* semaphore availability */
6673 _POLL_TYPE_SEM_AVAILABLE,
6674
6675 /* queue/FIFO/LIFO data availability */
6676 _POLL_TYPE_DATA_AVAILABLE,
6677
6678 /* msgq data availability */
6679 _POLL_TYPE_MSGQ_DATA_AVAILABLE,
6680
6681 /* pipe data availability */
6682 _POLL_TYPE_PIPE_DATA_AVAILABLE,
6683
6684 _POLL_NUM_TYPES
6685};
6686
6687#define Z_POLL_TYPE_BIT(type) (1U << ((type) - 1U))
6688
6689/* private - states bit positions */
6690enum _poll_states_bits {
6691 /* default state when creating event */
6692 _POLL_STATE_NOT_READY,
6693
6694 /* signaled by k_poll_signal_raise() */
6695 _POLL_STATE_SIGNALED,
6696
6697 /* semaphore is available */
6698 _POLL_STATE_SEM_AVAILABLE,
6699
6700 /* data is available to read on queue/FIFO/LIFO */
6701 _POLL_STATE_DATA_AVAILABLE,
6702
6703 /* queue/FIFO/LIFO wait was cancelled */
6704 _POLL_STATE_CANCELLED,
6705
6706 /* data is available to read on a message queue */
6707 _POLL_STATE_MSGQ_DATA_AVAILABLE,
6708
6709 /* data is available to read from a pipe */
6710 _POLL_STATE_PIPE_DATA_AVAILABLE,
6711
6712 _POLL_NUM_STATES
6713};
6714
6715#define Z_POLL_STATE_BIT(state) (1U << ((state) - 1U))
6716
6717#define _POLL_EVENT_NUM_UNUSED_BITS \
6718 (32 - (0 \
6719 + 8 /* tag */ \
6720 + _POLL_NUM_TYPES \
6721 + _POLL_NUM_STATES \
6722 + 1 /* modes */ \
6723 ))
6724
6725/* end of polling API - PRIVATE */
6726
6727
6735
6736/* Public polling API */
6737
6743
6745#define K_POLL_TYPE_IGNORE 0
6747#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
6749#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
6751#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
6753#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
6755#define K_POLL_TYPE_LIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
6757#define K_POLL_TYPE_MSGQ_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_MSGQ_DATA_AVAILABLE)
6759#define K_POLL_TYPE_PIPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_PIPE_DATA_AVAILABLE)
6760
6762
6772
6778
6780#define K_POLL_STATE_NOT_READY 0
6782#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
6784#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
6786#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
6788#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
6790#define K_POLL_STATE_LIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
6792#define K_POLL_STATE_MSGQ_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_MSGQ_DATA_AVAILABLE)
6794#define K_POLL_STATE_PIPE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_PIPE_DATA_AVAILABLE)
6796#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
6797
6799
6812 sys_dlist_t poll_events;
6816
6821 unsigned int signaled;
6822
6825};
6826
6832#define K_POLL_SIGNAL_INITIALIZER(obj) \
6833 { \
6834 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
6835 .signaled = 0, \
6836 .result = 0, \
6837 }
6838
6847 sys_dnode_t _node;
6848
6850 struct z_poller *poller;
6854
6857
6859 uint32_t type:_POLL_NUM_TYPES;
6860
6862 uint32_t state:_POLL_NUM_STATES;
6863
6866
6868 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
6869
6871 union {
6872 /* The _typed_* aliases below are used by the K_POLL_EVENT_*INITIALIZER() macros to
6873 * ensure type safety of polled objects.
6874 */
6876 void *obj, *_typed_K_POLL_TYPE_IGNORE;
6878 struct k_poll_signal *signal, *_typed_K_POLL_TYPE_SIGNAL;
6880 struct k_sem *sem, *_typed_K_POLL_TYPE_SEM_AVAILABLE;
6882 struct k_fifo *fifo, *_typed_K_POLL_TYPE_FIFO_DATA_AVAILABLE;
6884 struct k_lifo *lifo, *_typed_K_POLL_TYPE_LIFO_DATA_AVAILABLE;
6886 struct k_queue *queue, *_typed_K_POLL_TYPE_DATA_AVAILABLE;
6888 struct k_msgq *msgq, *_typed_K_POLL_TYPE_MSGQ_DATA_AVAILABLE;
6890 struct k_pipe *pipe, *_typed_K_POLL_TYPE_PIPE_DATA_AVAILABLE;
6891 };
6892};
6893
6902#define K_POLL_EVENT_INITIALIZER(_event_type, _event_mode, _event_obj) \
6903 { \
6904 .poller = NULL, \
6905 .type = _event_type, \
6906 .state = K_POLL_STATE_NOT_READY, \
6907 .mode = _event_mode, \
6908 .unused = 0, \
6909 { \
6910 ._typed_##_event_type = _event_obj, \
6911 }, \
6912 }
6913
6923#define K_POLL_EVENT_STATIC_INITIALIZER(_event_type, _event_mode, _event_obj, \
6924 event_tag) \
6925 { \
6926 .tag = event_tag, \
6927 .type = _event_type, \
6928 .state = K_POLL_STATE_NOT_READY, \
6929 .mode = _event_mode, \
6930 .unused = 0, \
6931 { \
6932 ._typed_##_event_type = _event_obj, \
6933 }, \
6934 }
6935
6950
6951void k_poll_event_init(struct k_poll_event *event, uint32_t type,
6952 int mode, void *obj);
6953
6996
6997__syscall int k_poll(struct k_poll_event *events, int num_events,
6998 k_timeout_t timeout);
6999
7007
7008__syscall void k_poll_signal_init(struct k_poll_signal *sig);
7009
7015__syscall void k_poll_signal_reset(struct k_poll_signal *sig);
7016
7027__syscall void k_poll_signal_check(struct k_poll_signal *sig,
7028 unsigned int *signaled, int *result);
7029
7052
7053__syscall int k_poll_signal_raise(struct k_poll_signal *sig, int result);
7054
7056
7075static inline void k_cpu_idle(void)
7076{
7077 arch_cpu_idle();
7078}
7079
7094static inline void k_cpu_atomic_idle(unsigned int key)
7095{
7097}
7098
7102
7107#ifdef ARCH_EXCEPT
7108/* This architecture has direct support for triggering a CPU exception */
7109#define z_except_reason(reason) ARCH_EXCEPT(reason)
7110#else
7111
7112#if defined(CONFIG_PRINTK) && !defined(CONFIG_ASSERT_NO_FILE_INFO)
7113#define __EXCEPT_LOC() printk("@ %s:%d\n", __FILE__, __LINE__)
7114#else
7115#define __EXCEPT_LOC()
7116#endif /* CONFIG_PRINTK */
7117
7118/* NOTE: This is the implementation for arches that do not implement
7119 * ARCH_EXCEPT() to generate a real CPU exception.
7120 *
7121 * We won't have a real exception frame to determine the PC value when
7122 * the oops occurred, so print file and line number before we jump into
7123 * the fatal error handler.
7124 */
7125#define z_except_reason(reason) do { \
7126 __EXCEPT_LOC(); \
7127 z_fatal_error(reason, NULL); \
7128 } while (false)
7129
7130#endif /* _ARCH__EXCEPT */
7134
7146#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
7147
7156#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
7157
7161/*
7162 * private APIs that are utilized by one or more public APIs
7163 */
7164
7168void z_timer_expiration_handler(struct _timeout *timeout);
7172
7173#ifdef CONFIG_PRINTK
7181__syscall void k_str_out(char *c, size_t n);
7182#endif
7183
7189
7210__syscall int k_float_disable(struct k_thread *thread);
7211
7250__syscall int k_float_enable(struct k_thread *thread, unsigned int options);
7251
7255
7265
7273
7282
7293
7304
7314
7323
7332
7333#ifdef __cplusplus
7334}
7335#endif
7336
7337#include <zephyr/tracing/tracing.h>
7338#include <zephyr/syscalls/kernel.h>
7339
7340#endif /* !_ASMLANGUAGE */
7341
7342#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */
static uint32_t arch_k_cycle_get_32(void)
Definition misc.h:26
static uint64_t arch_k_cycle_get_64(void)
Definition misc.h:33
void(* k_thread_entry_t)(void *p1, void *p2, void *p3)
Thread entry point function type.
Definition arch_interface.h:48
struct z_thread_stack_element k_thread_stack_t
Typedef of struct z_thread_stack_element.
Definition arch_interface.h:46
System error numbers.
void arch_cpu_atomic_idle(unsigned int key)
Atomically re-enable interrupts and enter low power mode.
void arch_cpu_idle(void)
Power save idle routine.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
static bool atomic_test_bit(const atomic_t *target, int bit)
Atomically get and test a bit.
Definition atomic.h:138
static void atomic_clear_bit(atomic_t *target, int bit)
Atomically clear a bit.
Definition atomic.h:227
static bool atomic_test_and_set_bit(atomic_t *target, int bit)
Atomically set a bit and test it.
Definition atomic.h:181
static uint32_t k_cycle_get_32(void)
Read the hardware clock.
Definition kernel.h:2288
#define K_NO_WAIT
Generate null timeout delay.
Definition kernel.h:1602
int64_t k_uptime_ticks(void)
Get system uptime, in system ticks.
static uint32_t k_uptime_get_32(void)
Get system uptime (32-bit version).
Definition kernel.h:2240
uint32_t k_ticks_t
Tick precision used in timeout APIs.
Definition clock.h:48
static int64_t k_uptime_delta(int64_t *reftime)
Get elapsed time, and update the referenced time.
Definition kernel.h:2269
static uint32_t k_uptime_seconds(void)
Get system uptime in seconds.
Definition kernel.h:2253
static uint64_t k_cycle_get_64(void)
Read the 64-bit hardware clock.
Definition kernel.h:2306
static int64_t k_uptime_get(void)
Get system uptime.
Definition kernel.h:2216
int k_condvar_signal(struct k_condvar *condvar)
Signals one thread that is pending on the condition variable.
int k_condvar_wait(struct k_condvar *condvar, struct k_mutex *mutex, k_timeout_t timeout)
Waits on the condition variable releasing the mutex lock.
int k_condvar_init(struct k_condvar *condvar)
Initialize a condition variable.
int k_condvar_broadcast(struct k_condvar *condvar)
Unblock all threads that are pending on the condition variable.
static void k_cpu_idle(void)
Make the CPU idle.
Definition kernel.h:7075
static void k_cpu_atomic_idle(unsigned int key)
Make the CPU idle in an atomic fashion.
Definition kernel.h:7094
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
static void sys_dnode_init(sys_dnode_t *node)
initialize node to its state when not in a list
Definition dlist.h:224
uint32_t k_event_wait(struct k_event *event, uint32_t events, bool reset, k_timeout_t timeout)
Wait for any of the specified events.
uint32_t k_event_set_masked(struct k_event *event, uint32_t events, uint32_t events_mask)
Set or clear the events in an event object.
uint32_t k_event_wait_all_safe(struct k_event *event, uint32_t events, bool reset, k_timeout_t timeout)
Wait for all of the specified events (safe version).
static uint32_t k_event_test(struct k_event *event, uint32_t events_mask)
Test the events currently tracked in the event object.
Definition kernel.h:2928
uint32_t k_event_wait_safe(struct k_event *event, uint32_t events, bool reset, k_timeout_t timeout)
Wait for any of the specified events (safe version).
uint32_t k_event_set(struct k_event *event, uint32_t events)
Set the events in an event object.
uint32_t k_event_post(struct k_event *event, uint32_t events)
Post one or more events to an event object.
void k_event_init(struct k_event *event)
Initialize an event object.
uint32_t k_event_clear(struct k_event *event, uint32_t events)
Clear the events in an event object.
uint32_t k_event_wait_all(struct k_event *event, uint32_t events, bool reset, k_timeout_t timeout)
Wait for all of the specified events.
static bool sys_sflist_is_empty(const sys_sflist_t *list)
Test if the given list is empty.
Definition sflist.h:339
struct _sflist sys_sflist_t
Flagged single-linked list structure.
Definition sflist.h:57
int k_float_disable(struct k_thread *thread)
Disable preservation of floating point context information.
int k_float_enable(struct k_thread *thread, unsigned int options)
Enable preservation of floating point context information.
int k_futex_wait(struct k_futex *futex, int expected, k_timeout_t timeout)
Pend the current thread on a futex.
int k_futex_wake(struct k_futex *futex, bool wake_all)
Wake one/all threads pending on a futex.
void * k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout)
Allocate memory from a k_heap.
int k_heap_array_get(struct k_heap **heap)
Get the array of statically defined heaps.
void * k_heap_calloc(struct k_heap *h, size_t num, size_t size, k_timeout_t timeout)
Allocate and initialize memory for an array of objects from a k_heap.
void k_heap_free(struct k_heap *h, void *mem)
Free memory allocated by k_heap_alloc().
void k_free(void *ptr)
Free memory allocated from heap.
void * k_realloc(void *ptr, size_t size)
Expand the size of an existing allocation.
void k_heap_init(struct k_heap *h, void *mem, size_t bytes)
Initialize a k_heap.
void * k_malloc(size_t size)
Allocate memory from the heap.
void * k_heap_realloc(struct k_heap *h, void *ptr, size_t bytes, k_timeout_t timeout)
Reallocate memory from a k_heap.
void * k_calloc(size_t nmemb, size_t size)
Allocate memory from heap, array style.
void * k_aligned_alloc(size_t align, size_t size)
Allocate memory from the heap with a specified alignment.
void * k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes, k_timeout_t timeout)
Allocate aligned memory from a k_heap.
bool k_is_in_isr(void)
Determine if code is running at interrupt level.
int k_is_preempt_thread(void)
Determine if code is running in a preemptible thread.
execution_context_types
Types of execution contexts.
Definition kernel.h:138
@ K_ISR
Executing in an interrupt service routine.
Definition kernel.h:139
@ K_COOP_THREAD
Executing in a cooperative thread.
Definition kernel.h:140
@ K_PREEMPT_THREAD
Executing in a preemptible thread.
Definition kernel.h:141
int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, void *buffer, k_timeout_t timeout)
Receive a mailbox message.
void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer)
Retrieve mailbox message data into a buffer.
void k_mbox_init(struct k_mbox *mbox)
Initialize a mailbox.
int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, k_timeout_t timeout)
Send a mailbox message in a synchronous manner.
void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, struct k_sem *sem)
Send a mailbox message in an asynchronous manner.
int k_mem_slab_init(struct k_mem_slab *slab, void *buffer, size_t block_size, uint32_t num_blocks)
Initialize a memory slab.
void k_mem_slab_free(struct k_mem_slab *slab, void *mem)
Free memory allocated from a memory slab.
int k_mem_slab_runtime_stats_get(struct k_mem_slab *slab, struct sys_memory_stats *stats)
Get the memory stats for a memory slab.
int k_mem_slab_runtime_stats_reset_max(struct k_mem_slab *slab)
Reset the maximum memory usage for a slab.
int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
Allocate memory from a memory slab.
static uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Get the number of used blocks in a memory slab.
Definition kernel.h:6217
static uint32_t k_mem_slab_max_used_get(struct k_mem_slab *slab)
Get the number of maximum used blocks so far in a memory slab.
Definition kernel.h:6234
static uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Get the number of unused blocks in a memory slab.
Definition kernel.h:6256
int k_msgq_peek(struct k_msgq *msgq, void *data)
Peek/read a message from a message queue.
uint32_t k_msgq_num_used_get(struct k_msgq *msgq)
Get the number of messages in a message queue.
void k_msgq_init(struct k_msgq *msgq, char *buffer, size_t msg_size, uint32_t max_msgs)
Initialize a message queue.
int k_msgq_put(struct k_msgq *msgq, const void *data, k_timeout_t timeout)
Send a message to the end of a message queue.
int k_msgq_peek_at(struct k_msgq *msgq, void *data, uint32_t idx)
Peek/read a message from a message queue at the specified index.
uint32_t k_msgq_num_free_get(struct k_msgq *msgq)
Get the amount of free space in a message queue.
void k_msgq_get_attrs(struct k_msgq *msgq, struct k_msgq_attrs *attrs)
Get basic attributes of a message queue.
void k_msgq_purge(struct k_msgq *msgq)
Purge a message queue.
int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size, uint32_t max_msgs)
Initialize a message queue.
int k_msgq_put_front(struct k_msgq *msgq, const void *data)
Send a message to the front of a message queue.
int k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout)
Receive a message from a message queue.
int k_msgq_cleanup(struct k_msgq *msgq)
Release allocated buffer for a queue.
int k_mutex_unlock(struct k_mutex *mutex)
Unlock a mutex.
int k_mutex_init(struct k_mutex *mutex)
Initialize a mutex.
int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout)
Lock a mutex.
int k_pipe_write(struct k_pipe *pipe, const uint8_t *data, size_t len, k_timeout_t timeout)
Write data to a pipe.
void k_pipe_close(struct k_pipe *pipe)
Close a pipe.
void k_pipe_reset(struct k_pipe *pipe)
Reset a pipe This routine resets the pipe, discarding any unread data and unblocking any threads wait...
void k_pipe_init(struct k_pipe *pipe, uint8_t *buffer, size_t buffer_size)
initialize a pipe
int k_pipe_read(struct k_pipe *pipe, uint8_t *data, size_t len, k_timeout_t timeout)
Read data from a pipe This routine reads up to len bytes of data from pipe.
void k_poll_signal_reset(struct k_poll_signal *sig)
Reset a poll signal object's state to unsignaled.
k_poll_modes
Modes of operation of a poll event.
Definition kernel.h:6764
void k_poll_signal_check(struct k_poll_signal *sig, unsigned int *signaled, int *result)
Fetch the signaled state and result value of a poll signal.
void k_poll_event_init(struct k_poll_event *event, uint32_t type, int mode, void *obj)
Initialize one struct k_poll_event instance.
int k_poll(struct k_poll_event *events, int num_events, k_timeout_t timeout)
Wait for one or many of multiple poll events to occur.
int k_poll_signal_raise(struct k_poll_signal *sig, int result)
Signal a poll signal object.
void k_poll_signal_init(struct k_poll_signal *sig)
Initialize a poll signal object.
@ K_POLL_MODE_NOTIFY_ONLY
Polling thread is notified of object availability, but does not take ownership of the object.
Definition kernel.h:6768
@ K_POLL_NUM_MODES
Number of poll modes.
Definition kernel.h:6770
void k_queue_init(struct k_queue *queue)
Initialize a queue.
void * k_queue_get(struct k_queue *queue, k_timeout_t timeout)
Get an element from a queue.
void * k_queue_peek_tail(struct k_queue *queue)
Peek element at the tail of queue.
bool k_queue_unique_append(struct k_queue *queue, void *data)
Append an element to a queue only if it's not present already.
bool k_queue_remove(struct k_queue *queue, void *data)
Remove an element from a queue.
int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list)
Atomically add a list of elements to a queue.
int32_t k_queue_alloc_append(struct k_queue *queue, void *data)
Append an element to a queue.
void k_queue_cancel_wait(struct k_queue *queue)
Cancel waiting on a queue.
void * k_queue_peek_head(struct k_queue *queue)
Peek element at the head of queue.
void k_queue_prepend(struct k_queue *queue, void *data)
Prepend an element to a queue.
int k_queue_append_list(struct k_queue *queue, void *head, void *tail)
Atomically append a list of elements to a queue.
void k_queue_append(struct k_queue *queue, void *data)
Append an element to the end of a queue.
int32_t k_queue_alloc_prepend(struct k_queue *queue, void *data)
Prepend an element to a queue.
void k_queue_insert(struct k_queue *queue, void *prev, void *data)
Inserts an element to a queue.
int k_queue_is_empty(struct k_queue *queue)
Query a queue to see if it has data available.
void k_sem_reset(struct k_sem *sem)
Resets a semaphore's count to zero.
unsigned int k_sem_count_get(struct k_sem *sem)
Get a semaphore's count.
void k_sem_give(struct k_sem *sem)
Give a semaphore.
int k_sem_take(struct k_sem *sem, k_timeout_t timeout)
Take a semaphore.
int k_sem_init(struct k_sem *sem, unsigned int initial_count, unsigned int limit)
Initialize a semaphore.
struct _slist sys_slist_t
Single-linked list structure.
Definition slist.h:54
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:44
int k_stack_pop(struct k_stack *stack, stack_data_t *data, k_timeout_t timeout)
Pop an element from a stack.
void k_stack_init(struct k_stack *stack, stack_data_t *buffer, uint32_t num_entries)
Initialize a stack.
int k_stack_cleanup(struct k_stack *stack)
Release a stack's allocated buffer.
int k_stack_push(struct k_stack *stack, stack_data_t data)
Push an element onto a stack.
int32_t k_stack_alloc_init(struct k_stack *stack, uint32_t num_entries)
Initialize a stack.
#define SYS_PORT_TRACING_TRACKING_FIELD(type)
Field added to kernel objects so they are tracked.
Definition tracing_macros.h:375
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:154
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define CONTAINER_OF(ptr, type, field)
Get a pointer to a structure containing the element.
Definition util.h:281
#define EBUSY
Mount device busy.
Definition errno.h:55
int k_thread_name_copy(k_tid_t thread, char *buf, size_t size)
Copy the thread name into a supplied buffer.
void k_yield(void)
Yield the current thread.
const char * k_thread_state_str(k_tid_t thread_id, char *buf, size_t buf_size)
Get thread state string.
void k_thread_resume(k_tid_t thread)
Resume a suspended thread.
void * k_thread_custom_data_get(void)
Get current thread's custom data.
void k_thread_abort(k_tid_t thread)
Abort a thread.
int k_thread_name_set(k_tid_t thread, const char *str)
Set current thread name.
void k_thread_priority_set(k_tid_t thread, int prio)
Set a thread's priority.
void k_thread_absolute_deadline_set(k_tid_t thread, int deadline)
Set absolute deadline expiration time for scheduler.
int k_thread_cpu_mask_enable(k_tid_t thread, int cpu)
Enable thread to run on specified CPU.
void k_thread_foreach_unlocked(k_thread_user_cb_t user_cb, void *user_data)
Iterate over all the threads in the system without locking.
bool k_can_yield(void)
Check whether it is possible to yield in the current context.
int k_thread_priority_get(k_tid_t thread)
Get a thread's priority.
static void k_thread_heap_assign(struct k_thread *thread, struct k_heap *heap)
Assign a resource memory pool to a thread.
Definition kernel.h:577
FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry, void *p1, void *p2, void *p3)
Drop a thread's privileges permanently to user mode.
int k_thread_join(struct k_thread *thread, k_timeout_t timeout)
Sleep until a thread exits.
k_ticks_t k_thread_timeout_remaining_ticks(const struct k_thread *thread)
Get time remaining before a thread wakes up, in system ticks.
void k_thread_custom_data_set(void *value)
Set current thread's custom data.
void k_sched_lock(void)
Lock the scheduler.
void k_busy_wait(uint32_t usec_to_wait)
Cause the current thread to busy wait.
void k_thread_time_slice_set(struct k_thread *th, int32_t slice_ticks, k_thread_timeslice_fn_t expired, void *data)
Set thread time slice.
static void k_thread_runtime_stats_longest_frame_reset(__maybe_unused struct k_thread *thread)
Resets thread longest frame usage data for specified thread.
Definition kernel.h:167
void k_thread_suspend(k_tid_t thread)
Suspend a thread.
void k_sched_unlock(void)
Unlock the scheduler.
static __attribute_const__ k_tid_t k_current_get(void)
Get thread ID of the current thread.
Definition kernel.h:859
int k_thread_cpu_mask_clear(k_tid_t thread)
Sets all CPU enable masks to zero.
void k_thread_foreach_filter_by_cpu(unsigned int cpu, k_thread_user_cb_t user_cb, void *user_data)
Iterate over all the threads in running on specified cpu.
void k_sched_time_slice_set(int32_t slice, int prio)
Set time-slicing period and scope.
int k_thread_cpu_mask_disable(k_tid_t thread, int cpu)
Prevent thread to run on specified CPU.
void k_wakeup(k_tid_t thread)
Wake up a sleeping thread.
int k_thread_stack_free(k_thread_stack_t *stack)
Free a dynamically allocated thread stack.
k_ticks_t k_thread_timeout_expires_ticks(const struct k_thread *thread)
Get time when a thread wakes up, in system ticks.
__attribute_const__ k_tid_t k_sched_current_thread_query(void)
Query thread ID of the current thread.
static void k_thread_start(k_tid_t thread)
Start an inactive thread.
Definition kernel.h:1348
k_tid_t k_thread_create(struct k_thread *new_thread, k_thread_stack_t *stack, size_t stack_size, k_thread_entry_t entry, void *p1, void *p2, void *p3, int prio, uint32_t options, k_timeout_t delay)
Create a thread.
void k_reschedule(void)
Invoke the scheduler.
void k_thread_deadline_set(k_tid_t thread, int deadline)
Set relative deadline expiration time for scheduler.
void k_thread_foreach_unlocked_filter_by_cpu(unsigned int cpu, k_thread_user_cb_t user_cb, void *user_data)
Iterate over the threads in running on current cpu without locking.
const char * k_thread_name_get(k_tid_t thread)
Get thread name.
void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data)
Iterate over all the threads in the system.
static bool k_is_pre_kernel(void)
Test whether startup is in the before-main-task phase.
Definition kernel.h:826
int k_thread_cpu_pin(k_tid_t thread, int cpu)
Pin a thread to a CPU.
int k_thread_cpu_mask_enable_all(k_tid_t thread)
Sets all CPU enable masks to one.
void(* k_thread_user_cb_t)(const struct k_thread *thread, void *user_data)
Callback type used by thread iteration functions.
Definition kernel.h:180
k_thread_stack_t * k_thread_stack_alloc(size_t size, int flags)
Dynamically allocate a thread stack.
k_ticks_t k_timer_expires_ticks(const struct k_timer *timer)
Get next expiration time of a timer, in system ticks.
void(* k_timer_stop_t)(struct k_timer *timer)
Timer stop function type.
Definition kernel.h:1932
k_ticks_t k_timer_remaining_ticks(const struct k_timer *timer)
Get time remaining before a timer next expires, in system ticks.
void * k_timer_user_data_get(const struct k_timer *timer)
Retrieve the user-specific data from a timer.
void(* k_timer_expiry_t)(struct k_timer *timer)
Timer expiry function type.
Definition kernel.h:1916
void k_timer_init(struct k_timer *timer, k_timer_expiry_t expiry_fn, k_timer_stop_t stop_fn)
Initialize a timer.
int k_timer_cleanup(struct k_timer *timer)
Clean up a dynamically allocated timer before freeing it.
void k_timer_start(struct k_timer *timer, k_timeout_t duration, k_timeout_t period)
Start a timer.
static uint32_t k_timer_remaining_get(struct k_timer *timer)
Get time remaining before a timer next expires.
Definition kernel.h:2118
uint32_t k_timer_status_sync(struct k_timer *timer)
Synchronize thread to timer expiration.
void k_timer_stop(struct k_timer *timer)
Stop a timer.
uint32_t k_timer_status_get(struct k_timer *timer)
Read timer status.
void k_timer_user_data_set(struct k_timer *timer, void *user_data)
Associate user-specific data with a timer.
#define k_ticks_to_ms_ceil32(t)
Convert ticks to milliseconds.
Definition time_units.h:1782
#define k_ticks_to_sec_floor32(t)
Convert ticks to seconds.
Definition time_units.h:1622
#define k_ticks_to_ms_floor64(t)
Convert ticks to milliseconds.
Definition time_units.h:1734
int k_work_poll_submit_to_queue(struct k_work_q *work_q, struct k_work_poll *work, struct k_poll_event *events, int num_events, k_timeout_t timeout)
Submit a triggered work item.
static k_tid_t k_work_queue_thread_get(struct k_work_q *queue)
Access the thread that animates a work queue.
Definition kernel.h:4919
static bool k_work_is_pending(const struct k_work *work)
Test whether a work item is currently pending.
Definition kernel.h:4890
int k_work_queue_drain(struct k_work_q *queue, bool plug)
Wait until the work queue has drained, optionally plugging it.
static k_ticks_t k_work_delayable_expires_get(const struct k_work_delayable *dwork)
Get the absolute tick count at which a scheduled delayable work will be submitted.
Definition kernel.h:4907
int k_work_schedule_for_queue(struct k_work_q *queue, struct k_work_delayable *dwork, k_timeout_t delay)
Submit an idle work item to a queue after a delay.
int k_work_delayable_busy_get(const struct k_work_delayable *dwork)
Busy state flags from the delayable work item.
int k_work_queue_stop(struct k_work_q *queue, k_timeout_t timeout)
Stop a work queue.
void k_work_init_delayable(struct k_work_delayable *dwork, k_work_handler_t handler)
Initialize a delayable work structure.
int k_work_poll_cancel(struct k_work_poll *work)
Cancel a triggered work item.
void k_work_user_queue_start(struct k_work_user_q *work_q, k_thread_stack_t *stack, size_t stack_size, int prio, const char *name)
Start a workqueue in user mode.
void k_work_poll_init(struct k_work_poll *work, k_work_handler_t handler)
Initialize a triggered work item.
int k_work_cancel(struct k_work *work)
Cancel a work item.
static int k_work_user_submit_to_queue(struct k_work_user_q *work_q, struct k_work_user *work)
Submit a work item to a user mode workqueue.
Definition kernel.h:5044
int k_work_submit_to_queue(struct k_work_q *queue, struct k_work *work)
Submit a work item to a queue.
static bool k_work_user_is_pending(struct k_work_user *work)
Check if a userspace work item is pending.
Definition kernel.h:5021
void(* k_work_handler_t)(struct k_work *work)
The signature for a work item handler function.
Definition kernel.h:4012
int k_work_schedule(struct k_work_delayable *dwork, k_timeout_t delay)
Submit an idle work item to the system work queue after a delay.
static bool k_work_delayable_is_pending(const struct k_work_delayable *dwork)
Test whether a delayed work item is currently pending.
Definition kernel.h:4901
bool k_work_cancel_delayable_sync(struct k_work_delayable *dwork, struct k_work_sync *sync)
Cancel delayable work and wait.
int k_work_cancel_delayable(struct k_work_delayable *dwork)
Cancel delayable work.
static void k_work_user_init(struct k_work_user *work, k_work_user_handler_t handler)
Initialize a userspace work item.
Definition kernel.h:4999
int k_work_queue_unplug(struct k_work_q *queue)
Release a work queue to accept new submissions.
int k_work_reschedule(struct k_work_delayable *dwork, k_timeout_t delay)
Reschedule a work item to the system work queue after a delay.
void(* k_work_user_handler_t)(struct k_work_user *work)
Work item handler function type for user work queues.
Definition kernel.h:4942
bool k_work_cancel_sync(struct k_work *work, struct k_work_sync *sync)
Cancel a work item and wait for it to complete.
static k_tid_t k_work_user_queue_thread_get(struct k_work_user_q *work_q)
Access the user mode thread that animates a work queue.
Definition kernel.h:5099
int k_work_busy_get(const struct k_work *work)
Busy state flags from the work item.
static struct k_work_delayable * k_work_delayable_from_work(struct k_work *work)
Get the parent delayable work structure from a work pointer.
Definition kernel.h:4896
static k_ticks_t k_work_delayable_remaining_get(const struct k_work_delayable *dwork)
Get the number of ticks until a scheduled delayable work will be submitted.
Definition kernel.h:4913
bool k_work_flush(struct k_work *work, struct k_work_sync *sync)
Wait for last-submitted instance to complete.
int k_work_reschedule_for_queue(struct k_work_q *queue, struct k_work_delayable *dwork, k_timeout_t delay)
Reschedule a work item to a queue after a delay.
void k_work_queue_run(struct k_work_q *queue, const struct k_work_queue_config *cfg)
Run work queue using calling thread.
int k_work_submit(struct k_work *work)
Submit a work item to the system queue.
bool k_work_flush_delayable(struct k_work_delayable *dwork, struct k_work_sync *sync)
Flush delayable work.
int k_work_poll_submit(struct k_work_poll *work, struct k_poll_event *events, int num_events, k_timeout_t timeout)
Submit a triggered work item to the system workqueue.
void k_work_queue_init(struct k_work_q *queue)
Initialize a work queue structure.
void k_work_queue_start(struct k_work_q *queue, k_thread_stack_t *stack, size_t stack_size, int prio, const struct k_work_queue_config *cfg)
Initialize a work queue.
void k_work_init(struct k_work *work, k_work_handler_t handler)
Initialize a (non-delayable) work structure.
@ K_WORK_CANCELING
Flag indicating a work item that is being canceled.
Definition kernel.h:4623
@ K_WORK_QUEUED
Flag indicating a work item that has been submitted to a queue but has not started running.
Definition kernel.h:4630
@ K_WORK_DELAYED
Flag indicating a delayed work item that is scheduled for submission to a queue.
Definition kernel.h:4637
@ K_WORK_RUNNING
Flag indicating a work item that is running under a work queue thread.
Definition kernel.h:4617
@ K_WORK_FLUSHING
Flag indicating a synced work item that is being flushed.
Definition kernel.h:4643
#define BUILD_ASSERT(EXPR, MSG...)
Definition llvm.h:51
struct k_thread * k_tid_t
Definition thread.h:415
struct k_thread_runtime_stats k_thread_runtime_stats_t
void k_sys_runtime_stats_disable(void)
Disable gathering of system runtime statistics.
int k_thread_runtime_stats_enable(k_tid_t thread)
Enable gathering of runtime statistics for specified thread.
int k_ipi_work_add(struct k_ipi_work *work, uint32_t cpu_bitmask, k_ipi_func_t func)
Add an IPI work item to the IPI work queue.
void k_sys_runtime_stats_enable(void)
Enable gathering of system runtime statistics.
int k_thread_runtime_stats_get(k_tid_t thread, k_thread_runtime_stats_t *stats)
Get the runtime statistics of a thread.
bool k_thread_runtime_stats_is_enabled(k_tid_t thread)
Check if runtime statistics gathering is enabled for a thread.
void k_ipi_work_signal(void)
Signal that there is one or more IPI work items to process.
int k_ipi_work_wait(struct k_ipi_work *work, k_timeout_t timeout)
Wait until the IPI work item has been processed by all targeted CPUs.
void(* k_ipi_func_t)(struct k_ipi_work *work)
IPI work item handler function type.
Definition kernel.h:3896
int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats)
Get the runtime statistics of all threads.
static void k_ipi_work_init(struct k_ipi_work *work)
Initialize the specified IPI work item.
Definition kernel.h:3925
int k_thread_runtime_stats_disable(k_tid_t thread)
Disable gathering of runtime statistics for specified thread.
int k_thread_runtime_stats_cpu_get(int cpu, k_thread_runtime_stats_t *stats)
Get the runtime statistics of all threads on specified cpu.
Header files included by kernel.h.
void(* k_thread_timeslice_fn_t)(struct k_thread *thread, void *data)
Definition kernel_structs.h:357
Memory Statistics.
flags
Definition parser.h:97
state
Definition parser_state.h:29
Header file for the ring buffer API.
Thread sleep APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INTPTR_TYPE__ intptr_t
Definition stdint.h:104
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
__INT64_TYPE__ int64_t
Definition stdint.h:75
Kernel condition variable structure.
Definition kernel.h:3633
Event Structure.
Definition kernel.h:2695
Kernel FIFO structure.
Definition kernel.h:2953
futex structure
Definition kernel.h:2623
atomic_t val
Futex value.
Definition kernel.h:2629
Kernel synchronized heap structure.
Definition kernel.h:6304
IPI work item structure.
Definition kernel.h:3904
Kernel LIFO structure.
Definition kernel.h:3211
Mailbox Message Structure.
Definition kernel.h:5644
k_tid_t tx_target_thread
target thread id
Definition kernel.h:5654
void * tx_data
sender's message data buffer
Definition kernel.h:5650
k_tid_t rx_source_thread
source thread id
Definition kernel.h:5652
uint32_t info
application-defined information value
Definition kernel.h:5648
size_t size
size of message (in bytes)
Definition kernel.h:5646
Mailbox Structure.
Definition kernel.h:5673
Memory Domain.
Definition mem_domain.h:80
Memory Partition.
Definition mem_domain.h:55
Message Queue Attributes.
Definition kernel.h:5323
uint32_t used_msgs
Used messages.
Definition kernel.h:5329
size_t msg_size
Message Size.
Definition kernel.h:5325
uint32_t max_msgs
Maximal number of messages.
Definition kernel.h:5327
Message Queue Structure.
Definition kernel.h:5259
Kernel mutex structure.
Definition kernel.h:3504
Object core structure.
Definition obj_core.h:123
Kernel pipe structure.
Definition kernel.h:5835
Poll Event.
Definition kernel.h:6842
struct k_poll_signal * signal
Poll signal being polled.
Definition kernel.h:6878
struct k_pipe * pipe
Pipe being polled.
Definition kernel.h:6890
uint32_t tag
optional user-specified tag, opaque, untouched by the API
Definition kernel.h:6856
struct k_fifo * fifo
FIFO being polled.
Definition kernel.h:6882
struct k_msgq * msgq
Message queue being polled.
Definition kernel.h:6888
struct k_queue * queue
Queue being polled.
Definition kernel.h:6886
uint32_t unused
unused bits in 32-bit word
Definition kernel.h:6868
uint32_t type
bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values)
Definition kernel.h:6859
struct k_sem * sem
Semaphore being polled.
Definition kernel.h:6880
uint32_t state
bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values)
Definition kernel.h:6862
uint32_t mode
mode of operation, from enum k_poll_modes
Definition kernel.h:6865
void * obj
Generic object pointer.
Definition kernel.h:6876
struct k_lifo * lifo
LIFO being polled.
Definition kernel.h:6884
Poll signal object.
Definition kernel.h:6807
int result
custom result value passed to k_poll_signal_raise() if needed
Definition kernel.h:6824
unsigned int signaled
1 if the event has been signaled, 0 otherwise.
Definition kernel.h:6821
Kernel queue structure.
Definition kernel.h:2327
Semaphore structure.
Definition kernel.h:3738
Kernel Spin Lock.
Definition spinlock.h:45
Thread Structure.
Definition thread.h:259
struct _thread_base base
Definition thread.h:261
struct k_heap * resource_pool
resource pool
Definition thread.h:364
struct __thread_entry entry
thread entry and parameters description
Definition thread.h:303
Kernel timeout type.
Definition clock.h:65
Kernel timer structure.
Definition kernel.h:1825
A structure used to submit work after a delay.
Definition kernel.h:4693
Kernel workqueue structure.
Definition kernel.h:4849
A structure holding optional configuration items for a work queue.
Definition kernel.h:4807
const char * name
The name to be given to the work queue thread.
Definition kernel.h:4812
uint32_t work_timeout_ms
Controls whether work queue monitors work timeouts.
Definition kernel.h:4841
bool essential
Control whether the work queue thread should be marked as essential thread.
Definition kernel.h:4831
bool no_yield
Control whether the work queue thread should yield between items.
Definition kernel.h:4826
A structure holding internal state for a pending synchronous operation on a work item or queue.
Definition kernel.h:4788
A structure used to submit work.
Definition kernel.h:4651
A structure to represent a ring buffer.
Definition ring_buffer.h:67
Definition sys_heap.h:60
Definition mem_stats.h:24
Iterable sections helpers.
static bool k_is_user_context(void)
Indicate whether the CPU is currently in user mode.
Definition syscall.h:120
Macros to abstract toolchain specific capabilities.
Main header file for tracing subsystem API.
Header file for tracing macros.