Update copyright notices with scripts/update-copyrights
[glibc.git] / nptl / sysdeps / sh / tls.h
blob59e49f743ce541fe63c440b85b18d8225a33ee7c
1 /* Definition for thread-local data handling. NPTL/SH version.
2 Copyright (C) 2003-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _TLS_H
20 #define _TLS_H
22 # 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>
30 # include <sysdep.h>
31 # include <kernel-features.h>
33 /* Type for the dtv. */
34 typedef union dtv
36 size_t counter;
37 struct
39 void *val;
40 bool is_static;
41 } pointer;
42 } dtv_t;
44 typedef struct
46 dtv_t *dtv;
47 uintptr_t pointer_guard;
48 } tcbhead_t;
50 # define TLS_MULTIPLE_THREADS_IN_TCB 1
52 #else /* __ASSEMBLER__ */
53 # include <tcb-offsets.h>
54 #endif /* __ASSEMBLER__ */
57 #ifndef __ASSEMBLER__
59 /* Get system call information. */
60 # include <sysdep.h>
62 /* This is the size of the initial TCB. */
63 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
65 /* Alignment requirements for the initial TCB. */
66 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
68 /* This is the size of the TCB. */
69 # define TLS_TCB_SIZE sizeof (tcbhead_t)
71 /* This is the size we need before TCB. */
72 # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
74 /* Alignment requirements for the TCB. */
75 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
77 /* The TLS blocks start right after the TCB. */
78 # define TLS_DTV_AT_TP 1
80 /* Get the thread descriptor definition. */
81 # include <nptl/descr.h>
83 /* Install the dtv pointer. The pointer passed is to the element with
84 index -1 which contain the length. */
85 # define INSTALL_DTV(tcbp, dtvp) \
86 ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
88 /* Install new dtv for current thread. */
89 # define INSTALL_NEW_DTV(dtv) \
90 ({ tcbhead_t *__tcbp; \
91 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
92 __tcbp->dtv = (dtv);})
94 /* Return dtv of given thread descriptor. */
95 # define GET_DTV(tcbp) \
96 (((tcbhead_t *) (tcbp))->dtv)
98 /* Code to initially initialize the thread pointer. This might need
99 special attention since 'errno' is not yet available and if the
100 operation can cause a failure 'errno' must not be touched. */
101 # define TLS_INIT_TP(tcbp, secondcall) \
102 ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); 0; })
104 /* Return the address of the dtv for the current thread. */
105 # define THREAD_DTV() \
106 ({ tcbhead_t *__tcbp; \
107 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
108 __tcbp->dtv;})
110 /* Return the thread descriptor for the current thread.
111 The contained asm must *not* be marked volatile since otherwise
112 assignments like
113 struct pthread *self = thread_self();
114 do not get optimized away. */
115 # define THREAD_SELF \
116 ({ struct pthread *__self; \
117 __asm ("stc gbr,%0" : "=r" (__self)); \
118 __self - 1;})
120 /* Magic for libthread_db to know how to do THREAD_SELF. */
121 # define DB_THREAD_SELF \
122 REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
124 /* Read member of the thread descriptor directly. */
125 # define THREAD_GETMEM(descr, member) (descr->member)
127 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
128 # define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
130 /* Set member of the thread descriptor directly. */
131 # define THREAD_SETMEM(descr, member, value) \
132 descr->member = (value)
134 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
135 # define THREAD_SETMEM_NC(descr, member, idx, value) \
136 descr->member[idx] = (value)
138 #define THREAD_GET_POINTER_GUARD() \
139 ({ tcbhead_t *__tcbp; \
140 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
141 __tcbp->pointer_guard;})
142 #define THREAD_SET_POINTER_GUARD(value) \
143 ({ tcbhead_t *__tcbp; \
144 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
145 __tcbp->pointer_guard = (value);})
146 #define THREAD_COPY_POINTER_GUARD(descr) \
147 ({ tcbhead_t *__tcbp; \
148 __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
149 ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
151 /* Get and set the global scope generation counter in struct pthread. */
152 #define THREAD_GSCOPE_FLAG_UNUSED 0
153 #define THREAD_GSCOPE_FLAG_USED 1
154 #define THREAD_GSCOPE_FLAG_WAIT 2
155 #define THREAD_GSCOPE_RESET_FLAG() \
156 do \
157 { int __res \
158 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
159 THREAD_GSCOPE_FLAG_UNUSED); \
160 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
161 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
163 while (0)
164 #define THREAD_GSCOPE_SET_FLAG() \
165 do \
167 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
168 atomic_write_barrier (); \
170 while (0)
171 #define THREAD_GSCOPE_WAIT() \
172 GL(dl_wait_lookup_done) ()
174 #endif /* __ASSEMBLER__ */
176 #endif /* tls.h */