Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2014 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
117
118#include <stdbool.h>
119
120#include "cib.h"
121#include "clist.h"
122#include "compiler_hints.h"
123#include "msg.h"
124#include "sched.h"
125#include "thread_config.h"
126
127#ifdef MODULE_CORE_THREAD_FLAGS
128#include "thread_flags.h"
129#endif
130
131#include "thread_arch.h" /* IWYU pragma: export */
132
133#ifdef __cplusplus
134extern "C" {
135#endif
136
143#ifdef THREAD_API_INLINED
144#define THREAD_MAYBE_INLINE static inline __attribute__((always_inline))
145#else
146#define THREAD_MAYBE_INLINE
147#endif /* THREAD_API_INLINED */
148
149#if defined(DEVELHELP) && !defined(CONFIG_THREAD_NAMES)
156#define CONFIG_THREAD_NAMES
157#endif
158
162typedef void *(*thread_task_func_t)(void *arg);
163
167struct _thread {
168 char *sp;
170 uint8_t priority;
171
173
174#if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
176#endif
177
179
180#if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
181 || defined(MODULE_CORE_MBOX) || defined(DOXYGEN)
182 void *wait_data;
184#endif
185#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
193#endif
194#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
195 || defined(MODULE_MPU_STACK_GUARD) || defined(DOXYGEN)
197#endif
198#if defined(CONFIG_THREAD_NAMES) || defined(DOXYGEN)
199 const char *name;
200#endif
201#if defined(DEVELHELP) || defined(DOXYGEN)
203#endif
204/* enable TLS only when Picolibc is compiled with TLS enabled */
205#ifdef PICOLIBC_TLS
206 void *tls;
207#endif
208};
209
217#define THREAD_CREATE_SLEEPING (1)
218
222#define THREAD_AUTO_FREE (2)
223
230#define THREAD_CREATE_WOUT_YIELD (4)
231
237#define THREAD_CREATE_NO_STACKTEST (8)
238
246#define THREAD_CREATE_STACKTEST (0)
248
276ACCESS(write_only, 1, 2)
278 int stacksize,
279 uint8_t priority,
280 int flags,
281 thread_task_func_t task_func,
282 void *arg,
283 const char *name);
284
313{
314 return (thread_t *)sched_threads[pid];
315}
316
344{
345 if (pid_is_valid(pid)) {
346 return thread_get_unchecked(pid);
347 }
348 return NULL;
349}
350
360
364void thread_sleep(void);
365
377#if defined(MODULE_CORE_THREAD) || DOXYGEN
378void thread_yield(void);
379#else
380static inline void thread_yield(void)
381{
382 /* NO-OP */
383}
384#endif
385
399
409void thread_zombify(void);
410
420
430
436static inline kernel_pid_t thread_getpid(void)
437{
438 extern volatile kernel_pid_t sched_active_pid;
439
440 return sched_active_pid;
441}
442
450static inline thread_t *thread_get_active(void)
451{
452 extern volatile thread_t *sched_active_thread;
453
454 return (thread_t *)sched_active_thread;
455}
456
467ACCESS(write_only, 3, 4)
468char *thread_stack_init(thread_task_func_t task_func, void *arg,
469 void *stack_start, int stack_size);
470
485
496#if defined(MODULE_CORE_THREAD) || DOXYGEN
498#else
499static inline const char *thread_getname(kernel_pid_t pid)
500{
501 (void)pid;
502 return "(none)";
503}
504#endif
505
521ACCESS(read_only, 1, 2)
522uintptr_t measure_stack_free_internal(const char *stack, size_t size);
523
528
533
538
543
548
562static inline bool thread_has_msg_queue(const thread_t *thread)
563{
564 assume(thread != NULL);
565#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
566 return (thread->msg_array != NULL);
567#else
568 (void)thread;
569 return 0;
570#endif
571}
572
579static inline thread_status_t thread_get_status(const thread_t *thread)
580{
581 return thread->status;
582}
583
590static inline uint8_t thread_get_priority(const thread_t *thread)
591{
592 return thread->priority;
593}
594
601static inline bool thread_is_active(const thread_t *thread)
602{
603 return thread->status >= STATUS_ON_RUNQUEUE;
604}
605
613
620static inline void *thread_get_stackstart(const thread_t *thread)
621{
622#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
623 || defined(MODULE_MPU_STACK_GUARD)
624 return thread->stack_start;
625#else
626 (void)thread;
627 return NULL;
628#endif
629}
630
639static inline void *thread_get_sp(const thread_t *thread)
640{
641 return thread->sp;
642}
643
650static inline size_t thread_get_stacksize(const thread_t *thread)
651{
652#if defined(DEVELHELP)
653 return thread->stack_size;
654#else
655 (void)thread;
656 return 0;
657#endif
658}
659
668static inline kernel_pid_t thread_getpid_of(const thread_t *thread)
669{
670 return thread->pid;
671}
672
679static inline const char *thread_get_name(const thread_t *thread)
680{
681#if defined(CONFIG_THREAD_NAMES)
682 return thread->name;
683#else
684 (void)thread;
685 return NULL;
686#endif
687}
688
699static inline uintptr_t thread_measure_stack_free(const thread_t *thread)
700{
701 /* explicitly casting void pointers is bad code style, but needed for C++
702 * compatibility */
703 return measure_stack_free_internal((const char *)thread_get_stackstart(thread),
704 thread_get_stacksize(thread));
705}
706
707#ifdef __cplusplus
708}
709#endif
710
Circular integer buffer interface.
Circular linked list.
list_node_t clist_node_t
List node structure.
Definition clist.h:104
Common macros and compiler attributes/pragmas configuration.
#define ACCESS(mode, ptr_idx, size_idx)
Emit an attribute (if supported by the compiler) that declares how a function will access its paramet...
#define assume(cond)
Behaves like an assert(), but tells the compiler that cond can never be false.
Messaging API for inter process communication.
Scheduler API definition.
struct _thread thread_t
forward declaration for thread_t, defined in thread.h
Definition sched.h:150
static int pid_is_valid(kernel_pid_t pid)
Determine if the given pid is valid.
Definition sched.h:143
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:134
volatile thread_t * sched_threads[KERNEL_PID_LAST+1]
Thread table.
thread_status_t
Definition sched.h:158
#define STATUS_ON_RUNQUEUE
to check if on run queue: st >= STATUS_ON_RUNQUEUE
Definition sched.h:180
uint16_t thread_flags_t
Type definition of thread_flags_t.
void thread_stack_print(void)
Print the current stack to stdout.
uintptr_t measure_stack_free_internal(const char *stack, size_t size)
Measures the stack usage of a stack.
thread_status_t thread_getstatus(kernel_pid_t pid)
Returns the status of a process.
const char * thread_state_to_string(thread_status_t state)
Convert a thread state code to a human readable string.
static uint8_t thread_get_priority(const thread_t *thread)
Get a thread's priority.
Definition thread.h:590
static size_t thread_get_stacksize(const thread_t *thread)
Get size of a thread's stack.
Definition thread.h:650
void *(* thread_task_func_t)(void *arg)
Prototype for a thread entry function.
Definition thread.h:162
static const char * thread_get_name(const thread_t *thread)
Get name of thread.
Definition thread.h:679
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:450
void thread_add_to_list(list_node_t *list, thread_t *thread)
Add thread to list, sorted by priority (internal)
static uintptr_t thread_measure_stack_free(const thread_t *thread)
Measures the stack usage of a stack.
Definition thread.h:699
void thread_zombify(void)
Puts the current thread into zombie state.
kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, int flags, thread_task_func_t task_func, void *arg, const char *name)
Creates a new thread.
THREAD_MAYBE_INLINE void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
void * thread_isr_stack_pointer(void)
Get the current ISR stack pointer.
int thread_kill_zombie(kernel_pid_t pid)
Terminates zombie thread.
void thread_sleep(void)
Puts the current thread into sleep mode.
static void * thread_get_stackstart(const thread_t *thread)
Get start address (lowest) of a thread's stack.
Definition thread.h:620
static thread_t * thread_get_unchecked(kernel_pid_t pid)
Retrieve a thread control block by PID without bounds checking.
Definition thread.h:312
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.
void thread_yield(void)
Lets current thread yield.
int thread_wakeup(kernel_pid_t pid)
Wakes up a sleeping thread.
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition thread.h:436
char * thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size)
Gets called upon thread creation to set CPU registers.
static bool thread_is_active(const thread_t *thread)
Returns if a thread is active (currently running or waiting to be scheduled)
Definition thread.h:601
int thread_isr_stack_usage(void)
Get the number of bytes used on the ISR stack.
static thread_status_t thread_get_status(const thread_t *thread)
Get a thread's status.
Definition thread.h:579
static thread_t * thread_get(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition thread.h:343
static void * thread_get_sp(const thread_t *thread)
Get stored Stack Pointer of thread.
Definition thread.h:639
void thread_print_stack(void)
Prints human readable, ps-like thread information for debugging purposes.
void * thread_isr_stack_start(void)
Get the start of the ISR stack.
#define THREAD_MAYBE_INLINE
Macro definition to inline some of the platform specific implementations.
Definition thread.h:146
static bool thread_has_msg_queue(const thread_t *thread)
Checks if a thread has an initialized message queue.
Definition thread.h:562
static kernel_pid_t thread_getpid_of(const thread_t *thread)
Get PID of thread.
Definition thread.h:668
struct list_node list_node_t
List node structure.
thread_t holds thread's context data.
Definition thread.h:167
char * sp
thread's stack pointer
Definition thread.h:168
char * stack_start
thread's stack start address
Definition thread.h:196
cib_t msg_queue
index of this [thread's message queue] (thread_t::msg_array), if any
Definition thread.h:189
thread_flags_t flags
currently set flags
Definition thread.h:175
list_node_t msg_waiters
threads waiting for their message to be delivered to this thread (i.e.
Definition thread.h:186
thread_status_t status
thread's status
Definition thread.h:169
msg_t * msg_array
memory holding messages sent to this thread's message queue
Definition thread.h:191
uint8_t priority
thread's priority
Definition thread.h:170
int stack_size
thread's stack size
Definition thread.h:202
const char * name
thread's name
Definition thread.h:199
clist_node_t rq_entry
run queue entry
Definition thread.h:178
void * wait_data
used by msg, mbox and thread flags
Definition thread.h:182
kernel_pid_t pid
thread's process id
Definition thread.h:172
circular integer buffer structure
Definition cib.h:41
Describes a message object which can be sent between threads.
Definition msg.h:193
Thread configuration defines.
Thread Flags API.