From f0ce992d4aad01c71bc8147109304ef744a46bf4 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Tue, 19 Jan 2021 10:28:31 +0100 Subject: [PATCH 1/9] core/thread: uncrustify header file --- core/include/thread.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/include/thread.h b/core/include/thread.h index 22733b654c..16d4e6a36f 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -594,7 +594,8 @@ static inline int thread_has_msg_queue(const volatile struct _thread *thread) * @param thread thread to work on * @returns status of thread */ -static inline thread_status_t thread_get_status(const thread_t *thread) { +static inline thread_status_t thread_get_status(const thread_t *thread) +{ return thread->status; } @@ -604,7 +605,8 @@ static inline thread_status_t thread_get_status(const thread_t *thread) { * @param thread thread to work on * @returns true if thread is active, false otherwise */ -static inline bool thread_is_active(const thread_t *thread) { +static inline bool thread_is_active(const thread_t *thread) +{ return thread->status >= STATUS_ON_RUNQUEUE; } From 9d5f87bf6750da92f89a0a31c034989a898be453 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 18 Jan 2021 17:07:09 +0100 Subject: [PATCH 2/9] core/thread: Allow for inline thread_yield_higher Similar to irq.h, this allows for inline the often trivial thread_yield_higher function --- core/include/thread.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core/include/thread.h b/core/include/thread.h index 16d4e6a36f..5b1ba1fdc4 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -129,10 +129,24 @@ #include "thread_flags.h" #endif +#include "thread_arch.h" + #ifdef __cplusplus extern "C" { #endif +/** + * @brief Macro definition to inline some of the platform specific + * implementations + * + * Should be enabled when advantageous by CPU's in their thread_arch.h header + */ +#ifdef THREAD_API_INLINED +#define THREAD_MAYBE_INLINE static inline __attribute__((always_inline)) +#else +#define THREAD_MAYBE_INLINE +#endif /* THREAD_API_INLINED */ + #if defined(DEVELHELP) && !defined(CONFIG_THREAD_NAMES) /** * @brief This global macro enable storage of thread names to help developers. @@ -431,7 +445,7 @@ void thread_yield(void); * * @see thread_yield() */ -void thread_yield_higher(void); +THREAD_MAYBE_INLINE void thread_yield_higher(void); /** * @brief Puts the current thread into zombie state. From 0d43c96ac787c6a332e1b7294276061452ba86f3 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 18 Jan 2021 17:08:07 +0100 Subject: [PATCH 3/9] cpu/avr8_common: Add dummy thread_arch.h header The thread_yield_higher function is complex enough to not inline it for the avr8 cpu --- cpu/avr8_common/include/thread_arch.h | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cpu/avr8_common/include/thread_arch.h diff --git a/cpu/avr8_common/include/thread_arch.h b/cpu/avr8_common/include/thread_arch.h new file mode 100644 index 0000000000..ee85fb511b --- /dev/null +++ b/cpu/avr8_common/include/thread_arch.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021 Koen Zandberg + * 2021 Inria + * + * 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. + */ + +/** + * @ingroup cpu_avr8_common + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Koen Zandberg + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */ From 0129e73ec02f5323665ed26761f658c39b9df11c Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 18 Jan 2021 17:08:58 +0100 Subject: [PATCH 4/9] cpu/cortexm_common: Inline thread_yield_higher function --- cpu/cortexm_common/include/thread_arch.h | 49 ++++++++++++++++++++++++ cpu/cortexm_common/thread_arch.c | 10 ----- 2 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 cpu/cortexm_common/include/thread_arch.h diff --git a/cpu/cortexm_common/include/thread_arch.h b/cpu/cortexm_common/include/thread_arch.h new file mode 100644 index 0000000000..e75ec0af57 --- /dev/null +++ b/cpu/cortexm_common/include/thread_arch.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2021 Koen Zandberg + * 2021 Inria + * + * 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. + */ + +/** + * @ingroup cpu_cortexm_common + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Koen Zandberg + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define THREAD_API_INLINED + +#ifndef DOXYGEN /* Doxygen is in core/include/thread.h */ + +static inline __attribute__((always_inline)) void thread_yield_higher(void) +{ + /* trigger the PENDSV interrupt to run scheduler and schedule new thread if + * applicable */ + SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; + /* flush the pipeline. Otherwise we risk that subsequent instructions are + * executed before the IRQ has actually triggered */ + __ISB(); +} + +#endif /* DOXYGEN */ + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */ diff --git a/cpu/cortexm_common/thread_arch.c b/cpu/cortexm_common/thread_arch.c index e6b25e6e67..cb8c9004aa 100644 --- a/cpu/cortexm_common/thread_arch.c +++ b/cpu/cortexm_common/thread_arch.c @@ -296,16 +296,6 @@ void NORETURN cpu_switch_context_exit(void) UNREACHABLE(); } -void thread_yield_higher(void) -{ - /* trigger the PENDSV interrupt to run scheduler and schedule new thread if - * applicable */ - SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; - /* flush the pipeline. Otherwise we risk that subsequent instructions are - * executed before the IRQ has actually triggered */ - __ISB(); -} - #if CPU_CORE_CORTEXM_FULL_THUMB void __attribute__((naked)) __attribute__((used)) isr_pendsv(void) { __asm__ volatile ( From 9979646b8bc0b4a1af41e3d1c87b16cfd4232dda Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 18 Jan 2021 17:09:19 +0100 Subject: [PATCH 5/9] cpu/fe310: Inline thread_yield_higher function --- cpu/fe310/include/thread_arch.h | 57 +++++++++++++++++++++++++++++++++ cpu/fe310/thread_arch.c | 18 ----------- 2 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 cpu/fe310/include/thread_arch.h diff --git a/cpu/fe310/include/thread_arch.h b/cpu/fe310/include/thread_arch.h new file mode 100644 index 0000000000..2af55690d2 --- /dev/null +++ b/cpu/fe310/include/thread_arch.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2021 Koen Zandberg + * 2021 Inria + * + * 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. + */ + +/** + * @ingroup cpu_fe310 + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Koen Zandberg + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define THREAD_API_INLINED + +#ifndef DOXYGEN /* Doxygen is in core/include/thread.h */ + +static inline void _ecall_dispatch(uint32_t num, void *ctx) +{ + /* function arguments are in a0 and a1 as per ABI */ + __asm__ volatile ( + "mv a0, %[num] \n" + "mv a1, %[ctx] \n" + "ECALL\n" + : /* No outputs */ + : [num] "r" (num), [ctx] "r" (ctx) + : "memory" + ); +} + +static inline __attribute__((always_inline)) void thread_yield_higher(void) +{ + _ecall_dispatch(0, NULL); +} + +#endif /* DOXYGEN */ + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */ diff --git a/cpu/fe310/thread_arch.c b/cpu/fe310/thread_arch.c index 54e5b3cf79..96674a15c6 100644 --- a/cpu/fe310/thread_arch.c +++ b/cpu/fe310/thread_arch.c @@ -179,24 +179,6 @@ void cpu_switch_context_exit(void) UNREACHABLE(); } -static inline void _ecall_dispatch(uint32_t num, void *ctx) -{ - /* function arguments are in a0 and a1 as per ABI */ - __asm__ volatile ( - "mv a0, %[num] \n" - "mv a1, %[ctx] \n" - "ECALL\n" - : /* No outputs */ - : [num] "r" (num), [ctx] "r" (ctx) - : "memory" - ); -} - -void thread_yield_higher(void) -{ - _ecall_dispatch(0, NULL); -} - /** * @brief Print heap statistics */ From 0b01a999bec958b6d44f7664dbe16f98b20c1740 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 18 Jan 2021 17:09:36 +0100 Subject: [PATCH 6/9] cpu/mips32r2_common: Inline thread_yield_higher function --- cpu/mips32r2_common/include/thread_arch.h | 51 +++++++++++++++++++++++ cpu/mips32r2_common/thread_arch.c | 12 ------ 2 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 cpu/mips32r2_common/include/thread_arch.h diff --git a/cpu/mips32r2_common/include/thread_arch.h b/cpu/mips32r2_common/include/thread_arch.h new file mode 100644 index 0000000000..00799085ca --- /dev/null +++ b/cpu/mips32r2_common/include/thread_arch.h @@ -0,0 +1,51 @@ +/* + * Copyright(C) 2017, 2016, Imagination Technologies Limited and/or its + * affiliated group companies. + * + * 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. + */ + +/** + * @ingroup cpu_mips32r2_common + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Neil Jones + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define THREAD_API_INLINED + +#ifndef DOXYGEN /* Doxygen is in core/include/thread.h */ + +static inline __attribute__((always_inline)) void thread_yield_higher(void) +{ + /* + * throw a syscall exception to get into exception level + * we context switch at exception level. + * + * Note syscall 1 is reserved for UHI see: + * http://wiki.prplfoundation.org/w/images/4/42/UHI_Reference_Manual.pdf + */ + __asm volatile ("syscall 2"); +} + +#endif /* DOXYGEN */ + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */ diff --git a/cpu/mips32r2_common/thread_arch.c b/cpu/mips32r2_common/thread_arch.c index 449ce1e203..b574462fc1 100644 --- a/cpu/mips32r2_common/thread_arch.c +++ b/cpu/mips32r2_common/thread_arch.c @@ -141,18 +141,6 @@ void cpu_switch_context_exit(void) UNREACHABLE(); } -void thread_yield_higher(void) -{ - /* - * throw a syscall exception to get into exception level - * we context switch at exception level. - * - * Note syscall 1 is reserved for UHI see: - * http://wiki.prplfoundation.org/w/images/4/42/UHI_Reference_Manual.pdf - */ - __asm volatile ("syscall 2"); -} - struct linkctx* exctx_find(reg_t id, struct gpctx *gp) { struct linkctx **ctx = (struct linkctx **)&gp->link; From bce9e3caeb6e20277f37a284f27c29925be2a820 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Tue, 19 Jan 2021 10:30:42 +0100 Subject: [PATCH 7/9] cpu/msp430_common: Add dummy thread_arch.h header --- cpu/msp430_common/include/thread_arch.h | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cpu/msp430_common/include/thread_arch.h diff --git a/cpu/msp430_common/include/thread_arch.h b/cpu/msp430_common/include/thread_arch.h new file mode 100644 index 0000000000..b123f7c513 --- /dev/null +++ b/cpu/msp430_common/include/thread_arch.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021 Koen Zandberg + * 2021 Inria + * + * 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. + */ + +/** + * @ingroup cpu_msp430_common + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Koen Zandberg + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */ From 84dfc88ca3f4d3a97e87a7e1b7859d6b55077e34 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Tue, 19 Jan 2021 10:40:40 +0100 Subject: [PATCH 8/9] cpu/arm7_common: Inline thread_yield_higher function --- cpu/arm7_common/arm_cpu.c | 10 ------ cpu/arm7_common/include/thread_arch.h | 52 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 cpu/arm7_common/include/thread_arch.h diff --git a/cpu/arm7_common/arm_cpu.c b/cpu/arm7_common/arm_cpu.c index 855318d5b6..d358f3c0ae 100644 --- a/cpu/arm7_common/arm_cpu.c +++ b/cpu/arm7_common/arm_cpu.c @@ -36,16 +36,6 @@ __attribute__((used, section(".svc_stack"), aligned(4))) uint8_t svc_stack[ISR_S #error "ISR_STACKSIZE must be a multiple of 4" #endif -void thread_yield_higher(void) -{ - if (irq_is_in()) { - sched_context_switch_request = 1; - } - else { - __asm__("svc 0\n"); - } -} - /*---------------------------------------------------------------------------- * Processor specific routine - here for ARM7 * sizeof(void*) = sizeof(int) diff --git a/cpu/arm7_common/include/thread_arch.h b/cpu/arm7_common/include/thread_arch.h new file mode 100644 index 0000000000..c3f66c2e51 --- /dev/null +++ b/cpu/arm7_common/include/thread_arch.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2008, 2009 Heiko Will + * Copyright (C) 2009 Kaspar Schleiser + * + * 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. + */ + +/** + * @ingroup cpu_arm7_common + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Kaspar Schleiser + * @author Heiko Will + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#include "irq.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define THREAD_API_INLINED + +#ifndef DOXYGEN /* Doxygen is in core/include/thread.h */ + +static inline __attribute__((always_inline)) void thread_yield_higher(void) +{ + if (irq_is_in()) { + sched_context_switch_request = 1; + } + else { + __asm__("svc 0\n"); + } +} + +#endif /* DOXYGEN */ + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */ From 517fc585b1e25451c5dbbf62f2c8139695ba42cb Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Fri, 22 Jan 2021 09:21:46 +0100 Subject: [PATCH 9/9] cpu/native: Add dummy thread_arch.h header --- cpu/native/include/thread_arch.h | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cpu/native/include/thread_arch.h diff --git a/cpu/native/include/thread_arch.h b/cpu/native/include/thread_arch.h new file mode 100644 index 0000000000..0f27dcbfe1 --- /dev/null +++ b/cpu/native/include/thread_arch.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021 Koen Zandberg + * 2021 Inria + * + * 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. + */ + +/** + * @ingroup cpu_native + * @{ + * + * @file + * @brief Implementation of the kernels thread interface + * + * @author Koen Zandberg + * + * @} + */ +#ifndef THREAD_ARCH_H +#define THREAD_ARCH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* THREAD_ARCH_H */ +/** @} */