OSDN Git Service

Merge branch 'master' into nptl
[uclinux-h8/uClibc.git] / libpthread / nptl / pthread_join.c
1 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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.  */
19
20 #include <errno.h>
21 #include <stdlib.h>
22
23 #include "atomic.h"
24 #include "pthreadP.h"
25
26
27 static void
28 cleanup (void *arg)
29 {
30   *(void **) arg = NULL;
31 }
32
33
34 int
35 pthread_join (pthread_t threadid, void **thread_return)
36 {
37   struct pthread *self;
38   struct pthread *pd = (struct pthread *) threadid;
39
40   /* Make sure the descriptor is valid.  */
41   if (INVALID_NOT_TERMINATED_TD_P (pd))
42     /* Not a valid thread handle.  */
43     return ESRCH;
44
45   /* Is the thread joinable?.  */
46   if (IS_DETACHED (pd))
47     /* We cannot wait for the thread.  */
48     return EINVAL;
49
50   self = THREAD_SELF;
51   if (pd == self
52       || (self->joinid == pd
53           && (pd->cancelhandling
54               & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
55                  | TERMINATED_BITMASK)) == 0))
56     /* This is a deadlock situation.  The threads are waiting for each
57        other to finish.  Note that this is a "may" error.  To be 100%
58        sure we catch this error we would have to lock the data
59        structures but it is not necessary.  In the unlikely case that
60        two threads are really caught in this situation they will
61        deadlock.  It is the programmer's problem to figure this
62        out.  */
63     return EDEADLK;
64
65   /* Wait for the thread to finish.  If it is already locked something
66      is wrong.  There can only be one waiter.  */
67   if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
68                                                               self,
69                                                               NULL), 0))
70     /* There is already somebody waiting for the thread.  */
71     return EINVAL;
72
73
74   /* During the wait we change to asynchronous cancellation.  If we
75      are cancelled the thread we are waiting for must be marked as
76      un-wait-ed for again.  */
77   pthread_cleanup_push (cleanup, &pd->joinid);
78
79   /* Switch to asynchronous cancellation.  */
80   int oldtype = CANCEL_ASYNC ();
81
82
83   /* Wait for the child.  */
84   lll_wait_tid (pd->tid);
85
86
87   /* Restore cancellation mode.  */
88   CANCEL_RESET (oldtype);
89
90   /* Remove the handler.  */
91   pthread_cleanup_pop (0);
92
93
94   /* We mark the thread as terminated and as joined.  */
95   pd->tid = -1;
96
97   /* Store the return value if the caller is interested.  */
98   if (thread_return != NULL)
99     *thread_return = pd->result;
100
101
102   /* Free the TCB.  */
103   __free_tcb (pd);
104
105   return 0;
106 }