pthread_start.c raw

   1  /*
   2   * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
   3   * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
   4   * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
   5   * Copyright (c) 2000-2010 by Hewlett-Packard Development Company.
   6   * All rights reserved.
   7   *
   8   * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
   9   * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  10   *
  11   * Permission is hereby granted to use or copy this program
  12   * for any purpose, provided the above notices are retained on all copies.
  13   * Permission to modify the code and to distribute modified code is granted,
  14   * provided the above notices are retained, and a notice that the code was
  15   * modified is included with the above copyright notice.
  16   */
  17  
  18  /*
  19   * We want to make sure that `GC_thread_exit_proc()` is unconditionally
  20   * invoked, even if the client is not compiled with `-fexceptions` option,
  21   * but the collector is.  The workaround is to put `GC_pthread_start_inner()`
  22   * in its own file (this one), and undefine `__EXCEPTIONS` in the gcc case
  23   * at the top of the file.
  24   * FIXME: It is still unclear whether this will actually cause the
  25   * `exit` handler to be invoked last when `thread_exit` is called (and
  26   * if `-fexceptions` option is used).
  27   */
  28  
  29  #if !defined(DONT_UNDEF_EXCEPTIONS) && defined(__GNUC__) && defined(__linux__)
  30  /*
  31   * We undefine `__EXCEPTIONS` to avoid using gcc `__cleanup__` attribute.
  32   * The current NPTL implementation of `pthread_cleanup_push` uses
  33   * `__cleanup__` attribute when `__EXCEPTIONS` is defined (`-fexceptions`
  34   * option is given).  The stack unwinding and cleanup with `__cleanup__`
  35   * attribute work correctly when everything is compiled with `-fexceptions`
  36   * option, but it is not the requirement for this library clients to use
  37   * `-fexceptions` option everywhere.  With `__EXCEPTIONS` undefined, the
  38   * cleanup routines are registered with `__pthread_register_cancel` thus
  39   * should work anyway.
  40   */
  41  #  undef __EXCEPTIONS
  42  #endif
  43  
  44  #include "private/pthread_support.h"
  45  
  46  #if defined(GC_PTHREADS) && !defined(PLATFORM_THREADS) \
  47      && !defined(SN_TARGET_PSP2)
  48  
  49  /* Invoked from `GC_pthread_start()`. */
  50  GC_INNER_PTHRSTART void *GC_CALLBACK
  51  GC_pthread_start_inner(struct GC_stack_base *sb, void *arg)
  52  {
  53    void *(*start)(void *);
  54    void *start_arg;
  55    void *result;
  56    volatile GC_thread me
  57        = GC_start_rtn_prepare_thread(&start, &start_arg, sb, arg);
  58  
  59  #  ifndef NACL
  60    pthread_cleanup_push(GC_thread_exit_proc, (/* no volatile */ void *)me);
  61  #  endif
  62    result = (*start)(start_arg);
  63  #  if defined(DEBUG_THREADS) && !defined(GC_PTHREAD_START_STANDALONE)
  64    GC_log_printf("Finishing thread %p\n", PTHREAD_TO_VPTR(pthread_self()));
  65  #  endif
  66    me->status = result;
  67    /* Note: we cannot use `GC_dirty()` instead. */
  68    GC_end_stubborn_change(me);
  69  
  70    /*
  71     * Cleanup acquires the allocator lock, ensuring that we cannot exit
  72     * while a collection that thinks we are alive is trying to stop us.
  73     */
  74  #  ifdef NACL
  75    GC_thread_exit_proc((/* no volatile */ void *)me);
  76  #  else
  77    pthread_cleanup_pop(1);
  78  #  endif
  79    return result;
  80  }
  81  
  82  #endif /* GC_PTHREADS */
  83