Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 61fb3fc | 2013-11-07 12:28:46 -0800 | [diff] [blame] | 31 | #include <signal.h> |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <sys/mman.h> |
| 34 | |
| 35 | #include "pthread_internal.h" |
| 36 | |
Elliott Hughes | 6203e7b | 2014-05-30 14:49:00 -0700 | [diff] [blame] | 37 | extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t); |
| 38 | extern "C" __noreturn void __exit(int); |
Christopher Ferris | 101fb7d | 2013-12-06 18:54:48 -0800 | [diff] [blame] | 39 | extern "C" int __set_tid_address(int*); |
Yabin Cui | cc5f654 | 2014-11-25 14:18:12 -0800 | [diff] [blame^] | 40 | extern "C" void __set_tls(void*); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 41 | |
| 42 | /* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions |
| 43 | * and thread cancelation |
| 44 | */ |
| 45 | |
| 46 | void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) { |
| 47 | pthread_internal_t* thread = __get_thread(); |
| 48 | c->__cleanup_routine = routine; |
| 49 | c->__cleanup_arg = arg; |
| 50 | c->__cleanup_prev = thread->cleanup_stack; |
| 51 | thread->cleanup_stack = c; |
| 52 | } |
| 53 | |
| 54 | void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) { |
| 55 | pthread_internal_t* thread = __get_thread(); |
| 56 | thread->cleanup_stack = c->__cleanup_prev; |
| 57 | if (execute) { |
| 58 | c->__cleanup_routine(c->__cleanup_arg); |
| 59 | } |
| 60 | } |
| 61 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 62 | void pthread_exit(void* return_value) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 63 | pthread_internal_t* thread = __get_thread(); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 64 | thread->return_value = return_value; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 65 | |
| 66 | // Call the cleanup handlers first. |
| 67 | while (thread->cleanup_stack) { |
| 68 | __pthread_cleanup_t* c = thread->cleanup_stack; |
| 69 | thread->cleanup_stack = c->__cleanup_prev; |
| 70 | c->__cleanup_routine(c->__cleanup_arg); |
| 71 | } |
| 72 | |
| 73 | // Call the TLS destructors. It is important to do that before removing this |
| 74 | // thread from the global list. This will ensure that if someone else deletes |
| 75 | // a TLS key, the corresponding value will be set to NULL in this thread's TLS |
| 76 | // space (see pthread_key_delete). |
| 77 | pthread_key_clean_all(); |
| 78 | |
Yabin Cui | cc5f654 | 2014-11-25 14:18:12 -0800 | [diff] [blame^] | 79 | // Clear tls to prevent further use of __get_tls(), see b/16847284. |
| 80 | __set_tls(NULL); |
| 81 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 82 | if (thread->alternate_signal_stack != NULL) { |
| 83 | // Tell the kernel to stop using the alternate signal stack. |
| 84 | stack_t ss; |
| 85 | ss.ss_sp = NULL; |
| 86 | ss.ss_flags = SS_DISABLE; |
| 87 | sigaltstack(&ss, NULL); |
| 88 | |
| 89 | // Free it. |
| 90 | munmap(thread->alternate_signal_stack, SIGSTKSZ); |
| 91 | thread->alternate_signal_stack = NULL; |
| 92 | } |
| 93 | |
| 94 | // Keep track of what we need to know about the stack before we lose the pthread_internal_t. |
| 95 | void* stack_base = thread->attr.stack_base; |
| 96 | size_t stack_size = thread->attr.stack_size; |
Elliott Hughes | 40a5217 | 2014-07-30 14:48:10 -0700 | [diff] [blame] | 97 | bool user_allocated_stack = thread->user_allocated_stack(); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 99 | pthread_mutex_lock(&g_thread_list_lock); |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 100 | if ((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) != 0) { |
Elliott Hughes | 960ee37 | 2013-12-11 12:41:54 -0800 | [diff] [blame] | 101 | // The thread is detached, so we can free the pthread_internal_t. |
| 102 | // First make sure that the kernel does not try to clear the tid field |
| 103 | // because we'll have freed the memory before the thread actually exits. |
Christopher Ferris | 101fb7d | 2013-12-06 18:54:48 -0800 | [diff] [blame] | 104 | __set_tid_address(NULL); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 105 | _pthread_internal_remove_locked(thread); |
| 106 | } else { |
| 107 | // Make sure that the pthread_internal_t doesn't have stale pointers to a stack that |
| 108 | // will be unmapped after the exit call below. |
| 109 | if (!user_allocated_stack) { |
| 110 | thread->attr.stack_base = NULL; |
| 111 | thread->attr.stack_size = 0; |
| 112 | thread->tls = NULL; |
| 113 | } |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 114 | // pthread_join is responsible for destroying the pthread_internal_t for non-detached threads. |
| 115 | // The kernel will futex_wake on the pthread_internal_t::tid field to wake pthread_join. |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 116 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 117 | pthread_mutex_unlock(&g_thread_list_lock); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 118 | |
| 119 | if (user_allocated_stack) { |
| 120 | // Cleaning up this thread's stack is the creator's responsibility, not ours. |
| 121 | __exit(0); |
| 122 | } else { |
| 123 | // We need to munmap the stack we're running on before calling exit. |
| 124 | // That's not something we can do in C. |
| 125 | |
| 126 | // We don't want to take a signal after we've unmapped the stack. |
| 127 | // That's one last thing we can handle in C. |
| 128 | sigset_t mask; |
| 129 | sigfillset(&mask); |
| 130 | sigprocmask(SIG_SETMASK, &mask, NULL); |
| 131 | |
Elliott Hughes | 2aace21 | 2013-12-21 15:30:49 -0800 | [diff] [blame] | 132 | _exit_with_stack_teardown(stack_base, stack_size); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 133 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 134 | } |