blob: e04cf8e7f8246682ee406e61117a77a1447d1998 [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>
33#include <sys/mman.h>
34
35#include "pthread_internal.h"
36
Elliott Hughes6203e7b2014-05-30 14:49:00 -070037extern "C" __noreturn void _exit_with_stack_teardown(void*, size_t);
38extern "C" __noreturn void __exit(int);
Christopher Ferris101fb7d2013-12-06 18:54:48 -080039extern "C" int __set_tid_address(int*);
Elliott Hughesc3f11402013-10-30 14:40:09 -070040
41/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
42 * and thread cancelation
43 */
44
45void __pthread_cleanup_push(__pthread_cleanup_t* c, __pthread_cleanup_func_t routine, void* arg) {
46 pthread_internal_t* thread = __get_thread();
47 c->__cleanup_routine = routine;
48 c->__cleanup_arg = arg;
49 c->__cleanup_prev = thread->cleanup_stack;
50 thread->cleanup_stack = c;
51}
52
53void __pthread_cleanup_pop(__pthread_cleanup_t* c, int execute) {
54 pthread_internal_t* thread = __get_thread();
55 thread->cleanup_stack = c->__cleanup_prev;
56 if (execute) {
57 c->__cleanup_routine(c->__cleanup_arg);
58 }
59}
60
Elliott Hughes877ec6d2013-11-15 17:40:18 -080061void pthread_exit(void* return_value) {
Elliott Hughesc3f11402013-10-30 14:40:09 -070062 pthread_internal_t* thread = __get_thread();
Elliott Hughes877ec6d2013-11-15 17:40:18 -080063 thread->return_value = return_value;
Elliott Hughesc3f11402013-10-30 14:40:09 -070064
65 // Call the cleanup handlers first.
66 while (thread->cleanup_stack) {
67 __pthread_cleanup_t* c = thread->cleanup_stack;
68 thread->cleanup_stack = c->__cleanup_prev;
69 c->__cleanup_routine(c->__cleanup_arg);
70 }
71
72 // Call the TLS destructors. It is important to do that before removing this
73 // thread from the global list. This will ensure that if someone else deletes
74 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
75 // space (see pthread_key_delete).
76 pthread_key_clean_all();
77
78 if (thread->alternate_signal_stack != NULL) {
79 // Tell the kernel to stop using the alternate signal stack.
80 stack_t ss;
81 ss.ss_sp = NULL;
82 ss.ss_flags = SS_DISABLE;
83 sigaltstack(&ss, NULL);
84
85 // Free it.
86 munmap(thread->alternate_signal_stack, SIGSTKSZ);
87 thread->alternate_signal_stack = NULL;
88 }
89
90 // Keep track of what we need to know about the stack before we lose the pthread_internal_t.
91 void* stack_base = thread->attr.stack_base;
92 size_t stack_size = thread->attr.stack_size;
Yabin Cui8cf1b302014-12-03 21:36:24 -080093 bool free_stack = false;
Elliott Hughesc3f11402013-10-30 14:40:09 -070094
Elliott Hughes1728b232014-05-14 10:02:03 -070095 pthread_mutex_lock(&g_thread_list_lock);
Elliott Hughes877ec6d2013-11-15 17:40:18 -080096 if ((thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) != 0) {
Elliott Hughes960ee372013-12-11 12:41:54 -080097 // The thread is detached, so we can free the pthread_internal_t.
98 // First make sure that the kernel does not try to clear the tid field
99 // because we'll have freed the memory before the thread actually exits.
Christopher Ferris101fb7d2013-12-06 18:54:48 -0800100 __set_tid_address(NULL);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800101
102 // pthread_internal_t is freed below with stack, not here.
103 _pthread_internal_remove_locked(thread, false);
104 if (!thread->user_allocated_stack()) {
105 free_stack = true;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700106 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700107 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700108 pthread_mutex_unlock(&g_thread_list_lock);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700109
Yabin Cui8cf1b302014-12-03 21:36:24 -0800110 // Detached threads exit with stack teardown, and everything deallocated here.
111 // Threads that can be joined exit but leave their stacks for the pthread_join caller to clean up.
112 if (free_stack) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700113 // We need to munmap the stack we're running on before calling exit.
114 // That's not something we can do in C.
115
116 // We don't want to take a signal after we've unmapped the stack.
117 // That's one last thing we can handle in C.
118 sigset_t mask;
119 sigfillset(&mask);
120 sigprocmask(SIG_SETMASK, &mask, NULL);
121
Elliott Hughes2aace212013-12-21 15:30:49 -0800122 _exit_with_stack_teardown(stack_base, stack_size);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800123 } else {
124 __exit(0);
Elliott Hughesc3f11402013-10-30 14:40:09 -0700125 }
Elliott Hughesc3f11402013-10-30 14:40:09 -0700126}