2
Copyright (C) 2000, 2005 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. */
24
#include <pt-internal.h>
26
/* Indicate that the storage for THREAD can be reclaimed when it
29
pthread_detach (pthread_t thread)
31
struct __pthread *pthread;
34
/* Lookup the thread structure for THREAD. */
35
pthread = __pthread_getid (thread);
39
__pthread_mutex_lock (&pthread->state_lock);
41
switch (pthread->state)
43
case PTHREAD_JOINABLE:
44
/* THREAD still running. Mark it as detached such that its
45
resources can be reclaimed as soon as the thread exits. */
46
pthread->state = PTHREAD_DETACHED;
48
/* Broadcast the condition. This will make threads that are
49
waiting to join THREAD continue with hopefully disastrous
50
consequences instead of blocking indefinitely. */
51
pthread_cond_broadcast (&pthread->state_cond);
52
__pthread_mutex_unlock (&pthread->state_lock);
56
/* THREAD has already exited. Make sure that nobody can
57
reference it anymore, and mark it as terminated. */
59
__pthread_mutex_unlock (&pthread->state_lock);
61
/* Make sure the thread is not running before we remove its
62
stack. (The only possibility is that it is in a call to
63
__pthread_thread_halt itself, but that is enough to cause a
65
__pthread_thread_halt (pthread);
67
/* Destroy the stack, the kernel resources and the control
69
assert (pthread->stack);
70
__pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
73
__pthread_thread_dealloc (pthread);
75
__pthread_dealloc (pthread);
78
case PTHREAD_TERMINATED:
79
/* Pretend THREAD wasn't there in the first place. */
80
__pthread_mutex_unlock (&pthread->state_lock);
85
/* Thou shalt not detach non-joinable threads! */
86
__pthread_mutex_unlock (&pthread->state_lock);