2.5-18.1
[glibc.git] / nptl / sysdeps / sh / tls.h
blob2873fe73a83bbaf674d97021713d71e42f1e6617
1 /* Definition for thread-local data handling. NPTL/SH version.
2 Copyright (C) 2003, 2005, 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>
30 /* Type for the dtv. */
31 typedef union dtv
33 size_t counter;
34 struct
36 void *val;
37 bool is_static;
38 } pointer;
39 } dtv_t;
41 typedef struct
43 dtv_t *dtv;
44 uintptr_t pointer_guard;
45 } tcbhead_t;
47 # define TLS_MULTIPLE_THREADS_IN_TCB 1
49 #else /* __ASSEMBLER__ */
50 # include <tcb-offsets.h>
51 #endif /* __ASSEMBLER__ */
54 /* We require TLS support in the tools. */
55 #ifndef HAVE_TLS_SUPPORT
56 # error "TLS support is required."
57 #endif
59 /* Signal that TLS support is available. */
60 # define USE_TLS 1
62 #ifndef __ASSEMBLER__
64 /* Get system call information. */
65 # include <sysdep.h>
67 /* This is the size of the initial TCB. */
68 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
70 /* Alignment requirements for the initial TCB. */
71 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
73 /* This is the size of the TCB. */
74 # define TLS_TCB_SIZE sizeof (tcbhead_t)
76 /* This is the size we need before TCB. */
77 # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
79 /* Alignment requirements for the TCB. */
80 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
82 /* The TLS blocks start right after the TCB. */
83 # define TLS_DTV_AT_TP 1
85 /* Get the thread descriptor definition. */
86 # include <nptl/descr.h>
88 /* Install the dtv pointer. The pointer passed is to the element with
89 index -1 which contain the length. */
90 # define INSTALL_DTV(tcbp, dtvp) \
91 ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
93 /* Install new dtv for current thread. */
94 # define INSTALL_NEW_DTV(dtv) \
95 ({ tcbhead_t *__tcbp; \
96 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
97 __tcbp->dtv = (dtv);})
99 /* Return dtv of given thread descriptor. */
100 # define GET_DTV(tcbp) \
101 (((tcbhead_t *) (tcbp))->dtv)
103 /* Code to initially initialize the thread pointer. This might need
104 special attention since 'errno' is not yet available and if the
105 operation can cause a failure 'errno' must not be touched. */
106 # define TLS_INIT_TP(tcbp, secondcall) \
107 ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); 0; })
109 /* Return the address of the dtv for the current thread. */
110 # define THREAD_DTV() \
111 ({ tcbhead_t *__tcbp; \
112 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
113 __tcbp->dtv;})
115 /* Return the thread descriptor for the current thread.
116 The contained asm must *not* be marked volatile since otherwise
117 assignments like
118 struct pthread *self = thread_self();
119 do not get optimized away. */
120 # define THREAD_SELF \
121 ({ struct pthread *__self; \
122 __asm ("stc gbr,%0" : "=r" (__self)); \
123 __self - 1;})
125 /* Magic for libthread_db to know how to do THREAD_SELF. */
126 # define DB_THREAD_SELF \
127 REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
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) (descr->member[idx])
135 /* Set member of the thread descriptor directly. */
136 # define THREAD_SETMEM(descr, member, value) \
137 descr->member = (value)
139 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
140 # define THREAD_SETMEM_NC(descr, member, idx, value) \
141 descr->member[idx] = (value)
143 #define THREAD_GET_POINTER_GUARD() \
144 ({ tcbhead_t *__tcbp; \
145 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
146 __tcbp->pointer_guard;})
147 #define THREAD_SET_POINTER_GUARD(value) \
148 ({ tcbhead_t *__tcbp; \
149 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
150 __tcbp->pointer_guard = (value);})
151 #define THREAD_COPY_POINTER_GUARD(descr) \
152 ({ tcbhead_t *__tcbp; \
153 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
154 ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
156 /* Get and set the global scope generation counter in struct pthread. */
157 #define THREAD_GSCOPE_FLAG_UNUSED 0
158 #define THREAD_GSCOPE_FLAG_USED 1
159 #define THREAD_GSCOPE_FLAG_WAIT 2
160 #define THREAD_GSCOPE_RESET_FLAG() \
161 do \
162 { int __res \
163 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
164 THREAD_GSCOPE_FLAG_UNUSED); \
165 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
166 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
168 while (0)
169 #define THREAD_GSCOPE_SET_FLAG() \
170 do \
172 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
173 atomic_write_barrier (); \
175 while (0)
176 #define THREAD_GSCOPE_WAIT() \
177 GL(dl_wait_lookup_done) ()
179 #endif /* __ASSEMBLER__ */
181 #endif /* tls.h */