From 31b3aeb48c5459fcf9e0e1760acba54b47463da0 Mon Sep 17 00:00:00 2001 From: JulianHolzwarth Date: Fri, 23 Aug 2019 15:56:41 +0200 Subject: [PATCH 1/6] sys/posix/pthread/pthread.c: Reaper fix before the reaper was never created --- sys/posix/pthread/pthread.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sys/posix/pthread/pthread.c b/sys/posix/pthread/pthread.c index eeb553e219..59de182fd4 100644 --- a/sys/posix/pthread/pthread.c +++ b/sys/posix/pthread/pthread.c @@ -135,10 +135,9 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta size_t stack_size = attr && attr->ss_size > 0 ? attr->ss_size : PTHREAD_STACKSIZE; void *stack = autofree ? malloc(stack_size) : attr->ss_sp; pt->stack = autofree ? stack : NULL; - - if (autofree && pthread_reaper_pid != KERNEL_PID_UNDEF) { + if (autofree && pthread_reaper_pid == KERNEL_PID_UNDEF) { mutex_lock(&pthread_mutex); - if (pthread_reaper_pid != KERNEL_PID_UNDEF) { + if (pthread_reaper_pid == KERNEL_PID_UNDEF) { /* volatile pid to overcome problems with double checking */ volatile kernel_pid_t pid = thread_create(pthread_reaper_stack, PTHREAD_REAPER_STACKSIZE, From 28719bae37b54a60e50c8ce1ce97fe67e75eb012 Mon Sep 17 00:00:00 2001 From: JulianHolzwarth Date: Fri, 23 Aug 2019 16:06:58 +0200 Subject: [PATCH 2/6] sys/posix/pthread/pthread.c: check reaper creation Checking whether or not the pthread reaper was created And handeling the error when it was not created --- sys/posix/pthread/pthread.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sys/posix/pthread/pthread.c b/sys/posix/pthread/pthread.c index 59de182fd4..b344420160 100644 --- a/sys/posix/pthread/pthread.c +++ b/sys/posix/pthread/pthread.c @@ -146,6 +146,13 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta pthread_reaper, NULL, "pthread-reaper"); + if (!pid_is_valid(pid)) { + free(pt->stack); + free(pt); + pthread_sched_threads[pthread_pid-1] = NULL; + mutex_unlock(&pthread_mutex); + return -1; + } pthread_reaper_pid = pid; } mutex_unlock(&pthread_mutex); From d6bc8685f7d28365f640bddd5d4b61c9ef3fccf5 Mon Sep 17 00:00:00 2001 From: JulianHolzwarth Date: Fri, 23 Aug 2019 17:11:19 +0200 Subject: [PATCH 3/6] sys/posix/pthread/pthread.c: create thread sleeping creating the pthread sleeping and waking it up instead of THREAD_CREATE_WOUT_YIELD and then yielding(sched_switch) --- sys/posix/pthread/pthread.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sys/posix/pthread/pthread.c b/sys/posix/pthread/pthread.c index b344420160..a2cc609b0c 100644 --- a/sys/posix/pthread/pthread.c +++ b/sys/posix/pthread/pthread.c @@ -161,7 +161,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta pt->thread_pid = thread_create(stack, stack_size, THREAD_PRIORITY_MAIN, - THREAD_CREATE_WOUT_YIELD | + THREAD_CREATE_SLEEPING | THREAD_CREATE_STACKTEST, pthread_start_routine, pt, @@ -173,7 +173,7 @@ int pthread_create(pthread_t *newthread, const pthread_attr_t *attr, void *(*sta return -1; } - sched_switch(THREAD_PRIORITY_MAIN); + thread_wakeup(pt->thread_pid); return 0; } From a4d0c8a8e76843d7c3c0ea903bfd65505bcf42ba Mon Sep 17 00:00:00 2001 From: JulianHolzwarth Date: Fri, 13 Sep 2019 15:31:18 +0200 Subject: [PATCH 4/6] sys/posix/pthread/pthread.c: reduce pthread reaper size --- sys/posix/pthread/pthread.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sys/posix/pthread/pthread.c b/sys/posix/pthread/pthread.c index a2cc609b0c..5fe763f199 100644 --- a/sys/posix/pthread/pthread.c +++ b/sys/posix/pthread/pthread.c @@ -38,10 +38,10 @@ #define ENABLE_DEBUG (0) #if ENABLE_DEBUG -# define PTHREAD_REAPER_STACKSIZE THREAD_STACKSIZE_MAIN +# define PTHREAD_REAPER_STACKSIZE (THREAD_STACKSIZE_IDLE + THREAD_EXTRA_STACKSIZE_PRINTF) # define PTHREAD_STACKSIZE THREAD_STACKSIZE_MAIN #else -# define PTHREAD_REAPER_STACKSIZE THREAD_STACKSIZE_DEFAULT +# define PTHREAD_REAPER_STACKSIZE THREAD_STACKSIZE_IDLE # define PTHREAD_STACKSIZE THREAD_STACKSIZE_DEFAULT #endif From a889e500f0ae0623850b687611e2f1b1fee1c61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 25 Sep 2019 15:35:53 +0200 Subject: [PATCH 5/6] pthread: make pthread reaper stacksize configurable new define 'CONFIG_PTHREAD_REAPER_BASE_STACKSIZE' to change pthread reaper for boards that need more stack default value is 'THREAD_STACKSIZE_IDLE' --- sys/posix/pthread/pthread.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sys/posix/pthread/pthread.c b/sys/posix/pthread/pthread.c index 5fe763f199..0cefd6fc7f 100644 --- a/sys/posix/pthread/pthread.c +++ b/sys/posix/pthread/pthread.c @@ -37,11 +37,15 @@ #define ENABLE_DEBUG (0) +#ifndef CONFIG_PTHREAD_REAPER_BASE_STACKSIZE +#define CONFIG_PTHREAD_REAPER_BASE_STACKSIZE (THREAD_STACKSIZE_IDLE) +#endif + #if ENABLE_DEBUG -# define PTHREAD_REAPER_STACKSIZE (THREAD_STACKSIZE_IDLE + THREAD_EXTRA_STACKSIZE_PRINTF) +# define PTHREAD_REAPER_STACKSIZE ((CONFIG_PTHREAD_REAPER_BASE_STACKSIZE) + THREAD_EXTRA_STACKSIZE_PRINTF) # define PTHREAD_STACKSIZE THREAD_STACKSIZE_MAIN #else -# define PTHREAD_REAPER_STACKSIZE THREAD_STACKSIZE_IDLE +# define PTHREAD_REAPER_STACKSIZE (CONFIG_PTHREAD_REAPER_BASE_STACKSIZE) # define PTHREAD_STACKSIZE THREAD_STACKSIZE_DEFAULT #endif From 1e561e9f63e1afb726876e5fadb1255e14f8a44c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 25 Sep 2019 15:37:24 +0200 Subject: [PATCH 6/6] cpu/lpc2387: increase 'pthread_reaper' stacksize At the time of configuration, the pthread-reaper uses '164' bytes of stack when 'idle' stack is only '160'. By having double it gives some margin. ps pid | name | state Q | pri | stack ( used) | base addr | current 1 | idle | pending Q | 15 | 160 ( 128) | 0x4000007c | 0x4000009c 2 | main | running Q | 7 | 2560 ( 1232) | 0x4000011c | 0x4000095c 3 | pthread-reaper | bl rx _ | 0 | 320 ( 164) | 0x40000bac | 0x40000c48 | SUM | | | 3040 ( 1524) --- cpu/lpc2387/include/cpu_conf.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cpu/lpc2387/include/cpu_conf.h b/cpu/lpc2387/include/cpu_conf.h index 02b22435cf..9f37cb9f0f 100644 --- a/cpu/lpc2387/include/cpu_conf.h +++ b/cpu/lpc2387/include/cpu_conf.h @@ -52,6 +52,15 @@ extern "C" { #define THREAD_STACKSIZE_IDLE (160) /** @} */ +/** + * @name Pthread configuration + * @{ + */ +/* The idle stack of '160' is not enough to do the 'msg_receive'. + * It currently used '164' bytes. */ +#define CONFIG_PTHREAD_REAPER_BASE_STACKSIZE (2*THREAD_STACKSIZE_IDLE) +/** @} */ + /** * @name Compiler specifics * @{