Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/hppa 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 __alignof__ (tcbhead_t)
00076 
00077 /* This is the size of the TCB.  */
00078 # define TLS_TCB_SIZE              sizeof (tcbhead_t)
00079 
00080 /* Alignment requirements for the TCB.  */
00081 # define TLS_TCB_ALIGN             __alignof__ (struct pthread)
00082 
00083 /* This is the size we need before TCB */
00084 # define TLS_PRE_TCB_SIZE   sizeof (struct pthread)
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   ({ tcbhead_t *__tcbp = (tcbhead_t *)__get_cr27();     \
00094        __tcbp->dtv = dtv;                        \
00095    })
00096 
00097 /* Return dtv of given thread descriptor.  */
00098 # define GET_DTV(tcbp) \
00099   (((tcbhead_t *) (tcbp))->dtv)
00100 
00101 /* Code to initially initialize the thread pointer.  This might need
00102    special attention since 'errno' is not yet available and if the
00103    operation can cause a failure 'errno' must not be touched.  */
00104 # define TLS_INIT_TP(tcbp, secondcall) \
00105   ({ __set_cr27(tcbp); NULL; })
00106 
00107 /* Return the address of the dtv for the current thread.  */
00108 # define THREAD_DTV() \
00109   ({ tcbhead_t *__tcbp = (tcbhead_t *)__get_cr27();     \
00110        __tcbp->dtv;                              \
00111    })
00112 
00113 /* Return the thread descriptor for the current thread.  */
00114 # define THREAD_SELF \
00115   ({ struct pthread *__self;                     \
00116        __self = __get_cr27();                    \
00117        __self - 1;                        \
00118    })
00119 
00120 /* Magic for libthread_db to know how to do THREAD_SELF.
00121    Our thread pointer is stored in cr27.  See asm/elf.h for the offset into
00122    elf_gregset_t.  The thread descriptor is sizeof (struct pthread) away.  */
00123 # define DB_THREAD_SELF \
00124   REGISTER (32, 32, 53 * 4, -sizeof (struct pthread))
00125  
00126 /* Access to data in the thread descriptor is easy.  */
00127 # define THREAD_GETMEM(descr, member) \
00128   descr->member
00129 # define THREAD_GETMEM_NC(descr, member, idx) \
00130   descr->member[idx]
00131 # define THREAD_SETMEM(descr, member, value) \
00132   descr->member = (value)
00133 # define THREAD_SETMEM_NC(descr, member, idx, value) \
00134   descr->member[idx] = (value)
00135 
00136 static inline struct pthread *__get_cr27(void)
00137 {
00138   long cr27;
00139   asm ("mfctl %%cr27, %0" : "=r" (cr27) : );
00140   return (struct pthread *) cr27;
00141 }
00142 
00143 static inline void __set_cr27(struct pthread *cr27)
00144 {
00145   asm ( "ble  0xe0(%%sr2, %%r0)\n\t"
00146        "copy  %0, %%r26"
00147        : : "r" (cr27) : "r26" );
00148 }
00149 
00150 /* Get and set the global scope generation counter in struct pthread.  */
00151 #define THREAD_GSCOPE_FLAG_UNUSED 0
00152 #define THREAD_GSCOPE_FLAG_USED   1
00153 #define THREAD_GSCOPE_FLAG_WAIT   2
00154 #define THREAD_GSCOPE_RESET_FLAG() \
00155   do                                                                \
00156     { int __res                                                            \
00157        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00158                             THREAD_GSCOPE_FLAG_UNUSED);             \
00159       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00160        lll_private_futex_wake (&THREAD_SELF->header.gscope_flag, 1);       \
00161     }                                                               \
00162   while (0)
00163 #define THREAD_GSCOPE_SET_FLAG() \
00164   do                                                                \
00165     {                                                               \
00166       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00167       atomic_write_barrier ();                                             \
00168     }                                                               \
00169   while (0)
00170 #define THREAD_GSCOPE_WAIT() \
00171   GL(dl_wait_lookup_done) ()
00172 
00173 #endif /* !__ASSEMBLER__ */
00174 
00175 #endif /* tls.h */