Blame nptl/pthread_join_common.c

Packit 6c4009
/* Common definition for pthread_{timed,try}join{_np}.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include "pthreadP.h"
Packit 6c4009
#include <atomic.h>
Packit 6c4009
#include <stap-probe.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
cleanup (void *arg)
Packit 6c4009
{
Packit 6c4009
  /* If we already changed the waiter ID, reset it.  The call cannot
Packit 6c4009
     fail for any reason but the thread not having done that yet so
Packit 6c4009
     there is no reason for a loop.  */
Packit 6c4009
  struct pthread *self = THREAD_SELF;
Packit 6c4009
  atomic_compare_exchange_weak_acquire (&arg, &self, NULL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__pthread_timedjoin_ex (pthread_t threadid, void **thread_return,
Packit 6c4009
			const struct timespec *abstime, bool block)
Packit 6c4009
{
Packit 6c4009
  struct pthread *pd = (struct pthread *) threadid;
Packit 6c4009
Packit 6c4009
  /* Make sure the descriptor is valid.  */
Packit 6c4009
  if (INVALID_NOT_TERMINATED_TD_P (pd))
Packit 6c4009
    /* Not a valid thread handle.  */
Packit 6c4009
    return ESRCH;
Packit 6c4009
Packit 6c4009
  /* Is the thread joinable?.  */
Packit 6c4009
  if (IS_DETACHED (pd))
Packit 6c4009
    /* We cannot wait for the thread.  */
Packit 6c4009
    return EINVAL;
Packit 6c4009
Packit 6c4009
  struct pthread *self = THREAD_SELF;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  LIBC_PROBE (pthread_join, 1, threadid);
Packit 6c4009
Packit 6c4009
  if ((pd == self
Packit 6c4009
       || (self->joinid == pd
Packit 6c4009
	   && (pd->cancelhandling
Packit 6c4009
	       & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
Packit 6c4009
		  | TERMINATED_BITMASK)) == 0))
Packit 6c4009
      && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
Packit 6c4009
    /* This is a deadlock situation.  The threads are waiting for each
Packit 6c4009
       other to finish.  Note that this is a "may" error.  To be 100%
Packit 6c4009
       sure we catch this error we would have to lock the data
Packit 6c4009
       structures but it is not necessary.  In the unlikely case that
Packit 6c4009
       two threads are really caught in this situation they will
Packit 6c4009
       deadlock.  It is the programmer's problem to figure this
Packit 6c4009
       out.  */
Packit 6c4009
    return EDEADLK;
Packit 6c4009
Packit 6c4009
  /* Wait for the thread to finish.  If it is already locked something
Packit 6c4009
     is wrong.  There can only be one waiter.  */
Packit 6c4009
  else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd->joinid,
Packit 6c4009
								   &self,
Packit 6c4009
								   NULL)))
Packit 6c4009
    /* There is already somebody waiting for the thread.  */
Packit 6c4009
    return EINVAL;
Packit 6c4009
Packit 6c4009
  if (block)
Packit 6c4009
    {
Packit 6c4009
      /* During the wait we change to asynchronous cancellation.  If we
Packit 6c4009
	 are cancelled the thread we are waiting for must be marked as
Packit 6c4009
	 un-wait-ed for again.  */
Packit 6c4009
      pthread_cleanup_push (cleanup, &pd->joinid);
Packit 6c4009
Packit 6c4009
      int oldtype = CANCEL_ASYNC ();
Packit 6c4009
Packit 6c4009
      if (abstime != NULL)
Packit 6c4009
	result = lll_timedwait_tid (pd->tid, abstime);
Packit 6c4009
      else
Packit 6c4009
	lll_wait_tid (pd->tid);
Packit 6c4009
Packit 6c4009
      CANCEL_RESET (oldtype);
Packit 6c4009
Packit 6c4009
      pthread_cleanup_pop (0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (__glibc_likely (result == 0))
Packit 6c4009
    {
Packit 6c4009
      /* We mark the thread as terminated and as joined.  */
Packit 6c4009
      pd->tid = -1;
Packit 6c4009
Packit 6c4009
      /* Store the return value if the caller is interested.  */
Packit 6c4009
      if (thread_return != NULL)
Packit 6c4009
	*thread_return = pd->result;
Packit 6c4009
Packit 6c4009
      /* Free the TCB.  */
Packit 6c4009
      __free_tcb (pd);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    pd->joinid = NULL;
Packit 6c4009
Packit 6c4009
  LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd->result);
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
hidden_def (__pthread_timedjoin_ex)