Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/SH 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 
00025 #ifndef __ASSEMBLER__
00026 # include <stdbool.h>
00027 # include <stddef.h>
00028 # include <stdint.h>
00029 # include <stdlib.h>
00030 # include <list.h>
00031 # include <sysdep.h>
00032 # include <kernel-features.h>
00033 
00034 /* Type for the dtv.  */
00035 typedef union dtv
00036 {
00037   size_t counter;
00038   struct
00039   {
00040     void *val;
00041     bool is_static;
00042   } pointer;
00043 } dtv_t;
00044 
00045 typedef struct
00046 {
00047   dtv_t *dtv;
00048   uintptr_t pointer_guard;
00049 } tcbhead_t;
00050 
00051 # define TLS_MULTIPLE_THREADS_IN_TCB 1
00052 
00053 #else /* __ASSEMBLER__ */
00054 # include <tcb-offsets.h>
00055 #endif /* __ASSEMBLER__ */
00056 
00057 
00058 /* We require TLS support in the tools.  */
00059 #ifndef HAVE_TLS_SUPPORT
00060 # error "TLS support is required."
00061 #endif
00062 
00063 #ifndef __ASSEMBLER__
00064 
00065 /* Get system call information.  */
00066 # include <sysdep.h>
00067 
00068 /* This is the size of the initial TCB.  */
00069 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
00070 
00071 /* Alignment requirements for the initial TCB.  */
00072 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
00073 
00074 /* This is the size of the TCB.  */
00075 # define TLS_TCB_SIZE sizeof (tcbhead_t)
00076 
00077 /* This is the size we need before TCB.  */
00078 # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
00079 
00080 /* Alignment requirements for the TCB.  */
00081 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
00082 
00083 /* The TLS blocks start right after the TCB.  */
00084 # define TLS_DTV_AT_TP      1
00085 
00086 /* Get the thread descriptor definition.  */
00087 # include <nptl/descr.h>
00088 
00089 /* Install the dtv pointer.  The pointer passed is to the element with
00090    index -1 which contain the length.  */
00091 # define INSTALL_DTV(tcbp, dtvp) \
00092   ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
00093 
00094 /* Install new dtv for current thread.  */
00095 # define INSTALL_NEW_DTV(dtv) \
00096   ({ tcbhead_t *__tcbp;                                                     \
00097      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00098      __tcbp->dtv = (dtv);})
00099 
00100 /* Return dtv of given thread descriptor.  */
00101 # define GET_DTV(tcbp) \
00102   (((tcbhead_t *) (tcbp))->dtv)
00103 
00104 /* Code to initially initialize the thread pointer.  This might need
00105    special attention since 'errno' is not yet available and if the
00106    operation can cause a failure 'errno' must not be touched.  */
00107 # define TLS_INIT_TP(tcbp, secondcall) \
00108   ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); 0; })
00109 
00110 /* Return the address of the dtv for the current thread.  */
00111 # define THREAD_DTV() \
00112   ({ tcbhead_t *__tcbp;                                                     \
00113      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00114      __tcbp->dtv;})
00115 
00116 /* Return the thread descriptor for the current thread.
00117    The contained asm must *not* be marked volatile since otherwise
00118    assignments like
00119         struct pthread *self = thread_self();
00120    do not get optimized away.  */
00121 # define THREAD_SELF \
00122   ({ struct pthread *__self;                                                \
00123      __asm ("stc gbr,%0" : "=r" (__self));                                  \
00124      __self - 1;})
00125 
00126 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00127 # define DB_THREAD_SELF \
00128   REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
00129 
00130 /* Read member of the thread descriptor directly.  */
00131 # define THREAD_GETMEM(descr, member) (descr->member)
00132 
00133 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
00134 # define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
00135 
00136 /* Set member of the thread descriptor directly.  */
00137 # define THREAD_SETMEM(descr, member, value) \
00138     descr->member = (value)
00139 
00140 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
00141 # define THREAD_SETMEM_NC(descr, member, idx, value) \
00142     descr->member[idx] = (value)
00143 
00144 #define THREAD_GET_POINTER_GUARD() \
00145   ({ tcbhead_t *__tcbp;                                                     \
00146      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00147      __tcbp->pointer_guard;})
00148  #define THREAD_SET_POINTER_GUARD(value) \
00149   ({ tcbhead_t *__tcbp;                                                     \
00150      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00151      __tcbp->pointer_guard = (value);})
00152 #define THREAD_COPY_POINTER_GUARD(descr) \
00153   ({ tcbhead_t *__tcbp;                                                     \
00154      __asm __volatile ("stc gbr,%0" : "=r" (__tcbp));                       \
00155      ((tcbhead_t *) (descr + 1))->pointer_guard  = __tcbp->pointer_guard;})
00156 
00157 /* Get and set the global scope generation counter in struct pthread.  */
00158 #define THREAD_GSCOPE_FLAG_UNUSED 0
00159 #define THREAD_GSCOPE_FLAG_USED   1
00160 #define THREAD_GSCOPE_FLAG_WAIT   2
00161 #define THREAD_GSCOPE_RESET_FLAG() \
00162   do                                                                \
00163     { int __res                                                            \
00164        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00165                             THREAD_GSCOPE_FLAG_UNUSED);             \
00166       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00167        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00168     }                                                               \
00169   while (0)
00170 #define THREAD_GSCOPE_SET_FLAG() \
00171   do                                                                \
00172     {                                                               \
00173       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00174       atomic_write_barrier ();                                             \
00175     }                                                               \
00176   while (0)
00177 #define THREAD_GSCOPE_WAIT() \
00178   GL(dl_wait_lookup_done) ()
00179 
00180 #endif /* __ASSEMBLER__ */
00181 
00182 #endif /* tls.h */