pthread_support.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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-2009 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. #ifndef GC_PTHREAD_SUPPORT_H
  18. #define GC_PTHREAD_SUPPORT_H
  19. #include "private/gc_priv.h"
  20. #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
  21. #if defined(GC_DARWIN_THREADS)
  22. # include "private/darwin_stop_world.h"
  23. #else
  24. # include "private/pthread_stop_world.h"
  25. #endif
  26. #ifdef THREAD_LOCAL_ALLOC
  27. # include "thread_local_alloc.h"
  28. #endif
  29. #ifdef THREAD_SANITIZER
  30. # include "dbg_mlc.h" /* for oh type */
  31. #endif
  32. EXTERN_C_BEGIN
  33. /* We use the allocation lock to protect thread-related data structures. */
  34. /* The set of all known threads. We intercept thread creation and */
  35. /* joins. */
  36. /* Protected by allocation/GC lock. */
  37. /* Some of this should be declared volatile, but that's inconsistent */
  38. /* with some library routine declarations. */
  39. typedef struct GC_Thread_Rep {
  40. # ifdef THREAD_SANITIZER
  41. char dummy[sizeof(oh)]; /* A dummy field to avoid TSan false */
  42. /* positive about the race between */
  43. /* GC_has_other_debug_info and */
  44. /* GC_suspend_handler_inner (which */
  45. /* sets store_stop.stack_ptr). */
  46. # endif
  47. struct GC_Thread_Rep * next; /* More recently allocated threads */
  48. /* with a given pthread id come */
  49. /* first. (All but the first are */
  50. /* guaranteed to be dead, but we may */
  51. /* not yet have registered the join.) */
  52. pthread_t id;
  53. # ifdef USE_TKILL_ON_ANDROID
  54. pid_t kernel_id;
  55. # endif
  56. /* Extra bookkeeping information the stopping code uses */
  57. struct thread_stop_info stop_info;
  58. # if defined(GC_ENABLE_SUSPEND_THREAD) && !defined(GC_DARWIN_THREADS) \
  59. && !defined(GC_OPENBSD_UTHREADS) && !defined(NACL)
  60. volatile AO_t suspended_ext; /* Thread was suspended externally. */
  61. # endif
  62. unsigned char flags;
  63. # define FINISHED 1 /* Thread has exited. */
  64. # define DETACHED 2 /* Thread is treated as detached. */
  65. /* Thread may really be detached, or */
  66. /* it may have been explicitly */
  67. /* registered, in which case we can */
  68. /* deallocate its GC_Thread_Rep once */
  69. /* it unregisters itself, since it */
  70. /* may not return a GC pointer. */
  71. # define MAIN_THREAD 4 /* True for the original thread only. */
  72. # define DISABLED_GC 0x10 /* Collections are disabled while the */
  73. /* thread is exiting. */
  74. unsigned char thread_blocked;
  75. /* Protected by GC lock. */
  76. /* Treated as a boolean value. If set, */
  77. /* thread will acquire GC lock before */
  78. /* doing any pointer manipulations, and */
  79. /* has set its SP value. Thus it does */
  80. /* not need to be sent a signal to stop */
  81. /* it. */
  82. unsigned short finalizer_skipped;
  83. unsigned char finalizer_nested;
  84. /* Used by GC_check_finalizer_nested() */
  85. /* to minimize the level of recursion */
  86. /* when a client finalizer allocates */
  87. /* memory (initially both are 0). */
  88. ptr_t stack_end; /* Cold end of the stack (except for */
  89. /* main thread). */
  90. ptr_t altstack; /* The start of the alt-stack if there */
  91. /* is one, NULL otherwise. */
  92. word altstack_size; /* The size of the alt-stack if exists. */
  93. ptr_t stack; /* The start and size of the normal */
  94. /* stack (set by GC_register_altstack). */
  95. word stack_size;
  96. # if defined(GC_DARWIN_THREADS) && !defined(DARWIN_DONT_PARSE_STACK)
  97. ptr_t topOfStack; /* Result of GC_FindTopOfStack(0); */
  98. /* valid only if the thread is blocked; */
  99. /* non-NULL value means already set. */
  100. # endif
  101. # ifdef IA64
  102. ptr_t backing_store_end;
  103. ptr_t backing_store_ptr;
  104. # endif
  105. struct GC_traced_stack_sect_s *traced_stack_sect;
  106. /* Points to the "frame" data held in stack by */
  107. /* the innermost GC_call_with_gc_active() of */
  108. /* this thread. May be NULL. */
  109. void * status; /* The value returned from the thread. */
  110. /* Used only to avoid premature */
  111. /* reclamation of any data it might */
  112. /* reference. */
  113. /* This is unfortunately also the */
  114. /* reason we need to intercept join */
  115. /* and detach. */
  116. # ifdef THREAD_LOCAL_ALLOC
  117. struct thread_local_freelists tlfs;
  118. # endif
  119. } * GC_thread;
  120. #ifndef THREAD_TABLE_SZ
  121. # define THREAD_TABLE_SZ 256 /* Power of 2 (for speed). */
  122. #endif
  123. #if CPP_WORDSZ == 64
  124. # define THREAD_TABLE_INDEX(id) \
  125. (int)(((((NUMERIC_THREAD_ID(id) >> 8) ^ NUMERIC_THREAD_ID(id)) >> 16) \
  126. ^ ((NUMERIC_THREAD_ID(id) >> 8) ^ NUMERIC_THREAD_ID(id))) \
  127. % THREAD_TABLE_SZ)
  128. #else
  129. # define THREAD_TABLE_INDEX(id) \
  130. (int)(((NUMERIC_THREAD_ID(id) >> 16) \
  131. ^ (NUMERIC_THREAD_ID(id) >> 8) \
  132. ^ NUMERIC_THREAD_ID(id)) % THREAD_TABLE_SZ)
  133. #endif
  134. GC_EXTERN volatile GC_thread GC_threads[THREAD_TABLE_SZ];
  135. GC_EXTERN GC_bool GC_thr_initialized;
  136. GC_INNER GC_thread GC_lookup_thread(pthread_t id);
  137. GC_EXTERN GC_bool GC_in_thread_creation;
  138. /* We may currently be in thread creation or destruction. */
  139. /* Only set to TRUE while allocation lock is held. */
  140. /* When set, it is OK to run GC from unknown thread. */
  141. #ifdef NACL
  142. GC_EXTERN __thread GC_thread GC_nacl_gc_thread_self;
  143. GC_INNER void GC_nacl_initialize_gc_thread(void);
  144. GC_INNER void GC_nacl_shutdown_gc_thread(void);
  145. #endif
  146. #ifdef GC_EXPLICIT_SIGNALS_UNBLOCK
  147. GC_INNER void GC_unblock_gc_signals(void);
  148. #endif
  149. #ifdef GC_PTHREAD_START_STANDALONE
  150. # define GC_INNER_PTHRSTART /* empty */
  151. #else
  152. # define GC_INNER_PTHRSTART GC_INNER
  153. #endif
  154. GC_INNER_PTHRSTART void * GC_CALLBACK GC_inner_start_routine(
  155. struct GC_stack_base *sb, void *arg);
  156. GC_INNER_PTHRSTART GC_thread GC_start_rtn_prepare_thread(
  157. void *(**pstart)(void *),
  158. void **pstart_arg,
  159. struct GC_stack_base *sb, void *arg);
  160. GC_INNER_PTHRSTART void GC_thread_exit_proc(void *);
  161. EXTERN_C_END
  162. #endif /* GC_PTHREADS && !GC_WIN32_THREADS */
  163. #endif /* GC_PTHREAD_SUPPORT_H */