tls.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2005-2017 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public License as
  4. published by the Free Software Foundation; either version 2.1 of the
  5. License, or (at your option) any later version.
  6. The GNU C Library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with the GNU C Library; if not, see
  12. <http://www.gnu.org/licenses/>. */
  13. #ifndef _TLS_H
  14. #define _TLS_H 1
  15. #ifndef __ASSEMBLER__
  16. # include <stdbool.h>
  17. # include <stddef.h>
  18. # include <stdint.h>
  19. /* Type for the dtv. */
  20. typedef union dtv
  21. {
  22. size_t counter;
  23. struct
  24. {
  25. void *val;
  26. bool is_static;
  27. } pointer;
  28. } dtv_t;
  29. #else /* __ASSEMBLER__ */
  30. # include <tcb-offsets.h>
  31. #endif /* __ASSEMBLER__ */
  32. /* We require TLS support in the tools. */
  33. #define HAVE_TLS_SUPPORT 1
  34. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  35. #define HAVE___THREAD 1
  36. /* Signal that TLS support is available. */
  37. #define USE_TLS 1
  38. #ifndef __ASSEMBLER__
  39. /* Get system call information. */
  40. # include <sysdep.h>
  41. /* The TP points to the start of the thread blocks. */
  42. # define TLS_DTV_AT_TP 1
  43. /* Get the thread descriptor definition. */
  44. # include <../../descr.h>
  45. typedef struct
  46. {
  47. dtv_t *dtv;
  48. void *private;
  49. } tcbhead_t;
  50. /* This is the size of the initial TCB. */
  51. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  52. /* Alignment requirements for the initial TCB. */
  53. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  54. /* This is the size of the TCB. */
  55. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  56. /* This is the size we need before TCB. */
  57. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  58. /* Alignment requirements for the TCB. */
  59. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  60. /* Install the dtv pointer. The pointer passed is to the element with
  61. index -1 which contain the length. */
  62. # define INSTALL_DTV(tcbp, dtvp) \
  63. (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
  64. /* Install new dtv for current thread. */
  65. # define INSTALL_NEW_DTV(dtv) \
  66. (THREAD_DTV() = (dtv))
  67. /* Return dtv of given thread descriptor. */
  68. # define GET_DTV(tcbp) \
  69. (((tcbhead_t *) (tcbp))->dtv)
  70. /* Code to initially initialize the thread pointer. This might need
  71. special attention since 'errno' is not yet available and if the
  72. operation can cause a failure 'errno' must not be touched. */
  73. # define TLS_INIT_TP(tcbp, secondcall) \
  74. ({ __asm __volatile ("msr tpidr_el0, %0" : : "r" (tcbp)); NULL; })
  75. /* Value passed to 'clone' for initialization of the thread register. */
  76. # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
  77. /* Return the address of the dtv for the current thread. */
  78. # define THREAD_DTV() \
  79. (((tcbhead_t *) __builtin_thread_pointer ())->dtv)
  80. /* Return the thread descriptor for the current thread. */
  81. # define THREAD_SELF \
  82. ((struct pthread *)__builtin_thread_pointer () - 1)
  83. /* Magic for libthread_db to know how to do THREAD_SELF. */
  84. # define DB_THREAD_SELF \
  85. CONST_THREAD_AREA (64, sizeof (struct pthread))
  86. /* Access to data in the thread descriptor is easy. */
  87. # define THREAD_GETMEM(descr, member) \
  88. descr->member
  89. # define THREAD_GETMEM_NC(descr, member, idx) \
  90. descr->member[idx]
  91. # define THREAD_SETMEM(descr, member, value) \
  92. descr->member = (value)
  93. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  94. descr->member[idx] = (value)
  95. /* Get and set the global scope generation counter in struct pthread. */
  96. # define THREAD_GSCOPE_FLAG_UNUSED 0
  97. # define THREAD_GSCOPE_FLAG_USED 1
  98. # define THREAD_GSCOPE_FLAG_WAIT 2
  99. # define THREAD_GSCOPE_RESET_FLAG() \
  100. do \
  101. { int __res \
  102. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  103. THREAD_GSCOPE_FLAG_UNUSED); \
  104. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  105. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  106. } \
  107. while (0)
  108. # define THREAD_GSCOPE_SET_FLAG() \
  109. do \
  110. { \
  111. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  112. atomic_write_barrier (); \
  113. } \
  114. while (0)
  115. # define THREAD_GSCOPE_WAIT() \
  116. GL(dl_wait_lookup_done) ()
  117. # endif /* __ASSEMBLER__ */
  118. #endif /* tls.h */