tls.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This file is subject to the terms and conditions of the LGPL V2.1
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2019 Kalray Inc.
  7. */
  8. #ifndef _TLS_H
  9. #define _TLS_H 1
  10. #ifndef __ASSEMBLER__
  11. # include <stdbool.h>
  12. # include <stddef.h>
  13. # include <stdint.h>
  14. /* Type for the dtv. */
  15. typedef union dtv
  16. {
  17. size_t counter;
  18. struct
  19. {
  20. void *val;
  21. bool is_static;
  22. } pointer;
  23. } dtv_t;
  24. #else /* __ASSEMBLER__ */
  25. # include <tcb-offsets.h>
  26. #endif /* __ASSEMBLER__ */
  27. /* We require TLS support in the tools. */
  28. #define HAVE_TLS_SUPPORT 1
  29. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  30. #define HAVE___THREAD 1
  31. /* Signal that TLS support is available. */
  32. #define USE_TLS 1
  33. #ifndef __ASSEMBLER__
  34. /* Get system call information. */
  35. # include <sysdep.h>
  36. /* The TP points to the start of the thread blocks. */
  37. # define TLS_DTV_AT_TP 1
  38. /* Get the thread descriptor definition. */
  39. # include <../../descr.h>
  40. typedef struct
  41. {
  42. dtv_t *dtv;
  43. } tcbhead_t;
  44. /* Thread Pointer $tp is $r13 */
  45. register tcbhead_t *__thread_self __asm__("$r13");
  46. /* This is the size of the initial TCB. */
  47. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  48. /* Alignment requirements for the initial TCB. */
  49. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  50. /* This is the size of the TCB. */
  51. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  52. /* This is the size we need before TCB. */
  53. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  54. /* Alignment requirements for the TCB. */
  55. # define TLS_TCB_ALIGN __alignof__ (tcbhead_t)
  56. /* Install the dtv pointer. The pointer passed is to the element with
  57. index -1 which contain the length. */
  58. # define INSTALL_DTV(tcbp, dtvp) \
  59. (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
  60. /* Install new dtv for current thread. */
  61. # define INSTALL_NEW_DTV(dtv) \
  62. (THREAD_DTV() = (dtv))
  63. /* Return dtv of given thread descriptor. */
  64. # define GET_DTV(tcbp) \
  65. (((tcbhead_t *) (tcbp))->dtv)
  66. /* Code to initially initialize the thread pointer.
  67. *
  68. * Set TP to the address _after_ tcbhead_t. This will allow us
  69. * to change the size of tcbhead_t without having to re-link everything.
  70. *
  71. * secondcall has something to do with USE__THREAD,
  72. * seems to always be 0 so we don't care about it.
  73. *
  74. * This has to return NULL on success (or a string with the failure text).
  75. * It's hard to fail this, so return NULL always.
  76. */
  77. # define TLS_INIT_TP(tcbp, secondcall) \
  78. ({__thread_self = ((tcbhead_t *)tcbp + 1); NULL;})
  79. /* Return the address of the dtv for the current thread.
  80. *
  81. * Dereference TP, offset to dtv - really straightforward.
  82. * Remember that we made TP point to after tcb, so we need to reverse that.
  83. */
  84. # define THREAD_DTV() \
  85. ((((tcbhead_t *)__thread_self)-1)->dtv)
  86. /* Return the thread descriptor for the current thread.
  87. *
  88. * Return a pointer to the TLS_PRE area where we allocated space for
  89. * a struct pthread. Again, TP points to after tcbhead_t, compensate with
  90. * TLS_INIT_TCB_SIZE.
  91. *
  92. * I regard this is a seperate system from the "normal" TLS.
  93. */
  94. # define THREAD_SELF \
  95. ((struct pthread *) ((char *) __thread_self - TLS_INIT_TCB_SIZE \
  96. - TLS_PRE_TCB_SIZE))
  97. /* Magic for libthread_db to know how to do THREAD_SELF. */
  98. # define DB_THREAD_SELF \
  99. CONST_THREAD_AREA (64, sizeof (struct pthread))
  100. /* Access to data in the thread descriptor is easy. */
  101. # define THREAD_GETMEM(descr, member) \
  102. descr->member
  103. # define THREAD_GETMEM_NC(descr, member, idx) \
  104. descr->member[idx]
  105. # define THREAD_SETMEM(descr, member, value) \
  106. descr->member = (value)
  107. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  108. descr->member[idx] = (value)
  109. /* Get and set the global scope generation counter in struct pthread. */
  110. # define THREAD_GSCOPE_FLAG_UNUSED 0
  111. # define THREAD_GSCOPE_FLAG_USED 1
  112. # define THREAD_GSCOPE_FLAG_WAIT 2
  113. # define THREAD_GSCOPE_RESET_FLAG() \
  114. do \
  115. { int __res \
  116. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  117. THREAD_GSCOPE_FLAG_UNUSED); \
  118. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  119. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  120. } \
  121. while (0)
  122. # define THREAD_GSCOPE_SET_FLAG() \
  123. do \
  124. { \
  125. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  126. atomic_write_barrier (); \
  127. } \
  128. while (0)
  129. # define THREAD_GSCOPE_WAIT() \
  130. GL(dl_wait_lookup_done) ()
  131. # endif /* __ASSEMBLER__ */
  132. #endif /* tls.h */