2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / ia64 / tls.h
blob936ff01a72a79900230af7f1e90887492bbc0c4e
1 /* Definition for thread-local data handling. nptl/IA-64 version.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _TLS_H
21 #define _TLS_H 1
23 #include <dl-sysdep.h>
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <stdlib.h>
29 # include <list.h>
32 /* Type for the dtv. */
33 typedef union dtv
35 size_t counter;
36 struct
38 void *val;
39 bool is_static;
40 } pointer;
41 } dtv_t;
44 typedef struct
46 dtv_t *dtv;
47 void *__private;
48 } tcbhead_t;
50 register struct pthread *__thread_self __asm__("r13");
52 # define TLS_MULTIPLE_THREADS_IN_TCB 1
54 #else /* __ASSEMBLER__ */
55 # include <tcb-offsets.h>
56 #endif
59 /* We require TLS support in the tools. */
60 #ifndef HAVE_TLS_SUPPORT
61 # error "TLS support is required."
62 #endif
64 /* Alignment requirement for the stack. */
65 #define STACK_ALIGN 16
67 #ifndef __ASSEMBLER__
68 /* Get system call information. */
69 # include <sysdep.h>
71 /* This is the size of the initial TCB. */
72 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
74 /* Alignment requirements for the initial TCB. */
75 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
77 /* This is the size of the TCB. */
78 # define TLS_TCB_SIZE sizeof (tcbhead_t)
80 /* This is the size we need before TCB.
81 If there is not any room for uintptr_t stack_guard and
82 uintptr_t pointer_guard in struct pthread's final padding,
83 we need to put struct pthread 16 byte slower. */
84 # define TLS_PRE_TCB_SIZE \
85 (sizeof (struct pthread) \
86 + (PTHREAD_STRUCT_END_PADDING < 2 * sizeof (uintptr_t) \
87 ? ((2 * sizeof (uintptr_t) + __alignof__ (struct pthread) - 1) \
88 & ~(__alignof__ (struct pthread) - 1)) \
89 : 0))
91 /* Alignment requirements for the TCB. */
92 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
94 /* The DTV is allocated at the TP; the TCB is placed elsewhere. */
95 # define TLS_DTV_AT_TP 1
97 /* Get the thread descriptor definition. */
98 # include <nptl/descr.h>
100 /* Install the dtv pointer. The pointer passed is to the element with
101 index -1 which contain the length. */
102 # define INSTALL_DTV(descr, dtvp) \
103 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
105 /* Install new dtv for current thread. */
106 # define INSTALL_NEW_DTV(DTV) \
107 (((tcbhead_t *)__thread_self)->dtv = (DTV))
109 /* Return dtv of given thread descriptor. */
110 # define GET_DTV(descr) \
111 (((tcbhead_t *) (descr))->dtv)
113 #define THREAD_SELF_SYSINFO (((tcbhead_t *) __thread_self)->__private)
114 #define THREAD_SYSINFO(pd) \
115 (((tcbhead_t *) ((char *) (pd) + TLS_PRE_TCB_SIZE))->__private)
117 #if defined NEED_DL_SYSINFO
118 # define INIT_SYSINFO THREAD_SELF_SYSINFO = (void *) GLRO(dl_sysinfo)
119 #else
120 # define INIT_SYSINFO NULL
121 #endif
123 /* Code to initially initialize the thread pointer. This might need
124 special attention since 'errno' is not yet available and if the
125 operation can cause a failure 'errno' must not be touched. */
126 # define TLS_INIT_TP(thrdescr, secondcall) \
127 (__thread_self = (thrdescr), INIT_SYSINFO, NULL)
129 /* Return the address of the dtv for the current thread. */
130 # define THREAD_DTV() \
131 (((tcbhead_t *)__thread_self)->dtv)
133 /* Return the thread descriptor for the current thread. */
134 # define THREAD_SELF \
135 ((struct pthread *) ((char *) __thread_self - TLS_PRE_TCB_SIZE))
137 /* Magic for libthread_db to know how to do THREAD_SELF. */
138 # define DB_THREAD_SELF REGISTER (64, 64, 13 * 8, -TLS_PRE_TCB_SIZE)
140 /* Access to data in the thread descriptor is easy. */
141 #define THREAD_GETMEM(descr, member) \
142 descr->member
143 #define THREAD_GETMEM_NC(descr, member, idx) \
144 descr->member[idx]
145 #define THREAD_SETMEM(descr, member, value) \
146 descr->member = (value)
147 #define THREAD_SETMEM_NC(descr, member, idx, value) \
148 descr->member[idx] = (value)
150 /* Set the stack guard field in TCB head. */
151 #define THREAD_SET_STACK_GUARD(value) \
152 (((uintptr_t *) __thread_self)[-1] = (value))
153 #define THREAD_COPY_STACK_GUARD(descr) \
154 (((uintptr_t *) ((char *) (descr) + TLS_PRE_TCB_SIZE))[-1] \
155 = ((uintptr_t *) __thread_self)[-1])
157 /* Set the pointer guard field in TCB head. */
158 #define THREAD_GET_POINTER_GUARD() \
159 (((uintptr_t *) __thread_self)[-2])
160 #define THREAD_SET_POINTER_GUARD(value) \
161 (((uintptr_t *) __thread_self)[-2] = (value))
162 #define THREAD_COPY_POINTER_GUARD(descr) \
163 (((uintptr_t *) ((char *) (descr) + TLS_PRE_TCB_SIZE))[-2] \
164 = THREAD_GET_POINTER_GUARD ())
166 /* Get and set the global scope generation counter in struct pthread. */
167 #define THREAD_GSCOPE_FLAG_UNUSED 0
168 #define THREAD_GSCOPE_FLAG_USED 1
169 #define THREAD_GSCOPE_FLAG_WAIT 2
170 #define THREAD_GSCOPE_RESET_FLAG() \
171 do \
172 { int __res \
173 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
174 THREAD_GSCOPE_FLAG_UNUSED); \
175 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
176 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
178 while (0)
179 #define THREAD_GSCOPE_SET_FLAG() \
180 do \
182 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
183 atomic_write_barrier (); \
185 while (0)
186 #define THREAD_GSCOPE_WAIT() \
187 GL(dl_wait_lookup_done) ()
189 #endif /* __ASSEMBLER__ */
191 #endif /* tls.h */