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
157 #define STATUS_COND_BLOCKED 9
164 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
166 #define STATUS_RUNNING 10
167 #define STATUS_PENDING 11
173 typedef void *(*thread_task_func_t)(void *arg);
178 struct _thread {
179  char *sp;
180  uint8_t status;
181  uint8_t priority;
183  kernel_pid_t pid;
185 #if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
187 #endif
188 
191 #if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
192  || defined(MODULE_CORE_MBOX) || defined(DOXYGEN)
193  void *wait_data;
195 #endif
196 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
200  cib_t msg_queue;
202  msg_t *msg_array;
204 #endif
205 #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \
206  || defined(MODULE_MPU_STACK_GUARD) || defined(DOXYGEN)
207  char *stack_start;
208 #endif
209 #if defined(DEVELHELP) || defined(DOXYGEN)
210  const char *name;
212 #endif
213 #ifdef HAVE_THREAD_ARCH_T
214  thread_arch_t arch;
215 #endif
216 };
217 
225 #ifndef THREAD_STACKSIZE_DEFAULT
226 #error THREAD_STACKSIZE_DEFAULT must be defined per CPU
227 #endif
228 #ifdef DOXYGEN
229 #define THREAD_STACKSIZE_DEFAULT
230 #endif
231 
239 #ifndef THREAD_STACKSIZE_IDLE
240 #error THREAD_STACKSIZE_IDLE must be defined per CPU
241 #endif
242 #ifdef DOXYGEN
243 #define THREAD_STACKSIZE_IDLE
244 #endif
245 
253 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
254 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
255 #endif
256 #ifdef DOXYGEN
257 #define THREAD_EXTRA_STACKSIZE_PRINTF
258 #endif
259 
264 #ifndef THREAD_STACKSIZE_MAIN
265 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
266 #endif
267 
271 #ifndef THREAD_STACKSIZE_LARGE
272 #define THREAD_STACKSIZE_LARGE (THREAD_STACKSIZE_MEDIUM * 2)
273 #endif
274 
278 #ifndef THREAD_STACKSIZE_MEDIUM
279 #define THREAD_STACKSIZE_MEDIUM THREAD_STACKSIZE_DEFAULT
280 #endif
281 
285 #ifndef THREAD_STACKSIZE_SMALL
286 #define THREAD_STACKSIZE_SMALL (THREAD_STACKSIZE_MEDIUM / 2)
287 #endif
288 
292 #ifndef THREAD_STACKSIZE_TINY
293 #define THREAD_STACKSIZE_TINY (THREAD_STACKSIZE_MEDIUM / 4)
294 #endif
295 
299 #ifndef THREAD_STACKSIZE_MINIMUM
300 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
301 #endif
302 
307 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
313 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
319 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
328 #define THREAD_CREATE_SLEEPING (1)
333 #define THREAD_AUTO_FREE (2)
341 #define THREAD_CREATE_WOUT_YIELD (4)
347 #define THREAD_CREATE_STACKTEST (8)
374 kernel_pid_t thread_create(char *stack,
375  int stacksize,
376  char priority,
377  int flags,
378  thread_task_func_t task_func,
379  void *arg,
380  const char *name);
381 
390 
400 
404 void thread_sleep(void);
405 
417 void thread_yield(void);
418 
431 void thread_yield_higher(void);
432 
442 
448 static inline kernel_pid_t thread_getpid(void)
449 {
450  extern volatile kernel_pid_t sched_active_pid;
451  return sched_active_pid;
452 }
453 
464 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
465 
479 void thread_add_to_list(list_node_t *list, thread_t *thread);
480 
491 const char *thread_getname(kernel_pid_t pid);
492 
493 #ifdef DEVELHELP
494 
503 uintptr_t thread_measure_stack_free(char *stack);
504 #endif /* DEVELHELP */
505 
509 int thread_isr_stack_usage(void);
510 
514 void *thread_isr_stack_pointer(void);
515 
519 void *thread_isr_stack_start(void);
520 
524 void thread_stack_print(void);
525 
529 void thread_print_stack(void);
530 
531 #ifdef __cplusplus
532 }
533 #endif
534 
536 #endif /* THREAD_H */
clist_node_t rq_entry
run queue entry
Definition: thread.h:190
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:198
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:184
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:203
void thread_stack_print(void)
Print the current stack to stdout.
thread_t holds thread's context data.
Definition: thread.h:179
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:187
Describes a message object which can be sent between threads.
Definition: msg.h:184
char * sp
thread's stack pointer
Definition: thread.h:180
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:211
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:174
uint8_t priority
thread's priority
Definition: thread.h:182
List node structure.
Definition: list.h:40
uint8_t status
thread's status
Definition: thread.h:181
void * wait_data
used by msg, mbox and thread flags
Definition: thread.h:194
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:201
int stack_size
thread's stack size
Definition: thread.h:212
char * stack_start
thread's stack start address
Definition: thread.h:208
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition: thread.h:449
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.