2.9
[glibc/nacl-glibc.git] / nptl / sysdeps / sh / tls.h
bloba2d4d565ff5ea8e16199666274b2432ec023de28
1 /* Definition for thread-local data handling. NPTL/SH version.
2 Copyright (C) 2003, 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
23 # include <dl-sysdep.h>
25 #ifndef __ASSEMBLER__
26 # include <stdbool.h>
27 # include <stddef.h>
28 # include <stdint.h>
29 # include <stdlib.h>
30 # include <list.h>
31 # include <sysdep.h>
32 # include <kernel-features.h>
34 /* Type for the dtv. */
35 typedef union dtv
37 size_t counter;
38 struct
40 void *val;
41 bool is_static;
42 } pointer;
43 } dtv_t;
45 typedef struct
47 dtv_t *dtv;
48 uintptr_t pointer_guard;
49 } tcbhead_t;
51 # define TLS_MULTIPLE_THREADS_IN_TCB 1
53 #else /* __ASSEMBLER__ */
54 # include <tcb-offsets.h>
55 #endif /* __ASSEMBLER__ */
58 /* We require TLS support in the tools. */
59 #ifndef HAVE_TLS_SUPPORT
60 # error "TLS support is required."
61 #endif
63 #ifndef __ASSEMBLER__
65 /* Get system call information. */
66 # include <sysdep.h>
68 /* This is the size of the initial TCB. */
69 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
71 /* Alignment requirements for the initial TCB. */
72 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
74 /* This is the size of the TCB. */
75 # define TLS_TCB_SIZE sizeof (tcbhead_t)
77 /* This is the size we need before TCB. */
78 # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
80 /* Alignment requirements for the TCB. */
81 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
83 /* The TLS blocks start right after the TCB. */
84 # define TLS_DTV_AT_TP 1
86 /* Get the thread descriptor definition. */
87 # include <nptl/descr.h>
89 /* Install the dtv pointer. The pointer passed is to the element with
90 index -1 which contain the length. */
91 # define INSTALL_DTV(tcbp, dtvp) \
92 ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
94 /* Install new dtv for current thread. */
95 # define INSTALL_NEW_DTV(dtv) \
96 ({ tcbhead_t *__tcbp; \
97 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
98 __tcbp->dtv = (dtv);})
100 /* Return dtv of given thread descriptor. */
101 # define GET_DTV(tcbp) \
102 (((tcbhead_t *) (tcbp))->dtv)
104 /* Code to initially initialize the thread pointer. This might need
105 special attention since 'errno' is not yet available and if the
106 operation can cause a failure 'errno' must not be touched. */
107 # define TLS_INIT_TP(tcbp, secondcall) \
108 ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); 0; })
110 /* Return the address of the dtv for the current thread. */
111 # define THREAD_DTV() \
112 ({ tcbhead_t *__tcbp; \
113 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
114 __tcbp->dtv;})
116 /* Return the thread descriptor for the current thread.
117 The contained asm must *not* be marked volatile since otherwise
118 assignments like
119 struct pthread *self = thread_self();
120 do not get optimized away. */
121 # define THREAD_SELF \
122 ({ struct pthread *__self; \
123 __asm ("stc gbr,%0" : "=r" (__self)); \
124 __self - 1;})
126 /* Magic for libthread_db to know how to do THREAD_SELF. */
127 # define DB_THREAD_SELF \
128 REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
130 /* Read member of the thread descriptor directly. */
131 # define THREAD_GETMEM(descr, member) (descr->member)
133 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
134 # define THREAD_GETMEM_NC(descr, member, idx) (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 #define THREAD_GET_POINTER_GUARD() \
145 ({ tcbhead_t *__tcbp; \
146 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
147 __tcbp->pointer_guard;})
148 #define THREAD_SET_POINTER_GUARD(value) \
149 ({ tcbhead_t *__tcbp; \
150 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
151 __tcbp->pointer_guard = (value);})
152 #define THREAD_COPY_POINTER_GUARD(descr) \
153 ({ tcbhead_t *__tcbp; \
154 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
155 ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
157 /* Get and set the global scope generation counter in struct pthread. */
158 #define THREAD_GSCOPE_FLAG_UNUSED 0
159 #define THREAD_GSCOPE_FLAG_USED 1
160 #define THREAD_GSCOPE_FLAG_WAIT 2
161 #define THREAD_GSCOPE_RESET_FLAG() \
162 do \
163 { int __res \
164 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
165 THREAD_GSCOPE_FLAG_UNUSED); \
166 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
167 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
169 while (0)
170 #define THREAD_GSCOPE_SET_FLAG() \
171 do \
173 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
174 atomic_write_barrier (); \
176 while (0)
177 #define THREAD_GSCOPE_WAIT() \
178 GL(dl_wait_lookup_done) ()
180 #endif /* __ASSEMBLER__ */
182 #endif /* tls.h */