Updated to fedora-glibc-20050208T0948
[glibc.git] / linuxthreads / sysdeps / powerpc / tls.h
blob35472081d6467457aa117760341de68e61664fc5
1 /* Definitions for thread-local data handling. linuxthreads/PPC version.
2 Copyright (C) 2003, 2005 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 #ifndef __ASSEMBLER__
25 # include <pt-machine.h>
26 # include <stdbool.h>
27 # include <stddef.h>
29 /* Type for the dtv. */
30 typedef union dtv
32 size_t counter;
33 struct
35 void *val;
36 bool is_static;
37 } pointer;
38 } dtv_t;
40 #else /* __ASSEMBLER__ */
41 # include <tcb-offsets.h>
42 #endif /* __ASSEMBLER__ */
44 #ifdef HAVE_TLS_SUPPORT
46 /* Signal that TLS support is available. */
47 # define USE_TLS 1
49 # ifndef __ASSEMBLER__
51 /* This layout is actually wholly private and not affected by the ABI.
52 Nor does it overlap the pthread data structure, so we need nothing
53 extra here at all. */
54 typedef struct
56 dtv_t *dtv;
57 } tcbhead_t;
59 /* This is the size of the initial TCB. */
60 # define TLS_INIT_TCB_SIZE 0
62 /* Alignment requirements for the initial TCB. */
63 # define TLS_INIT_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
65 /* This is the size of the TCB. */
66 # define TLS_TCB_SIZE 0
68 /* Alignment requirements for the TCB. */
69 # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
71 /* This is the size we need before TCB. */
72 # ifndef IS_IN_rtld
73 # define TLS_PRE_TCB_SIZE \
74 (sizeof (struct _pthread_descr_struct) \
75 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
76 # else
77 # include <nptl-struct-pthread.h>
78 # define TLS_PRE_TCB_SIZE \
79 ((sizeof (struct _pthread_descr_struct) > NPTL_STRUCT_PTHREAD_SIZE \
80 ? sizeof (struct _pthread_descr_struct) : NPTL_STRUCT_PTHREAD_SIZE) \
81 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
82 # endif
84 /* The following assumes that TP (R2 or R13) points to the end of the
85 TCB + 0x7000 (per the ABI). This implies that TCB address is
86 TP - 0x7000. As we define TLS_DTV_AT_TP we can
87 assume that the pthread_descr is allocated immediately ahead of the
88 TCB. This implies that the pthread_descr address is
89 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
90 #define TLS_TCB_OFFSET 0x7000
92 /* The DTV is allocated at the TP; the TCB is placed elsewhere. */
93 /* This is not really true for powerpc64. We are following alpha
94 where the DTV pointer is first doubleword in the TCB. */
95 # define TLS_DTV_AT_TP 1
97 /* Install the dtv pointer. The pointer passed is to the element with
98 index -1 which contain the length. */
99 # define INSTALL_DTV(TCBP, DTVP) \
100 (((tcbhead_t *) (TCBP))[-1].dtv = (DTVP) + 1)
102 /* Install new dtv for current thread. */
103 # define INSTALL_NEW_DTV(DTV) (THREAD_DTV() = (DTV))
105 /* Return dtv of given thread descriptor. */
106 # define GET_DTV(TCBP) (((tcbhead_t *) (TCBP))[-1].dtv)
108 /* We still need this define so that tcb-offsets.sym can override it and
109 use THREAD_SELF to generate MULTIPLE_THREADS_OFFSET. */
110 # define __thread_register ((void *) __thread_self)
112 /* Code to initially initialize the thread pointer. This might need
113 special attention since 'errno' is not yet available and if the
114 operation can cause a failure 'errno' must not be touched.
116 The global register variable is declared in pt-machine.h with the
117 wrong type, so we need some extra casts to get the desired result.
118 This avoids a lvalue cast that gcc-3.4 does not like. */
119 # define TLS_INIT_TP(TCBP, SECONDCALL) \
120 (__thread_self = (struct _pthread_descr_struct *) \
121 ((void *) (TCBP) + TLS_TCB_OFFSET), NULL)
123 /* Return the address of the dtv for the current thread. */
124 # define THREAD_DTV() \
125 (((tcbhead_t *) ((void *) __thread_self - TLS_TCB_OFFSET))[-1].dtv)
127 /* Return the thread descriptor for the current thread. */
128 # undef THREAD_SELF
129 # define THREAD_SELF \
130 ((pthread_descr) (__thread_register \
131 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
133 # undef INIT_THREAD_SELF
134 # define INIT_THREAD_SELF(DESCR, NR) \
135 (__thread_self = (struct _pthread_descr_struct *)((void *) (DESCR) \
136 + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE))
138 /* Make sure we have the p_multiple_threads member in the thread structure.
139 See below. */
140 # define TLS_MULTIPLE_THREADS_IN_TCB 1
142 /* Get the thread descriptor definition. */
143 # include <linuxthreads/descr.h>
145 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
146 different value to mean unset l_tls_offset. */
147 # define NO_TLS_OFFSET -1
149 # endif /* __ASSEMBLER__ */
151 #elif !defined __ASSEMBLER__
153 /* This overlaps the start of the pthread_descr. System calls
154 and such use this to find the multiple_threads flag and need
155 to use the same offset relative to the thread register in both
156 single-threaded and multi-threaded code. */
157 typedef struct
159 void *tcb; /* Never used. */
160 dtv_t *dtv; /* Never used. */
161 void *self; /* Used only if multithreaded, and rarely. */
162 int multiple_threads; /* Only this member is really used. */
163 } tcbhead_t;
165 #define NONTLS_INIT_TP \
166 do { \
167 static const tcbhead_t nontls_init_tp = { .multiple_threads = 0 }; \
168 __thread_self = (__typeof (__thread_self)) &nontls_init_tp; \
169 } while (0)
171 #endif /* HAVE_TLS_SUPPORT */
173 #endif /* tls.h */