Mark __libc_resp with attribute_tls_model_ie for consistency with __resp
[glibc/nacl-glibc.git] / nptl / pthread_join.c
blob6a87a8b329c3e34a57d65e86d45fa97a5fdb2fe2
1 /* Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdlib.h>
23 #include <atomic.h>
24 #include "pthreadP.h"
27 static void
28 cleanup (void *arg)
30 /* If we already changed the waiter ID, reset it. The call cannot
31 fail for any reason but the thread not having done that yet so
32 there is no reason for a loop. */
33 (void) atomic_compare_and_exchange_bool_acq ((struct pthread **) arg, NULL,
34 THREAD_SELF);
38 int
39 pthread_join (threadid, thread_return)
40 pthread_t threadid;
41 void **thread_return;
43 struct pthread *pd = (struct pthread *) threadid;
45 /* Make sure the descriptor is valid. */
46 if (INVALID_NOT_TERMINATED_TD_P (pd))
47 /* Not a valid thread handle. */
48 return ESRCH;
50 /* Is the thread joinable?. */
51 if (IS_DETACHED (pd))
52 /* We cannot wait for the thread. */
53 return EINVAL;
55 struct pthread *self = THREAD_SELF;
56 int result = 0;
58 /* During the wait we change to asynchronous cancellation. If we
59 are canceled the thread we are waiting for must be marked as
60 un-wait-ed for again. */
61 pthread_cleanup_push (cleanup, &pd->joinid);
63 /* Switch to asynchronous cancellation. */
64 int oldtype = CANCEL_ASYNC ();
66 if ((pd == self
67 || (self->joinid == pd
68 && (pd->cancelhandling
69 & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
70 | TERMINATED_BITMASK)) == 0))
71 && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
72 /* This is a deadlock situation. The threads are waiting for each
73 other to finish. Note that this is a "may" error. To be 100%
74 sure we catch this error we would have to lock the data
75 structures but it is not necessary. In the unlikely case that
76 two threads are really caught in this situation they will
77 deadlock. It is the programmer's problem to figure this
78 out. */
79 result = EDEADLK;
80 /* Wait for the thread to finish. If it is already locked something
81 is wrong. There can only be one waiter. */
82 else if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
83 self,
84 NULL), 0))
85 /* There is already somebody waiting for the thread. */
86 result = EINVAL;
87 else
88 /* Wait for the child. */
89 lll_wait_tid (pd->tid);
92 /* Restore cancellation mode. */
93 CANCEL_RESET (oldtype);
95 /* Remove the handler. */
96 pthread_cleanup_pop (0);
99 if (__builtin_expect (result == 0, 1))
101 /* We mark the thread as terminated and as joined. */
102 pd->tid = -1;
104 /* Store the return value if the caller is interested. */
105 if (thread_return != NULL)
106 *thread_return = pd->result;
109 /* Free the TCB. */
110 __free_tcb (pd);
113 return result;