blob: 8b4c44e15f9c69f2dd3f666ddd127618bfe2b9d2 [file] [log] [blame]
Elliott Hughesc3f11402013-10-30 14:40:09 -07001/*
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 Hughes61fb3fc2013-11-07 12:28:46 -080031#include <signal.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070032#include <stdlib.h>
Elliott Hughesf86c4492016-02-25 22:05:28 -080033#include <string.h>
Elliott Hughesc3f11402013-10-30 14:40:09 -070034#include <sys/mman.h>
35
dimitryfa432522017-10-25 13:07:45 +020036#include "private/bionic_defs.h"
Elliott Hughesc3f11402013-10-30 14:40:09 -070037#include "pthread_internal.h"
38
Elliott Hughes6203e7b2014-05-30 14:49:00 -070039extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t);
40extern "C" __noreturn void __exit(int);
Christopher Ferris101fb7d2013-12-06 18:54:48 -080041extern "C" int __set_tid_address(int*);
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070042extern "C" void __cxa_thread_finalize();
Elliott Hughesc3f11402013-10-30 14:40:09 -070043
44/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
45 * and thread cancelation
46 */
47
dimitryfa432522017-10-25 13:07:45 +020048__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -070049void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
50 pthread_internal_t* thread = __get_thread();
51 c->__cleanup_routine = routine;
52 c->__cleanup_arg = arg;
53 c->__cleanup_prev = thread->cleanup_stack;
54 thread->cleanup_stack = c;
55}
56
dimitryfa432522017-10-25 13:07:45 +020057__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesc3f11402013-10-30 14:40:09 -070058void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
59 pthread_internal_t* thread = __get_thread();
60 thread->cleanup_stack = c->__cleanup_prev;
61 if (execute) {
62 c->__cleanup_routine(c->__cleanup_arg);
63 }
64}
65
dimitryfa432522017-10-25 13:07:45 +020066__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes877ec6d2013-11-15 17:40:18 -080067void pthread_exit(void* return_value) {
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070068 // Call dtors for thread_local objects first.
69 __cxa_thread_finalize();
70
Elliott Hughesc3f11402013-10-30 14:40:09 -070071 pthread_internal_t* thread = __get_thread();
Elliott Hughes877ec6d2013-11-15 17:40:18 -080072 thread->return_value = return_value;
Elliott Hughesc3f11402013-10-30 14:40:09 -070073
Dmitriy Ivanovdf79c332015-03-25 17:38:10 -070074 // Call the cleanup handlers.
Elliott Hughesc3f11402013-10-30 14:40:09 -070075 while (thread->cleanup_stack) {
76 __pthread_cleanup_t* c = thread->cleanup_stack;
77 thread->cleanup_stack = c->__cleanup_prev;
78 c->__cleanup_routine(c->__cleanup_arg);
79 }
80
81 // Call the TLS destructors. It is important to do that before removing this
82 // thread from the global list. This will ensure that if someone else deletes
83 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
84 // space (see pthread_key_delete).
85 pthread_key_clean_all();
86
87 if (thread->alternate_signal_stack != NULL) {
88 // Tell the kernel to stop using the alternate signal stack.
Elliott Hughes2c6c9532016-02-25 21:51:50 -080089 stack_t ss;
90 memset(&ss, 0, sizeof(ss));
Elliott Hughesc3f11402013-10-30 14:40:09 -070091 ss.ss_flags = SS_DISABLE;
92 sigaltstack(&ss, NULL);
93
94 // Free it.
Yabin Cuief115002015-03-30 20:03:57 -070095 munmap(thread->alternate_signal_stack, SIGNAL_STACK_SIZE);
Elliott Hughesc3f11402013-10-30 14:40:09 -070096 thread->alternate_signal_stack = NULL;
97 }
98
Josh Gao5450f862017-03-07 23:05:08 -080099 // Unmap the bionic TLS, including guard pages.
Elliott Hughesd6c678c2017-06-27 17:01:57 -0700100 void* allocation = reinterpret_cast<char*>(thread->bionic_tls) - PTHREAD_GUARD_SIZE;
101 munmap(allocation, BIONIC_TLS_SIZE + 2 * PTHREAD_GUARD_SIZE);
Josh Gao5450f862017-03-07 23:05:08 -0800102
Yabin Cui58cf31b2015-03-06 17:23:53 -0800103 ThreadJoinState old_state = THREAD_NOT_JOINED;
104 while (old_state == THREAD_NOT_JOINED &&
105 !atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
106 }
107
108 if (old_state == THREAD_DETACHED) {
Yabin Cuiba8dfc22015-01-06 09:31:00 -0800109 // The thread is detached, no one will use pthread_internal_t after pthread_exit.
110 // So we can free mapped space, which includes pthread_internal_t and thread stack.
Elliott Hughes960ee372013-12-11 12:41:54 -0800111 // First make sure that the kernel does not try to clear the tid field
112 // because we'll have freed the memory before the thread actually exits.
Christopher Ferris101fb7d2013-12-06 18:54:48 -0800113 __set_tid_address(NULL);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800114
Elliott Hughes7484c212017-02-02 02:41:38 +0000115 // pthread_internal_t is freed below with stack, not here.
116 __pthread_internal_remove(thread);
117
Yabin Cui58cf31b2015-03-06 17:23:53 -0800118 if (thread->mmap_size != 0) {
119 // We need to free mapped space for detached threads when they exit.
120 // That's not something we can do in C.
121
122 // We don't want to take a signal after we've unmapped the stack.
123 // That's one last thing we can handle in C.
124 sigset_t mask;
125 sigfillset(&mask);
126 sigprocmask(SIG_SETMASK, &mask, NULL);
127
128 _exit_with_stack_teardown(thread->attr.stack_base, thread->mmap_size);
129 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700130 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700131
Yabin Cui58cf31b2015-03-06 17:23:53 -0800132 // No need to free mapped space. Either there was no space mapped, or it is left for
133 // the pthread_join caller to clean up.
134 __exit(0);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700135}