Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / tile / nptl / tls.h
blob08e1d54346a94c07069e4d006f96bfeb09d6a3e3
1 /* Copyright (C) 2011-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
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.
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.
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/>. */
19 #ifndef _TLS_H
20 #define _TLS_H 1
22 # include <dl-sysdep.h>
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
29 /* Type for the dtv. */
30 typedef union dtv
32 size_t counter;
33 struct
35 void *val;
36 bool is_static;
37 } pointer;
38 } dtv_t;
40 #else /* __ASSEMBLER__ */
41 # include <tcb-offsets.h>
42 #endif /* __ASSEMBLER__ */
45 #ifndef __ASSEMBLER__
47 /* Get system call information. */
48 # include <sysdep.h>
50 /* The TP points to the start of the thread blocks. */
51 # define TLS_DTV_AT_TP 1
52 # define TLS_TCB_AT_TP 0
54 /* We use the multiple_threads field in the pthread struct */
55 #define TLS_MULTIPLE_THREADS_IN_TCB 1
57 /* Get the thread descriptor definition. */
58 # include <nptl/descr.h>
60 /* The stack_guard is accessed directly by GCC -fstack-protector code,
61 so it is a part of public ABI. The dtv and pointer_guard fields
62 are private. */
63 typedef struct
65 void *feedback_data;
66 uintptr_t pointer_guard;
67 uintptr_t stack_guard;
68 dtv_t *dtv;
69 } tcbhead_t;
71 /* This is the size of the initial TCB. Because our TCB is before the thread
72 pointer, we don't need this. */
73 # define TLS_INIT_TCB_SIZE 0
75 /* Alignment requirements for the initial TCB. */
76 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
78 /* This is the size of the TCB. Because our TCB is before the thread
79 pointer, we don't need this. */
80 # define TLS_TCB_SIZE 0
82 /* Alignment requirements for the TCB. */
83 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
85 /* This is the size we need before TCB - actually, it includes the TCB. */
86 # define TLS_PRE_TCB_SIZE \
87 (sizeof (struct pthread) \
88 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
90 /* Return the thread descriptor (tp) for the current thread. */
91 register void *__thread_pointer asm ("tp");
93 /* The thread pointer (in hardware register tp) points to the end of
94 the TCB. The pthread_descr structure is immediately in front of the TCB. */
95 # define TLS_TCB_OFFSET 0
97 /* Install the dtv pointer. The pointer passed is to the element with
98 index -1 which contain the length. */
99 # define INSTALL_DTV(tcbp, dtvp) \
100 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
102 /* Install new dtv for current thread. */
103 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
105 /* Return dtv of given thread descriptor. */
106 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
108 /* Code to initially initialize the thread pointer (tp). */
109 # define TLS_INIT_TP(tcbp) \
110 (__thread_pointer = (char *)(tcbp) + TLS_TCB_OFFSET, NULL)
112 /* Value passed to 'clone' for initialization of the thread register. */
113 # define TLS_DEFINE_INIT_TP(tp, pd) \
114 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
116 /* Return the address of the dtv for the current thread. */
117 # define THREAD_DTV() \
118 (((tcbhead_t *) (__thread_pointer - TLS_TCB_OFFSET))[-1].dtv)
120 /* Return the thread descriptor for the current thread. */
121 # define THREAD_SELF \
122 ((struct pthread *) (__thread_pointer \
123 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
125 /* Magic for libthread_db to know how to do THREAD_SELF. */
126 #ifdef __tilegx__
127 # define DB_THREAD_SELF \
128 REGISTER (64, 64, REG_TP * 8, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
129 #else
130 # define DB_THREAD_SELF \
131 REGISTER (32, 32, REG_TP * 4, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
132 #endif
134 /* Read member of the thread descriptor directly. */
135 # define THREAD_GETMEM(descr, member) (descr->member)
137 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
138 # define THREAD_GETMEM_NC(descr, member, idx) \
139 (descr->member[idx])
141 /* Set member of the thread descriptor directly. */
142 # define THREAD_SETMEM(descr, member, value) \
143 (descr->member = (value))
145 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
146 # define THREAD_SETMEM_NC(descr, member, idx, value) \
147 (descr->member[idx] = (value))
149 /* Set the stack guard field in TCB head. */
150 # define THREAD_SET_STACK_GUARD(value) \
151 (((tcbhead_t *) ((char *) __thread_pointer \
152 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
153 # define THREAD_COPY_STACK_GUARD(descr) \
154 (((tcbhead_t *) ((char *) (descr) \
155 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
156 = ((tcbhead_t *) ((char *) __thread_pointer \
157 - TLS_TCB_OFFSET))[-1].stack_guard)
159 /* Set the pointer guard field in TCB head. */
160 # define THREAD_GET_POINTER_GUARD() \
161 (((tcbhead_t *) ((char *) __thread_pointer \
162 - TLS_TCB_OFFSET))[-1].pointer_guard)
163 # define THREAD_SET_POINTER_GUARD(value) \
164 (THREAD_GET_POINTER_GUARD () = (value))
165 # define THREAD_COPY_POINTER_GUARD(descr) \
166 (((tcbhead_t *) ((char *) (descr) \
167 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
168 = THREAD_GET_POINTER_GUARD())
170 /* l_tls_offset == 0 is perfectly valid on Tile, so we have to use some
171 different value to mean unset l_tls_offset. */
172 # define NO_TLS_OFFSET -1
174 /* Get and set the global scope generation counter in struct pthread. */
175 #define THREAD_GSCOPE_FLAG_UNUSED 0
176 #define THREAD_GSCOPE_FLAG_USED 1
177 #define THREAD_GSCOPE_FLAG_WAIT 2
178 #define THREAD_GSCOPE_RESET_FLAG() \
179 do \
180 { int __res \
181 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
182 THREAD_GSCOPE_FLAG_UNUSED); \
183 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
184 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
186 while (0)
187 #define THREAD_GSCOPE_SET_FLAG() \
188 do \
190 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
191 atomic_write_barrier (); \
193 while (0)
194 #define THREAD_GSCOPE_WAIT() \
195 GL(dl_wait_lookup_done) ()
197 #endif /* __ASSEMBLER__ */
199 #endif /* tls.h */