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 #ifdef HAVE_THREAD_ARCH_T
213  thread_arch_t arch;
214 #endif
215 };
216 
224 #ifndef THREAD_STACKSIZE_DEFAULT
225 #error THREAD_STACKSIZE_DEFAULT must be defined per CPU
226 #endif
227 #ifdef DOXYGEN
228 #define THREAD_STACKSIZE_DEFAULT
229 #endif
230 
238 #ifndef THREAD_STACKSIZE_IDLE
239 #error THREAD_STACKSIZE_IDLE must be defined per CPU
240 #endif
241 #ifdef DOXYGEN
242 #define THREAD_STACKSIZE_IDLE
243 #endif
244 
252 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
253 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
254 #endif
255 #ifdef DOXYGEN
256 #define THREAD_EXTRA_STACKSIZE_PRINTF
257 #endif
258 
263 #ifndef THREAD_STACKSIZE_MAIN
264 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
265 #endif
266 
270 #ifndef THREAD_STACKSIZE_LARGE
271 #define THREAD_STACKSIZE_LARGE (THREAD_STACKSIZE_MEDIUM * 2)
272 #endif
273 
277 #ifndef THREAD_STACKSIZE_MEDIUM
278 #define THREAD_STACKSIZE_MEDIUM THREAD_STACKSIZE_DEFAULT
279 #endif
280 
284 #ifndef THREAD_STACKSIZE_SMALL
285 #define THREAD_STACKSIZE_SMALL (THREAD_STACKSIZE_MEDIUM / 2)
286 #endif
287 
291 #ifndef THREAD_STACKSIZE_TINY
292 #define THREAD_STACKSIZE_TINY (THREAD_STACKSIZE_MEDIUM / 4)
293 #endif
294 
298 #ifndef THREAD_STACKSIZE_MINIMUM
299 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
300 #endif
301 
306 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
312 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
318 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
327 #define THREAD_CREATE_SLEEPING (1)
332 #define THREAD_AUTO_FREE (2)
340 #define THREAD_CREATE_WOUT_YIELD (4)
346 #define THREAD_CREATE_STACKTEST (8)
373 kernel_pid_t thread_create(char *stack,
374  int stacksize,
375  char priority,
376  int flags,
377  thread_task_func_t task_func,
378  void *arg,
379  const char *name);
380 
389 
399 
403 void thread_sleep(void);
404 
416 void thread_yield(void);
417 
430 void thread_yield_higher(void);
431 
441 
447 static inline kernel_pid_t thread_getpid(void)
448 {
449  extern volatile kernel_pid_t sched_active_pid;
450  return sched_active_pid;
451 }
452 
463 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
464 
478 void thread_add_to_list(list_node_t *list, thread_t *thread);
479 
490 const char *thread_getname(kernel_pid_t pid);
491 
492 #ifdef DEVELHELP
493 
502 uintptr_t thread_measure_stack_free(char *stack);
503 #endif /* DEVELHELP */
504 
508 int thread_isr_stack_usage(void);
509 
513 void *thread_isr_stack_pointer(void);
514 
518 void *thread_isr_stack_start(void);
519 
523 void thread_stack_print(void);
524 
528 void thread_print_stack(void);
529 
530 #ifdef __cplusplus
531 }
532 #endif
533 
535 #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:448
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.