tls.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Definition for thread-local data handling. NPTL/SH version.
  2. Copyright (C) 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. #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. typedef struct
  33. {
  34. dtv_t *dtv;
  35. void *private;
  36. } tcbhead_t;
  37. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  38. #else /* __ASSEMBLER__ */
  39. # include <tcb-offsets.h>
  40. #endif /* __ASSEMBLER__ */
  41. /* We require TLS support in the tools. */
  42. #define HAVE_TLS_SUPPORT
  43. #define HAVE___THREAD 1
  44. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  45. /* Signal that TLS support is available. */
  46. # define USE_TLS 1
  47. #ifndef __ASSEMBLER__
  48. /* Get system call information. */
  49. # include <sysdep.h>
  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__ (tcbhead_t)
  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. /* The TLS blocks start right after the TCB. */
  61. # define TLS_DTV_AT_TP 1
  62. /* Get the thread descriptor definition. */
  63. # include <descr.h>
  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. The contained asm must *not* be marked volatile since otherwise
  88. assignments like
  89. struct pthread *self = thread_self();
  90. do not get optimized away. */
  91. # define THREAD_SELF \
  92. ({ struct pthread *__thread_self; \
  93. __asm ("stc gbr,%0" : "=r" (__thread_self)); \
  94. __thread_self - 1;})
  95. /* Magic for libthread_db to know how to do THREAD_SELF. */
  96. # define DB_THREAD_SELF \
  97. REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
  98. /* Read member of the thread descriptor directly. */
  99. # define THREAD_GETMEM(descr, member) (descr->member)
  100. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  101. # define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
  102. /* Set member of the thread descriptor directly. */
  103. # define THREAD_SETMEM(descr, member, value) \
  104. descr->member = (value)
  105. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  106. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  107. descr->member[idx] = (value)
  108. #endif /* __ASSEMBLER__ */
  109. #endif /* tls.h */