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
237#define THREAD_CREATE_STACKTEST (8)
265 int stacksize,
266 uint8_t priority,
267 int flags,
268 thread_task_func_t task_func,
269 void *arg,
270 const char *name);
271
279{
280 return (thread_t *)sched_threads[pid];
281}
282
291{
292 if (pid_is_valid(pid)) {
293 return thread_get_unchecked(pid);
294 }
295 return NULL;
296}
297
307
311void thread_sleep(void);
312
324#if defined(MODULE_CORE_THREAD) || DOXYGEN
325void thread_yield(void);
326#else
327static inline void thread_yield(void)
328{
329 /* NO-OP */
330}
331#endif
332
346
356void thread_zombify(void);
357
367
377
383static inline kernel_pid_t thread_getpid(void)
384{
385 extern volatile kernel_pid_t sched_active_pid;
386
387 return sched_active_pid;
388}
389
397static inline thread_t *thread_get_active(void)
398{
399 extern volatile thread_t *sched_active_thread;
400
401 return (thread_t *)sched_active_thread;
402}
403
414char *thread_stack_init(thread_task_func_t task_func, void *arg,
415 void *stack_start, int stack_size);
416
431
442#if defined(MODULE_CORE_THREAD) || DOXYGEN
444#else
445static inline const char *thread_getname(kernel_pid_t pid)
446{
447 (void)pid;
448 return "(none)";
449}
450#endif
451
466uintptr_t measure_stack_free_internal(const char *stack, size_t size);
467
472
477
482
487
492
503static inline int thread_has_msg_queue(const volatile struct _thread *thread)
504{
505#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
506 return (thread->msg_array != NULL);
507#else
508 (void)thread;
509 return 0;
510#endif
511}
512
519static inline thread_status_t thread_get_status(const thread_t *thread)
520{
521 return thread->status;
522}
523
530static inline uint8_t thread_get_priority(const thread_t *thread)
531{
532 return thread->priority;
533}
534
541static inline bool thread_is_active(const thread_t *thread)
542{
543 return thread->status >= STATUS_ON_RUNQUEUE;
544}
545
553
560static inline void *thread_get_stackstart(const thread_t *thread)
561{
562#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
563 || defined(MODULE_MPU_STACK_GUARD)
564 return thread->stack_start;
565#else
566 (void)thread;
567 return NULL;
568#endif
569}
570
579static inline void *thread_get_sp(const thread_t *thread)
580{
581 return thread->sp;
582}
583
590static inline size_t thread_get_stacksize(const thread_t *thread)
591{
592#if defined(DEVELHELP)
593 return thread->stack_size;
594#else
595 (void)thread;
596 return 0;
597#endif
598}
599
608static inline kernel_pid_t thread_getpid_of(const thread_t *thread)
609{
610 return thread->pid;
611}
612
619static inline const char *thread_get_name(const thread_t *thread)
620{
621#if defined(CONFIG_THREAD_NAMES)
622 return thread->name;
623#else
624 (void)thread;
625 return NULL;
626#endif
627}
628
639static inline uintptr_t thread_measure_stack_free(const thread_t *thread)
640{
641 /* explicitly casting void pointers is bad code style, but needed for C++
642 * compatibility */
643 return measure_stack_free_internal((const char *)thread_get_stackstart(thread),
644 thread_get_stacksize(thread));
645}
646
647#ifdef __cplusplus
648}
649#endif
650
652#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:530
static size_t thread_get_stacksize(const thread_t *thread)
Get size of a thread's stack.
Definition thread.h:590
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:619
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:397
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:639
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:503
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:560
static thread_t * thread_get_unchecked(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition thread.h:278
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:383
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:541
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:519
static thread_t * thread_get(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition thread.h:290
static void * thread_get_sp(const thread_t *thread)
Get stored Stack Pointer of thread.
Definition thread.h:579
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:608
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.