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 
116 #ifndef THREAD_H
117 #define THREAD_H
118 
119 #include "clist.h"
120 #include "cib.h"
121 #include "msg.h"
122 #include "arch/thread_arch.h"
123 #include "cpu_conf.h"
124 #include "sched.h"
125 
126 #ifdef MODULE_CORE_THREAD_FLAGS
127 #include "thread_flags.h"
128 #endif
129 
130 #ifdef __cplusplus
131  extern "C" {
132 #endif
133 
138 #define STATUS_NOT_FOUND (-1)
144 #define STATUS_STOPPED 0
145 #define STATUS_SLEEPING 1
146 #define STATUS_MUTEX_BLOCKED 2
147 #define STATUS_RECEIVE_BLOCKED 3
148 #define STATUS_SEND_BLOCKED 4
149 #define STATUS_REPLY_BLOCKED 5
150 #define STATUS_FLAG_BLOCKED_ANY 6
151 #define STATUS_FLAG_BLOCKED_ALL 7
152 #define STATUS_MBOX_BLOCKED 8
158 #define STATUS_ON_RUNQUEUE STATUS_RUNNING
160 #define STATUS_RUNNING 9
161 #define STATUS_PENDING 10
168 struct _thread {
169  char *sp;
170  uint8_t status;
171  uint8_t priority;
173  kernel_pid_t pid;
175 #ifdef MODULE_CORE_THREAD_FLAGS
176  thread_flags_t flags;
177 #endif
178 
179  clist_node_t rq_entry;
181 #if defined(MODULE_CORE_MSG) || defined(MODULE_CORE_THREAD_FLAGS) \
182  || defined(MODULE_CORE_MBOX)
183  void *wait_data;
185 #endif
186 #if defined(MODULE_CORE_MSG)
187  list_node_t msg_waiters;
188  cib_t msg_queue;
189  msg_t *msg_array;
190 #endif
191 
192 #if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD)
193  char *stack_start;
194 #endif
195 #ifdef DEVELHELP
196  const char *name;
197  int stack_size;
198 #endif
199 };
200 
208 #ifndef THREAD_STACKSIZE_DEFAULT
209 #error THREAD_STACKSIZE_DEFAULT must be defined per CPU
210 #endif
211 #ifdef DOXYGEN
212 #define THREAD_STACKSIZE_DEFAULT
213 #endif
214 
222 #ifndef THREAD_STACKSIZE_IDLE
223 #error THREAD_STACKSIZE_IDLE must be defined per CPU
224 #endif
225 #ifdef DOXYGEN
226 #define THREAD_STACKSIZE_IDLE
227 #endif
228 
237 #ifndef THREAD_EXTRA_STACKSIZE_PRINTF
238 #error THREAD_EXTRA_STACKSIZE_PRINTF must be defined per CPU
239 #endif
240 #ifdef DOXYGEN
241 #define THREAD_EXTRA_STACKSIZE_PRINTF
242 #endif
243 
248 #ifndef THREAD_STACKSIZE_MAIN
249 #define THREAD_STACKSIZE_MAIN (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
250 #endif
251 
255 #ifndef THREAD_STACKSIZE_MINIMUM
256 #define THREAD_STACKSIZE_MINIMUM (sizeof(thread_t))
257 #endif
258 
263 #define THREAD_PRIORITY_MIN (SCHED_PRIO_LEVELS-1)
269 #define THREAD_PRIORITY_IDLE (THREAD_PRIORITY_MIN)
275 #define THREAD_PRIORITY_MAIN (THREAD_PRIORITY_MIN - (SCHED_PRIO_LEVELS/2))
284 #define THREAD_CREATE_SLEEPING (1)
289 #define THREAD_AUTO_FREE (2)
297 #define THREAD_CREATE_WOUT_YIELD (4)
303 #define THREAD_CREATE_STACKTEST (8)
330 kernel_pid_t thread_create(char *stack,
331  int stacksize,
332  char priority,
333  int flags,
334  thread_task_func_t task_func,
335  void *arg,
336  const char *name);
337 
345 volatile thread_t *thread_get(kernel_pid_t pid);
346 
356 
360 void thread_sleep(void);
361 
373 void thread_yield(void);
374 
387 void thread_yield_higher(void);
388 
397 int thread_wakeup(kernel_pid_t pid);
398 
404 static inline kernel_pid_t thread_getpid(void)
405 {
406  extern volatile kernel_pid_t sched_active_pid;
407  return sched_active_pid;
408 }
409 
420 char *thread_stack_init(thread_task_func_t task_func, void *arg, void *stack_start, int stack_size);
421 
435 void thread_add_to_list(list_node_t *list, thread_t *thread);
436 
437 #ifdef DEVELHELP
438 
446 const char *thread_getname(kernel_pid_t pid);
447 
457 uintptr_t thread_measure_stack_free(char *stack);
458 #endif /* DEVELHELP */
459 
463 void thread_print_stack(void);
464 
465 #ifdef __cplusplus
466 }
467 #endif
468 
470 #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:405
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:169
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.