1 /* Definition for thread-local data handling.  NPTL/Meta version.
2    Copyright (C) 2003, 2005, 2006, 2007 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; see the file COPYING.LIB.  If
17    not, see <http://www.gnu.org/licenses/>.  */
18 
19 #ifndef _TLS_H
20 #define _TLS_H
21 
22 #ifndef __ASSEMBLER__
23 # include <stdbool.h>
24 # include <stddef.h>
25 # include <stdint.h>
26 # include <stdlib.h>
27 # include <list.h>
28 # include <sysdep.h>
29 # include <bits/kernel-features.h>
30 
31 /* Type for the dtv.  */
32 typedef union dtv
33 {
34   size_t counter;
35   struct
36   {
37     void *val;
38     bool is_static;
39   } pointer;
40 } dtv_t;
41 
42 typedef struct
43 {
44   dtv_t *dtv;
45   uintptr_t pointer_guard;
46 } tcbhead_t;
47 
48 # define TLS_MULTIPLE_THREADS_IN_TCB 1
49 
50 #else /* __ASSEMBLER__ */
51 # include <tcb-offsets.h>
52 #endif /* __ASSEMBLER__ */
53 
54 
55 /* We require TLS support in the tools.  */
56 #define HAVE_TLS_SUPPORT
57 #define HAVE___THREAD   1
58 #define HAVE_TLS_MODEL_ATTRIBUTE       1
59 /* Signal that TLS support is available.  */
60 # define USE_TLS       1
61 
62 #ifndef __ASSEMBLER__
63 
64 /* Get system call information.  */
65 # include <sysdep.h>
66 
67 /* This is the size of the initial TCB.  */
68 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
69 
70 /* Alignment requirements for the initial TCB.  */
71 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
72 
73 /* This is the size of the TCB.  */
74 # define TLS_TCB_SIZE sizeof (tcbhead_t)
75 
76 /* This is the size we need before TCB.  */
77 # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
78 
79 /* Alignment requirements for the TCB.  */
80 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
81 
82 /* The TLS blocks start right after the TCB.  */
83 # define TLS_DTV_AT_TP	1
84 
85 /* Get the thread descriptor definition.  */
86 # include <descr.h>
87 
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
92 
93 /* Install new dtv for current thread.  */
94 # define INSTALL_NEW_DTV(dtv) \
95   (((tcbhead_t *)__builtin_thread_pointer ())->dtv = (dtv))
96 
97 /* Return dtv of given thread descriptor.  */
98 # define GET_DTV(tcbp) \
99   (((tcbhead_t *) (tcbp))->dtv)
100 
101 /* Code to initially initialize the thread pointer.  This might need
102    special attention since 'errno' is not yet available and if the
103    operation can cause a failure 'errno' must not be touched.  */
104 # define TLS_INIT_TP(tcbp, secondcall) \
105   ({ INTERNAL_SYSCALL_DECL (err);					\
106      long result_var;							\
107      result_var = INTERNAL_SYSCALL (metag_set_tls, err, 1, (tcbp));	\
108      INTERNAL_SYSCALL_ERROR_P (result_var, err)				\
109        ? "unknown error" : NULL; })
110 
111 /* Return the address of the dtv for the current thread.  */
112 # define THREAD_DTV() \
113   (((tcbhead_t *)__builtin_thread_pointer ())->dtv)
114 
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 *)__builtin_thread_pointer () - 1)
122 
123 /* Magic for libthread_db to know how to do THREAD_SELF.  */
124 # define DB_THREAD_SELF \
125   CONST_THREAD_AREA (32, sizeof (struct pthread))
126 
127 /* Access to data in the thread descriptor is easy.  */
128 #define THREAD_GETMEM(descr, member) \
129   descr->member
130 #define THREAD_GETMEM_NC(descr, member, idx) \
131   descr->member[idx]
132 #define THREAD_SETMEM(descr, member, value) \
133   descr->member = (value)
134 #define THREAD_SETMEM_NC(descr, member, idx, value) \
135   descr->member[idx] = (value)
136 
137 /* Get and set the global scope generation counter in struct pthread.  */
138 #define THREAD_GSCOPE_FLAG_UNUSED 0
139 #define THREAD_GSCOPE_FLAG_USED   1
140 #define THREAD_GSCOPE_FLAG_WAIT   2
141 #define THREAD_GSCOPE_RESET_FLAG() \
142   do									     \
143     { int __res								     \
144 	= atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,	     \
145 			       THREAD_GSCOPE_FLAG_UNUSED);		     \
146       if (__res == THREAD_GSCOPE_FLAG_WAIT)				     \
147 	lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
148     }									     \
149   while (0)
150 #define THREAD_GSCOPE_SET_FLAG() \
151   do									     \
152     {									     \
153       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;	     \
154       atomic_write_barrier ();						     \
155     }									     \
156   while (0)
157 #define THREAD_GSCOPE_WAIT() \
158   GL(dl_wait_lookup_done) ()
159 
160 #endif /* __ASSEMBLER__ */
161 
162 #endif	/* tls.h */
163