tls.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Definition for thread-local data handling. NPTL/RISCV32 version.
  2. Copyright (C) 2005, 2007, 2011 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifndef _TLS_H
  15. #define _TLS_H 1
  16. #ifndef __ASSEMBLER__
  17. # include <stdbool.h>
  18. # include <stddef.h>
  19. # include <stdint.h>
  20. /* Type for the dtv. */
  21. typedef union dtv
  22. {
  23. size_t counter;
  24. struct
  25. {
  26. void *val;
  27. bool is_static;
  28. } pointer;
  29. } dtv_t;
  30. #else /* __ASSEMBLER__ */
  31. # include <tcb-offsets.h>
  32. #endif /* __ASSEMBLER__ */
  33. /* We require TLS support in the tools. */
  34. #define HAVE_TLS_SUPPORT 1
  35. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  36. #define HAVE___THREAD 1
  37. /* Signal that TLS support is available. */
  38. #define USE_TLS 1
  39. #ifndef __ASSEMBLER__
  40. register void *__thread_self __asm__("tp");
  41. # define READ_THREAD_POINTER() ({ __thread_self; })
  42. /* Get system call information. */
  43. # include <sysdep.h>
  44. /* The TP points to the start of the TLS block. */
  45. # define TLS_DTV_AT_TP 1
  46. /* Get the thread descriptor definition. */
  47. # include <../../descr.h>
  48. typedef struct
  49. {
  50. dtv_t *dtv;
  51. void *private;
  52. } tcbhead_t;
  53. /* This is the size of the initial TCB. Because our TCB is before the thread
  54. pointer, we don't need this. */
  55. # define TLS_INIT_TCB_SIZE 0
  56. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  57. /* This is the size of the TCB. Because our TCB is before the thread
  58. pointer, we don't need this. */
  59. # define TLS_TCB_SIZE 0
  60. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  61. /* This is the size we need before TCB - actually, it includes the TCB. */
  62. # define TLS_PRE_TCB_SIZE \
  63. (sizeof (struct pthread) \
  64. + ((sizeof (tcbhead_t) + __alignof (struct pthread) - 1) \
  65. & ~(__alignof (struct pthread) - 1)))
  66. /* The thread pointer tp points to the end of the TCB.
  67. The pthread_descr structure is immediately in front of the TCB. */
  68. # define TLS_TCB_OFFSET 0
  69. /* Install the dtv pointer. The pointer passed is to the element with
  70. index -1 which contain the length. */
  71. # define INSTALL_DTV(tcbp, dtvp) \
  72. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  73. /* Install new dtv for current thread */
  74. # define INSTALL_NEW_DTV(dtv) \
  75. (THREAD_DTV() = (dtv))
  76. /* Return dtv of given thread descriptor. */
  77. # define GET_DTV(tcbp) \
  78. (((tcbhead_t *) (tcbp))[-1].dtv)
  79. /* Code to initially initialize the thread pointer.
  80. *
  81. * Set TP to the address _after_ tcbhead_t. This will allow us
  82. * to change the size of tcbhead_t without having to re-link everything.
  83. *
  84. * secondcall has something to do with USE__THREAD,
  85. * seems to always be 0 so we don't care about it.
  86. *
  87. * This has to return NULL on success (or a string with the failure text).
  88. * It's hard to fail this, so return NULL always.
  89. */
  90. # define TLS_INIT_TP(tcbp, secondcall) \
  91. ({ __thread_self = (char*)tcbp + TLS_TCB_OFFSET; NULL; })
  92. /* Return the address of the dtv for the current thread. */
  93. # define THREAD_DTV() \
  94. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  95. /* Return the thread descriptor for the current thread. */
  96. # define THREAD_SELF \
  97. ((struct pthread *) (READ_THREAD_POINTER () \
  98. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  99. /* Informs libthread_db that the thread pointer is register 4, which is used
  100. * to know how to do THREAD_SELF. */
  101. # define DB_THREAD_SELF \
  102. REGISTER (64, 64, 4 * 8, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
  103. /* Access to data in the thread descriptor is easy. */
  104. #define THREAD_GETMEM(descr, member) \
  105. descr->member
  106. #define THREAD_GETMEM_NC(descr, member, idx) \
  107. descr->member[idx]
  108. #define THREAD_SETMEM(descr, member, value) \
  109. descr->member = (value)
  110. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  111. descr->member[idx] = (value)
  112. /* l_tls_offset == 0 is perfectly valid, so we have to use some different
  113. value to mean unset l_tls_offset. */
  114. # define NO_TLS_OFFSET -1
  115. /* Get and set the global scope generation counter in struct pthread. */
  116. #define THREAD_GSCOPE_FLAG_UNUSED 0
  117. #define THREAD_GSCOPE_FLAG_USED 1
  118. #define THREAD_GSCOPE_FLAG_WAIT 2
  119. #define THREAD_GSCOPE_RESET_FLAG() \
  120. do \
  121. { int __res \
  122. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  123. THREAD_GSCOPE_FLAG_UNUSED); \
  124. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  125. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  126. } \
  127. while (0)
  128. #define THREAD_GSCOPE_SET_FLAG() \
  129. do \
  130. { \
  131. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  132. atomic_write_barrier (); \
  133. } \
  134. while (0)
  135. #define THREAD_GSCOPE_WAIT() \
  136. GL(dl_wait_lookup_done) ()
  137. #endif /* __ASSEMBLER__ */
  138. #endif /* tls.h */