Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/MIPS 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 /* Note: rd must be $v1 to be ABI-conformant.  */
00042 # define READ_THREAD_POINTER() \
00043     ({ void *__result;                                                      \
00044        asm volatile (".set\tpush\n\t.set\tmips32r2\n\t"                     \
00045                    "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result));        \
00046        __result; })
00047 
00048 #else /* __ASSEMBLER__ */
00049 # include <tcb-offsets.h>
00050 
00051 # define READ_THREAD_POINTER(rd) \
00052        .set   push;                                                  \
00053        .set   mips32r2;                                              \
00054        rdhwr  rd, $29;                                               \
00055        .set   pop
00056 #endif /* __ASSEMBLER__ */
00057 
00058 
00059 /* We require TLS support in the tools.  */
00060 #ifndef HAVE_TLS_SUPPORT
00061 # error "TLS support is required."
00062 #endif
00063 
00064 /* Signal that TLS support is available.  */
00065 #define USE_TLS      1
00066 
00067 #ifndef __ASSEMBLER__
00068 
00069 /* Get system call information.  */
00070 # include <sysdep.h>
00071 
00072 /* The TP points to the start of the thread blocks.  */
00073 # define TLS_DTV_AT_TP      1
00074 
00075 /* Get the thread descriptor definition.  */
00076 # include <nptl/descr.h>
00077 
00078 typedef struct
00079 {
00080   dtv_t *dtv;
00081   void *private;
00082 } tcbhead_t;
00083 
00084 /* This is the size of the initial TCB.  Because our TCB is before the thread
00085    pointer, we don't need this.  */
00086 # define TLS_INIT_TCB_SIZE  0
00087 
00088 /* Alignment requirements for the initial TCB.  */
00089 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
00090 
00091 /* This is the size of the TCB.  Because our TCB is before the thread
00092    pointer, we don't need this.  */
00093 # define TLS_TCB_SIZE              0
00094 
00095 /* Alignment requirements for the TCB.  */
00096 # define TLS_TCB_ALIGN             __alignof__ (struct pthread)
00097 
00098 /* This is the size we need before TCB - actually, it includes the TCB.  */
00099 # define TLS_PRE_TCB_SIZE \
00100   (sizeof (struct pthread)                                           \
00101    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
00102 
00103 /* The thread pointer (in hardware register $29) points to the end of
00104    the TCB + 0x7000, as for PowerPC.  The pthread_descr structure is
00105    immediately in front of the TCB.  */
00106 # define TLS_TCB_OFFSET     0x7000
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))[-1].dtv = (dtvp) + 1)
00112 
00113 /* Install new dtv for current thread.  */
00114 # define INSTALL_NEW_DTV(dtv) \
00115   (THREAD_DTV() = (dtv))
00116 
00117 /* Return dtv of given thread descriptor.  */
00118 # define GET_DTV(tcbp) \
00119   (((tcbhead_t *) (tcbp))[-1].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 (set_thread_area, err, 1,          \
00128                                 (char *) (tcbp) + TLS_TCB_OFFSET);    \
00129      INTERNAL_SYSCALL_ERROR_P (result_var, err)                       \
00130        ? "unknown error" : NULL; })
00131 
00132 /* Return the address of the dtv for the current thread.  */
00133 # define THREAD_DTV() \
00134   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
00135 
00136 /* Return the thread descriptor for the current thread.  */
00137 # define THREAD_SELF \
00138  ((struct pthread *) (READ_THREAD_POINTER ()                        \
00139                     - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
00140 
00141 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00142 # define DB_THREAD_SELF \
00143   CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
00144 
00145 /* Access to data in the thread descriptor is easy.  */
00146 # define THREAD_GETMEM(descr, member) \
00147   descr->member
00148 # define THREAD_GETMEM_NC(descr, member, idx) \
00149   descr->member[idx]
00150 # define THREAD_SETMEM(descr, member, value) \
00151   descr->member = (value)
00152 # define THREAD_SETMEM_NC(descr, member, idx, value) \
00153   descr->member[idx] = (value)
00154 
00155 /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
00156    different value to mean unset l_tls_offset.  */
00157 # define NO_TLS_OFFSET             -1
00158 
00159 /* Get and set the global scope generation counter in struct pthread.  */
00160 #define THREAD_GSCOPE_FLAG_UNUSED 0
00161 #define THREAD_GSCOPE_FLAG_USED   1
00162 #define THREAD_GSCOPE_FLAG_WAIT   2
00163 #define THREAD_GSCOPE_RESET_FLAG() \
00164   do                                                                \
00165     { int __res                                                            \
00166        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00167                             THREAD_GSCOPE_FLAG_UNUSED);             \
00168       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00169        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00170     }                                                               \
00171   while (0)
00172 #define THREAD_GSCOPE_SET_FLAG() \
00173   do                                                                \
00174     {                                                               \
00175       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00176       atomic_write_barrier ();                                             \
00177     }                                                               \
00178   while (0)
00179 #define THREAD_GSCOPE_WAIT() \
00180   GL(dl_wait_lookup_done) ()
00181 
00182 #endif /* __ASSEMBLER__ */
00183 
00184 #endif /* tls.h */