]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/nios2/nptl/tls.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / nios2 / nptl / tls.h
1 /* Definition for thread-local data handling. NPTL/Nios II version.
2 Copyright (C) 2012-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #ifndef _TLS_H
20 #define _TLS_H 1
21
22 #include <dl-sysdep.h>
23
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28
29 /* Type for the dtv. */
30 typedef union dtv
31 {
32 size_t counter;
33 struct
34 {
35 void *val;
36 bool is_static;
37 } pointer;
38 } dtv_t;
39
40 #else /* __ASSEMBLER__ */
41 # include <tcb-offsets.h>
42 #endif /* __ASSEMBLER__ */
43
44
45 #ifndef __ASSEMBLER__
46
47 /* Get system call information. */
48 # include <sysdep.h>
49
50 /* The TP points to the start of the thread blocks. */
51 # define TLS_DTV_AT_TP 1
52 # define TLS_TCB_AT_TP 0
53
54 /* Get the thread descriptor definition. */
55 # include <nptl/descr.h>
56
57 typedef struct
58 {
59 dtv_t *dtv;
60 uintptr_t pointer_guard;
61 unsigned spare[6];
62 } tcbhead_t;
63
64 register struct pthread *__thread_self __asm__("r23");
65
66 #define READ_THREAD_POINTER() ((void *) __thread_self)
67
68 /* This is the size of the initial TCB. Because our TCB is before the thread
69 pointer, we don't need this. */
70 # define TLS_INIT_TCB_SIZE 0
71
72 /* Alignment requirements for the initial TCB. */
73 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
74
75 /* This is the size of the TCB. Because our TCB is before the thread
76 pointer, we don't need this. */
77 # define TLS_TCB_SIZE 0
78
79 /* Alignment requirements for the TCB. */
80 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
81
82 /* This is the size we need before TCB - actually, it includes the TCB. */
83 # define TLS_PRE_TCB_SIZE \
84 (sizeof (struct pthread) \
85 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
86
87 /* The thread pointer (in hardware register r23) points to the end of
88 the TCB + 0x7000, as for PowerPC and MIPS. */
89 # define TLS_TCB_OFFSET 0x7000
90
91 /* Install the dtv pointer. The pointer passed is to the element with
92 index -1 which contain the length. */
93 # define INSTALL_DTV(tcbp, dtvp) \
94 (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
95
96 /* Install new dtv for current thread. */
97 # define INSTALL_NEW_DTV(dtv) \
98 (THREAD_DTV() = (dtv))
99
100 /* Return dtv of given thread descriptor. */
101 # define GET_DTV(tcbp) \
102 (((tcbhead_t *) (tcbp))[-1].dtv)
103
104 /* Code to initially initialize the thread pointer. */
105 # define TLS_INIT_TP(tcbp) \
106 (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
107
108 /* Value passed to 'clone' for initialization of the thread register. */
109 # define TLS_DEFINE_INIT_TP(tp, pd) \
110 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
111
112 /* Return the address of the dtv for the current thread. */
113 # define THREAD_DTV() \
114 (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
115
116 /* Return the thread descriptor for the current thread. */
117 # define THREAD_SELF \
118 ((struct pthread *) (READ_THREAD_POINTER () \
119 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
120
121 /* Magic for libthread_db to know how to do THREAD_SELF. */
122 # define DB_THREAD_SELF \
123 REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
124
125 /* Access to data in the thread descriptor is easy. */
126 # define THREAD_GETMEM(descr, member) \
127 descr->member
128 # define THREAD_GETMEM_NC(descr, member, idx) \
129 descr->member[idx]
130 # define THREAD_SETMEM(descr, member, value) \
131 descr->member = (value)
132 # define THREAD_SETMEM_NC(descr, member, idx, value) \
133 descr->member[idx] = (value)
134
135 # define THREAD_GET_POINTER_GUARD() \
136 (((tcbhead_t *) (READ_THREAD_POINTER () \
137 - TLS_TCB_OFFSET))[-1].pointer_guard)
138 # define THREAD_SET_POINTER_GUARD(value) \
139 (THREAD_GET_POINTER_GUARD () = (value))
140 # define THREAD_COPY_POINTER_GUARD(descr) \
141 (((tcbhead_t *) ((void *) (descr) \
142 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
143 = THREAD_GET_POINTER_GUARD())
144
145 /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
146 different value to mean unset l_tls_offset. */
147 # define NO_TLS_OFFSET -1
148
149 /* Get and set the global scope generation counter in struct pthread. */
150 #define THREAD_GSCOPE_FLAG_UNUSED 0
151 #define THREAD_GSCOPE_FLAG_USED 1
152 #define THREAD_GSCOPE_FLAG_WAIT 2
153 #define THREAD_GSCOPE_RESET_FLAG() \
154 do \
155 { int __res \
156 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
157 THREAD_GSCOPE_FLAG_UNUSED); \
158 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
159 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
160 } \
161 while (0)
162 #define THREAD_GSCOPE_SET_FLAG() \
163 do \
164 { \
165 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
166 atomic_write_barrier (); \
167 } \
168 while (0)
169 #define THREAD_GSCOPE_WAIT() \
170 GL(dl_wait_lookup_done) ()
171
172 #endif /* __ASSEMBLER__ */
173
174 #endif /* tls.h */