1 /* Copyright (C) 2002, 2003, 2005 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/>. */
28 *(void **) arg
= NULL
;
33 pthread_timedjoin_np (threadid
, thread_return
, abstime
)
36 const struct timespec
*abstime
;
39 struct pthread
*pd
= (struct pthread
*) threadid
;
42 /* Make sure the descriptor is valid. */
43 if (INVALID_NOT_TERMINATED_TD_P (pd
))
44 /* Not a valid thread handle. */
47 /* Is the thread joinable?. */
49 /* We cannot wait for the thread. */
53 if (pd
== self
|| self
->joinid
== pd
)
54 /* This is a deadlock situation. The threads are waiting for each
55 other to finish. Note that this is a "may" error. To be 100%
56 sure we catch this error we would have to lock the data
57 structures but it is not necessary. In the unlikely case that
58 two threads are really caught in this situation they will
59 deadlock. It is the programmer's problem to figure this
63 /* Wait for the thread to finish. If it is already locked something
64 is wrong. There can only be one waiter. */
65 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd
->joinid
,
67 /* There is already somebody waiting for the thread. */
71 /* During the wait we change to asynchronous cancellation. If we
72 are cancelled the thread we are waiting for must be marked as
73 un-wait-ed for again. */
74 pthread_cleanup_push (cleanup
, &pd
->joinid
);
76 /* Switch to asynchronous cancellation. */
77 int oldtype
= CANCEL_ASYNC ();
80 /* Wait for the child. */
81 result
= lll_timedwait_tid (pd
->tid
, abstime
);
84 /* Restore cancellation mode. */
85 CANCEL_RESET (oldtype
);
87 /* Remove the handler. */
88 pthread_cleanup_pop (0);
91 /* We might have timed out. */
94 /* Store the return value if the caller is interested. */
95 if (thread_return
!= NULL
)
96 *thread_return
= pd
->result
;