Blame htl/pt-exit.c

Packit 6c4009
/* Thread termination.
Packit 6c4009
   Copyright (C) 2000-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 <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#include <pt-internal.h>
Packit 6c4009
#include <pthreadP.h>
Packit 6c4009
Packit 6c4009
#include <atomic.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Terminate the current thread and make STATUS available to any
Packit 6c4009
   thread that might join it.  */
Packit 6c4009
void
Packit 6c4009
__pthread_exit (void *status)
Packit 6c4009
{
Packit 6c4009
  struct __pthread *self = _pthread_self ();
Packit 6c4009
  struct __pthread_cancelation_handler **handlers;
Packit 6c4009
  int oldstate;
Packit 6c4009
Packit 6c4009
  /* Run any cancelation handlers.  According to POSIX, the
Packit 6c4009
     cancellation cleanup handlers should be called with cancellation
Packit 6c4009
     disabled.  */
Packit 6c4009
  __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
Packit 6c4009
Packit 6c4009
  for (handlers = ___pthread_get_cleanup_stack ();
Packit 6c4009
       *handlers != NULL;
Packit 6c4009
       *handlers = (*handlers)->__next)
Packit 6c4009
    (*handlers)->__handler ((*handlers)->__arg);
Packit 6c4009
Packit 6c4009
  __pthread_setcancelstate (oldstate, &oldstate);
Packit 6c4009
Packit 6c4009
  /* Decrease the number of threads.  We use an atomic operation to
Packit 6c4009
     make sure that only the last thread calls `exit'.  */
Packit 6c4009
  if (atomic_decrement_and_test (&__pthread_total))
Packit 6c4009
    /* We are the last thread.  */
Packit 6c4009
    exit (0);
Packit 6c4009
Packit 6c4009
  /* Note that after this point the process can be terminated at any
Packit 6c4009
     point if another thread calls `pthread_exit' and happens to be
Packit 6c4009
     the last thread.  */
Packit 6c4009
Packit 6c4009
  __pthread_mutex_lock (&self->state_lock);
Packit 6c4009
Packit 6c4009
  if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending)
Packit 6c4009
    status = PTHREAD_CANCELED;
Packit 6c4009
Packit 6c4009
  switch (self->state)
Packit 6c4009
    {
Packit 6c4009
    default:
Packit 6c4009
      assert (!"Consistency error: unexpected self->state");
Packit 6c4009
      abort ();
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case PTHREAD_DETACHED:
Packit 6c4009
      __pthread_mutex_unlock (&self->state_lock);
Packit 6c4009
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case PTHREAD_JOINABLE:
Packit 6c4009
      /* We need to stay around for a while since another thread
Packit 6c4009
         might want to join us.  */
Packit 6c4009
      self->state = PTHREAD_EXITED;
Packit 6c4009
Packit 6c4009
      /* We need to remember the exit status.  A thread joining us
Packit 6c4009
         might ask for it.  */
Packit 6c4009
      self->status = status;
Packit 6c4009
Packit 6c4009
      /* Broadcast the condition.  This will wake up threads that are
Packit 6c4009
         waiting to join us.  */
Packit 6c4009
      __pthread_cond_broadcast (&self->state_cond);
Packit 6c4009
      __pthread_mutex_unlock (&self->state_lock);
Packit 6c4009
Packit 6c4009
      break;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Destroy any thread specific data.  */
Packit 6c4009
  __pthread_destroy_specific (self);
Packit 6c4009
Packit 6c4009
  /* Destroy any signal state.  */
Packit 6c4009
  __pthread_sigstate_destroy (self);
Packit 6c4009
Packit 6c4009
  /* Self terminating requires TLS, so defer the release of the TCB until
Packit 6c4009
     the thread structure is reused.  */
Packit 6c4009
Packit 6c4009
  /* Release kernel resources, including the kernel thread and the stack,
Packit 6c4009
     and drop the self reference.  */
Packit 6c4009
  __pthread_thread_terminate (self);
Packit 6c4009
Packit 6c4009
  /* NOTREACHED */
Packit 6c4009
  abort ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
strong_alias (__pthread_exit, pthread_exit);