tls.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Definition for thread-local data handling. linuxthreads/SH version.
  2. Copyright (C) 2002, 2003, 2005 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. # include <pt-machine.h>
  18. #ifndef __ASSEMBLER__
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdint.h>
  22. /* Type for the dtv. */
  23. typedef union dtv
  24. {
  25. size_t counter;
  26. struct
  27. {
  28. void *val;
  29. bool is_static;
  30. } pointer;
  31. } dtv_t;
  32. #else /* __ASSEMBLER__ */
  33. # include <tcb-offsets.h>
  34. #endif /* __ASSEMBLER__ */
  35. /* We can support TLS only if the floating-stack support is available. */
  36. #if defined HAVE_TLS_SUPPORT \
  37. && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
  38. /* Signal that TLS support is available. */
  39. # define USE_TLS 1
  40. /* Include padding in _pthread_descr_struct so that libc can find p_errno,
  41. if libpthread will only include the padding because of the !IS_IN_libpthread
  42. check. */
  43. #ifndef FLOATING_STACKS
  44. # define INCLUDE_TLS_PADDING 1
  45. #endif
  46. # ifndef __ASSEMBLER__
  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 __alignof__ (tcbhead_t)
  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_descr_struct)
  60. /* Alignment requirements for the TCB. */
  61. # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  62. /* The TLS blocks start right after the TCB. */
  63. # define TLS_DTV_AT_TP 1
  64. /* Install the dtv pointer. The pointer passed is to the element with
  65. index -1 which contain the length. */
  66. # define INSTALL_DTV(tcbp, dtvp) \
  67. ((tcbhead_t *) (tcbp))->dtv = dtvp + 1
  68. /* Install new dtv for current thread. */
  69. # define INSTALL_NEW_DTV(dtv) \
  70. ({ tcbhead_t *__tcbp; \
  71. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  72. __tcbp->dtv = (dtv);})
  73. /* Return dtv of given thread descriptor. */
  74. # define GET_DTV(tcbp) \
  75. (((tcbhead_t *) (tcbp))->dtv)
  76. /* Code to initially initialize the thread pointer. This might need
  77. special attention since 'errno' is not yet available and if the
  78. operation can cause a failure 'errno' must not be touched. */
  79. # define TLS_INIT_TP(tcbp, secondcall) \
  80. ({ __asm__ __volatile__ ("ldc %0,gbr" : : "r" (tcbp)); 0; })
  81. /* Return the address of the dtv for the current thread. */
  82. # define THREAD_DTV() \
  83. ({ tcbhead_t *__tcbp; \
  84. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  85. __tcbp->dtv;})
  86. /* Return the thread descriptor for the current thread. */
  87. # undef THREAD_SELF
  88. # define THREAD_SELF \
  89. ({ struct _pthread_descr_struct *__self; \
  90. __asm__ ("stc gbr,%0" : "=r" (__self)); \
  91. __self - 1;})
  92. # undef INIT_THREAD_SELF
  93. # define INIT_THREAD_SELF(descr, nr) \
  94. ({ struct _pthread_descr_struct *__self = (void *) descr; \
  95. __asm__ __volatile__ ("ldc %0,gbr" : : "r" (__self + 1)); \
  96. 0; })
  97. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  98. /* Get the thread descriptor definition. This must be after the
  99. the definition of THREAD_SELF for TLS. */
  100. # include <linuxthreads/descr.h>
  101. # endif /* __ASSEMBLER__ */
  102. #else
  103. # ifndef __ASSEMBLER__
  104. typedef struct
  105. {
  106. void *tcb;
  107. dtv_t *dtv;
  108. void *self;
  109. int multiple_threads;
  110. } tcbhead_t;
  111. /* Get the thread descriptor definition. */
  112. # include <linuxthreads/descr.h>
  113. # define NONTLS_INIT_TP \
  114. do { \
  115. static const tcbhead_t nontls_init_tp = { .multiple_threads = 0 }; \
  116. __asm__ __volatile__ ("ldc %0,gbr" : : "r" (&nontls_init_tp)); \
  117. } while (0)
  118. # endif /* __ASSEMBLER__ */
  119. #endif /* HAVE_TLS_SUPPORT && (FLOATING_STACKS || !IS_IN_libpthread) */
  120. #endif /* tls.h */