]>
Commit | Line | Data |
---|---|---|
33574c17 | 1 | /* Thread termination. |
dff8da6b | 2 | Copyright (C) 2000-2024 Free Software Foundation, Inc. |
33574c17 ST |
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 | |
ad2b41bf ST |
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. | |
33574c17 ST |
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 | |
ad2b41bf | 13 | Lesser General Public License for more details. |
33574c17 | 14 | |
ad2b41bf ST |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
33574c17 ST |
18 | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <pthread.h> | |
22 | #include <stdlib.h> | |
23 | ||
24 | #include <pt-internal.h> | |
fe9748cc | 25 | #include <pthreadP.h> |
33574c17 ST |
26 | |
27 | #include <atomic.h> | |
28 | ||
29 | ||
30 | /* Terminate the current thread and make STATUS available to any | |
31 | thread that might join it. */ | |
32 | void | |
33 | __pthread_exit (void *status) | |
34 | { | |
35 | struct __pthread *self = _pthread_self (); | |
36 | struct __pthread_cancelation_handler **handlers; | |
37 | int oldstate; | |
38 | ||
39 | /* Run any cancelation handlers. According to POSIX, the | |
40 | cancellation cleanup handlers should be called with cancellation | |
41 | disabled. */ | |
f6fb29d2 | 42 | __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); |
33574c17 | 43 | |
f1cd3407 | 44 | for (handlers = __pthread_get_cleanup_stack (); |
33574c17 ST |
45 | *handlers != NULL; |
46 | *handlers = (*handlers)->__next) | |
47 | (*handlers)->__handler ((*handlers)->__arg); | |
48 | ||
89ade8d8 | 49 | /* Call destructors for the thread_local TLS variables. */ |
6333a601 | 50 | call_function_static_weak (__call_tls_dtors); |
89ade8d8 | 51 | |
f6fb29d2 | 52 | __pthread_setcancelstate (oldstate, &oldstate); |
33574c17 ST |
53 | |
54 | /* Decrease the number of threads. We use an atomic operation to | |
55 | make sure that only the last thread calls `exit'. */ | |
4a07fbb6 | 56 | if (atomic_fetch_add_relaxed (&__pthread_total, -1) == 1) |
33574c17 ST |
57 | /* We are the last thread. */ |
58 | exit (0); | |
59 | ||
7a06be05 ST |
60 | /* Destroy any thread specific data. */ |
61 | __pthread_destroy_specific (self); | |
62 | ||
33574c17 ST |
63 | /* Note that after this point the process can be terminated at any |
64 | point if another thread calls `pthread_exit' and happens to be | |
65 | the last thread. */ | |
66 | ||
67 | __pthread_mutex_lock (&self->state_lock); | |
68 | ||
69 | if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending) | |
70 | status = PTHREAD_CANCELED; | |
71 | ||
72 | switch (self->state) | |
73 | { | |
74 | default: | |
75 | assert (!"Consistency error: unexpected self->state"); | |
76 | abort (); | |
77 | break; | |
78 | ||
79 | case PTHREAD_DETACHED: | |
80 | __pthread_mutex_unlock (&self->state_lock); | |
81 | ||
82 | break; | |
83 | ||
84 | case PTHREAD_JOINABLE: | |
85 | /* We need to stay around for a while since another thread | |
86 | might want to join us. */ | |
87 | self->state = PTHREAD_EXITED; | |
88 | ||
89 | /* We need to remember the exit status. A thread joining us | |
90 | might ask for it. */ | |
91 | self->status = status; | |
92 | ||
93 | /* Broadcast the condition. This will wake up threads that are | |
94 | waiting to join us. */ | |
95 | __pthread_cond_broadcast (&self->state_cond); | |
96 | __pthread_mutex_unlock (&self->state_lock); | |
97 | ||
98 | break; | |
99 | } | |
100 | ||
33574c17 ST |
101 | /* Destroy any signal state. */ |
102 | __pthread_sigstate_destroy (self); | |
103 | ||
104 | /* Self terminating requires TLS, so defer the release of the TCB until | |
105 | the thread structure is reused. */ | |
106 | ||
107 | /* Release kernel resources, including the kernel thread and the stack, | |
108 | and drop the self reference. */ | |
109 | __pthread_thread_terminate (self); | |
110 | ||
111 | /* NOTREACHED */ | |
112 | abort (); | |
113 | } | |
114 | ||
fba7fc5a | 115 | weak_alias (__pthread_exit, pthread_exit); |