pthread_start.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /* We want to make sure that GC_thread_exit_proc() is unconditionally */
  18. /* invoked, even if the client is not compiled with -fexceptions, but */
  19. /* the GC is. The workaround is to put GC_inner_start_routine() in its */
  20. /* own file (pthread_start.c), and undefine __EXCEPTIONS in the GCC */
  21. /* case at the top of the file. FIXME: it's still unclear whether this */
  22. /* will actually cause the exit handler to be invoked last when */
  23. /* thread_exit is called (and if -fexceptions is used). */
  24. #if defined(__GNUC__) && defined(__linux__)
  25. /* We undefine __EXCEPTIONS to avoid using GCC __cleanup__ attribute. */
  26. /* The current NPTL implementation of pthread_cleanup_push uses */
  27. /* __cleanup__ attribute when __EXCEPTIONS is defined (-fexceptions). */
  28. /* The stack unwinding and cleanup with __cleanup__ attributes work */
  29. /* correctly when everything is compiled with -fexceptions, but it is */
  30. /* not the requirement for this library clients to use -fexceptions */
  31. /* everywhere. With __EXCEPTIONS undefined, the cleanup routines are */
  32. /* registered with __pthread_register_cancel thus should work anyway. */
  33. # undef __EXCEPTIONS
  34. #endif
  35. #include "private/pthread_support.h"
  36. #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
  37. #include <pthread.h>
  38. #include <sched.h>
  39. /* Invoked from GC_start_routine(). */
  40. GC_INNER_PTHRSTART void * GC_CALLBACK GC_inner_start_routine(
  41. struct GC_stack_base *sb, void *arg)
  42. {
  43. void * (*start)(void *);
  44. void * start_arg;
  45. void * result;
  46. volatile GC_thread me =
  47. GC_start_rtn_prepare_thread(&start, &start_arg, sb, arg);
  48. # ifndef NACL
  49. pthread_cleanup_push(GC_thread_exit_proc, me);
  50. # endif
  51. result = (*start)(start_arg);
  52. # if defined(DEBUG_THREADS) && !defined(GC_PTHREAD_START_STANDALONE)
  53. GC_log_printf("Finishing thread %p\n", (void *)pthread_self());
  54. # endif
  55. me -> status = result;
  56. GC_dirty(me);
  57. # ifndef NACL
  58. pthread_cleanup_pop(1);
  59. /* Cleanup acquires lock, ensuring that we can't exit while */
  60. /* a collection that thinks we're alive is trying to stop us. */
  61. # endif
  62. return result;
  63. }
  64. #endif /* GC_PTHREADS */