Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definitions for thread-local data handling.  linuxthreads/IA-64 version.
00002    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _TLS_H
00021 #define _TLS_H
00022 
00023 #ifndef __ASSEMBLER__
00024 
00025 # include <dl-sysdep.h>
00026 # include <pt-machine.h>
00027 # include <stdbool.h>
00028 # include <stddef.h>
00029 # include <stdint.h>
00030 
00031 /* Type for the dtv.  */
00032 typedef union dtv
00033 {
00034   size_t counter;
00035   struct
00036   {
00037     void *val;
00038     bool is_static;
00039   } pointer;
00040 } dtv_t;
00041 
00042 #else /* __ASSEMBLER__ */
00043 # include <tcb-offsets.h>
00044 #endif /* __ASSEMBLER__ */
00045 
00046 #ifdef HAVE_TLS_SUPPORT
00047 
00048 /* Signal that TLS support is available.  */
00049 # define USE_TLS        1
00050 
00051 # ifndef __ASSEMBLER__
00052 
00053 typedef struct
00054 {
00055   dtv_t *dtv;
00056   void *private;
00057 } tcbhead_t;
00058 
00059 /* This is the size of the initial TCB.  */
00060 #  define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
00061 
00062 /* Alignment requirements for the initial TCB.  */
00063 #  define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
00064 
00065 /* This is the size of the TCB.  */
00066 #  define TLS_TCB_SIZE sizeof (tcbhead_t)
00067 
00068 /* This is the size we need before TCB.
00069    If there is not any room for uintptr_t stack_guard and
00070    uintptr_t pointer_guard in struct pthread's final padding,
00071    we need to put struct pthread 16 byte slower.  */
00072 # define TLS_PRE_TCB_SIZE \
00073   (sizeof (struct pthread)                                     \
00074    + (PTHREAD_STRUCT_END_PADDING < 2 * sizeof (uintptr_t)             \
00075       ? ((2 * sizeof (uintptr_t) + __alignof__ (struct pthread) - 1)  \
00076         & ~(__alignof__ (struct pthread) - 1))                        \
00077       : 0))
00078 
00079 /* Alignment requirements for the TCB.  */
00080 #  define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
00081 
00082 /* The DTV is allocated at the TP; the TCB is placed elsewhere.  */
00083 #  define TLS_DTV_AT_TP 1
00084 
00085 /* Install the dtv pointer.  The pointer passed is to the element with
00086    index -1 which contain the length.  */
00087 #  define INSTALL_DTV(tcbp, dtvp) \
00088   ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
00089 
00090 /* Install new dtv for current thread.  */
00091 #  define INSTALL_NEW_DTV(DTV) \
00092   (((tcbhead_t *)__thread_self)->dtv = (DTV))
00093 
00094 /* Return dtv of given thread descriptor.  */
00095 #  define GET_DTV(tcbp) \
00096   (((tcbhead_t *) (tcbp))->dtv)
00097 
00098 #if defined NEED_DL_SYSINFO
00099 # define INIT_SYSINFO \
00100   (((tcbhead_t *) __thread_self)->private = (void *) GLRO(dl_sysinfo))
00101 #else
00102 # define INIT_SYSINFO 0
00103 #endif
00104 
00105 /* Code to initially initialize the thread pointer.  This might need
00106    special attention since 'errno' is not yet available and if the
00107    operation can cause a failure 'errno' must not be touched.  */
00108 #  define TLS_INIT_TP(tcbp, secondcall) \
00109   (__thread_self = (__typeof (__thread_self)) (tcbp), INIT_SYSINFO, NULL)
00110 
00111 /* Return the address of the dtv for the current thread.  */
00112 #  define THREAD_DTV() \
00113   (((tcbhead_t *)__thread_self)->dtv)
00114 
00115 /* Return the thread descriptor for the current thread.  */
00116 #  undef THREAD_SELF
00117 #  define THREAD_SELF \
00118   ((struct pthread *) ((char *) __thread_self - TLS_PRE_TCB_SIZE))
00119 
00120 #  undef INIT_THREAD_SELF
00121 #  define INIT_THREAD_SELF(descr, nr) \
00122   (__thread_self = (struct _pthread_descr_struct *)(descr) + 1)
00123 
00124 # define TLS_MULTIPLE_THREADS_IN_TCB 1
00125 
00126 /* Set the stack guard field in TCB head.  */
00127 #define THREAD_SET_STACK_GUARD(value) \
00128   (((uintptr_t *) __thread_self)[-1] = (value))
00129 #define THREAD_COPY_STACK_GUARD(descr) \
00130   (((uintptr_t *) ((char *) (descr) + TLS_PRE_TCB_SIZE))[-1] \
00131    = ((uintptr_t *) __thread_self)[-1])
00132 
00133 /* Set the pointer guard field in TCB head.  */
00134 #define THREAD_GET_POINTER_GUARD() \
00135   (((uintptr_t *) __thread_self)[-2])
00136 #define THREAD_SET_POINTER_GUARD(value) \
00137   (((uintptr_t *) __thread_self)[-2] = (value))
00138 #define THREAD_COPY_POINTER_GUARD(descr) \
00139   (((uintptr_t *) ((char *) (descr) + TLS_PRE_TCB_SIZE))[-2] \
00140    = THREAD_GET_POINTER_GUARD ())
00141 
00142 /* Get the thread descriptor definition.  */
00143 #  include <linuxthreads/descr.h>
00144 
00145 # endif
00146 
00147 #else
00148 
00149 # ifndef __ASSEMBLER__
00150 
00151 typedef struct
00152 {
00153   void *tcb;
00154   dtv_t *dtv;
00155   void *self;
00156   int multiple_threads;
00157 } tcbhead_t;
00158 
00159 /* Get the thread descriptor definition.  */
00160 #  include <linuxthreads/descr.h>
00161 
00162 #  define NONTLS_INIT_TP \
00163   do {                                                                \
00164     static const tcbhead_t nontls_init_tp = { .multiple_threads = 0 };       \
00165     __thread_self = (__typeof (__thread_self)) &nontls_init_tp;              \
00166   } while (0)
00167 
00168 #endif
00169 
00170 #endif /* USE_TLS */
00171 
00172 #endif /* tls.h */