Back to index

glibc  2.9
pthread_tryjoin.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <stdlib.h>
00022 
00023 #include <atomic.h>
00024 #include "pthreadP.h"
00025 
00026 
00027 int
00028 pthread_tryjoin_np (threadid, thread_return)
00029      pthread_t threadid;
00030      void **thread_return;
00031 {
00032   struct pthread *self;
00033   struct pthread *pd = (struct pthread *) threadid;
00034 
00035   /* Make sure the descriptor is valid.  */
00036   if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
00037     /* Not a valid thread handle.  */
00038     return ESRCH;
00039 
00040   /* Is the thread joinable?.  */
00041   if (IS_DETACHED (pd))
00042     /* We cannot wait for the thread.  */
00043     return EINVAL;
00044 
00045   self = THREAD_SELF;
00046   if (pd == self || self->joinid == pd)
00047     /* This is a deadlock situation.  The threads are waiting for each
00048        other to finish.  Note that this is a "may" error.  To be 100%
00049        sure we catch this error we would have to lock the data
00050        structures but it is not necessary.  In the unlikely case that
00051        two threads are really caught in this situation they will
00052        deadlock.  It is the programmer's problem to figure this
00053        out.  */
00054     return EDEADLK;
00055 
00056   /* Return right away if the thread hasn't terminated yet.  */
00057   if (pd->tid != 0)
00058     return EBUSY;
00059 
00060   /* Wait for the thread to finish.  If it is already locked something
00061      is wrong.  There can only be one waiter.  */
00062   if (atomic_compare_and_exchange_bool_acq (&pd->joinid, self, NULL))
00063     /* There is already somebody waiting for the thread.  */
00064     return EINVAL;
00065 
00066   /* Store the return value if the caller is interested.  */
00067   if (thread_return != NULL)
00068     *thread_return = pd->result;
00069 
00070 
00071   /* Free the TCB.  */
00072   __free_tcb (pd);
00073 
00074   return 0;
00075 }