2013-11-27 16:28:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-11-27 16:28:31 +01:00
|
|
|
* @defgroup core_thread Threading
|
|
|
|
* @ingroup core
|
|
|
|
* @brief Support for multi-threading
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file thread.h
|
|
|
|
* @brief Threading API
|
|
|
|
*
|
2013-03-08 11:30:23 +01:00
|
|
|
* @author Freie Universität Berlin, Computer Systems & Telematics
|
2010-09-22 15:10:42 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*/
|
|
|
|
|
2013-11-27 16:28:31 +01:00
|
|
|
#ifndef __THREAD_H
|
|
|
|
#define __THREAD_H
|
|
|
|
|
|
|
|
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "tcb.h"
|
2010-09-30 15:08:50 +02:00
|
|
|
|
2014-02-17 12:28:54 +01:00
|
|
|
#define STATUS_NOT_FOUND (-1)
|
|
|
|
|
2010-11-05 19:33:45 +01:00
|
|
|
/** Minimum stack size */
|
2013-05-14 18:31:47 +02:00
|
|
|
#ifndef MINIMUM_STACK_SIZE
|
2013-11-27 16:28:31 +01:00
|
|
|
#define MINIMUM_STACK_SIZE (sizeof(tcb_t))
|
2013-05-14 18:31:47 +02:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Creates a new thread.
|
2013-06-20 18:18:29 +02:00
|
|
|
*
|
2010-10-25 15:40:01 +02:00
|
|
|
* @param stack Lowest address of preallocated stack space
|
2010-09-22 15:10:42 +02:00
|
|
|
* @param stacksize
|
2013-06-20 18:18:29 +02:00
|
|
|
* @param flags Options:
|
2010-09-22 15:10:42 +02:00
|
|
|
* YIELD: force context switch.
|
|
|
|
* CREATE_SLEEPING: set new thread to sleeping state, thread must be woken up manually.
|
|
|
|
* CREATE_STACKTEST: initialize stack with values needed for stack overflow testing.
|
|
|
|
*
|
|
|
|
* @param priority Priority of newly created thread. Lower number means higher
|
|
|
|
* priority. 0 means highest possible priority. Lowest priority is
|
|
|
|
* PRIORITY_IDLE-1, usually 30.
|
|
|
|
*
|
|
|
|
* @return returns <0 on error, pid of newly created task else.
|
|
|
|
*/
|
2013-06-20 18:18:29 +02:00
|
|
|
int thread_create(char *stack, int stacksize, char priority, int flags, void (*function) (void), const char *name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief returns the status of a process.
|
|
|
|
* @return STATUS_NOT_FOUND if pid is unknown
|
|
|
|
*/
|
2014-02-17 12:28:54 +01:00
|
|
|
int thread_getstatus(int pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-10-17 15:25:43 +02:00
|
|
|
/**
|
|
|
|
* @brief returns the name of a process.
|
|
|
|
* @return NULL if pid is unknown
|
|
|
|
*/
|
|
|
|
const char *thread_getname(int pid);
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Puts the current thread into sleep mode. Has to be woken up externally.
|
|
|
|
*/
|
2013-02-06 13:20:21 +01:00
|
|
|
void thread_sleep(void);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-12-18 17:34:42 +01:00
|
|
|
/**
|
|
|
|
* @brief The current thread yields and let the scheduler run.
|
|
|
|
*/
|
|
|
|
void thread_yield(void);
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Wakes up a sleeping thread.
|
|
|
|
* @param pid The PID of the thread to be woken up
|
|
|
|
* @return STATUS_NOT_FOUND if pid is unknown or not sleeping
|
|
|
|
*/
|
|
|
|
int thread_wakeup(int pid);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the process ID of the currently running thread.
|
|
|
|
* @return Obviously you are not a golfer.
|
|
|
|
*/
|
2013-02-06 13:20:21 +01:00
|
|
|
int thread_getpid(void);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2012-11-13 17:26:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns the process ID of the thread running before the current one.
|
|
|
|
* @return Obviously you are not a golfer.
|
|
|
|
*/
|
2013-02-06 13:20:21 +01:00
|
|
|
int thread_getlastpid(void);
|
2012-11-13 17:26:43 +01:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Measures the stack usage of a stack.
|
|
|
|
* Only works if the thread was created with the flag CREATE_STACKTEST.
|
|
|
|
*
|
2010-10-28 11:22:57 +02:00
|
|
|
* @param stack The stack you want to measure. try active_thread->stack_start.
|
2014-01-20 10:42:59 +01:00
|
|
|
* @return The amount of unused space of the thread's stack
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-01-20 10:46:20 +01:00
|
|
|
int thread_measure_stack_free(char *stack);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/* @} */
|
|
|
|
#endif /* __THREAD_H */
|