Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/s390 version.
00002    Copyright (C) 2003, 2004, 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       1
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 
00033 /* Type for the dtv.  */
00034 typedef union dtv
00035 {
00036   size_t counter;
00037   struct
00038   {
00039     void *val;
00040     bool is_static;
00041   } pointer;
00042 } dtv_t;
00043 
00044 
00045 typedef struct
00046 {
00047   void *tcb;         /* Pointer to the TCB.  Not necessary the
00048                         thread descriptor used by libpthread.  */
00049   dtv_t *dtv;
00050   void *self;        /* Pointer to the thread descriptor.  */
00051   int multiple_threads;
00052   uintptr_t sysinfo;
00053   uintptr_t stack_guard;
00054   int gscope_flag;
00055 #ifndef __ASSUME_PRIVATE_FUTEX
00056   int private_futex;
00057 #endif
00058 } tcbhead_t;
00059 
00060 # ifndef __s390x__
00061 #  define TLS_MULTIPLE_THREADS_IN_TCB 1
00062 # endif
00063 
00064 #else /* __ASSEMBLER__ */
00065 # include <tcb-offsets.h>
00066 #endif
00067 
00068 
00069 /* We require TLS support in the tools.  */
00070 #ifndef HAVE_TLS_SUPPORT
00071 # error "TLS support is required."
00072 #endif
00073 
00074 /* Alignment requirement for the stack.  For IA-32 this is governed by
00075    the SSE memory functions.  */
00076 #define STACK_ALIGN  16
00077 
00078 #ifndef __ASSEMBLER__
00079 /* Get system call information.  */
00080 # include <sysdep.h>
00081 
00082 /* Get the thread descriptor definition.  */
00083 # include <nptl/descr.h>
00084 
00085 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
00086    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
00087    struct pthread even when not linked with -lpthread.  */
00088 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
00089 
00090 /* Alignment requirements for the initial TCB.  */
00091 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
00092 
00093 /* This is the size of the TCB.  */
00094 # define TLS_TCB_SIZE sizeof (struct pthread)
00095 
00096 /* Alignment requirements for the TCB.  */
00097 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
00098 
00099 /* The TCB can have any size and the memory following the address the
00100    thread pointer points to is unspecified.  Allocate the TCB there.  */
00101 # define TLS_TCB_AT_TP      1
00102 
00103 
00104 /* Install the dtv pointer.  The pointer passed is to the element with
00105    index -1 which contain the length.  */
00106 #  define INSTALL_DTV(descr, dtvp) \
00107   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
00108 
00109 /* Install new dtv for current thread.  */
00110 #  define INSTALL_NEW_DTV(dtv) \
00111   (((tcbhead_t *) __builtin_thread_pointer ())->dtv = (dtv))
00112 
00113 /* Return dtv of given thread descriptor.  */
00114 #  define GET_DTV(descr) \
00115   (((tcbhead_t *) (descr))->dtv)
00116 
00117 #if defined NEED_DL_SYSINFO && defined SHARED
00118 # define INIT_SYSINFO \
00119   _head->sysinfo = GLRO(dl_sysinfo)
00120 #else
00121 # define INIT_SYSINFO
00122 #endif
00123 
00124 /* Code to initially initialize the thread pointer.  This might need
00125    special attention since 'errno' is not yet available and if the
00126    operation can cause a failure 'errno' must not be touched.  */
00127 # define TLS_INIT_TP(thrdescr, secondcall) \
00128   ({ void *_thrdescr = (thrdescr);                                   \
00129      tcbhead_t *_head = _thrdescr;                                   \
00130                                                                      \
00131      _head->tcb = _thrdescr;                                                \
00132      /* For now the thread descriptor is at the same address.  */           \
00133      _head->self = _thrdescr;                                               \
00134      /* New syscall handling support.  */                            \
00135      INIT_SYSINFO;                                                   \
00136                                                                      \
00137     __builtin_set_thread_pointer (_thrdescr);                               \
00138     NULL;                                                            \
00139   })
00140 
00141 /* Return the address of the dtv for the current thread.  */
00142 #  define THREAD_DTV() \
00143   (((tcbhead_t *) __builtin_thread_pointer ())->dtv)
00144 
00145 /* Return the thread descriptor for the current thread.  */
00146 # define THREAD_SELF ((struct pthread *) __builtin_thread_pointer ())
00147 
00148 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00149 # define DB_THREAD_SELF REGISTER (32, 32, 18 * 4, 0) \
00150                      REGISTER (64, __WORDSIZE, 18 * 8, 0)
00151 
00152 /* Access to data in the thread descriptor is easy.  */
00153 #define THREAD_GETMEM(descr, member) \
00154   descr->member
00155 #define THREAD_GETMEM_NC(descr, member, idx) \
00156   descr->member[idx]
00157 #define THREAD_SETMEM(descr, member, value) \
00158   descr->member = (value)
00159 #define THREAD_SETMEM_NC(descr, member, idx, value) \
00160   descr->member[idx] = (value)
00161 
00162 /* Set the stack guard field in TCB head.  */
00163 #define THREAD_SET_STACK_GUARD(value) \
00164   do                                                                 \
00165    {                                                                 \
00166      __asm __volatile ("" : : : "a0", "a1");                                \
00167      THREAD_SETMEM (THREAD_SELF, header.stack_guard, value);                \
00168    }                                                                 \
00169   while (0)
00170 #define THREAD_COPY_STACK_GUARD(descr) \
00171   ((descr)->header.stack_guard                                              \
00172    = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
00173 
00174 /* s390 doesn't have HP_TIMING_*, so for the time being
00175    use stack_guard as pointer_guard.  */
00176 #define THREAD_GET_POINTER_GUARD() \
00177   THREAD_GETMEM (THREAD_SELF, header.stack_guard)
00178 #define THREAD_SET_POINTER_GUARD(value)
00179 #define THREAD_COPY_POINTER_GUARD(descr)
00180 
00181 /* Get and set the global scope generation counter in struct pthread.  */
00182 #define THREAD_GSCOPE_FLAG_UNUSED 0
00183 #define THREAD_GSCOPE_FLAG_USED   1
00184 #define THREAD_GSCOPE_FLAG_WAIT   2
00185 #define THREAD_GSCOPE_RESET_FLAG() \
00186   do                                                                \
00187     { int __res                                                            \
00188        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00189                             THREAD_GSCOPE_FLAG_UNUSED);             \
00190       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00191        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00192     }                                                               \
00193   while (0)
00194 #define THREAD_GSCOPE_SET_FLAG() \
00195   do                                                                \
00196     {                                                               \
00197       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00198       atomic_write_barrier ();                                             \
00199     }                                                               \
00200   while (0)
00201 #define THREAD_GSCOPE_WAIT() \
00202   GL(dl_wait_lookup_done) ()
00203 
00204 #endif /* __ASSEMBLER__ */
00205 
00206 #endif /* tls.h */