PowerPC: remove branch prediction from rint implementation
[glibc.git] / nptl / pthread_join.c
blobbf1a01dbc93235de596bed0a747a0c076a9fa73e
1 /* Copyright (C) 2002-2012 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <stdlib.h>
22 #include <atomic.h>
23 #include "pthreadP.h"
25 #include <stap-probe.h>
28 static void
29 cleanup (void *arg)
31 /* If we already changed the waiter ID, reset it. The call cannot
32 fail for any reason but the thread not having done that yet so
33 there is no reason for a loop. */
34 (void) atomic_compare_and_exchange_bool_acq ((struct pthread **) arg, NULL,
35 THREAD_SELF);
39 int
40 pthread_join (threadid, thread_return)
41 pthread_t threadid;
42 void **thread_return;
44 struct pthread *pd = (struct pthread *) threadid;
46 /* Make sure the descriptor is valid. */
47 if (INVALID_NOT_TERMINATED_TD_P (pd))
48 /* Not a valid thread handle. */
49 return ESRCH;
51 /* Is the thread joinable?. */
52 if (IS_DETACHED (pd))
53 /* We cannot wait for the thread. */
54 return EINVAL;
56 struct pthread *self = THREAD_SELF;
57 int result = 0;
59 LIBC_PROBE (pthread_join, 1, threadid);
61 /* During the wait we change to asynchronous cancellation. If we
62 are canceled the thread we are waiting for must be marked as
63 un-wait-ed for again. */
64 pthread_cleanup_push (cleanup, &pd->joinid);
66 /* Switch to asynchronous cancellation. */
67 int oldtype = CANCEL_ASYNC ();
69 if ((pd == self
70 || (self->joinid == pd
71 && (pd->cancelhandling
72 & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
73 | TERMINATED_BITMASK)) == 0))
74 && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
75 /* This is a deadlock situation. The threads are waiting for each
76 other to finish. Note that this is a "may" error. To be 100%
77 sure we catch this error we would have to lock the data
78 structures but it is not necessary. In the unlikely case that
79 two threads are really caught in this situation they will
80 deadlock. It is the programmer's problem to figure this
81 out. */
82 result = EDEADLK;
83 /* Wait for the thread to finish. If it is already locked something
84 is wrong. There can only be one waiter. */
85 else if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
86 self,
87 NULL), 0))
88 /* There is already somebody waiting for the thread. */
89 result = EINVAL;
90 else
91 /* Wait for the child. */
92 lll_wait_tid (pd->tid);
95 /* Restore cancellation mode. */
96 CANCEL_RESET (oldtype);
98 /* Remove the handler. */
99 pthread_cleanup_pop (0);
102 if (__builtin_expect (result == 0, 1))
104 /* We mark the thread as terminated and as joined. */
105 pd->tid = -1;
107 /* Store the return value if the caller is interested. */
108 if (thread_return != NULL)
109 *thread_return = pd->result;
112 /* Free the TCB. */
113 __free_tcb (pd);
116 LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd->result);
118 return result;