• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
28 
29 #include <pthread.h>
30 
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/mman.h>
35 
36 #include "pthread_internal.h"
37 
38 extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t);
39 extern "C" __noreturn void __exit(int);
40 extern "C" int __set_tid_address(int*);
41 extern "C" void __cxa_thread_finalize();
42 
43 /* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
44  *         and thread cancelation
45  */
46 
__pthread_cleanup_push(__pthread_cleanup_t * c,__pthread_cleanup_func_t routine,void * arg)47 void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
48   pthread_internal_t* thread = __get_thread();
49   c->__cleanup_routine = routine;
50   c->__cleanup_arg = arg;
51   c->__cleanup_prev = thread->cleanup_stack;
52   thread->cleanup_stack = c;
53 }
54 
__pthread_cleanup_pop(__pthread_cleanup_t * c,int execute)55 void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
56   pthread_internal_t* thread = __get_thread();
57   thread->cleanup_stack = c->__cleanup_prev;
58   if (execute) {
59     c->__cleanup_routine(c->__cleanup_arg);
60   }
61 }
62 
pthread_exit(void * return_value)63 void pthread_exit(void* return_value) {
64   // Call dtors for thread_local objects first.
65   __cxa_thread_finalize();
66 
67   pthread_internal_t* thread = __get_thread();
68   thread->return_value = return_value;
69 
70   // Call the cleanup handlers.
71   while (thread->cleanup_stack) {
72     __pthread_cleanup_t* c = thread->cleanup_stack;
73     thread->cleanup_stack = c->__cleanup_prev;
74     c->__cleanup_routine(c->__cleanup_arg);
75   }
76 
77   // Call the TLS destructors. It is important to do that before removing this
78   // thread from the global list. This will ensure that if someone else deletes
79   // a TLS key, the corresponding value will be set to NULL in this thread's TLS
80   // space (see pthread_key_delete).
81   pthread_key_clean_all();
82 
83   if (thread->alternate_signal_stack != NULL) {
84     // Tell the kernel to stop using the alternate signal stack.
85     stack_t ss;
86     memset(&ss, 0, sizeof(ss));
87     ss.ss_flags = SS_DISABLE;
88     sigaltstack(&ss, NULL);
89 
90     // Free it.
91     munmap(thread->alternate_signal_stack, SIGNAL_STACK_SIZE);
92     thread->alternate_signal_stack = NULL;
93   }
94 
95   // Unmap the bionic TLS, including guard pages.
96   void* allocation = reinterpret_cast<char*>(thread->bionic_tls) - PAGE_SIZE;
97   munmap(allocation, BIONIC_TLS_SIZE + 2 * PAGE_SIZE);
98 
99   ThreadJoinState old_state = THREAD_NOT_JOINED;
100   while (old_state == THREAD_NOT_JOINED &&
101          !atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
102   }
103 
104   if (old_state == THREAD_DETACHED) {
105     // The thread is detached, no one will use pthread_internal_t after pthread_exit.
106     // So we can free mapped space, which includes pthread_internal_t and thread stack.
107     // First make sure that the kernel does not try to clear the tid field
108     // because we'll have freed the memory before the thread actually exits.
109     __set_tid_address(NULL);
110 
111     // pthread_internal_t is freed below with stack, not here.
112     __pthread_internal_remove(thread);
113 
114     if (thread->mmap_size != 0) {
115       // We need to free mapped space for detached threads when they exit.
116       // That's not something we can do in C.
117 
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);
123 
124       _exit_with_stack_teardown(thread->attr.stack_base, thread->mmap_size);
125     }
126   }
127 
128   // No need to free mapped space. Either there was no space mapped, or it is left for
129   // the pthread_join caller to clean up.
130   __exit(0);
131 }
132