From ab595cc8640ba998f7f3172d573742c56f577e33 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Mon, 13 May 2019 09:19:44 +0800 Subject: [PATCH] [pthreads] fix the free ptd issue when there is no pthread slot. --- components/libc/pthreads/pthread.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/components/libc/pthreads/pthread.c b/components/libc/pthreads/pthread.c index 64ef81d680..7ecf304038 100644 --- a/components/libc/pthreads/pthread.c +++ b/components/libc/pthreads/pthread.c @@ -10,11 +10,11 @@ * 2019-02-07 Bernard Add _pthread_destroy to release pthread resource. */ +#include #include #include #include "pthread_internal.h" -#include RT_DEFINE_SPINLOCK(pth_lock); _pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL}; @@ -75,6 +75,13 @@ pthread_t _pthread_data_create(void) } rt_hw_spin_unlock(&pth_lock); + /* full of pthreads, clean magic and release ptd */ + if (index == PTHREAD_NUM_MAX) + { + ptd->magic = 0x0; + rt_free(ptd); + } + return index; } @@ -102,7 +109,7 @@ void _pthread_data_destroy(pthread_t pth) } /* clean stack addr pointer */ ptd->tid->stack_addr = RT_NULL; - + /* * if this thread create the local thread data, * delete it @@ -188,7 +195,7 @@ int pthread_create(pthread_t *pid, pthread_t pth_id; _pthread_data_t *ptd; - /* tid shall be provided */ + /* pid shall be provided */ RT_ASSERT(pid != RT_NULL); /* allocate posix thread data */ @@ -198,7 +205,6 @@ int pthread_create(pthread_t *pid, ret = ENOMEM; goto __exit; } - /* get pthread data */ ptd = _pthread_get_data(pth_id); -- GitLab