tls.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Definition for thread-local data handling. NPTL/ARC version.
  2. *
  3. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  4. *
  5. * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  6. */
  7. #ifndef _TLS_H
  8. #define _TLS_H
  9. #ifndef __ASSEMBLER__
  10. # include <stdbool.h>
  11. # include <stddef.h>
  12. # include <stdint.h>
  13. # include <stdlib.h>
  14. # include <list.h>
  15. # include <sysdep.h>
  16. # include <bits/kernel-features.h>
  17. /* Type for the dtv. */
  18. typedef union dtv
  19. {
  20. size_t counter;
  21. struct
  22. {
  23. void *val;
  24. bool is_static;
  25. } pointer;
  26. } dtv_t;
  27. typedef struct
  28. {
  29. dtv_t *dtv;
  30. uintptr_t pointer_guard;
  31. } tcbhead_t;
  32. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  33. #else /* __ASSEMBLER__ */
  34. # include <tcb-offsets.h>
  35. .macro THREAD_SELF reg
  36. # struct pthread is just ahead of TCB
  37. sub \reg, r25, TLS_PRE_TCB_SIZE
  38. .endm
  39. .macro SET_TP tcb
  40. mov r25, \tcb
  41. .endm
  42. #endif /* __ASSEMBLER__ */
  43. /* We require TLS support in the tools. */
  44. #define HAVE_TLS_SUPPORT
  45. #define HAVE___THREAD 1
  46. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  47. /* Signal that TLS support is available. */
  48. # define USE_TLS 1
  49. #ifndef __ASSEMBLER__
  50. /* Get system call information. */
  51. # include <sysdep.h>
  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. #ifndef TLS_TCB_SIZE
  58. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  59. #endif
  60. /* This is the size we need before TCB. */
  61. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  62. /* Alignment requirements for the TCB. */
  63. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  64. /* The TLS blocks start right after the TCB. */
  65. # define TLS_DTV_AT_TP 1
  66. /* Get the thread descriptor definition. */
  67. # include <descr.h>
  68. /* Install the dtv pointer. The pointer passed is to the element with
  69. index -1 which contain the length. */
  70. # define INSTALL_DTV(tcbp, dtvp) \
  71. ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
  72. /* Install new dtv for current thread. */
  73. # define INSTALL_NEW_DTV(dtv) \
  74. (THREAD_DTV() = (dtv))
  75. /* Return dtv of given thread descriptor. */
  76. # define GET_DTV(tcbp) \
  77. (((tcbhead_t *) (tcbp))->dtv)
  78. /* Code to initially initialize the thread pointer. This might need
  79. special attention since 'errno' is not yet available and if the
  80. operation can cause a failure 'errno' must not be touched. */
  81. # define TLS_INIT_TP(tcbp, secondcall) \
  82. ({ \
  83. __builtin_set_thread_pointer(tcbp); \
  84. NULL; \
  85. })
  86. /* Return the address of the dtv for the current thread.
  87. TP points to TCB where 1st item is dtv pointer */
  88. # define THREAD_DTV() \
  89. (((tcbhead_t *)__builtin_thread_pointer())->dtv)
  90. /* Return the thread descriptor for the current thread.
  91. pthread sits right before TCB */
  92. # define THREAD_SELF \
  93. ((struct pthread *)__builtin_thread_pointer() - 1)
  94. /* Magic for libthread_db to know how to do THREAD_SELF. */
  95. # define DB_THREAD_SELF \
  96. CONST_THREAD_AREA (32, sizeof (struct pthread))
  97. /* Access to data in the thread descriptor is easy. */
  98. #define THREAD_GETMEM(descr, member) \
  99. descr->member
  100. #define THREAD_GETMEM_NC(descr, member, idx) \
  101. descr->member[idx]
  102. #define THREAD_SETMEM(descr, member, value) \
  103. descr->member = (value)
  104. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  105. descr->member[idx] = (value)
  106. /* Get and set the global scope generation counter in struct pthread. */
  107. #define THREAD_GSCOPE_FLAG_UNUSED 0
  108. #define THREAD_GSCOPE_FLAG_USED 1
  109. #define THREAD_GSCOPE_FLAG_WAIT 2
  110. #define THREAD_GSCOPE_RESET_FLAG() \
  111. do \
  112. { int __res \
  113. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  114. THREAD_GSCOPE_FLAG_UNUSED); \
  115. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  116. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  117. } \
  118. while (0)
  119. #define THREAD_GSCOPE_SET_FLAG() \
  120. do \
  121. { \
  122. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  123. atomic_write_barrier (); \
  124. } \
  125. while (0)
  126. #define THREAD_GSCOPE_WAIT() \
  127. GL(dl_wait_lookup_done) ()
  128. #endif /* __ASSEMBLER__ */
  129. #endif /* tls.h */