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 
9 #pragma once
10 
121 #include "clist.h"
122 #include "cib.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
134 extern "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 
162 typedef void *(*thread_task_func_t)(void *arg);
163 
167 struct _thread {
168  char *sp;
170  uint8_t priority;
174 #if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
176 #endif
177 
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)
196  char *stack_start;
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)
274  int stacksize,
275  uint8_t priority,
276  int flags,
277  thread_task_func_t task_func,
278  void *arg,
279  const char *name);
280 
288 {
289  return (thread_t *)sched_threads[pid];
290 }
291 
299 static inline thread_t *thread_get(kernel_pid_t pid)
300 {
301  if (pid_is_valid(pid)) {
302  return thread_get_unchecked(pid);
303  }
304  return NULL;
305 }
306 
316 
320 void thread_sleep(void);
321 
333 #if defined(MODULE_CORE_THREAD) || DOXYGEN
334 void thread_yield(void);
335 #else
336 static inline void thread_yield(void)
337 {
338  /* NO-OP */
339 }
340 #endif
341 
355 
365 void thread_zombify(void);
366 
376 
386 
392 static inline kernel_pid_t thread_getpid(void)
393 {
394  extern volatile kernel_pid_t sched_active_pid;
395 
396  return sched_active_pid;
397 }
398 
406 static inline thread_t *thread_get_active(void)
407 {
408  extern volatile thread_t *sched_active_thread;
409 
410  return (thread_t *)sched_active_thread;
411 }
412 
423 char *thread_stack_init(thread_task_func_t task_func, void *arg,
424  void *stack_start, int stack_size);
425 
440 
451 #if defined(MODULE_CORE_THREAD) || DOXYGEN
452 const char *thread_getname(kernel_pid_t pid);
453 #else
454 static inline const char *thread_getname(kernel_pid_t pid)
455 {
456  (void)pid;
457  return "(none)";
458 }
459 #endif
460 
476 uintptr_t measure_stack_free_internal(const char *stack, size_t size);
477 
482 
487 
492 
497 
502 
513 static inline int thread_has_msg_queue(const volatile struct _thread *thread)
514 {
515 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
516  return (thread->msg_array != NULL);
517 #else
518  (void)thread;
519  return 0;
520 #endif
521 }
522 
529 static inline thread_status_t thread_get_status(const thread_t *thread)
530 {
531  return thread->status;
532 }
533 
540 static inline uint8_t thread_get_priority(const thread_t *thread)
541 {
542  return thread->priority;
543 }
544 
551 static inline bool thread_is_active(const thread_t *thread)
552 {
553  return thread->status >= STATUS_ON_RUNQUEUE;
554 }
555 
563 
570 static inline void *thread_get_stackstart(const thread_t *thread)
571 {
572 #if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) \
573  || defined(MODULE_MPU_STACK_GUARD)
574  return thread->stack_start;
575 #else
576  (void)thread;
577  return NULL;
578 #endif
579 }
580 
589 static inline void *thread_get_sp(const thread_t *thread)
590 {
591  return thread->sp;
592 }
593 
600 static inline size_t thread_get_stacksize(const thread_t *thread)
601 {
602 #if defined(DEVELHELP)
603  return thread->stack_size;
604 #else
605  (void)thread;
606  return 0;
607 #endif
608 }
609 
618 static inline kernel_pid_t thread_getpid_of(const thread_t *thread)
619 {
620  return thread->pid;
621 }
622 
629 static inline const char *thread_get_name(const thread_t *thread)
630 {
631 #if defined(CONFIG_THREAD_NAMES)
632  return thread->name;
633 #else
634  (void)thread;
635  return NULL;
636 #endif
637 }
638 
649 static inline uintptr_t thread_measure_stack_free(const thread_t *thread)
650 {
651  /* explicitly casting void pointers is bad code style, but needed for C++
652  * compatibility */
653  return measure_stack_free_internal((const char *)thread_get_stackstart(thread),
654  thread_get_stacksize(thread));
655 }
656 
657 #ifdef __cplusplus
658 }
659 #endif
660 
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:147
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:138
volatile thread_t * sched_threads[KERNEL_PID_LAST+1]
Thread table.
thread_status_t
Definition: sched.h:162
#define STATUS_ON_RUNQUEUE
to check if on run queue: st >= STATUS_ON_RUNQUEUE
Definition: sched.h:184
uint16_t thread_flags_t
Type definition of thread_flags_t.
Definition: thread_flags.h:121
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.
static uint8_t thread_get_priority(const thread_t *thread)
Get a thread's priority.
Definition: thread.h:540
static thread_t * thread_get(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition: thread.h:299
static size_t thread_get_stacksize(const thread_t *thread)
Get size of a thread's stack.
Definition: thread.h:600
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.
const char * thread_state_to_string(thread_status_t state)
Convert a thread state code to a human readable string.
static thread_t * thread_get_unchecked(kernel_pid_t pid)
Retrieve a thread control block by PID.
Definition: thread.h:287
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition: thread.h:406
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:649
static const char * thread_get_name(const thread_t *thread)
Get name of thread.
Definition: thread.h:629
void thread_zombify(void)
Puts the current thread into zombie state.
void * thread_isr_stack_pointer(void)
Get the current ISR stack pointer.
static int thread_has_msg_queue(const volatile struct _thread *thread)
Checks if a thread has an initialized message queue.
Definition: thread.h:513
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.
void * thread_isr_stack_start(void)
Get the start of the ISR stack.
THREAD_MAYBE_INLINE void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
int thread_kill_zombie(kernel_pid_t pid)
Terminates zombie thread.
void thread_sleep(void)
Puts the current thread into sleep mode.
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 void * thread_get_sp(const thread_t *thread)
Get stored Stack Pointer of thread.
Definition: thread.h:589
void *(* thread_task_func_t)(void *arg)
Prototype for a thread entry function.
Definition: thread.h:162
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition: thread.h:392
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:551
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:529
static void * thread_get_stackstart(const thread_t *thread)
Get start address (lowest) of a thread's stack.
Definition: thread.h:570
void thread_print_stack(void)
Prints human readable, ps-like thread information for debugging purposes.
#define THREAD_MAYBE_INLINE
Macro definition to inline some of the platform specific implementations.
Definition: thread.h:146
static kernel_pid_t thread_getpid_of(const thread_t *thread)
Get PID of thread.
Definition: thread.h:618
Scheduler API definition.
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:44
List node structure.
Definition: list.h:39
Describes a message object which can be sent between threads.
Definition: msg.h:195
Definitions for parsing and composition of DNS messages.
Thread configuration defines.
Thread Flags API.