tls.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Definition for thread-local data handling. NPTL/NDS32 version.
  2. Copyright (C) 2005, 2007 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. /* Get system call information. */
  41. # include <sysdep.h>
  42. /* The TP points to the start of the thread blocks. */
  43. # define TLS_DTV_AT_TP 1
  44. /* We use the multiple_threads field in the pthread struct */
  45. #define TLS_MULTIPLE_THREADS_IN_TCB 1
  46. /* Get the thread descriptor definition. */
  47. # include <../../descr.h>
  48. /* The stack_guard is accessed directly by GCC -fstack-protector code,
  49. so it is a part of public ABI. The dtv and pointer_guard fields
  50. are private. */
  51. typedef struct
  52. {
  53. dtv_t *dtv;
  54. void *private;
  55. } tcbhead_t;
  56. /* This is the size of the initial TCB. */
  57. # define TLS_INIT_TCB_SIZE 0
  58. /* Alignment requirements for the initial TCB. */
  59. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  60. /* This is the size of the TCB. */
  61. # define TLS_TCB_SIZE 0
  62. /* Alignment requirements for the TCB. */
  63. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  64. /* This is the size we need before TCB - actually, it includes the TCB. */
  65. # define TLS_PRE_TCB_SIZE \
  66. (sizeof (struct pthread) \
  67. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  68. /* Return the thread descriptor (tp) for the current thread. */
  69. register void *__thread_pointer __asm__ ("$r25");
  70. /* The thread pointer (in hardware register tp) points to the end of
  71. the TCB. The pthread_descr structure is immediately in front of the TCB. */
  72. #ifndef TLS_TCB_OFFSET
  73. # define TLS_TCB_OFFSET 0
  74. #endif
  75. /* Install the dtv pointer. The pointer passed is to the element with
  76. index -1 which contain the length. */
  77. # define INSTALL_DTV(tcbp, dtvp) \
  78. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  79. /* Install new dtv for current thread. */
  80. # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
  81. /* Return dtv of given thread descriptor. */
  82. # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
  83. /* Code to initially initialize the thread pointer (tp). */
  84. # define TLS_INIT_TP(tcbp, secondcall) \
  85. (__thread_pointer = (char *)(tcbp) + TLS_TCB_OFFSET, NULL)
  86. /* Return the address of the dtv for the current thread. */
  87. # define THREAD_DTV() \
  88. (((tcbhead_t *) (__thread_pointer - TLS_TCB_OFFSET))[-1].dtv)
  89. /* Return the thread descriptor for the current thread. */
  90. # define THREAD_SELF \
  91. ((struct pthread *) (__thread_pointer \
  92. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  93. /* Magic for libthread_db to know how to do THREAD_SELF. */
  94. # define DB_THREAD_SELF \
  95. REGISTER (32, 32, 152, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
  96. /* Read member of the thread descriptor directly. */
  97. # define THREAD_GETMEM(descr, member) (descr->member)
  98. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  99. # define THREAD_GETMEM_NC(descr, member, idx) \
  100. (descr->member[idx])
  101. /* Set member of the thread descriptor directly. */
  102. # define THREAD_SETMEM(descr, member, value) \
  103. (descr->member = (value))
  104. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  105. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  106. (descr->member[idx] = (value))
  107. /* l_tls_offset == 0 is perfectly valid on Tile, so we have to use some
  108. different value to mean unset l_tls_offset. */
  109. # define NO_TLS_OFFSET -1
  110. /* Get and set the global scope generation counter in struct pthread. */
  111. #define THREAD_GSCOPE_FLAG_UNUSED 0
  112. #define THREAD_GSCOPE_FLAG_USED 1
  113. #define THREAD_GSCOPE_FLAG_WAIT 2
  114. #define THREAD_GSCOPE_RESET_FLAG() \
  115. do \
  116. { int __res \
  117. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  118. THREAD_GSCOPE_FLAG_UNUSED); \
  119. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  120. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  121. } \
  122. while (0)
  123. #define THREAD_GSCOPE_SET_FLAG() \
  124. do \
  125. { \
  126. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  127. atomic_write_barrier (); \
  128. } \
  129. while (0)
  130. #define THREAD_GSCOPE_WAIT() \
  131. GL(dl_wait_lookup_done) ()
  132. #endif /* __ASSEMBLER__ */
  133. #endif /* tls.h */