diff --git a/arch/arc/core/thread.c b/arch/arc/core/thread.c index c0e1419f3b7..3b14a03fbeb 100644 --- a/arch/arc/core/thread.c +++ b/arch/arc/core/thread.c @@ -83,11 +83,9 @@ void _new_thread(char *pStackMem, size_t stackSize, char *stackEnd = pStackMem + stackSize; struct init_stack_frame *pInitCtx; - struct k_thread *thread = (struct k_thread *) pStackMem; + struct k_thread *thread; -#ifdef CONFIG_INIT_STACKS - memset(pStackMem, 0xaa, stackSize); -#endif + thread = _new_thread_init(pStackMem, stackSize, priority, options); /* carve the thread entry struct from the "base" of the stack */ @@ -113,18 +111,6 @@ void _new_thread(char *pStackMem, size_t stackSize, pInitCtx->status32 = _ARC_V2_STATUS32_E(_ARC_V2_DEF_IRQ_LEVEL); #endif - _init_thread_base(&thread->base, priority, _THREAD_PRESTART, options); - - /* static threads overwrite them afterwards with real values */ - thread->init_data = NULL; - thread->fn_abort = NULL; - -#ifdef CONFIG_THREAD_CUSTOM_DATA - /* Initialize custom data field (value is opaque to kernel) */ - - thread->custom_data = NULL; -#endif - #ifdef CONFIG_THREAD_MONITOR /* * In debug mode thread->entry give direct access to the thread entry diff --git a/arch/arm/core/thread.c b/arch/arm/core/thread.c index a64896d48fe..afe93719248 100644 --- a/arch/arm/core/thread.c +++ b/arch/arm/core/thread.c @@ -83,9 +83,7 @@ void _new_thread(char *pStackMem, size_t stackSize, struct __esf *pInitCtx; struct k_thread *thread = (struct k_thread *) pStackMem; -#ifdef CONFIG_INIT_STACKS - memset(pStackMem, 0xaa, stackSize); -#endif + thread = _new_thread_init(pStackMem, stackSize, priority, options); /* carve the thread entry struct from the "base" of the stack */ @@ -100,18 +98,6 @@ void _new_thread(char *pStackMem, size_t stackSize, pInitCtx->xpsr = 0x01000000UL; /* clear all, thumb bit is 1, even if RO */ - _init_thread_base(&thread->base, priority, _THREAD_PRESTART, options); - - /* static threads overwrite it afterwards with real value */ - thread->init_data = NULL; - thread->fn_abort = NULL; - -#ifdef CONFIG_THREAD_CUSTOM_DATA - /* Initialize custom data field (value is opaque to kernel) */ - - thread->custom_data = NULL; -#endif - #ifdef CONFIG_THREAD_MONITOR /* * In debug mode thread->entry give direct access to the thread entry diff --git a/arch/nios2/core/thread.c b/arch/nios2/core/thread.c index cd011242edb..941cb1dccff 100644 --- a/arch/nios2/core/thread.c +++ b/arch/nios2/core/thread.c @@ -60,9 +60,8 @@ void _new_thread(char *stack_memory, size_t stack_size, struct k_thread *thread; struct init_stack_frame *iframe; -#ifdef CONFIG_INIT_STACKS - memset(stack_memory, 0xaa, stack_size); -#endif + thread = _new_thread_init(stack_memory, stack_size, priority, options); + /* Initial stack frame data, stored at the base of the stack */ iframe = (struct init_stack_frame *) STACK_ROUND_DOWN(stack_memory + stack_size - sizeof(*iframe)); @@ -73,19 +72,6 @@ void _new_thread(char *stack_memory, size_t stack_size, iframe->arg2 = arg2; iframe->arg3 = arg3; - /* Initialize various struct k_thread members */ - thread = (struct k_thread *)stack_memory; - - _init_thread_base(&thread->base, priority, _THREAD_PRESTART, options); - - /* static threads overwrite it afterwards with real value */ - thread->init_data = NULL; - thread->fn_abort = NULL; - -#ifdef CONFIG_THREAD_CUSTOM_DATA - /* Initialize custom data field (value is opaque to kernel) */ - thread->custom_data = NULL; -#endif thread->callee_saved.sp = (u32_t)iframe; thread->callee_saved.ra = (u32_t)_thread_entry_wrapper; thread->callee_saved.key = NIOS2_STATUS_PIE_MSK; diff --git a/arch/riscv32/core/thread.c b/arch/riscv32/core/thread.c index d209e172f81..435e222331a 100644 --- a/arch/riscv32/core/thread.c +++ b/arch/riscv32/core/thread.c @@ -44,9 +44,8 @@ void _new_thread(char *stack_memory, size_t stack_size, struct k_thread *thread; struct __esf *stack_init; -#ifdef CONFIG_INIT_STACKS - memset(stack_memory, 0xaa, stack_size); -#endif + thread = _new_thread_init(stack_memory, stack_size, priority, options); + /* Initial stack frame for thread */ stack_init = (struct __esf *) STACK_ROUND_DOWN(stack_memory + @@ -83,20 +82,6 @@ void _new_thread(char *stack_memory, size_t stack_size, stack_init->mstatus = SOC_MSTATUS_DEF_RESTORE; stack_init->mepc = (u32_t)_thread_entry_wrapper; - /* Initialize various struct k_thread members */ - thread = (struct k_thread *)stack_memory; - - _init_thread_base(&thread->base, priority, _THREAD_PRESTART, options); - - /* static threads overwrite it afterwards with real value */ - thread->init_data = NULL; - thread->fn_abort = NULL; - -#ifdef CONFIG_THREAD_CUSTOM_DATA - /* Initialize custom data field (value is opaque to kernel) */ - thread->custom_data = NULL; -#endif - thread->callee_saved.sp = (u32_t)stack_init; thread_monitor_init(thread); diff --git a/arch/x86/core/thread.c b/arch/x86/core/thread.c index 66abe031edc..d9d948a05c6 100644 --- a/arch/x86/core/thread.c +++ b/arch/x86/core/thread.c @@ -67,28 +67,15 @@ static ALWAYS_INLINE void thread_monitor_init(struct k_thread *thread) */ static void _new_thread_internal(char *pStackMem, unsigned int stackSize, int priority, - unsigned int options) + unsigned int options, + struct k_thread *thread) { unsigned long *pInitialCtx; - /* ptr to the new task's k_thread */ - struct k_thread *thread = (struct k_thread *)pStackMem; #if (defined(CONFIG_FP_SHARING) || defined(CONFIG_GDB_INFO)) thread->arch.excNestCount = 0; #endif /* CONFIG_FP_SHARING || CONFIG_GDB_INFO */ - _init_thread_base(&thread->base, priority, _THREAD_PRESTART, options); - - /* static threads overwrite it afterwards with real value */ - thread->init_data = NULL; - thread->fn_abort = NULL; - -#ifdef CONFIG_THREAD_CUSTOM_DATA - /* Initialize custom data field (value is opaque to kernel) */ - - thread->custom_data = NULL; -#endif - /* * The creation of the initial stack for the task has already been done. * Now all that is needed is to set the ESP. However, we have been passed @@ -235,10 +222,9 @@ void _new_thread(char *pStackMem, size_t stackSize, _ASSERT_VALID_PRIO(priority, pEntry); unsigned long *pInitialThread; + struct k_thread *thread; -#ifdef CONFIG_INIT_STACKS - memset(pStackMem, 0xaa, stackSize); -#endif + thread = _new_thread_init(pStackMem, stackSize, priority, options); /* carve the thread entry struct from the "base" of the stack */ @@ -288,5 +274,5 @@ void _new_thread(char *pStackMem, size_t stackSize, * aside for the thread's stack. */ - _new_thread_internal(pStackMem, stackSize, priority, options); + _new_thread_internal(pStackMem, stackSize, priority, options, thread); } diff --git a/arch/xtensa/core/thread.c b/arch/xtensa/core/thread.c index bcee8a7f5ef..874ca39bf08 100644 --- a/arch/xtensa/core/thread.c +++ b/arch/xtensa/core/thread.c @@ -86,19 +86,18 @@ void _new_thread(char *pStack, size_t stackSize, /* k_thread is located at top of stack while frames are located at end * of it */ - struct k_thread *thread = (struct k_thread *)(pStack); + struct k_thread *thread; #if XCHAL_CP_NUM > 0 u32_t *cpSA; char *cpStack; #endif + thread = _new_thread_init(pStack, stackSize, priority, options); + #ifdef CONFIG_DEBUG printk("\nstackPtr = %p, stackSize = %d\n", pStack, stackSize); printk("stackEnd = %p\n", stackEnd); #endif -#ifdef CONFIG_INIT_STACKS - memset(pStack, 0xaa, stackSize); -#endif #if XCHAL_CP_NUM > 0 /* Ensure CP state descriptor is correctly initialized */ cpStack = thread->arch.preempCoprocReg.cpStack; /* short hand alias */ @@ -154,14 +153,6 @@ void _new_thread(char *pStack, size_t stackSize, #endif thread->callee_saved.topOfStack = pInitCtx; thread->arch.flags = 0; - _init_thread_base(&thread->base, prio, _THREAD_PRESTART, options); - /* static threads overwrite it afterwards with real value */ - thread->init_data = NULL; - thread->fn_abort = NULL; -#ifdef CONFIG_THREAD_CUSTOM_DATA - /* Initialize custom data field (value is opaque to kernel) */ - thread->custom_data = NULL; -#endif #ifdef CONFIG_THREAD_MONITOR /* * In debug mode thread->entry give direct access to the thread entry diff --git a/kernel/include/kernel_structs.h b/kernel/include/kernel_structs.h index 9f18f365a3c..752d0720204 100644 --- a/kernel/include/kernel_structs.h +++ b/kernel/include/kernel_structs.h @@ -12,6 +12,7 @@ #if !defined(_ASMLANGUAGE) #include #include +#include #endif /* @@ -239,6 +240,34 @@ extern void _init_thread_base(struct _thread_base *thread_base, int priority, u32_t initial_state, unsigned int options); +static ALWAYS_INLINE struct k_thread *_new_thread_init(char *pStack, + size_t stackSize, + int prio, + unsigned int options) +{ + struct k_thread *thread; + +#ifdef CONFIG_INIT_STACKS + memset(pStack, 0xaa, stackSize); +#endif + + /* Initialize various struct k_thread members */ + thread = (struct k_thread *)pStack; + + _init_thread_base(&thread->base, prio, _THREAD_PRESTART, options); + + /* static threads overwrite it afterwards with real value */ + thread->init_data = NULL; + thread->fn_abort = NULL; + +#ifdef CONFIG_THREAD_CUSTOM_DATA + /* Initialize custom data field (value is opaque to kernel) */ + thread->custom_data = NULL; +#endif + + return thread; +} + #endif /* _ASMLANGUAGE */ #endif /* _kernel_structs__h_ */