]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - libc/bionic/pthread_exit.cpp
Merge "bionic: call stdio cleanup on exit"
[android-sdk/platform-bionic.git] / libc / bionic / pthread_exit.cpp
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
29 #include <pthread.h>
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <sys/mman.h>
35 #include "pthread_internal.h"
37 extern "C" void _exit_with_stack_teardown(void*, size_t, int);
38 extern "C" void __exit(int);
40 /* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
41  *         and thread cancelation
42  */
44 void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
45   pthread_internal_t* thread = __get_thread();
46   c->__cleanup_routine = routine;
47   c->__cleanup_arg = arg;
48   c->__cleanup_prev = thread->cleanup_stack;
49   thread->cleanup_stack = c;
50 }
52 void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
53   pthread_internal_t* thread = __get_thread();
54   thread->cleanup_stack = c->__cleanup_prev;
55   if (execute) {
56     c->__cleanup_routine(c->__cleanup_arg);
57   }
58 }
60 void pthread_exit(void* return_value) {
61   pthread_internal_t* thread = __get_thread();
62   thread->return_value = return_value;
64   // Call the cleanup handlers first.
65   while (thread->cleanup_stack) {
66     __pthread_cleanup_t* c = thread->cleanup_stack;
67     thread->cleanup_stack = c->__cleanup_prev;
68     c->__cleanup_routine(c->__cleanup_arg);
69   }
71   // Call the TLS destructors. It is important to do that before removing this
72   // thread from the global list. This will ensure that if someone else deletes
73   // a TLS key, the corresponding value will be set to NULL in this thread's TLS
74   // space (see pthread_key_delete).
75   pthread_key_clean_all();
77   if (thread->alternate_signal_stack != NULL) {
78     // Tell the kernel to stop using the alternate signal stack.
79     stack_t ss;
80     ss.ss_sp = NULL;
81     ss.ss_flags = SS_DISABLE;
82     sigaltstack(&ss, NULL);
84     // Free it.
85     munmap(thread->alternate_signal_stack, SIGSTKSZ);
86     thread->alternate_signal_stack = NULL;
87   }
89   // Keep track of what we need to know about the stack before we lose the pthread_internal_t.
90   void* stack_base = thread->attr.stack_base;
91   size_t stack_size = thread->attr.stack_size;
92   bool user_allocated_stack = ((thread->attr.flags & PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK) != 0);
94   pthread_mutex_lock(&gThreadListLock);
95   if ((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) != 0) {
96     // The thread is detached, so we can destroy the pthread_internal_t.
97     _pthread_internal_remove_locked(thread);
98   } else {
99     // Make sure that the pthread_internal_t doesn't have stale pointers to a stack that
100     // will be unmapped after the exit call below.
101     if (!user_allocated_stack) {
102       thread->attr.stack_base = NULL;
103       thread->attr.stack_size = 0;
104       thread->tls = NULL;
105     }
106     // pthread_join is responsible for destroying the pthread_internal_t for non-detached threads.
107     // The kernel will futex_wake on the pthread_internal_t::tid field to wake pthread_join.
108   }
109   pthread_mutex_unlock(&gThreadListLock);
111   if (user_allocated_stack) {
112     // Cleaning up this thread's stack is the creator's responsibility, not ours.
113     __exit(0);
114   } else {
115     // We need to munmap the stack we're running on before calling exit.
116     // That's not something we can do in C.
118     // We don't want to take a signal after we've unmapped the stack.
119     // That's one last thing we can handle in C.
120     sigset_t mask;
121     sigfillset(&mask);
122     sigprocmask(SIG_SETMASK, &mask, NULL);
124     _exit_with_stack_teardown(stack_base, stack_size, 0);
125   }
127   // NOTREACHED, but we told the compiler this function is noreturn, and it doesn't believe us.
128   abort();