tls.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Definitions for thread-local data handling. NPTL/sparc version.
  2. Copyright (C) 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _TLS_H
  16. #define _TLS_H
  17. #ifndef __ASSEMBLER__
  18. # include <stdbool.h>
  19. # include <stddef.h>
  20. # include <stdint.h>
  21. # include <stdlib.h>
  22. # include <list.h>
  23. # include <bits/kernel-features.h>
  24. /* Type for the dtv. */
  25. typedef union dtv
  26. {
  27. size_t counter;
  28. struct
  29. {
  30. void *val;
  31. bool is_static;
  32. } pointer;
  33. } dtv_t;
  34. typedef struct
  35. {
  36. void *tcb; /* Pointer to the TCB. Not necessary the
  37. thread descriptor used by libpthread. */
  38. dtv_t *dtv;
  39. void *self;
  40. int multiple_threads;
  41. #if __WORDSIZE == 64
  42. int gscope_flag;
  43. #endif
  44. uintptr_t sysinfo;
  45. uintptr_t stack_guard;
  46. uintptr_t pointer_guard;
  47. #if __WORDSIZE != 64
  48. int gscope_flag;
  49. #endif
  50. #ifndef __ASSUME_PRIVATE_FUTEX
  51. int private_futex;
  52. #endif
  53. } tcbhead_t;
  54. #else /* __ASSEMBLER__ */
  55. # include <tcb-offsets.h>
  56. #endif /* __ASSEMBLER__ */
  57. /* We require TLS support in the tools. */
  58. #define HAVE_TLS_SUPPORT
  59. #define HAVE___THREAD 1
  60. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  61. /* Signal that TLS support is available. */
  62. #define USE_TLS 1
  63. #ifndef __ASSEMBLER__
  64. /* Get system call information. */
  65. # include <sysdep.h>
  66. /* Get the thread descriptor definition. */
  67. # include <descr.h>
  68. register struct pthread *__thread_self __asm__("%g7");
  69. /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
  70. because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
  71. struct pthread even when not linked with -lpthread. */
  72. # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
  73. /* Alignment requirements for the initial TCB. */
  74. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  75. /* This is the size of the TCB. */
  76. # define TLS_TCB_SIZE sizeof (struct pthread)
  77. /* Alignment requirements for the TCB. */
  78. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  79. /* The TCB can have any size and the memory following the address the
  80. thread pointer points to is unspecified. Allocate the TCB there. */
  81. # define TLS_TCB_AT_TP 1
  82. /* Install the dtv pointer. The pointer passed is to the element with
  83. index -1 which contain the length. */
  84. # define INSTALL_DTV(descr, dtvp) \
  85. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  86. /* Install new dtv for current thread. */
  87. # define INSTALL_NEW_DTV(DTV) \
  88. (((tcbhead_t *) __thread_self)->dtv = (DTV))
  89. /* Return dtv of given thread descriptor. */
  90. # define GET_DTV(descr) \
  91. (((tcbhead_t *) (descr))->dtv)
  92. /* Code to initially initialize the thread pointer. */
  93. # define TLS_INIT_TP(descr, secondcall) \
  94. (__thread_self = (__typeof (__thread_self)) (descr), NULL)
  95. /* Return the address of the dtv for the current thread. */
  96. # define THREAD_DTV() \
  97. (((tcbhead_t *) __thread_self)->dtv)
  98. /* Return the thread descriptor for the current thread. */
  99. #define THREAD_SELF __thread_self
  100. /* Magic for libthread_db to know how to do THREAD_SELF. */
  101. # define DB_THREAD_SELF_INCLUDE <sys/ucontext.h>
  102. # define DB_THREAD_SELF \
  103. REGISTER (32, 32, REG_G7 * 4, 0) REGISTER (64, 64, REG_G7 * 8, 0)
  104. /* Access to data in the thread descriptor is easy. */
  105. #define THREAD_GETMEM(descr, member) \
  106. descr->member
  107. #define THREAD_GETMEM_NC(descr, member, idx) \
  108. descr->member[idx]
  109. #define THREAD_SETMEM(descr, member, value) \
  110. descr->member = (value)
  111. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  112. descr->member[idx] = (value)
  113. /* Set the stack guard field in TCB head. */
  114. #define THREAD_SET_STACK_GUARD(value) \
  115. THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
  116. # define THREAD_COPY_STACK_GUARD(descr) \
  117. ((descr)->header.stack_guard \
  118. = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
  119. /* Get/set the stack guard field in TCB head. */
  120. #define THREAD_GET_POINTER_GUARD() \
  121. THREAD_GETMEM (THREAD_SELF, header.pointer_guard)
  122. #define THREAD_SET_POINTER_GUARD(value) \
  123. THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
  124. # define THREAD_COPY_POINTER_GUARD(descr) \
  125. ((descr)->header.pointer_guard = THREAD_GET_POINTER_GUARD ())
  126. /* Get and set the global scope generation counter in struct pthread. */
  127. #define THREAD_GSCOPE_FLAG_UNUSED 0
  128. #define THREAD_GSCOPE_FLAG_USED 1
  129. #define THREAD_GSCOPE_FLAG_WAIT 2
  130. #define THREAD_GSCOPE_RESET_FLAG() \
  131. do \
  132. { int __res \
  133. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  134. THREAD_GSCOPE_FLAG_UNUSED); \
  135. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  136. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  137. } \
  138. while (0)
  139. #define THREAD_GSCOPE_SET_FLAG() \
  140. do \
  141. { \
  142. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  143. atomic_write_barrier (); \
  144. } \
  145. while (0)
  146. #define THREAD_GSCOPE_WAIT() \
  147. GL(dl_wait_lookup_done) ()
  148. #endif /* !ASSEMBLER */
  149. #endif /* tls.h */