[pthread] Fix pthread_detach issue for a none-joinable thread
/*
* File : pthread.c
* This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2012, RT-Thread Development Team
+ * COPYRIGHT (C) 2012 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* Change Logs:
* Date Author Notes
+ * 2018-01-26 Bernard Fix pthread_detach issue for a none-joinable
+ * thread.
*/
#include <pthread.h>
@@ -94,7 +96,9 @@ int pthread_create(pthread_t *tid,
ptd->magic = PTHREAD_MAGIC;
if (attr != RT_NULL)
+ {
ptd->attr = *attr;
+ }
else
{
/* use default attribute */
@@ -107,7 +111,9 @@ int pthread_create(pthread_t *tid,
stack = (void *)rt_malloc(ptd->attr.stack_size);
}
else
+ {
stack = (void *)(ptd->attr.stack_base);
+ }
if (stack == RT_NULL)
{
@@ -140,7 +146,9 @@ int pthread_create(pthread_t *tid,
}
}
else
+ {
ptd->joinable_sem = RT_NULL;
+ }
/* set parameter */
ptd->thread_entry = start;
@@ -191,6 +199,14 @@ int pthread_detach(pthread_t thread)
ptd = _pthread_get_data(thread);
+ if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
+ {
+ /* The implementation has detected that the value specified by thread does not refer
+ * to a joinable thread.
+ */
+ return EINVAL;
+ }
+
if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
{
/* delete joinable semaphore */
@@ -205,6 +221,11 @@ int pthread_detach(pthread_t thread)
/* release thread allocated stack */
rt_free(ptd->tid->stack_addr);
}
+ else
+ {
+ /* clean stack addr pointer */
+ ptd->tid->stack_addr = RT_NULL;
+ }
/*
* if this thread create the local thread data,
@@ -257,7 +278,9 @@ int pthread_join (pthread_t thread, void **value_ptr)
pthread_detach(thread);
}
else
+ {
return ESRCH;
+ }
return 0;
}
@@ -532,4 +555,3 @@ int pthread_cancel(pthread_t thread)
return 0;
}
RTM_EXPORT(pthread_cancel);
-
