tls.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Definition for thread-local data handling. NPTL/ARM version.
  2. Copyright (C) 2005,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 1
  17. #ifndef __ASSEMBLER__
  18. # include <stdbool.h>
  19. # include <stddef.h>
  20. # include <stdint.h>
  21. /* Type for the dtv. */
  22. typedef union dtv
  23. {
  24. size_t counter;
  25. struct
  26. {
  27. void *val;
  28. bool is_static;
  29. } pointer;
  30. } dtv_t;
  31. #else /* __ASSEMBLER__ */
  32. # include <tcb-offsets.h>
  33. #endif /* __ASSEMBLER__ */
  34. /* We require TLS support in the tools. */
  35. #define HAVE_TLS_SUPPORT 1
  36. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  37. #define HAVE___THREAD 1
  38. /* Signal that TLS support is available. */
  39. #define USE_TLS 1
  40. #ifndef __ASSEMBLER__
  41. /* Get system call information. */
  42. # include <sysdep.h>
  43. /* The TP points to the start of the thread blocks. */
  44. # define TLS_DTV_AT_TP 1
  45. /* Get the thread descriptor definition. */
  46. # include <../../descr.h>
  47. typedef struct
  48. {
  49. dtv_t *dtv;
  50. void *private;
  51. } tcbhead_t;
  52. /* This is the size of the initial TCB. */
  53. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  54. /* Alignment requirements for the initial TCB. */
  55. # define TLS_INIT_TCB_ALIGN 16
  56. /* This is the size of the TCB. */
  57. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  58. /* This is the size we need before TCB. */
  59. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  60. /* Alignment requirements for the TCB. */
  61. # define TLS_TCB_ALIGN 16
  62. /* Install the dtv pointer. The pointer passed is to the element with
  63. index -1 which contain the length. */
  64. # define INSTALL_DTV(tcbp, dtvp) \
  65. (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
  66. /* Install new dtv for current thread. */
  67. # define INSTALL_NEW_DTV(dtv) \
  68. (THREAD_DTV() = (dtv))
  69. /* Return dtv of given thread descriptor. */
  70. # define GET_DTV(tcbp) \
  71. (((tcbhead_t *) (tcbp))->dtv)
  72. /* Code to initially initialize the thread pointer. This might need
  73. special attention since 'errno' is not yet available and if the
  74. operation can cause a failure 'errno' must not be touched. */
  75. # define TLS_INIT_TP(tcbp, secondcall) \
  76. ({ INTERNAL_SYSCALL_DECL (err); \
  77. long result_var; \
  78. result_var = INTERNAL_SYSCALL_ARM (set_tls, err, 1, (tcbp)); \
  79. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  80. ? "unknown error" : NULL; })
  81. /* Return the address of the dtv for the current thread. */
  82. # define THREAD_DTV() \
  83. (((tcbhead_t *) __builtin_thread_pointer ())->dtv)
  84. /* Return the thread descriptor for the current thread. */
  85. # define THREAD_SELF \
  86. ((struct pthread *)__builtin_thread_pointer () - 1)
  87. /* Magic for libthread_db to know how to do THREAD_SELF. */
  88. # define DB_THREAD_SELF \
  89. CONST_THREAD_AREA (32, sizeof (struct pthread))
  90. /* Access to data in the thread descriptor is easy. */
  91. #define THREAD_GETMEM(descr, member) \
  92. descr->member
  93. #define THREAD_GETMEM_NC(descr, member, idx) \
  94. descr->member[idx]
  95. #define THREAD_SETMEM(descr, member, value) \
  96. descr->member = (value)
  97. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  98. descr->member[idx] = (value)
  99. /* Initializing the thread pointer will generate a SIGILL if the syscall
  100. is not available. */
  101. #define TLS_INIT_TP_EXPENSIVE 1
  102. /* Get and set the global scope generation counter in struct pthread. */
  103. #define THREAD_GSCOPE_FLAG_UNUSED 0
  104. #define THREAD_GSCOPE_FLAG_USED 1
  105. #define THREAD_GSCOPE_FLAG_WAIT 2
  106. #define THREAD_GSCOPE_RESET_FLAG() \
  107. do \
  108. { int __res \
  109. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  110. THREAD_GSCOPE_FLAG_UNUSED); \
  111. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  112. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  113. } \
  114. while (0)
  115. #define THREAD_GSCOPE_SET_FLAG() \
  116. do \
  117. { \
  118. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  119. atomic_write_barrier (); \
  120. } \
  121. while (0)
  122. #define THREAD_GSCOPE_WAIT() \
  123. GL(dl_wait_lookup_done) ()
  124. #endif /* __ASSEMBLER__ */
  125. #endif /* tls.h */