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 
141 #define STATUS_NOT_FOUND (-1)
147 #define STATUS_STOPPED 0
148 #define STATUS_SLEEPING 1
149 #define STATUS_MUTEX_BLOCKED 2
150 #define STATUS_RECEIVE_BLOCKED 3
151 #define STATUS_SEND_BLOCKED 4
152 #define STATUS_REPLY_BLOCKED 5
153 #define STATUS_FLAG_BLOCKED_ANY 6
154 #define STATUS_FLAG_BLOCKED_ALL 7
155 #define STATUS_MBOX_BLOCKED 8
161 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
163 #define STATUS_RUNNING 9
164 #define STATUS_PENDING 10
171 struct _thread {
172  char *sp;
173  uint8_t status;
174  uint8_t priority;
176  kernel_pid_t pid;
178 #ifdef MODULE_CORE_THREAD_FLAGS
179  thread_flags_t flags;
180 #endif
181 
182  clist_node_t rq_entry;
184 #if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
185  || defined(MODULE_CORE_MBOX)
186  void *wait_data;
188 #endif
189 #if defined(MODULE_CORE_MSG)
190  list_node_t msg_waiters;
191  cib_t msg_queue;
192  msg_t *msg_array;
193 #endif
194 
195 #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD)
196  char *stack_start;
197 #endif
198 #ifdef DEVELHELP
199  const char *name;
200  int stack_size;
201 #endif
202 };
203 
211 #ifndef THREAD_STACKSIZE_DEFAULT
212 #error THREAD_STACKSIZE_DEFAULT must be defined per CPU
213 #endif
214 #ifdef DOXYGEN
215 #define THREAD_STACKSIZE_DEFAULT
216 #endif
217 
225 #ifndef THREAD_STACKSIZE_IDLE
226 #error THREAD_STACKSIZE_IDLE must be defined per CPU
227 #endif
228 #ifdef DOXYGEN
229 #define THREAD_STACKSIZE_IDLE
230 #endif
231 
240 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
241 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
242 #endif
243 #ifdef DOXYGEN
244 #define THREAD_EXTRA_STACKSIZE_PRINTF
245 #endif
246 
251 #ifndef THREAD_STACKSIZE_MAIN
252 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
253 #endif
254 
258 #ifndef THREAD_STACKSIZE_MINIMUM
259 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
260 #endif
261 
266 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
272 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
278 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
287 #define THREAD_CREATE_SLEEPING (1)
292 #define THREAD_AUTO_FREE (2)
300 #define THREAD_CREATE_WOUT_YIELD (4)
306 #define THREAD_CREATE_STACKTEST (8)
333 kernel_pid_t thread_create(char *stack,
334  int stacksize,
335  char priority,
336  int flags,
337  thread_task_func_t task_func,
338  void *arg,
339  const char *name);
340 
348 volatile thread_t *thread_get(kernel_pid_t pid);
349 
359 
363 void thread_sleep(void);
364 
376 void thread_yield(void);
377 
390 void thread_yield_higher(void);
391 
400 int thread_wakeup(kernel_pid_t pid);
401 
407 static inline kernel_pid_t thread_getpid(void)
408 {
409  extern volatile kernel_pid_t sched_active_pid;
410  return sched_active_pid;
411 }
412 
423 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
424 
438 void thread_add_to_list(list_node_t *list, thread_t *thread);
439 
440 #ifdef DEVELHELP
441 
449 const char *thread_getname(kernel_pid_t pid);
450 
460 uintptr_t thread_measure_stack_free(char *stack);
461 #endif /* DEVELHELP */
462 
466 void thread_print_stack(void);
467 
468 #ifdef __cplusplus
469 }
470 #endif
471 
473 #endif /* THREAD_H */
volatile kernel_pid_t sched_active_pid
Process ID of active thread.
static kernel_pid_t thread_getpid(void)
Returns the process ID of the currently running thread.
Definition: thread.h:408
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
void thread_yield(void)
Lets current thread yield.
thread_t holds thread's context data.
Definition: thread.h:172
void thread_print_stack(void)
Prints human readable, ps-like thread information for debugging purposes.
const char * thread_getname(kernel_pid_t pid)
Returns the name of a process.
int thread_wakeup(kernel_pid_t pid)
Wakes up a sleeping thread.
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.
Scheduler API definition.
Messaging API for inter process communication.
Thread flags.
Describes a message object which can be sent between threads.
Definition: msg.h:184
volatile thread_t * thread_get(kernel_pid_t pid)
Retreive a thread control block by PID.
List node structure.
Definition: list.h:40
void thread_yield_higher(void)
Lets current thread yield in favor of a higher prioritized thread.
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_sleep(void)
Puts the current thread into sleep mode.
void thread_add_to_list(list_node_t *list, thread_t *thread)
Add thread to list, sorted by priority (internal)
uintptr_t thread_measure_stack_free(char *stack)
Measures the stack usage of a stack.
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.
int thread_getstatus(kernel_pid_t pid)
Returns the status of a process.