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"
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
461uintptr_t thread_measure_stack_free(const char *stack);
462
467
472
477
482
487
498static inline int thread_has_msg_queue(const volatile struct _thread *thread)
499{
500#if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
501 return (thread->msg_array != NULL);
502#else
503 (void)thread;
504 return 0;
505#endif
506}
507
514static inline thread_status_t thread_get_status(const thread_t *thread)
515{
516 return thread->status;
517}
518
525static inline uint8_t thread_get_priority(const thread_t *thread)
526{
527 return thread->priority;
528}
529
536static inline bool thread_is_active(const thread_t *thread)
537{
538 return thread->status >= STATUS_ON_RUNQUEUE;
539}
540
548
555static inline void *thread_get_stackstart(const thread_t *thread)
556{
557#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
558 || defined(MODULE_MPU_STACK_GUARD)
559 return thread->stack_start;
560#else
561 (void)thread;
562 return NULL;
563#endif
564}
565
574static inline void *thread_get_sp(const thread_t *thread)
575{
576 return thread->sp;
577}
578
585static inline size_t thread_get_stacksize(const thread_t *thread)
586{
587#if defined(DEVELHELP)
588 return thread->stack_size;
589#else
590 (void)thread;
591 return 0;
592#endif
593}
594
603static inline kernel_pid_t thread_getpid_of(const thread_t *thread)
604{
605 return thread->pid;
606}
607
614static inline const char *thread_get_name(const thread_t *thread)
615{
616#if defined(CONFIG_THREAD_NAMES)
617 return thread->name;
618#else
619 (void)thread;
620 return NULL;
621#endif
622}
623
624#ifdef __cplusplus
625}
626#endif
627
629#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.
Definition: thread_flags.h:122
void thread_stack_print(void)
Print the current stack to stdout.
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:525
static size_t thread_get_stacksize(const thread_t *thread)
Get size of a thread's stack.
Definition: thread.h:585
void *(* thread_task_func_t)(void *arg)
Prototype for a thread entry function.
Definition: thread.h:163
uintptr_t thread_measure_stack_free(const char *stack)
Measures the stack usage of a stack.
static const char * thread_get_name(const thread_t *thread)
Get name of thread.
Definition: thread.h:614
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)
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:498
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:555
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:536
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:514
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:574
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:603
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.