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 "cpu_conf.h"
126 #include "sched.h"
127 
128 #ifdef MODULE_CORE_THREAD_FLAGS
129 #include "thread_flags.h"
130 #endif
131 
132 #ifdef __cplusplus
133  extern "C" {
134 #endif
135 
136 /* Thread states */
141 #define STATUS_NOT_FOUND (-1)
148 #define STATUS_STOPPED 0
149 #define STATUS_SLEEPING 1
150 #define STATUS_MUTEX_BLOCKED 2
151 #define STATUS_RECEIVE_BLOCKED 3
152 #define STATUS_SEND_BLOCKED 4
153 #define STATUS_REPLY_BLOCKED 5
154 #define STATUS_FLAG_BLOCKED_ANY 6
155 #define STATUS_FLAG_BLOCKED_ALL 7
156 #define STATUS_MBOX_BLOCKED 8
163 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
165 #define STATUS_RUNNING 9
166 #define STATUS_PENDING 10
172 typedef void *(*thread_task_func_t)(void *arg);
177 struct _thread {
178  char *sp;
179  uint8_t status;
180  uint8_t priority;
182  kernel_pid_t pid;
184 #if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
186 #endif
187 
190 #if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
191  || defined(MODULE_CORE_MBOX) || defined(DOXYGEN)
192  void *wait_data;
194 #endif
195 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
199  cib_t msg_queue;
201  msg_t *msg_array;
203 #endif
204 #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \
205  || defined(MODULE_MPU_STACK_GUARD) || defined(DOXYGEN)
206  char *stack_start;
207 #endif
208 #if defined(DEVELHELP) || defined(DOXYGEN)
209  const char *name;
211 #endif
212 };
213 
221 #ifndef THREAD_STACKSIZE_DEFAULT
222 #error THREAD_STACKSIZE_DEFAULT must be defined per CPU
223 #endif
224 #ifdef DOXYGEN
225 #define THREAD_STACKSIZE_DEFAULT
226 #endif
227 
235 #ifndef THREAD_STACKSIZE_IDLE
236 #error THREAD_STACKSIZE_IDLE must be defined per CPU
237 #endif
238 #ifdef DOXYGEN
239 #define THREAD_STACKSIZE_IDLE
240 #endif
241 
250 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
251 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
252 #endif
253 #ifdef DOXYGEN
254 #define THREAD_EXTRA_STACKSIZE_PRINTF
255 #endif
256 
261 #ifndef THREAD_STACKSIZE_MAIN
262 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
263 #endif
264 
268 #ifndef THREAD_STACKSIZE_MINIMUM
269 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
270 #endif
271 
276 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
282 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
288 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
297 #define THREAD_CREATE_SLEEPING (1)
302 #define THREAD_AUTO_FREE (2)
310 #define THREAD_CREATE_WOUT_YIELD (4)
316 #define THREAD_CREATE_STACKTEST (8)
343 kernel_pid_t thread_create(char *stack,
344  int stacksize,
345  char priority,
346  int flags,
347  thread_task_func_t task_func,
348  void *arg,
349  const char *name);
350 
359 
369 
373 void thread_sleep(void);
374 
386 void thread_yield(void);
387 
400 void thread_yield_higher(void);
401 
411 
417 static inline kernel_pid_t thread_getpid(void)
418 {
419  extern volatile kernel_pid_t sched_active_pid;
420  return sched_active_pid;
421 }
422 
433 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
434 
448 void thread_add_to_list(list_node_t *list, thread_t *thread);
449 
450 #ifdef DEVELHELP
451 
459 const char *thread_getname(kernel_pid_t pid);
460 
470 uintptr_t thread_measure_stack_free(char *stack);
471 #endif /* DEVELHELP */
472 
476 int thread_isr_stack_usage(void);
477 
481 void *thread_isr_stack_pointer(void);
482 
486 void *thread_isr_stack_start(void);
487 
491 void thread_stack_print(void);
492 
496 void thread_print_stack(void);
497 
498 #ifdef __cplusplus
499 }
500 #endif
501 
503 #endif /* THREAD_H */
clist_node_t rq_entry
run queue entry
Definition: thread.h:189
volatile kernel_pid_t sched_active_pid
Process ID of active thread.
list_node_t msg_waiters
threads waiting for their message to be delivered to this thread (i.e.
Definition: thread.h:197
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
circular integer buffer structure
Definition: cib.h:34
kernel_pid_t pid
thread's process id
Definition: thread.h:183
uintptr_t thread_measure_stack_free(char *stack)
Measures the stack usage of a stack.
int thread_isr_stack_usage(void)
Get the number of bytes used on the ISR stack.
void thread_add_to_list(list_node_t *list, thread_t *thread)
Add thread to list, sorted by priority (internal)
volatile thread_t * thread_get(kernel_pid_t pid)
Retreive a thread control block by PID.
msg_t * msg_array
memory holding messages sent to this thread's message queue
Definition: thread.h:202
void thread_stack_print(void)
Print the current stack to stdout.
thread_t holds thread's context data.
Definition: thread.h:178
int thread_getstatus(kernel_pid_t pid)
Returns the status of a process.
uint16_t thread_flags_t
Type definition of thread_flags_t.
Definition: thread_flags.h:111
Scheduler API definition.
Messaging API for inter process communication.
Thread Flags API.
kernel_pid_t thread_create(char *stack, int stacksize, char 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_flags_t flags
currently set flags
Definition: thread.h:186
Describes a message object which can be sent between threads.
Definition: msg.h:184
char * sp
thread's stack pointer
Definition: thread.h:179
void thread_print_stack(void)
Prints human readable, ps-like thread information for debugging purposes.
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.
void * thread_isr_stack_pointer(void)
Get the current ISR stack pointer.
const char * name
thread's name
Definition: thread.h:210
void thread_sleep(void)
Puts the current thread into sleep mode.
void *(* thread_task_func_t)(void *arg)
Prototype for a thread entry function.
Definition: thread.h:173
uint8_t priority
thread's priority
Definition: thread.h:181
List node structure.
Definition: list.h:40
uint8_t status
thread's status
Definition: thread.h:180
void * wait_data
used by msg, mbox and thread flags
Definition: thread.h:193
void thread_yield(void)
Lets current thread yield.
int thread_wakeup(kernel_pid_t pid)
Wakes up a sleeping thread.
Circular integer buffer interface.
Circular linked list.
void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
cib_t msg_queue
index of this [thread's message queue] (thread_t::msg_array), if any
Definition: thread.h:200
int stack_size
thread's stack size
Definition: thread.h:211
char * stack_start
thread's stack start address
Definition: thread.h:207
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition: thread.h:418
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.