Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definitions for thread-local data handling.  linuxthreads/ARM version.
00002    Copyright (C) 2004 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 <stdbool.h>
00026 # include <pt-machine.h>
00027 # include <stddef.h>
00028 
00029 /* Type for the dtv.  */
00030 typedef union dtv
00031 {
00032   size_t counter;
00033   struct
00034   {
00035     void *val;
00036     bool is_static;
00037   } pointer;
00038 } dtv_t;
00039 
00040 typedef struct
00041 {
00042   dtv_t *dtv;
00043 
00044   /* Reserved for the thread implementation.  Unused in LinuxThreads.  */
00045   void *private;
00046 } tcbhead_t;
00047 #endif
00048 
00049 
00050 /* We can support TLS only if the floating-stack support is available.
00051    However, we want to compile in the support and test at runtime whether
00052    the running kernel can support it or not.  To avoid bothering with the
00053    TLS support code at all, use configure --without-tls.
00054 
00055    We need USE_TLS to be consistently defined, for ldsodefs.h conditionals.
00056    But some of the code below can cause problems in building libpthread
00057    (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't).  */
00058 
00059 /* LinuxThreads can only support TLS if both floating stacks and support
00060    from the tools are available.
00061 
00062    We have to define USE_TLS consistently, or ldsodefs.h will lay out types
00063    differently between an NPTL build and a LinuxThreads build.  It can be set
00064    for libc.so and not libpthread.so, but only if we provide appropriate padding
00065    in the _pthread_descr_struct.
00066 
00067    Currently nothing defines FLOATING_STACKS.  We could assume this based on
00068    kernel version once the TLS patches are available in kernel.org.
00069 
00070    To avoid bothering with the TLS support code at all, use configure
00071    --without-tls.  */
00072 
00073 #if defined HAVE_TLS_SUPPORT \
00074     && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
00075 
00076 /* Signal that TLS support is available.  */
00077 # define USE_TLS     1
00078 
00079 /* Include padding in _pthread_descr_struct so that libc can find p_errno,
00080    if libpthread will only include the padding because of the !IS_IN_libpthread
00081    check.  */
00082 #ifndef FLOATING_STACKS
00083 # define INCLUDE_TLS_PADDING       1
00084 #endif
00085 
00086 # ifndef __ASSEMBLER__
00087 /* Get system call information.  */
00088 #  include <sysdep.h>
00089 
00090 /* This is the size of the initial TCB.  */
00091 #  define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
00092 
00093 /* Alignment requirements for the initial TCB.  */
00094 #  define TLS_INIT_TCB_ALIGN       __alignof__ (tcbhead_t)
00095 
00096 /* This is the size of the TCB.  */
00097 #  define TLS_TCB_SIZE             sizeof (tcbhead_t)
00098 
00099 /* Alignment requirements for the TCB.  */
00100 #  define TLS_TCB_ALIGN            __alignof__ (tcbhead_t)
00101 
00102 /* This is the size we need before TCB.  */
00103 #  define TLS_PRE_TCB_SIZE  sizeof (struct _pthread_descr_struct)
00104 
00105 /* The DTV is allocated at the TP; the TCB is placed elsewhere.  */
00106 #  define TLS_DTV_AT_TP 1
00107 
00108 /* Install the dtv pointer.  The pointer passed is to the element with
00109    index -1 which contain the length.  */
00110 #  define INSTALL_DTV(TCBP, DTVP) \
00111   (((tcbhead_t *) (TCBP))->dtv = (DTVP) + 1)
00112 
00113 /* Install new dtv for current thread.  */
00114 #  define INSTALL_NEW_DTV(DTV) \
00115   (((tcbhead_t *)__builtin_thread_pointer ())->dtv = (DTV))
00116 
00117 /* Return dtv of given thread descriptor.  */
00118 #  define GET_DTV(TCBP) \
00119   (((tcbhead_t *) (TCBP))->dtv)
00120 
00121 /* Code to initially initialize the thread pointer.  This might need
00122    special attention since 'errno' is not yet available and if the
00123    operation can cause a failure 'errno' must not be touched.  */
00124 # define TLS_INIT_TP(TCBP, SECONDCALL) \
00125   ({ INTERNAL_SYSCALL_DECL (err);                              \
00126      long result_var;                                                 \
00127      result_var = INTERNAL_SYSCALL_ARM (set_tls, err, 1, (TCBP));     \
00128      INTERNAL_SYSCALL_ERROR_P (result_var, err)                       \
00129        ? "unknown error" : NULL; })
00130 
00131 /* Return the address of the dtv for the current thread.  */
00132 #  define THREAD_DTV() \
00133   (((tcbhead_t *)__builtin_thread_pointer ())->dtv)
00134 
00135 /* Return the thread descriptor for the current thread.  */
00136 #  undef THREAD_SELF
00137 #  define THREAD_SELF \
00138   ((pthread_descr)__builtin_thread_pointer () - 1)
00139 
00140 #  undef INIT_THREAD_SELF
00141 #  define INIT_THREAD_SELF(DESCR, NR) \
00142   TLS_INIT_TP ((struct _pthread_descr_struct *)(DESCR) + 1, 0)
00143 
00144 /* Get the thread descriptor definition.  */
00145 #  include <linuxthreads/descr.h>
00146 
00147 /* ??? Generic bits of LinuxThreads may call these macros with
00148    DESCR set to NULL.  We are expected to be able to reference
00149    the "current" value.
00150 
00151    In our case, we'd really prefer to use DESCR, since lots of
00152    PAL_code calls would be expensive.  We can only trust that
00153    the compiler does its job and unifies the multiple
00154    __builtin_thread_pointer instances.  */
00155 
00156 #define THREAD_GETMEM(descr, member) \
00157   ((void) sizeof (descr), THREAD_SELF->member)
00158 #define THREAD_GETMEM_NC(descr, member) \
00159   ((void) sizeof (descr), THREAD_SELF->member)
00160 #define THREAD_SETMEM(descr, member, value) \
00161   ((void) sizeof (descr), THREAD_SELF->member = (value))
00162 #define THREAD_SETMEM_NC(descr, member, value) \
00163   ((void) sizeof (descr), THREAD_SELF->member = (value))
00164 
00165 /* Initializing the thread pointer will generate a SIGILL if the syscall
00166    is not available.  */
00167 #define TLS_INIT_TP_EXPENSIVE 1
00168 
00169 # endif       /* HAVE_TLS_SUPPORT */
00170 #endif /* __ASSEMBLER__ */
00171 
00172 #endif /* tls.h */