2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_thread
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Threading implementation
|
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-01-17 15:17:19 +01:00
|
|
|
#include "assert.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "thread.h"
|
2014-02-15 18:49:27 +01:00
|
|
|
#include "irq.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-07-24 00:36:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "debug.h"
|
|
|
|
#include "bitarithm.h"
|
2010-10-28 11:22:57 +02:00
|
|
|
#include "sched.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
volatile thread_t *thread_get(kernel_pid_t pid)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-10-10 15:02:15 +02:00
|
|
|
if (pid_is_valid(pid)) {
|
2014-08-13 09:26:27 +02:00
|
|
|
return sched_threads[pid];
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
2014-08-13 09:26:27 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-08-13 09:26:27 +02:00
|
|
|
int thread_getstatus(kernel_pid_t pid)
|
|
|
|
{
|
2016-01-04 22:29:34 +01:00
|
|
|
volatile thread_t *t = thread_get(pid);
|
2019-02-11 14:25:46 +01:00
|
|
|
return t ? (int)t->status : (int)STATUS_NOT_FOUND;
|
2010-09-22 15:10:42 +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
|
|
|
{
|
2017-10-20 23:10:20 +02:00
|
|
|
#ifdef DEVELHELP
|
2016-01-04 22:29:34 +01:00
|
|
|
volatile thread_t *t = thread_get(pid);
|
2014-08-13 09:26:27 +02:00
|
|
|
return t ? t->name : NULL;
|
2017-10-20 23:10:20 +02:00
|
|
|
#else
|
|
|
|
(void)pid;
|
|
|
|
return NULL;
|
2014-10-15 00:04:58 +02:00
|
|
|
#endif
|
2017-10-20 23:10:20 +02:00
|
|
|
}
|
2013-10-17 15:25:43 +02:00
|
|
|
|
2014-05-07 12:36:32 +02:00
|
|
|
void thread_sleep(void)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
if (irq_is_in()) {
|
2013-06-20 18:18:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2016-01-04 22:29:34 +01:00
|
|
|
sched_set_status((thread_t *)sched_active_thread, STATUS_SLEEPING);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 22:57:56 +02:00
|
|
|
int thread_wakeup(kernel_pid_t pid)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-07-06 22:57:56 +02:00
|
|
|
DEBUG("thread_wakeup: Trying to wakeup PID %" PRIkernel_pid "...\n", pid);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned old_state = irq_disable();
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2016-03-03 20:29:55 +01:00
|
|
|
thread_t *other_thread = (thread_t *) thread_get(pid);
|
|
|
|
|
|
|
|
if (!other_thread) {
|
|
|
|
DEBUG("thread_wakeup: Thread does not exist!\n");
|
|
|
|
}
|
|
|
|
else if (other_thread->status == STATUS_SLEEPING) {
|
2010-11-02 11:40:10 +01:00
|
|
|
DEBUG("thread_wakeup: Thread is sleeping.\n");
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-02-13 23:17:36 +01:00
|
|
|
sched_set_status(other_thread, STATUS_RUNNING);
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2014-04-25 18:20:42 +02:00
|
|
|
sched_switch(other_thread->priority);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-11-04 16:21:39 +01:00
|
|
|
return 1;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-11-02 11:40:10 +01:00
|
|
|
DEBUG("thread_wakeup: Thread is not sleeping!\n");
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2016-03-03 20:29:55 +01:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2019-02-11 14:25:46 +01:00
|
|
|
return (int)STATUS_NOT_FOUND;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 00:49:02 +01:00
|
|
|
void thread_yield(void)
|
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned old_state = irq_disable();
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t *me = (thread_t *)sched_active_thread;
|
2014-10-28 00:49:02 +01:00
|
|
|
if (me->status >= STATUS_ON_RUNQUEUE) {
|
2016-07-13 00:53:14 +02:00
|
|
|
clist_lpoprpush(&sched_runqueues[me->priority]);
|
2014-10-28 00:49:02 +01:00
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(old_state);
|
2014-10-28 00:49:02 +01:00
|
|
|
|
|
|
|
thread_yield_higher();
|
|
|
|
}
|
|
|
|
|
2015-01-17 15:17:19 +01:00
|
|
|
void thread_add_to_list(list_node_t *list, thread_t *thread)
|
|
|
|
{
|
|
|
|
assert (thread->status < STATUS_ON_RUNQUEUE);
|
|
|
|
|
|
|
|
uint16_t my_prio = thread->priority;
|
|
|
|
list_node_t *new_node = (list_node_t*)&thread->rq_entry;
|
|
|
|
|
|
|
|
while (list->next) {
|
|
|
|
thread_t *list_entry = container_of((clist_node_t*)list->next, thread_t, rq_entry);
|
|
|
|
if (list_entry->priority > my_prio) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_node->next = list->next;
|
|
|
|
list->next = new_node;
|
|
|
|
}
|
|
|
|
|
2014-06-06 01:59:41 +02:00
|
|
|
#ifdef DEVELHELP
|
2014-06-03 21:53:15 +02:00
|
|
|
uintptr_t thread_measure_stack_free(char *stack)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-06-03 21:53:15 +02:00
|
|
|
uintptr_t *stackp = (uintptr_t *)stack;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-01-20 10:42:59 +01:00
|
|
|
/* assume that the comparison fails before or after end of stack */
|
|
|
|
/* assume that the stack grows "downwards" */
|
2014-06-03 21:53:15 +02:00
|
|
|
while (*stackp == (uintptr_t) stackp) {
|
2010-09-22 15:10:42 +02:00
|
|
|
stackp++;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-06-03 21:53:15 +02:00
|
|
|
uintptr_t space_free = (uintptr_t) stackp - (uintptr_t) stack;
|
2014-01-20 10:42:59 +01:00
|
|
|
return space_free;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-06-06 01:59:41 +02:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-10-19 23:10:14 +02:00
|
|
|
kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags, thread_task_func_t function, void *arg, const char *name)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2014-06-03 21:53:15 +02:00
|
|
|
if (priority >= SCHED_PRIO_LEVELS) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-06-06 01:55:37 +02:00
|
|
|
#ifdef DEVELHELP
|
2010-11-04 16:47:24 +01:00
|
|
|
int total_stacksize = stacksize;
|
2014-10-15 00:04:58 +02:00
|
|
|
#else
|
|
|
|
(void) name;
|
2014-06-06 01:55:37 +02:00
|
|
|
#endif
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-06-03 21:53:15 +02:00
|
|
|
/* align the stack on a 16/32bit boundary */
|
|
|
|
uintptr_t misalignment = (uintptr_t) stack % ALIGN_OF(void *);
|
|
|
|
if (misalignment) {
|
|
|
|
misalignment = ALIGN_OF(void *) - misalignment;
|
|
|
|
stack += misalignment;
|
|
|
|
stacksize -= misalignment;
|
2010-11-09 17:01:52 +01:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2014-06-03 21:53:15 +02:00
|
|
|
/* make room for the thread control block */
|
2016-01-04 22:29:34 +01:00
|
|
|
stacksize -= sizeof(thread_t);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
/* round down the stacksize to a multiple of thread_t alignments (usually 16/32bit) */
|
|
|
|
stacksize -= stacksize % ALIGN_OF(thread_t);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2016-03-03 20:30:09 +01:00
|
|
|
if (stacksize < 0) {
|
|
|
|
DEBUG("thread_create: stacksize is too small!\n");
|
|
|
|
}
|
2014-06-03 21:53:15 +02:00
|
|
|
/* allocate our thread control block at the top of our stackspace */
|
2016-01-04 22:29:34 +01:00
|
|
|
thread_t *cb = (thread_t *) (stack + stacksize);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-09-22 17:59:21 +02:00
|
|
|
#if defined(DEVELHELP) || defined(SCHED_TEST_STACK)
|
2015-12-02 11:59:20 +01:00
|
|
|
if (flags & THREAD_CREATE_STACKTEST) {
|
2010-09-22 15:10:42 +02:00
|
|
|
/* assign each int of the stack the value of it's address */
|
2014-06-03 21:53:15 +02:00
|
|
|
uintptr_t *stackmax = (uintptr_t *) (stack + stacksize);
|
|
|
|
uintptr_t *stackp = (uintptr_t *) stack;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (stackp < stackmax) {
|
2014-06-03 21:53:15 +02:00
|
|
|
*stackp = (uintptr_t) stackp;
|
2010-09-22 15:10:42 +02:00
|
|
|
stackp++;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-09-22 15:10:42 +02:00
|
|
|
/* create stack guard */
|
2015-06-13 01:02:40 +02:00
|
|
|
*(uintptr_t *) stack = (uintptr_t) stack;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-06-06 01:59:41 +02:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-08-13 09:08:50 +02:00
|
|
|
kernel_pid_t pid = KERNEL_PID_UNDEF;
|
|
|
|
for (kernel_pid_t i = KERNEL_PID_FIRST; i <= KERNEL_PID_LAST; ++i) {
|
|
|
|
if (sched_threads[i] == NULL) {
|
|
|
|
pid = i;
|
2010-09-22 15:10:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 09:08:50 +02:00
|
|
|
if (pid == KERNEL_PID_UNDEF) {
|
2010-09-22 15:10:42 +02:00
|
|
|
DEBUG("thread_create(): too many threads!\n");
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
return -EOVERFLOW;
|
|
|
|
}
|
|
|
|
|
2014-08-13 09:08:50 +02:00
|
|
|
sched_threads[pid] = cb;
|
|
|
|
|
|
|
|
cb->pid = pid;
|
2014-03-04 20:20:01 +01:00
|
|
|
cb->sp = thread_stack_init(function, arg, stack, stacksize);
|
2014-06-06 01:55:37 +02:00
|
|
|
|
2016-10-24 18:36:15 +02:00
|
|
|
#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) || defined(MODULE_MPU_STACK_GUARD)
|
2014-10-15 00:04:58 +02:00
|
|
|
cb->stack_start = stack;
|
2015-09-22 17:59:21 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEVELHELP
|
2010-11-04 16:47:24 +01:00
|
|
|
cb->stack_size = total_stacksize;
|
2014-10-15 00:04:58 +02:00
|
|
|
cb->name = name;
|
2014-06-06 01:55:37 +02:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-10-25 15:40:01 +02:00
|
|
|
cb->priority = priority;
|
2019-02-11 14:25:46 +01:00
|
|
|
cb->status = STATUS_STOPPED;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-11-26 14:21:48 +01:00
|
|
|
cb->rq_entry.next = NULL;
|
|
|
|
|
2016-02-26 22:19:31 +01:00
|
|
|
#ifdef MODULE_CORE_MSG
|
2010-10-25 15:40:01 +02:00
|
|
|
cb->wait_data = NULL;
|
2016-04-10 00:16:48 +02:00
|
|
|
cb->msg_waiters.next = NULL;
|
2013-06-20 18:18:29 +02:00
|
|
|
cib_init(&(cb->msg_queue), 0);
|
2010-11-26 15:02:15 +01:00
|
|
|
cb->msg_array = NULL;
|
2016-02-26 22:19:31 +01:00
|
|
|
#endif
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-04-10 22:28:35 +02:00
|
|
|
sched_num_threads++;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-07-06 22:57:56 +02:00
|
|
|
DEBUG("Created thread %s. PID: %" PRIkernel_pid ". Priority: %u.\n", name, cb->pid, priority);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2015-12-02 11:59:20 +01:00
|
|
|
if (flags & THREAD_CREATE_SLEEPING) {
|
2010-10-25 15:40:01 +02:00
|
|
|
sched_set_status(cb, STATUS_SLEEPING);
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-10-25 15:40:01 +02:00
|
|
|
sched_set_status(cb, STATUS_PENDING);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2015-12-02 11:59:20 +01:00
|
|
|
if (!(flags & THREAD_CREATE_WOUT_YIELD)) {
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-12-03 20:45:00 +01:00
|
|
|
sched_switch(priority);
|
|
|
|
return pid;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
return pid;
|
|
|
|
}
|