From f0bf2143f6d3fa3271865b126e4b5b2b08df1ed7 Mon Sep 17 00:00:00 2001 From: Masayuki Ishikawa Date: Tue, 10 Nov 2020 11:03:12 +0900 Subject: [PATCH] sched: pthread: Fix pthread_join() for SMP Summary: - I noticed 'pthread_rwlock test' in ostest sometimes stops - This issue happened with spresense:wifi_smp (NCPUS=4) and sim:smp - Finally, I found an issue in pthread_join() - In pthread_join(), sched_lock() is used to avoid pre-emption - However, this is not enough for SMP - Because another CPU would continue the pthread and exit sequences - So we need to protect it with a critical section Impact: - Affect SMP only Testing: - Tested with ostest with the following configurations - spresnese:smp - spresense:wifi_smp (NCPUS=2, NCPUS=4) - sabre-6quad:smp (QEMU) - esp32-core:smp (QEMU) - maix-bit:smp (QEMU) - sim:smp - lc823450-xgevk:rndis Signed-off-by: Masayuki Ishikawa --- sched/pthread/pthread_join.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sched/pthread/pthread_join.c b/sched/pthread/pthread_join.c index 994bc7d5a1..328708bc31 100644 --- a/sched/pthread/pthread_join.c +++ b/sched/pthread/pthread_join.c @@ -137,6 +137,15 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) } else { + /* NOTE: sched_lock() is not enough for SMP + * because another CPU would continue the pthread and exit + * sequences so need to protect it with a critical section + */ + +#ifdef CONFIG_SMP + irqstate_t flags = enter_critical_section(); +#endif + /* We found the join info structure. Increment for the reference * to the join structure that we have. This will keep things * stable for we have to do @@ -211,6 +220,10 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) sched_unlock(); +#ifdef CONFIG_SMP + leave_critical_section(flags); +#endif + /* Release our reference to the join structure and, if the reference * count decrements to zero, deallocate the join structure. */