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 
249 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
250 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
251 #endif
252 #ifdef DOXYGEN
253 #define THREAD_EXTRA_STACKSIZE_PRINTF
254 #endif
255 
260 #ifndef THREAD_STACKSIZE_MAIN
261 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
262 #endif
263 
267 #ifndef THREAD_STACKSIZE_LARGE
268 #define THREAD_STACKSIZE_LARGE (THREAD_STACKSIZE_MEDIUM * 2)
269 #endif
270 
274 #ifndef THREAD_STACKSIZE_MEDIUM
275 #define THREAD_STACKSIZE_MEDIUM THREAD_STACKSIZE_DEFAULT
276 #endif
277 
281 #ifndef THREAD_STACKSIZE_SMALL
282 #define THREAD_STACKSIZE_SMALL (THREAD_STACKSIZE_MEDIUM / 2)
283 #endif
284 
288 #ifndef THREAD_STACKSIZE_TINY
289 #define THREAD_STACKSIZE_TINY (THREAD_STACKSIZE_MEDIUM / 4)
290 #endif
291 
295 #ifndef THREAD_STACKSIZE_MINIMUM
296 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
297 #endif
298 
303 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
309 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
315 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
324 #define THREAD_CREATE_SLEEPING (1)
329 #define THREAD_AUTO_FREE (2)
337 #define THREAD_CREATE_WOUT_YIELD (4)
343 #define THREAD_CREATE_STACKTEST (8)
370 kernel_pid_t thread_create(char *stack,
371  int stacksize,
372  char priority,
373  int flags,
374  thread_task_func_t task_func,
375  void *arg,
376  const char *name);
377 
386 
396 
400 void thread_sleep(void);
401 
413 void thread_yield(void);
414 
427 void thread_yield_higher(void);
428 
438 
444 static inline kernel_pid_t thread_getpid(void)
445 {
446  extern volatile kernel_pid_t sched_active_pid;
447  return sched_active_pid;
448 }
449 
460 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
461 
475 void thread_add_to_list(list_node_t *list, thread_t *thread);
476 
487 const char *thread_getname(kernel_pid_t pid);
488 
489 #ifdef DEVELHELP
490 
499 uintptr_t thread_measure_stack_free(char *stack);
500 #endif /* DEVELHELP */
501 
505 int thread_isr_stack_usage(void);
506 
510 void *thread_isr_stack_pointer(void);
511 
515 void *thread_isr_stack_start(void);
516 
520 void thread_stack_print(void);
521 
525 void thread_print_stack(void);
526 
527 #ifdef __cplusplus
528 }
529 #endif
530 
532 #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:445
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.