Skip to content

Commit 28eca64

Browse files
xiaoxiang781216pkarashchenko
authored andcommitted
libc: Cleanup per-thread resource in exit/quick_exit
copy from pthread_exit to avoid the resource leak Signed-off-by: Xiang Xiao <[email protected]>
1 parent 006afeb commit 28eca64

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

libs/libc/stdlib/lib_exit.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include <stdlib.h>
3232
#include <unistd.h>
3333

34+
#include <nuttx/tls.h>
35+
#include <nuttx/pthread.h>
36+
3437
#if defined(CONFIG_BUILD_FLAT) || !defined(__KERNEL__)
3538

3639
/****************************************************************************
@@ -86,6 +89,21 @@ FAR void *__dso_handle = &__dso_handle;
8689

8790
void exit(int status)
8891
{
92+
/* Mark the pthread as non-cancelable to avoid additional calls to
93+
* pthread_exit() due to any cancellation point logic that might get
94+
* kicked off by actions taken during pthread_exit processing.
95+
*/
96+
97+
task_setcancelstate(TASK_CANCEL_DISABLE, NULL);
98+
99+
#ifdef CONFIG_PTHREAD_CLEANUP
100+
pthread_cleanup_popall(tls_get_info());
101+
#endif
102+
103+
#if CONFIG_TLS_NELEM > 0
104+
tls_destruct();
105+
#endif
106+
89107
/* Run the registered exit functions */
90108

91109
atexit_call_exitfuncs(status, false);
@@ -120,6 +138,21 @@ void exit(int status)
120138

121139
void quick_exit(int status)
122140
{
141+
/* Mark the pthread as non-cancelable to avoid additional calls to
142+
* pthread_exit() due to any cancellation point logic that might get
143+
* kicked off by actions taken during pthread_exit processing.
144+
*/
145+
146+
task_setcancelstate(TASK_CANCEL_DISABLE, NULL);
147+
148+
#ifdef CONFIG_PTHREAD_CLEANUP
149+
pthread_cleanup_popall(tls_get_info());
150+
#endif
151+
152+
#if CONFIG_TLS_NELEM > 0
153+
tls_destruct();
154+
#endif
155+
123156
/* Run the registered exit functions */
124157

125158
atexit_call_exitfuncs(status, true);

0 commit comments

Comments
 (0)