Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definitions for thread-local data handling.  NPTL/sparc version.
00002    Copyright (C) 2003, 2005, 2006, 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
00022 
00023 #include <dl-sysdep.h>
00024 #ifndef __ASSEMBLER__
00025 # include <stdbool.h>
00026 # include <stddef.h>
00027 # include <stdint.h>
00028 # include <stdlib.h>
00029 # include <list.h>
00030 # include <kernel-features.h>
00031 
00032 /* Type for the dtv.  */
00033 typedef union dtv
00034 {
00035   size_t counter;
00036   struct
00037   {
00038     void *val;
00039     bool is_static;
00040   } pointer;
00041 } dtv_t;
00042 
00043 typedef struct
00044 {
00045   void *tcb;         /* Pointer to the TCB.  Not necessary the
00046                         thread descriptor used by libpthread.  */
00047   dtv_t *dtv;
00048   void *self;
00049   int multiple_threads;
00050 #if __WORDSIZE == 64
00051   int gscope_flag;
00052 #endif
00053   uintptr_t sysinfo;
00054   uintptr_t stack_guard;
00055   uintptr_t pointer_guard;
00056 #if __WORDSIZE != 64
00057   int gscope_flag;
00058 #endif
00059 #ifndef __ASSUME_PRIVATE_FUTEX
00060   int private_futex;
00061 #endif
00062 } tcbhead_t;
00063 
00064 #else /* __ASSEMBLER__ */
00065 # include <tcb-offsets.h>
00066 #endif /* __ASSEMBLER__ */
00067 
00068 /* We require TLS support in the tools.  */
00069 #ifndef HAVE_TLS_SUPPORT
00070 # error "TLS support is required."
00071 #endif
00072 
00073 #ifndef __ASSEMBLER__
00074 /* Get system call information.  */
00075 # include <sysdep.h>
00076 
00077 /* Get the thread descriptor definition.  */
00078 # include <nptl/descr.h>
00079 
00080 register struct pthread *__thread_self __asm__("%g7");
00081 
00082 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
00083    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
00084    struct pthread even when not linked with -lpthread.  */
00085 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
00086 
00087 /* Alignment requirements for the initial TCB.  */
00088 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
00089 
00090 /* This is the size of the TCB.  */
00091 # define TLS_TCB_SIZE sizeof (struct pthread)
00092 
00093 /* Alignment requirements for the TCB.  */
00094 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
00095 
00096 /* The TCB can have any size and the memory following the address the
00097    thread pointer points to is unspecified.  Allocate the TCB there.  */
00098 # define TLS_TCB_AT_TP      1
00099 
00100 /* Install the dtv pointer.  The pointer passed is to the element with
00101    index -1 which contain the length.  */
00102 # define INSTALL_DTV(descr, dtvp) \
00103   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
00104 
00105 /* Install new dtv for current thread.  */
00106 # define INSTALL_NEW_DTV(DTV) \
00107   (((tcbhead_t *) __thread_self)->dtv = (DTV))
00108 
00109 /* Return dtv of given thread descriptor.  */
00110 # define GET_DTV(descr) \
00111   (((tcbhead_t *) (descr))->dtv)
00112 
00113 /* Code to initially initialize the thread pointer.  */
00114 # define TLS_INIT_TP(descr, secondcall) \
00115   (__thread_self = (__typeof (__thread_self)) (descr), NULL)
00116 
00117 /* Return the address of the dtv for the current thread.  */
00118 # define THREAD_DTV() \
00119   (((tcbhead_t *) __thread_self)->dtv)
00120 
00121 /* Return the thread descriptor for the current thread.  */
00122 #define THREAD_SELF  __thread_self
00123 
00124 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00125 # define DB_THREAD_SELF_INCLUDE <sys/ucontext.h>
00126 # define DB_THREAD_SELF \
00127   REGISTER (32, 32, REG_G7 * 4, 0) REGISTER (64, 64, REG_G7 * 8, 0)
00128 
00129 /* Access to data in the thread descriptor is easy.  */
00130 #define THREAD_GETMEM(descr, member) \
00131   descr->member
00132 #define THREAD_GETMEM_NC(descr, member, idx) \
00133   descr->member[idx]
00134 #define THREAD_SETMEM(descr, member, value) \
00135   descr->member = (value)
00136 #define THREAD_SETMEM_NC(descr, member, idx, value) \
00137   descr->member[idx] = (value)
00138 
00139 /* Set the stack guard field in TCB head.  */
00140 #define THREAD_SET_STACK_GUARD(value) \
00141   THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
00142 # define THREAD_COPY_STACK_GUARD(descr) \
00143   ((descr)->header.stack_guard \
00144    = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
00145 
00146 /* Get/set the stack guard field in TCB head.  */
00147 #define THREAD_GET_POINTER_GUARD() \
00148   THREAD_GETMEM (THREAD_SELF, header.pointer_guard)
00149 #define THREAD_SET_POINTER_GUARD(value) \
00150   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
00151 # define THREAD_COPY_POINTER_GUARD(descr) \
00152   ((descr)->header.pointer_guard = THREAD_GET_POINTER_GUARD ())
00153 
00154 /* Get and set the global scope generation counter in struct pthread.  */
00155 #define THREAD_GSCOPE_FLAG_UNUSED 0
00156 #define THREAD_GSCOPE_FLAG_USED   1
00157 #define THREAD_GSCOPE_FLAG_WAIT   2
00158 #define THREAD_GSCOPE_RESET_FLAG() \
00159   do                                                                \
00160     { int __res                                                            \
00161        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00162                             THREAD_GSCOPE_FLAG_UNUSED);             \
00163       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00164        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00165     }                                                               \
00166   while (0)
00167 #define THREAD_GSCOPE_SET_FLAG() \
00168   do                                                                \
00169     {                                                               \
00170       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00171       atomic_write_barrier ();                                             \
00172     }                                                               \
00173   while (0)
00174 #define THREAD_GSCOPE_WAIT() \
00175   GL(dl_wait_lookup_done) ()
00176 
00177 #endif /* !ASSEMBLER */
00178 
00179 #endif /* tls.h */