Move all files into ports/ subdirectory in preparation for merge with glibc
[glibc.git] / ports / sysdeps / tile / nptl / tls.h
blob303a95e000fee30f01f1d35b391bbc73e6b02b27
1 /* Copyright (C) 2011 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
53 /* We use the multiple_threads field in the pthread struct */
54 #define TLS_MULTIPLE_THREADS_IN_TCB 1
56 /* Get the thread descriptor definition. */
57 # include <nptl/descr.h>
59 /* The stack_guard is accessed directly by GCC -fstack-protector code,
60 so it is a part of public ABI. The dtv and pointer_guard fields
61 are private. */
62 typedef struct
64 void *feedback_data;
65 uintptr_t pointer_guard;
66 uintptr_t stack_guard;
67 dtv_t *dtv;
68 } tcbhead_t;
70 /* This is the size of the initial TCB. Because our TCB is before the thread
71 pointer, we don't need this. */
72 # define TLS_INIT_TCB_SIZE 0
74 /* Alignment requirements for the initial TCB. */
75 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
77 /* This is the size of the TCB. Because our TCB is before the thread
78 pointer, we don't need this. */
79 # define TLS_TCB_SIZE 0
81 /* Alignment requirements for the TCB. */
82 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
84 /* This is the size we need before TCB - actually, it includes the TCB. */
85 # define TLS_PRE_TCB_SIZE \
86 (sizeof (struct pthread) \
87 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
89 /* Return the thread descriptor (tp) for the current thread. */
90 register void *__thread_pointer asm ("tp");
92 /* The thread pointer (in hardware register tp) points to the end of
93 the TCB. The pthread_descr structure is immediately in front of the TCB. */
94 # define TLS_TCB_OFFSET 0
96 /* Install the dtv pointer. The pointer passed is to the element with
97 index -1 which contain the length. */
98 # define INSTALL_DTV(tcbp, dtvp) \
99 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
101 /* Install new dtv for current thread. */
102 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
104 /* Return dtv of given thread descriptor. */
105 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
107 /* Code to initially initialize the thread pointer (tp). */
108 # define TLS_INIT_TP(tcbp, secondcall) \
109 (__thread_pointer = (char *)(tcbp) + TLS_TCB_OFFSET, NULL)
111 /* Return the address of the dtv for the current thread. */
112 # define THREAD_DTV() \
113 (((tcbhead_t *) (__thread_pointer - TLS_TCB_OFFSET))[-1].dtv)
115 /* Return the thread descriptor for the current thread. */
116 # define THREAD_SELF \
117 ((struct pthread *) (__thread_pointer \
118 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
120 /* Magic for libthread_db to know how to do THREAD_SELF. */
121 #ifdef __tilegx__
122 # define DB_THREAD_SELF \
123 REGISTER (64, 64, REG_TP * 8, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
124 #else
125 # define DB_THREAD_SELF \
126 REGISTER (32, 32, REG_TP * 4, - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
127 #endif
129 /* Read member of the thread descriptor directly. */
130 # define THREAD_GETMEM(descr, member) (descr->member)
132 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
133 # define THREAD_GETMEM_NC(descr, member, idx) \
134 (descr->member[idx])
136 /* Set member of the thread descriptor directly. */
137 # define THREAD_SETMEM(descr, member, value) \
138 (descr->member = (value))
140 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
141 # define THREAD_SETMEM_NC(descr, member, idx, value) \
142 (descr->member[idx] = (value))
144 /* Set the stack guard field in TCB head. */
145 # define THREAD_SET_STACK_GUARD(value) \
146 (((tcbhead_t *) ((char *) __thread_pointer \
147 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
148 # define THREAD_COPY_STACK_GUARD(descr) \
149 (((tcbhead_t *) ((char *) (descr) \
150 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
151 = ((tcbhead_t *) ((char *) __thread_pointer \
152 - TLS_TCB_OFFSET))[-1].stack_guard)
154 /* Set the pointer guard field in TCB head. */
155 # define THREAD_GET_POINTER_GUARD() \
156 (((tcbhead_t *) ((char *) __thread_pointer \
157 - TLS_TCB_OFFSET))[-1].pointer_guard)
158 # define THREAD_SET_POINTER_GUARD(value) \
159 (THREAD_GET_POINTER_GUARD () = (value))
160 # define THREAD_COPY_POINTER_GUARD(descr) \
161 (((tcbhead_t *) ((char *) (descr) \
162 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
163 = THREAD_GET_POINTER_GUARD())
165 /* l_tls_offset == 0 is perfectly valid on Tile, so we have to use some
166 different value to mean unset l_tls_offset. */
167 # define NO_TLS_OFFSET -1
169 /* Get and set the global scope generation counter in struct pthread. */
170 #define THREAD_GSCOPE_FLAG_UNUSED 0
171 #define THREAD_GSCOPE_FLAG_USED 1
172 #define THREAD_GSCOPE_FLAG_WAIT 2
173 #define THREAD_GSCOPE_RESET_FLAG() \
174 do \
175 { int __res \
176 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
177 THREAD_GSCOPE_FLAG_UNUSED); \
178 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
179 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
181 while (0)
182 #define THREAD_GSCOPE_SET_FLAG() \
183 do \
185 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
186 atomic_write_barrier (); \
188 while (0)
189 #define THREAD_GSCOPE_WAIT() \
190 GL(dl_wait_lookup_done) ()
192 #endif /* __ASSEMBLER__ */
194 #endif /* tls.h */