2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2014-04-01 11:46:21 +02:00
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
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"
|
2014-03-17 17:59:06 +01:00
|
|
|
#include "arch/thread_arch.h"
|
2010-09-30 15:08:50 +02:00
|
|
|
|
2014-02-17 12:28:54 +01:00
|
|
|
#define STATUS_NOT_FOUND (-1)
|
|
|
|
|
2014-04-01 11:46:21 +02:00
|
|
|
/**
|
|
|
|
* @name 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
|
|
|
|
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Creates a new thread
|
|
|
|
*
|
|
|
|
* Creating a new thread is done in two steps:
|
|
|
|
* 1. the new thread's stack is initialized depending on the platform
|
|
|
|
* 2. the new thread is added to the scheduler to be run
|
|
|
|
*
|
|
|
|
* As RIOT is using a fixed priority scheduling algorithm, threads
|
|
|
|
* are scheduled base on their priority. The priority is fixed for every thread
|
|
|
|
* and specified during the threads creation by the *priority* parameter.
|
|
|
|
*
|
|
|
|
* A low value for *priority* number means the thread having a high priority
|
|
|
|
* with 0 being the highest possible priority.
|
2013-06-20 18:18:29 +02:00
|
|
|
*
|
2014-04-01 11:46:21 +02:00
|
|
|
* The lowest possible priority is *PRIORITY_IDLE - 1*. The value is depending
|
|
|
|
* on the platforms architecture, e.g. 30 in 32-bit systems, 14 in 16-bit systems.
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
|
|
|
*
|
2014-04-01 11:46:21 +02:00
|
|
|
* In addition to the priority, the *flags* argument can be used to alter the
|
|
|
|
* newly created threads behavior after creation. The following flags are available:
|
|
|
|
* - CREATE_SLEEPING the newly created thread will be put to sleeping state and
|
|
|
|
* must be waken up manually
|
|
|
|
* - CREATE_WOUT_YIELD the newly created thread will not run immediately after creation
|
|
|
|
* - CREATE_STACKTEST write markers into the thread's stack to measure the stack's memory
|
|
|
|
* usage (for debugging and profiling purposes)
|
|
|
|
*
|
|
|
|
* @param[out] stack start address of the preallocated stack memory
|
|
|
|
* @param[in] stacksize the size of the thread's stack in bytes
|
|
|
|
* @param[in] priority priority of the new thread, lower mean higher priority
|
|
|
|
* @param[in] flags optional flags for the creation of the new thread
|
|
|
|
* @param[in] function pointer to the code that is executed in the new thread
|
2014-03-04 20:20:01 +01:00
|
|
|
* @param[in] arg the argument to the function
|
2014-04-01 11:46:21 +02:00
|
|
|
* @param[in] name a human readable descriptor for the thread
|
|
|
|
*
|
|
|
|
* @return value ``<0`` on error
|
|
|
|
* @return pid of newly created task, otherwise
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
kernel_pid_t thread_create(char *stack,
|
2014-04-01 11:46:21 +02:00
|
|
|
int stacksize,
|
|
|
|
char priority,
|
|
|
|
int flags,
|
2014-03-04 20:20:01 +01:00
|
|
|
void *(*function)(void *arg),
|
|
|
|
void *arg,
|
2014-04-01 11:46:21 +02:00
|
|
|
const char *name);
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Returns the status of a process
|
|
|
|
*
|
|
|
|
* @param[in] pid the PID of the thread to get the status from
|
|
|
|
*
|
|
|
|
* @return status of the thread
|
|
|
|
* @return `STATUS_NOT_FOUND` if pid is unknown
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
int thread_getstatus(kernel_pid_t pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-10-17 15:25:43 +02:00
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Returns the name of a process
|
|
|
|
*
|
|
|
|
* @param[in] pid the PID of the thread to get the name from
|
|
|
|
*
|
|
|
|
* @return the threads name
|
|
|
|
* @return `NULL` if pid is unknown
|
2013-10-17 15:25:43 +02:00
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
const char *thread_getname(kernel_pid_t pid);
|
2013-10-17 15:25:43 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Puts the current thread into sleep mode. Has to be woken up externally.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
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
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief The current thread yields and let the scheduler run
|
|
|
|
*
|
|
|
|
* The current thread will resume operation immediately if there is no other thread with the same
|
|
|
|
* or a higher priority.
|
2013-12-18 17:34:42 +01:00
|
|
|
*/
|
|
|
|
void thread_yield(void);
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Wakes up a sleeping thread.
|
|
|
|
*
|
|
|
|
* @param[in] pid the PID of the thread to be woken up
|
|
|
|
*
|
|
|
|
* @return `1` on success
|
|
|
|
* @return `STATUS_NOT_FOUND` if pid is unknown or not sleeping
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
int thread_wakeup(kernel_pid_t pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Returns the process ID of the currently running thread
|
|
|
|
*
|
|
|
|
* @return obviously you are not a golfer.
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
kernel_pid_t thread_getpid(void);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-06-06 01:59:41 +02:00
|
|
|
#ifdef DEVELHELP
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
2014-04-01 11:46:21 +02:00
|
|
|
* @brief Measures the stack usage of a stack
|
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* Only works if the thread was created with the flag CREATE_STACKTEST.
|
|
|
|
*
|
2014-04-10 22:28:35 +02:00
|
|
|
* @param[in] stack the stack you want to measure. try `sched_active_thread->stack_start`
|
2014-04-01 11:46:21 +02: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);
|
2014-06-06 01:59:41 +02:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/* @} */
|
|
|
|
#endif /* __THREAD_H */
|