Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  linuxthreads/SH version.
00002    Copyright (C) 2002, 2003, 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 # include <dl-sysdep.h>
00024 # include <pt-machine.h>
00025 
00026 #ifndef __ASSEMBLER__
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 
00047 /* We can support TLS only if the floating-stack support is available.  */
00048 #if defined HAVE_TLS_SUPPORT \
00049     && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
00050 
00051 /* Signal that TLS support is available.  */
00052 # define USE_TLS     1
00053 
00054 /* Include padding in _pthread_descr_struct so that libc can find p_errno,
00055    if libpthread will only include the padding because of the !IS_IN_libpthread
00056    check.  */
00057 #ifndef FLOATING_STACKS
00058 # define INCLUDE_TLS_PADDING       1
00059 #endif
00060 
00061 # ifndef __ASSEMBLER__
00062 
00063 typedef struct
00064 {
00065   dtv_t *dtv;
00066   uintptr_t pointer_guard;
00067 } tcbhead_t;
00068 
00069 /* This is the size of the initial TCB.  */
00070 #  define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
00071 
00072 /* Alignment requirements for the initial TCB.  */
00073 #  define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
00074 
00075 /* This is the size of the TCB.  */
00076 #  define TLS_TCB_SIZE sizeof (tcbhead_t)
00077 
00078 /* This is the size we need before TCB.  */
00079 #  define TLS_PRE_TCB_SIZE sizeof (struct _pthread_descr_struct)
00080 
00081 /* Alignment requirements for the TCB.  */
00082 #  define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
00083 
00084 /* The TLS blocks start right after the TCB.  */
00085 #  define TLS_DTV_AT_TP     1
00086 
00087 /* Install the dtv pointer.  The pointer passed is to the element with
00088    index -1 which contain the length.  */
00089 #  define INSTALL_DTV(tcbp, dtvp) \
00090   ((tcbhead_t *) (tcbp))->dtv = dtvp + 1
00091 
00092 /* Install new dtv for current thread.  */
00093 #  define INSTALL_NEW_DTV(dtv) \
00094   ({ tcbhead_t *__tcbp;                                                     \
00095      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00096      __tcbp->dtv = (dtv);})
00097 
00098 /* Return dtv of given thread descriptor.  */
00099 #  define GET_DTV(tcbp) \
00100   (((tcbhead_t *) (tcbp))->dtv)
00101 
00102 /* Code to initially initialize the thread pointer.  This might need
00103    special attention since 'errno' is not yet available and if the
00104    operation can cause a failure 'errno' must not be touched.  */
00105 #  define TLS_INIT_TP(tcbp, secondcall) \
00106   ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); 0; })
00107 
00108 /* Return the address of the dtv for the current thread.  */
00109 #  define THREAD_DTV() \
00110   ({ tcbhead_t *__tcbp;                                                     \
00111      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00112      __tcbp->dtv;})
00113 
00114 /* Return the thread descriptor for the current thread.  */
00115 #  undef THREAD_SELF
00116 #  define THREAD_SELF \
00117   ({ struct _pthread_descr_struct *__self;                                  \
00118      __asm ("stc gbr,%0" : "=r" (__self));                                  \
00119      __self - 1;})
00120 
00121 #  undef INIT_THREAD_SELF
00122 #  define INIT_THREAD_SELF(descr, nr) \
00123   ({ struct _pthread_descr_struct *__self = (void *) descr;                 \
00124      __asm __volatile ("ldc %0,gbr" : : "r" (__self + 1));                  \
00125      0; })
00126 
00127 # define TLS_MULTIPLE_THREADS_IN_TCB 1
00128 
00129 #define THREAD_GET_POINTER_GUARD() \
00130   ({ tcbhead_t *__tcbp;                                                     \
00131      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00132      __tcbp->pointer_guard;})
00133  #define THREAD_SET_POINTER_GUARD(value) \
00134   ({ tcbhead_t *__tcbp;                                                     \
00135      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00136      __tcbp->pointer_guard = (value);})
00137 #define THREAD_COPY_POINTER_GUARD(descr) \
00138   ({ tcbhead_t *__tcbp;                                                     \
00139      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00140      ((tcbhead_t *) (descr + 1))->pointer_guard  = __tcbp->pointer_guard;})
00141 
00142 /* Get the thread descriptor definition.  This must be after the
00143    the definition of THREAD_SELF for TLS.  */
00144 #  include <linuxthreads/descr.h>
00145 
00146 # endif /* __ASSEMBLER__ */
00147 
00148 #else
00149 
00150 # ifndef __ASSEMBLER__
00151 
00152 typedef struct
00153 {
00154   void *tcb;
00155   dtv_t *dtv;
00156   void *self;
00157   int multiple_threads;
00158 } tcbhead_t;
00159 
00160 /* Get the thread descriptor definition.  */
00161 #  include <linuxthreads/descr.h>
00162 
00163 #  define NONTLS_INIT_TP \
00164   do {                                                                \
00165     static const tcbhead_t nontls_init_tp = { .multiple_threads = 0 };       \
00166     __asm __volatile ("ldc %0,gbr" : : "r" (&nontls_init_tp));         \
00167   } while (0)
00168 
00169 # endif /* __ASSEMBLER__ */
00170 
00171 #endif /* HAVE_TLS_SUPPORT && (FLOATING_STACKS || !IS_IN_libpthread) */
00172 
00173 #endif /* tls.h */