]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/pt-exit.c
hurd: Avoid local PLTs in libpthread.
[thirdparty/glibc.git] / htl / pt-exit.c
1 /* Thread termination.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23
24 #include <pt-internal.h>
25
26 #include <atomic.h>
27
28
29 /* Terminate the current thread and make STATUS available to any
30 thread that might join it. */
31 void
32 __pthread_exit (void *status)
33 {
34 struct __pthread *self = _pthread_self ();
35 struct __pthread_cancelation_handler **handlers;
36 int oldstate;
37
38 /* Run any cancelation handlers. According to POSIX, the
39 cancellation cleanup handlers should be called with cancellation
40 disabled. */
41 __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
42
43 for (handlers = __pthread_get_cleanup_stack ();
44 *handlers != NULL;
45 *handlers = (*handlers)->__next)
46 (*handlers)->__handler ((*handlers)->__arg);
47
48 __pthread_setcancelstate (oldstate, &oldstate);
49
50 /* Decrease the number of threads. We use an atomic operation to
51 make sure that only the last thread calls `exit'. */
52 if (atomic_decrement_and_test (&__pthread_total))
53 /* We are the last thread. */
54 exit (0);
55
56 /* Note that after this point the process can be terminated at any
57 point if another thread calls `pthread_exit' and happens to be
58 the last thread. */
59
60 __pthread_mutex_lock (&self->state_lock);
61
62 if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending)
63 status = PTHREAD_CANCELED;
64
65 switch (self->state)
66 {
67 default:
68 assert (!"Consistency error: unexpected self->state");
69 abort ();
70 break;
71
72 case PTHREAD_DETACHED:
73 __pthread_mutex_unlock (&self->state_lock);
74
75 break;
76
77 case PTHREAD_JOINABLE:
78 /* We need to stay around for a while since another thread
79 might want to join us. */
80 self->state = PTHREAD_EXITED;
81
82 /* We need to remember the exit status. A thread joining us
83 might ask for it. */
84 self->status = status;
85
86 /* Broadcast the condition. This will wake up threads that are
87 waiting to join us. */
88 __pthread_cond_broadcast (&self->state_cond);
89 __pthread_mutex_unlock (&self->state_lock);
90
91 break;
92 }
93
94 /* Destroy any thread specific data. */
95 __pthread_destroy_specific (self);
96
97 /* Destroy any signal state. */
98 __pthread_sigstate_destroy (self);
99
100 /* Self terminating requires TLS, so defer the release of the TCB until
101 the thread structure is reused. */
102
103 /* Release kernel resources, including the kernel thread and the stack,
104 and drop the self reference. */
105 __pthread_thread_terminate (self);
106
107 /* NOTREACHED */
108 abort ();
109 }
110
111 strong_alias (__pthread_exit, pthread_exit);