2
Copyright (C) 2000, 2002, 2005, 2007, 2011 Free Software Foundation, Inc.
3
This file is part of the GNU C Library.
5
The GNU C Library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public License as
7
published by the Free Software Foundation; either version 2 of the
8
License, or (at your option) any later version.
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
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with the GNU C Library; see the file COPYING.LIB. If not,
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA. */
25
#include <pt-internal.h>
27
#include <bits/pt-atomic.h>
30
/* Terminate the current thread and make STATUS available to any
31
thread that might join it. */
33
__pthread_exit (void *status)
35
struct __pthread *self = _pthread_self ();
36
struct __pthread_cancelation_handler **handlers;
39
/* Run any cancelation handlers. According to POSIX, the
40
cancellation cleanup handlers should be called with cancellation
42
pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
44
for (handlers = __pthread_get_cleanup_stack ();
46
*handlers = (*handlers)->next)
47
(*handlers)->handler ((*handlers)->arg);
49
pthread_setcancelstate (oldstate, &oldstate);
51
/* Destory any thread specific data. */
52
__pthread_destroy_specific (self);
54
/* Destroy any signal state. */
55
__pthread_sigstate_destroy (self);
57
/* Decrease the number of threads. We use an atomic operation to
58
make sure that only the last thread calls `exit'. */
59
if (__atomic_dec_and_test (&__pthread_total))
60
/* We are the last thread. */
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
67
__pthread_mutex_lock (&self->state_lock);
69
if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending)
70
status = PTHREAD_CANCELED;
74
_dl_deallocate_tls (self->tcb, 1);
75
#endif /* ENABLE_TLS */
80
assert (! "Consistency error: unexpected self->state");
84
case PTHREAD_DETACHED:
85
/* Make sure that nobody can reference this thread anymore, and
86
mark it as terminated. Our thread ID will immediately become
87
available for re-use. For obvious reasons, we cannot
88
deallocate our own stack. However, it will eventually be
89
reused when this thread structure is recycled. */
90
__pthread_mutex_unlock (&self->state_lock);
92
__pthread_dealloc (self);
96
case PTHREAD_JOINABLE:
97
/* We need to stay around for a while since another thread
98
might want to join us. */
99
self->state = PTHREAD_EXITED;
101
/* We need to remember the exit status. A thread joining us
103
self->status = status;
105
/* Broadcast the condition. This will wake up threads that are
106
waiting to join us. */
107
pthread_cond_broadcast (&self->state_cond);
108
__pthread_mutex_unlock (&self->state_lock);
113
/* Note that after this point the resources used by this thread can
114
be freed at any moment if another thread joins or detaches us.
115
This means that before freeing any resources, such a thread
116
should make sure that this thread is really halted. */
118
__pthread_thread_halt (self);
124
strong_alias (__pthread_exit, pthread_exit);