Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/ARM version.
00002    Copyright (C) 2005, 2007 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       1
00022 
00023 #include <dl-sysdep.h>
00024 
00025 #ifndef __ASSEMBLER__
00026 # include <stdbool.h>
00027 # include <stddef.h>
00028 # include <stdint.h>
00029 
00030 /* Type for the dtv.  */
00031 typedef union dtv
00032 {
00033   size_t counter;
00034   struct
00035   {
00036     void *val;
00037     bool is_static;
00038   } pointer;
00039 } dtv_t;
00040 
00041 #else /* __ASSEMBLER__ */
00042 # include <tcb-offsets.h>
00043 #endif /* __ASSEMBLER__ */
00044 
00045 
00046 /* We require TLS support in the tools.  */
00047 #ifndef HAVE_TLS_SUPPORT
00048 # error "TLS support is required."
00049 #endif
00050 
00051 /* Signal that TLS support is available.  */
00052 # define USE_TLS     1
00053 
00054 #ifndef __ASSEMBLER__
00055 
00056 /* Get system call information.  */
00057 # include <sysdep.h>
00058 
00059 /* The TP points to the start of the thread blocks.  */
00060 # define TLS_DTV_AT_TP      1
00061 
00062 /* Get the thread descriptor definition.  */
00063 # include <nptl/descr.h>
00064 
00065 typedef struct
00066 {
00067   dtv_t *dtv;
00068   void *private;
00069 } tcbhead_t;
00070 
00071 /* This is the size of the initial TCB.  */
00072 # define TLS_INIT_TCB_SIZE  sizeof (tcbhead_t)
00073 
00074 /* Alignment requirements for the initial TCB.  */
00075 # define TLS_INIT_TCB_ALIGN 16
00076 
00077 /* This is the size of the TCB.  */
00078 # define TLS_TCB_SIZE              sizeof (tcbhead_t)
00079 
00080 /* This is the size we need before TCB.  */
00081 # define TLS_PRE_TCB_SIZE   sizeof (struct pthread)
00082 
00083 /* Alignment requirements for the TCB.  */
00084 # define TLS_TCB_ALIGN             16
00085 
00086 /* Install the dtv pointer.  The pointer passed is to the element with
00087    index -1 which contain the length.  */
00088 # define INSTALL_DTV(tcbp, dtvp) \
00089   (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
00090 
00091 /* Install new dtv for current thread.  */
00092 # define INSTALL_NEW_DTV(dtv) \
00093   (THREAD_DTV() = (dtv))
00094 
00095 /* Return dtv of given thread descriptor.  */
00096 # define GET_DTV(tcbp) \
00097   (((tcbhead_t *) (tcbp))->dtv)
00098 
00099 /* Code to initially initialize the thread pointer.  This might need
00100    special attention since 'errno' is not yet available and if the
00101    operation can cause a failure 'errno' must not be touched.  */
00102 # define TLS_INIT_TP(tcbp, secondcall) \
00103   ({ INTERNAL_SYSCALL_DECL (err);                              \
00104      long result_var;                                                 \
00105      result_var = INTERNAL_SYSCALL_ARM (set_tls, err, 1, (tcbp));     \
00106      INTERNAL_SYSCALL_ERROR_P (result_var, err)                       \
00107        ? "unknown error" : NULL; })
00108 
00109 /* Return the address of the dtv for the current thread.  */
00110 # define THREAD_DTV() \
00111   (((tcbhead_t *) __builtin_thread_pointer ())->dtv)
00112 
00113 /* Return the thread descriptor for the current thread.  */
00114 # define THREAD_SELF \
00115  ((struct pthread *)__builtin_thread_pointer () - 1)
00116 
00117 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00118 # define DB_THREAD_SELF \
00119   CONST_THREAD_AREA (32, sizeof (struct pthread))
00120 
00121 /* Access to data in the thread descriptor is easy.  */
00122 #define THREAD_GETMEM(descr, member) \
00123   descr->member
00124 #define THREAD_GETMEM_NC(descr, member, idx) \
00125   descr->member[idx]
00126 #define THREAD_SETMEM(descr, member, value) \
00127   descr->member = (value)
00128 #define THREAD_SETMEM_NC(descr, member, idx, value) \
00129   descr->member[idx] = (value)
00130 
00131 /* Initializing the thread pointer will generate a SIGILL if the syscall
00132    is not available.  */
00133 #define TLS_INIT_TP_EXPENSIVE 1
00134 
00135 /* Get and set the global scope generation counter in struct pthread.  */
00136 #define THREAD_GSCOPE_FLAG_UNUSED 0
00137 #define THREAD_GSCOPE_FLAG_USED   1
00138 #define THREAD_GSCOPE_FLAG_WAIT   2
00139 #define THREAD_GSCOPE_RESET_FLAG() \
00140   do                                                                \
00141     { int __res                                                            \
00142        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00143                             THREAD_GSCOPE_FLAG_UNUSED);             \
00144       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00145        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00146     }                                                               \
00147   while (0)
00148 #define THREAD_GSCOPE_SET_FLAG() \
00149   do                                                                \
00150     {                                                               \
00151       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00152       atomic_write_barrier ();                                             \
00153     }                                                               \
00154   while (0)
00155 #define THREAD_GSCOPE_WAIT() \
00156   GL(dl_wait_lookup_done) ()
00157 
00158 #endif /* __ASSEMBLER__ */
00159 
00160 #endif /* tls.h */