* locales/as_IN: Fix currency_symbol, abday for Sunday, abmon for
[glibc/pb-stable.git] / nptl / pthread_timedjoin.c
blob5df6ab6a8085d6273bd80fb363437dd92b87fe23
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, 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>
22 #include <atomic.h>
23 #include "pthreadP.h"
26 static void
27 cleanup (void *arg)
29 *(void **) arg = NULL;
33 int
34 pthread_timedjoin_np (threadid, thread_return, abstime)
35 pthread_t threadid;
36 void **thread_return;
37 const struct timespec *abstime;
39 struct pthread *self;
40 struct pthread *pd = (struct pthread *) threadid;
41 int result;
43 /* Make sure the descriptor is valid. */
44 if (INVALID_NOT_TERMINATED_TD_P (pd))
45 /* Not a valid thread handle. */
46 return ESRCH;
48 /* Is the thread joinable?. */
49 if (IS_DETACHED (pd))
50 /* We cannot wait for the thread. */
51 return EINVAL;
53 self = THREAD_SELF;
54 if (pd == self || self->joinid == pd)
55 /* This is a deadlock situation. The threads are waiting for each
56 other to finish. Note that this is a "may" error. To be 100%
57 sure we catch this error we would have to lock the data
58 structures but it is not necessary. In the unlikely case that
59 two threads are really caught in this situation they will
60 deadlock. It is the programmer's problem to figure this
61 out. */
62 return EDEADLK;
64 /* Wait for the thread to finish. If it is already locked something
65 is wrong. There can only be one waiter. */
66 if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
67 self, NULL), 0))
68 /* There is already somebody waiting for the thread. */
69 return EINVAL;
72 /* During the wait we change to asynchronous cancellation. If we
73 are cancelled the thread we are waiting for must be marked as
74 un-wait-ed for again. */
75 pthread_cleanup_push (cleanup, &pd->joinid);
77 /* Switch to asynchronous cancellation. */
78 int oldtype = CANCEL_ASYNC ();
81 /* Wait for the child. */
82 result = lll_timedwait_tid (pd->tid, abstime);
85 /* Restore cancellation mode. */
86 CANCEL_RESET (oldtype);
88 /* Remove the handler. */
89 pthread_cleanup_pop (0);
92 /* We might have timed out. */
93 if (result == 0)
95 /* Store the return value if the caller is interested. */
96 if (thread_return != NULL)
97 *thread_return = pd->result;
100 /* Free the TCB. */
101 __free_tcb (pd);
103 else
104 pd->joinid = NULL;
106 return result;