tls.h 4.5 KB

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