Back to index

glibc  2.9
tls.h
Go to the documentation of this file.
00001 /* Definition for thread-local data handling.  NPTL/Alpha 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       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 #ifndef __ASSEMBLER__
00052 
00053 /* Get system call information.  */
00054 # include <sysdep.h>
00055 
00056 /* The TP points to the start of the thread blocks.  */
00057 # define TLS_DTV_AT_TP      1
00058 
00059 /* Get the thread descriptor definition.  */
00060 # include <nptl/descr.h>
00061 
00062 typedef struct
00063 {
00064   dtv_t *dtv;
00065   void *__private;
00066 } tcbhead_t;
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 16
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             16
00082 
00083 /* Install the dtv pointer.  The pointer passed is to the element with
00084    index -1 which contain the length.  */
00085 # define INSTALL_DTV(tcbp, dtvp) \
00086   (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
00087 
00088 /* Install new dtv for current thread.  */
00089 # define INSTALL_NEW_DTV(dtv) \
00090   (THREAD_DTV() = (dtv))
00091 
00092 /* Return dtv of given thread descriptor.  */
00093 # define GET_DTV(tcbp) \
00094   (((tcbhead_t *) (tcbp))->dtv)
00095 
00096 /* Code to initially initialize the thread pointer.  This might need
00097    special attention since 'errno' is not yet available and if the
00098    operation can cause a failure 'errno' must not be touched.  */
00099 # define TLS_INIT_TP(tcbp, secondcall) \
00100   (__builtin_set_thread_pointer ((void *)(tcbp)), NULL)
00101 
00102 /* Return the address of the dtv for the current thread.  */
00103 # define THREAD_DTV() \
00104   (((tcbhead_t *) __builtin_thread_pointer ())->dtv)
00105 
00106 /* Return the thread descriptor for the current thread.  */
00107 # define THREAD_SELF \
00108  ((struct pthread *)__builtin_thread_pointer () - 1)
00109 
00110 /* Magic for libthread_db to know how to do THREAD_SELF.  */
00111 # define DB_THREAD_SELF \
00112   REGISTER (64, 64, 32 * 8, -sizeof (struct pthread))
00113 
00114 /* Access to data in the thread descriptor is easy.  */
00115 #define THREAD_GETMEM(descr, member) \
00116   descr->member
00117 #define THREAD_GETMEM_NC(descr, member, idx) \
00118   descr->member[idx]
00119 #define THREAD_SETMEM(descr, member, value) \
00120   descr->member = (value)
00121 #define THREAD_SETMEM_NC(descr, member, idx, value) \
00122   descr->member[idx] = (value)
00123 
00124 /* Get and set the global scope generation counter in struct pthread.  */
00125 #define THREAD_GSCOPE_FLAG_UNUSED 0
00126 #define THREAD_GSCOPE_FLAG_USED   1
00127 #define THREAD_GSCOPE_FLAG_WAIT   2
00128 #define THREAD_GSCOPE_RESET_FLAG() \
00129   do                                                                \
00130     { int __res                                                            \
00131        = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,            \
00132                             THREAD_GSCOPE_FLAG_UNUSED);             \
00133       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                \
00134        lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
00135     }                                                               \
00136   while (0)
00137 #define THREAD_GSCOPE_SET_FLAG() \
00138   do                                                                \
00139     {                                                               \
00140       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;           \
00141       atomic_write_barrier ();                                             \
00142     }                                                               \
00143   while (0)
00144 #define THREAD_GSCOPE_WAIT() \
00145   GL(dl_wait_lookup_done) ()
00146 
00147 #endif /* __ASSEMBLER__ */
00148 
00149 #endif /* tls.h */