Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2014 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
119#ifndef THREAD_H
120#define THREAD_H
121
122#include "clist.h"
123#include "cib.h"
124#include "msg.h"
125#include "sched.h"
126#include "thread_config.h"
127
128#ifdef MODULE_CORE_THREAD_FLAGS
129#include "thread_flags.h"
130#endif
131
132#include "thread_arch.h" /* IWYU pragma: export */
133
134#ifdef __cplusplus
135extern "C" {
136#endif
137
144#ifdef THREAD_API_INLINED
145#define THREAD_MAYBE_INLINE static inline __attribute__((always_inline))
146#else
147#define THREAD_MAYBE_INLINE
148#endif /* THREAD_API_INLINED */
149
150#if defined(DEVELHELP) && !defined(CONFIG_THREAD_NAMES)
157#define CONFIG_THREAD_NAMES
158#endif
159
163typedef void *(*thread_task_func_t)(void *arg);
164
168struct _thread {
169 char *sp;
171 uint8_t priority;
175#if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
177#endif
178
181#if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
182 || defined(MODULE_CORE_MBOX) || defined(DOXYGEN)
183 void *wait_data;
185#endif
186#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
194#endif
195#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
196 || defined(MODULE_MPU_STACK_GUARD) || defined(DOXYGEN)
198#endif
199#if defined(CONFIG_THREAD_NAMES) || defined(DOXYGEN)
200 const char *name;
201#endif
202#if defined(DEVELHELP) || defined(DOXYGEN)
204#endif
205/* enable TLS only when Picolibc is compiled with TLS enabled */
206#ifdef PICOLIBC_TLS
207 void *tls;
208#endif
209};
210
218#define THREAD_CREATE_SLEEPING (1)
219
223#define THREAD_AUTO_FREE (2)
224
231#define THREAD_CREATE_WOUT_YIELD (4)
232
238#define THREAD_CREATE_NO_STACKTEST (8)
239
247#define THREAD_CREATE_STACKTEST (0)
275 int stacksize,
276 uint8_t priority,
277 int flags,
278 thread_task_func_t task_func,
279 void *arg,
280 const char *name);
281
289{
290 return (thread_t *)sched_threads[pid];
291}
292
301{
302 if (pid_is_valid(pid)) {
303 return thread_get_unchecked(pid);
304 }
305 return NULL;
306}
307
317
321void thread_sleep(void);
322
334#if defined(MODULE_CORE_THREAD) || DOXYGEN
335void thread_yield(void);
336#else
337static inline void thread_yield(void)
338{
339 /* NO-OP */
340}
341#endif
342
356
366void thread_zombify(void);
367
377
387
393static inline kernel_pid_t thread_getpid(void)
394{
395 extern volatile kernel_pid_t sched_active_pid;
396
397 return sched_active_pid;
398}
399
407static inline thread_t *thread_get_active(void)
408{
409 extern volatile thread_t *sched_active_thread;
410
411 return (thread_t *)sched_active_thread;
412}
413
424char *thread_stack_init(thread_task_func_t task_func, void *arg,
425 void *stack_start, int stack_size);
426
441
452#if defined(MODULE_CORE_THREAD) || DOXYGEN
454#else
455static inline const char *thread_getname(kernel_pid_t pid)
456{
457 (void)pid;
458 return "(none)";
459}
460#endif
461
477uintptr_t measure_stack_free_internal(const char *stack, size_t size);
478
483
488
493
498
503
514static inline int thread_has_msg_queue(const volatile struct _thread *thread)
515{
516#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
517 return (thread->msg_array != NULL);
518#else
519 (void)thread;
520 return 0;
521#endif
522}
523
530static inline thread_status_t thread_get_status(const thread_t *thread)
531{
532 return thread->status;
533}
534
541static inline uint8_t thread_get_priority(const thread_t *thread)
542{
543 return thread->priority;
544}
545
552static inline bool thread_is_active(const thread_t *thread)
553{
554 return thread->status >= STATUS_ON_RUNQUEUE;
555}
556
564
571static inline void *thread_get_stackstart(const thread_t *thread)
572{
573#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
574 || defined(MODULE_MPU_STACK_GUARD)
575 return thread->stack_start;
576#else
577 (void)thread;
578 return NULL;
579#endif
580}
581
590static inline void *thread_get_sp(const thread_t *thread)
591{
592 return thread->sp;
593}
594
601static inline size_t thread_get_stacksize(const thread_t *thread)
602{
603#if defined(DEVELHELP)
604 return thread->stack_size;
605#else
606 (void)thread;
607 return 0;
608#endif
609}
610
619static inline kernel_pid_t thread_getpid_of(const thread_t *thread)
620{
621 return thread->pid;
622}
623
630static inline const char *thread_get_name(const thread_t *thread)
631{
632#if defined(CONFIG_THREAD_NAMES)
633 return thread->name;
634#else
635 (void)thread;
636 return NULL;
637#endif
638}
639
650static inline uintptr_t thread_measure_stack_free(const thread_t *thread)
651{
652 /* explicitly casting void pointers is bad code style, but needed for C++
653 * compatibility */
654 return measure_stack_free_internal((const char *)thread_get_stackstart(thread),
655 thread_get_stacksize(thread));
656}
657
658#ifdef __cplusplus
659}
660#endif
661
663#endif /* THREAD_H */
Circular integer buffer interface.
Circular linked list.
static int pid_is_valid(kernel_pid_t pid)
Determine if the given pid is valid.
Definition sched.h:148
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
volatile thread_t * sched_threads[KERNEL_PID_LAST+1]
Thread table.
thread_status_t
Definition sched.h:163
#define STATUS_ON_RUNQUEUE
to check if on run queue: st >= STATUS_ON_RUNQUEUE
Definition sched.h:185
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:541
static size_t thread_get_stacksize(const thread_t *thread)
Get size of a thread's stack.
Definition thread.h:601
void *(* thread_task_func_t)(void *arg)
Prototype for a thread entry function.
Definition thread.h:163
static const char * thread_get_name(const thread_t *thread)
Get name of thread.
Definition thread.h:630
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:407
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:650
void thread_zombify(void)
Puts the current thread into zombie state.
static int thread_has_msg_queue(const volatile struct _thread *thread)
Checks if a thread has an initialized message queue.
Definition thread.h:514
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:571
static thread_t * thread_get_unchecked(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition thread.h:288
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:393
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:552
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:530
static thread_t * thread_get(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition thread.h:300
static void * thread_get_sp(const thread_t *thread)
Get stored Stack Pointer of thread.
Definition thread.h:590
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:147
static kernel_pid_t thread_getpid_of(const thread_t *thread)
Get PID of thread.
Definition thread.h:619
Scheduler API definition.
thread_t holds thread's context data.
Definition thread.h:168
char * sp
thread's stack pointer
Definition thread.h:169
char * stack_start
thread's stack start address
Definition thread.h:197
cib_t msg_queue
index of this [thread's message queue] (thread_t::msg_array), if any
Definition thread.h:190
thread_flags_t flags
currently set flags
Definition thread.h:176
list_node_t msg_waiters
threads waiting for their message to be delivered to this thread (i.e.
Definition thread.h:187
thread_status_t status
thread's status
Definition thread.h:170
msg_t * msg_array
memory holding messages sent to this thread's message queue
Definition thread.h:192
uint8_t priority
thread's priority
Definition thread.h:171
int stack_size
thread's stack size
Definition thread.h:203
const char * name
thread's name
Definition thread.h:200
clist_node_t rq_entry
run queue entry
Definition thread.h:179
void * wait_data
used by msg, mbox and thread flags
Definition thread.h:183
kernel_pid_t pid
thread's process id
Definition thread.h:173
circular integer buffer structure
Definition cib.h:34
List node structure.
Definition list.h:40
Describes a message object which can be sent between threads.
Definition msg.h:196
Definitions for parsing and composition of DNS messages.
Thread configuration defines.
Thread Flags API.