1 | /* Thread termination. |
2 | Copyright (C) 2000-2022 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 | <https://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 | #include <pthreadP.h> |
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. */ |
42 | __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); |
43 | |
44 | for (handlers = __pthread_get_cleanup_stack (); |
45 | *handlers != NULL; |
46 | *handlers = (*handlers)->__next) |
47 | (*handlers)->__handler ((*handlers)->__arg); |
48 | |
49 | __pthread_setcancelstate (oldstate, &oldstate); |
50 | |
51 | /* Decrease the number of threads. We use an atomic operation to |
52 | make sure that only the last thread calls `exit'. */ |
53 | if (atomic_decrement_and_test (&__pthread_total)) |
54 | /* We are the last thread. */ |
55 | exit (status: 0); |
56 | |
57 | /* Note that after this point the process can be terminated at any |
58 | point if another thread calls `pthread_exit' and happens to be |
59 | the last thread. */ |
60 | |
61 | __pthread_mutex_lock (mutex: &self->state_lock); |
62 | |
63 | if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending) |
64 | status = PTHREAD_CANCELED; |
65 | |
66 | switch (self->state) |
67 | { |
68 | default: |
69 | assert (!"Consistency error: unexpected self->state" ); |
70 | abort (); |
71 | break; |
72 | |
73 | case PTHREAD_DETACHED: |
74 | __pthread_mutex_unlock (mutex: &self->state_lock); |
75 | |
76 | break; |
77 | |
78 | case PTHREAD_JOINABLE: |
79 | /* We need to stay around for a while since another thread |
80 | might want to join us. */ |
81 | self->state = PTHREAD_EXITED; |
82 | |
83 | /* We need to remember the exit status. A thread joining us |
84 | might ask for it. */ |
85 | self->status = status; |
86 | |
87 | /* Broadcast the condition. This will wake up threads that are |
88 | waiting to join us. */ |
89 | __pthread_cond_broadcast (cond: &self->state_cond); |
90 | __pthread_mutex_unlock (mutex: &self->state_lock); |
91 | |
92 | break; |
93 | } |
94 | |
95 | /* Destroy any thread specific data. */ |
96 | __pthread_destroy_specific (thread: self); |
97 | |
98 | /* Destroy any signal state. */ |
99 | __pthread_sigstate_destroy (thread: self); |
100 | |
101 | /* Self terminating requires TLS, so defer the release of the TCB until |
102 | the thread structure is reused. */ |
103 | |
104 | /* Release kernel resources, including the kernel thread and the stack, |
105 | and drop the self reference. */ |
106 | __pthread_thread_terminate (thread: self); |
107 | |
108 | /* NOTREACHED */ |
109 | abort (); |
110 | } |
111 | |
112 | weak_alias (__pthread_exit, pthread_exit); |
113 | |