[pthreads] fix the free ptd issue when there is no pthread slot.

This commit is contained in:
Bernard Xiong 2019-05-13 09:19:44 +08:00
parent 7038465d13
commit ab595cc864

View File

@ -10,11 +10,11 @@
* 2019-02-07 Bernard Add _pthread_destroy to release pthread resource. * 2019-02-07 Bernard Add _pthread_destroy to release pthread resource.
*/ */
#include <rthw.h>
#include <pthread.h> #include <pthread.h>
#include <sched.h> #include <sched.h>
#include "pthread_internal.h" #include "pthread_internal.h"
#include <rthw.h>
RT_DEFINE_SPINLOCK(pth_lock); RT_DEFINE_SPINLOCK(pth_lock);
_pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL}; _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); 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; return index;
} }
@ -102,7 +109,7 @@ void _pthread_data_destroy(pthread_t pth)
} }
/* clean stack addr pointer */ /* clean stack addr pointer */
ptd->tid->stack_addr = RT_NULL; ptd->tid->stack_addr = RT_NULL;
/* /*
* if this thread create the local thread data, * if this thread create the local thread data,
* delete it * delete it
@ -188,7 +195,7 @@ int pthread_create(pthread_t *pid,
pthread_t pth_id; pthread_t pth_id;
_pthread_data_t *ptd; _pthread_data_t *ptd;
/* tid shall be provided */ /* pid shall be provided */
RT_ASSERT(pid != RT_NULL); RT_ASSERT(pid != RT_NULL);
/* allocate posix thread data */ /* allocate posix thread data */
@ -198,7 +205,6 @@ int pthread_create(pthread_t *pid,
ret = ENOMEM; ret = ENOMEM;
goto __exit; goto __exit;
} }
/* get pthread data */ /* get pthread data */
ptd = _pthread_get_data(pth_id); ptd = _pthread_get_data(pth_id);