]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/sh/nptl/tls.h
5681792c16bf476812fa204a2b4cb337c7ba4e36
[thirdparty/glibc.git] / sysdeps / sh / nptl / tls.h
1 /* Definition for thread-local data handling. NPTL/SH version.
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifndef _TLS_H
20 #define _TLS_H
21
22 # include <dl-sysdep.h>
23
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <stdlib.h>
29 # include <list.h>
30 # include <sysdep.h>
31 # include <dl-dtv.h>
32
33 typedef struct
34 {
35 dtv_t *dtv;
36 uintptr_t pointer_guard;
37 } tcbhead_t;
38
39 # define TLS_MULTIPLE_THREADS_IN_TCB 1
40
41 #else /* __ASSEMBLER__ */
42 # include <tcb-offsets.h>
43 #endif /* __ASSEMBLER__ */
44
45
46 #ifndef __ASSEMBLER__
47
48 /* Get system call information. */
49 # include <sysdep.h>
50
51 /* This is the size of the initial TCB. */
52 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
53
54 /* Alignment requirements for the initial TCB. */
55 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
56
57 /* This is the size of the TCB. */
58 # define TLS_TCB_SIZE sizeof (tcbhead_t)
59
60 /* This is the size we need before TCB. */
61 # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
62
63 /* Alignment requirements for the TCB. */
64 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
65
66 /* The TLS blocks start right after the TCB. */
67 # define TLS_DTV_AT_TP 1
68 # define TLS_TCB_AT_TP 0
69
70 /* Get the thread descriptor definition. */
71 # include <nptl/descr.h>
72
73 /* Install the dtv pointer. The pointer passed is to the element with
74 index -1 which contain the length. */
75 # define INSTALL_DTV(tcbp, dtvp) \
76 ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
77
78 /* Install new dtv for current thread. */
79 # define INSTALL_NEW_DTV(dtv) \
80 ({ tcbhead_t *__tcbp; \
81 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
82 __tcbp->dtv = (dtv);})
83
84 /* Return dtv of given thread descriptor. */
85 # define GET_DTV(tcbp) \
86 (((tcbhead_t *) (tcbp))->dtv)
87
88 /* Code to initially initialize the thread pointer. This might need
89 special attention since 'errno' is not yet available and if the
90 operation can cause a failure 'errno' must not be touched. */
91 # define TLS_INIT_TP(tcbp) \
92 ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); NULL; })
93
94 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
95
96 /* Return the address of the dtv for the current thread. */
97 # define THREAD_DTV() \
98 ({ tcbhead_t *__tcbp; \
99 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
100 __tcbp->dtv;})
101
102 /* Return the thread descriptor for the current thread.
103 The contained asm must *not* be marked volatile since otherwise
104 assignments like
105 struct pthread *self = thread_self();
106 do not get optimized away. */
107 # define THREAD_SELF \
108 ({ struct pthread *__self; \
109 __asm ("stc gbr,%0" : "=r" (__self)); \
110 __self - 1;})
111
112 /* Magic for libthread_db to know how to do THREAD_SELF. */
113 # define DB_THREAD_SELF \
114 REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
115
116 /* Read member of the thread descriptor directly. */
117 # define THREAD_GETMEM(descr, member) (descr->member)
118
119 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
120 # define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
121
122 /* Set member of the thread descriptor directly. */
123 # define THREAD_SETMEM(descr, member, value) \
124 descr->member = (value)
125
126 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
127 # define THREAD_SETMEM_NC(descr, member, idx, value) \
128 descr->member[idx] = (value)
129
130 #define THREAD_GET_POINTER_GUARD() \
131 ({ tcbhead_t *__tcbp; \
132 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
133 __tcbp->pointer_guard;})
134 #define THREAD_SET_POINTER_GUARD(value) \
135 ({ tcbhead_t *__tcbp; \
136 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
137 __tcbp->pointer_guard = (value);})
138 #define THREAD_COPY_POINTER_GUARD(descr) \
139 ({ tcbhead_t *__tcbp; \
140 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
141 ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
142
143 /* Get and set the global scope generation counter in struct pthread. */
144 #define THREAD_GSCOPE_IN_TCB 1
145 #define THREAD_GSCOPE_FLAG_UNUSED 0
146 #define THREAD_GSCOPE_FLAG_USED 1
147 #define THREAD_GSCOPE_FLAG_WAIT 2
148 #define THREAD_GSCOPE_RESET_FLAG() \
149 do \
150 { int __res \
151 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
152 THREAD_GSCOPE_FLAG_UNUSED); \
153 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
154 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
155 } \
156 while (0)
157 #define THREAD_GSCOPE_SET_FLAG() \
158 do \
159 { \
160 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
161 atomic_write_barrier (); \
162 } \
163 while (0)
164 #define THREAD_GSCOPE_WAIT() \
165 GL(dl_wait_lookup_done) ()
166
167 #endif /* __ASSEMBLER__ */
168
169 #endif /* tls.h */