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 "arch/thread_arch.h"
126 #include "cpu_conf.h"
127 #include "sched.h"
128 
129 #ifdef MODULE_CORE_THREAD_FLAGS
130 #include "thread_flags.h"
131 #endif
132 
133 #ifdef __cplusplus
134  extern "C" {
135 #endif
136 
137 /* Thread states */
142 #define STATUS_NOT_FOUND (-1)
149 #define STATUS_STOPPED 0
150 #define STATUS_SLEEPING 1
151 #define STATUS_MUTEX_BLOCKED 2
152 #define STATUS_RECEIVE_BLOCKED 3
153 #define STATUS_SEND_BLOCKED 4
154 #define STATUS_REPLY_BLOCKED 5
155 #define STATUS_FLAG_BLOCKED_ANY 6
156 #define STATUS_FLAG_BLOCKED_ALL 7
157 #define STATUS_MBOX_BLOCKED 8
164 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
166 #define STATUS_RUNNING 9
167 #define STATUS_PENDING 10
173 struct _thread {
174  char *sp;
175  uint8_t status;
176  uint8_t priority;
178  kernel_pid_t pid;
180 #ifdef MODULE_CORE_THREAD_FLAGS
181  thread_flags_t flags;
182 #endif
183 
184  clist_node_t rq_entry;
186 #if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
187  || defined(MODULE_CORE_MBOX)
188  void *wait_data;
190 #endif
191 #if defined(MODULE_CORE_MSG)
192  list_node_t msg_waiters;
193  cib_t msg_queue;
194  msg_t *msg_array;
195 #endif
196 
197 #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD)
198  char *stack_start;
199 #endif
200 #ifdef DEVELHELP
201  const char *name;
202  int stack_size;
203 #endif
204 };
205 
213 #ifndef THREAD_STACKSIZE_DEFAULT
214 #error THREAD_STACKSIZE_DEFAULT must be defined per CPU
215 #endif
216 #ifdef DOXYGEN
217 #define THREAD_STACKSIZE_DEFAULT
218 #endif
219 
227 #ifndef THREAD_STACKSIZE_IDLE
228 #error THREAD_STACKSIZE_IDLE must be defined per CPU
229 #endif
230 #ifdef DOXYGEN
231 #define THREAD_STACKSIZE_IDLE
232 #endif
233 
242 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
243 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
244 #endif
245 #ifdef DOXYGEN
246 #define THREAD_EXTRA_STACKSIZE_PRINTF
247 #endif
248 
253 #ifndef THREAD_STACKSIZE_MAIN
254 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
255 #endif
256 
260 #ifndef THREAD_STACKSIZE_MINIMUM
261 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
262 #endif
263 
268 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
274 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
280 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
289 #define THREAD_CREATE_SLEEPING (1)
294 #define THREAD_AUTO_FREE (2)
302 #define THREAD_CREATE_WOUT_YIELD (4)
308 #define THREAD_CREATE_STACKTEST (8)
335 kernel_pid_t thread_create(char *stack,
336  int stacksize,
337  char priority,
338  int flags,
339  thread_task_func_t task_func,
340  void *arg,
341  const char *name);
342 
350 volatile thread_t *thread_get(kernel_pid_t pid);
351 
361 
365 void thread_sleep(void);
366 
378 void thread_yield(void);
379 
392 void thread_yield_higher(void);
393 
402 int thread_wakeup(kernel_pid_t pid);
403 
409 static inline kernel_pid_t thread_getpid(void)
410 {
411  extern volatile kernel_pid_t sched_active_pid;
412  return sched_active_pid;
413 }
414 
425 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
426 
440 void thread_add_to_list(list_node_t *list, thread_t *thread);
441 
442 #ifdef DEVELHELP
443 
451 const char *thread_getname(kernel_pid_t pid);
452 
462 uintptr_t thread_measure_stack_free(char *stack);
463 #endif /* DEVELHELP */
464 
468 void thread_print_stack(void);
469 
470 #ifdef __cplusplus
471 }
472 #endif
473 
475 #endif /* THREAD_H */
volatile kernel_pid_t sched_active_pid
Process ID of active thread.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Architecture dependent kernel interface for handling and managing threads.
circular integer buffer structure
Definition: cib.h:34
uintptr_t thread_measure_stack_free(char *stack)
Measures the stack usage of a 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.
thread_t holds thread's context data.
Definition: thread.h:174
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:108
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.
Describes a message object which can be sent between threads.
Definition: msg.h:184
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_sleep(void)
Puts the current thread into sleep mode.
List node structure.
Definition: list.h:40
void thread_yield(void)
Lets current thread yield.
int thread_wakeup(kernel_pid_t pid)
Wakes up a sleeping thread.
void *(* thread_task_func_t)(void *arg)
Prototype for a thread entry function.
Definition: thread_arch.h:47
Circular integer buffer interface.
Circular linked list.
void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition: thread.h:410
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.