Back to index

glibc  2.9
descr.h
Go to the documentation of this file.
00001 /* Copyright (C) 2002-2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 _DESCR_H
00021 #define _DESCR_H     1
00022 
00023 #include <limits.h>
00024 #include <sched.h>
00025 #include <setjmp.h>
00026 #include <stdbool.h>
00027 #include <sys/types.h>
00028 #include <hp-timing.h>
00029 #include <list.h>
00030 #include <lowlevellock.h>
00031 #include <pthreaddef.h>
00032 #include <dl-sysdep.h>
00033 #include "../nptl_db/thread_db.h"
00034 #include <tls.h>
00035 #ifdef HAVE_FORCED_UNWIND
00036 # include <unwind.h>
00037 #endif
00038 #define __need_res_state
00039 #include <resolv.h>
00040 #include <kernel-features.h>
00041 
00042 #ifndef TCB_ALIGNMENT
00043 # define TCB_ALIGNMENT      sizeof (double)
00044 #endif
00045 
00046 
00047 /* We keep thread specific data in a special data structure, a two-level
00048    array.  The top-level array contains pointers to dynamically allocated
00049    arrays of a certain number of data pointers.  So we can implement a
00050    sparse array.  Each dynamic second-level array has
00051         PTHREAD_KEY_2NDLEVEL_SIZE
00052    entries.  This value shouldn't be too large.  */
00053 #define PTHREAD_KEY_2NDLEVEL_SIZE       32
00054 
00055 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
00056    keys in each subarray.  */
00057 #define PTHREAD_KEY_1STLEVEL_SIZE \
00058   ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
00059    / PTHREAD_KEY_2NDLEVEL_SIZE)
00060 
00061 
00062 
00063 
00064 /* Internal version of the buffer to store cancellation handler
00065    information.  */
00066 struct pthread_unwind_buf
00067 {
00068   struct
00069   {
00070     __jmp_buf jmp_buf;
00071     int mask_was_saved;
00072   } cancel_jmp_buf[1];
00073 
00074   union
00075   {
00076     /* This is the placeholder of the public version.  */
00077     void *pad[4];
00078 
00079     struct
00080     {
00081       /* Pointer to the previous cleanup buffer.  */
00082       struct pthread_unwind_buf *prev;
00083 
00084       /* Backward compatibility: state of the old-style cleanup
00085         handler at the time of the previous new-style cleanup handler
00086         installment.  */
00087       struct _pthread_cleanup_buffer *cleanup;
00088 
00089       /* Cancellation type before the push call.  */
00090       int canceltype;
00091     } data;
00092   } priv;
00093 };
00094 
00095 
00096 /* Opcodes and data types for communication with the signal handler to
00097    change user/group IDs.  */
00098 struct xid_command
00099 {
00100   int syscall_no;
00101   long int id[3];
00102   volatile int cntr;
00103 };
00104 
00105 
00106 /* Data structure used by the kernel to find robust futexes.  */
00107 struct robust_list_head
00108 {
00109   void *list;
00110   long int futex_offset;
00111   void *list_op_pending;
00112 };
00113 
00114 
00115 /* Data strcture used to handle thread priority protection.  */
00116 struct priority_protection_data
00117 {
00118   int priomax;
00119   unsigned int priomap[];
00120 };
00121 
00122 
00123 /* Thread descriptor data structure.  */
00124 struct pthread
00125 {
00126   union
00127   {
00128 #if !TLS_DTV_AT_TP
00129     /* This overlaps the TCB as used for TLS without threads (see tls.h).  */
00130     tcbhead_t header;
00131 #else
00132     struct
00133     {
00134       int multiple_threads;
00135       int gscope_flag;
00136 # ifndef __ASSUME_PRIVATE_FUTEX
00137       int private_futex;
00138 # endif
00139     } header;
00140 #endif
00141 
00142     /* This extra padding has no special purpose, and this structure layout
00143        is private and subject to change without affecting the official ABI.
00144        We just have it here in case it might be convenient for some
00145        implementation-specific instrumentation hack or suchlike.  */
00146     void *__padding[16];
00147   };
00148 
00149   /* This descriptor's link on the `stack_used' or `__stack_user' list.  */
00150   list_t list;
00151 
00152   /* Thread ID - which is also a 'is this thread descriptor (and
00153      therefore stack) used' flag.  */
00154   pid_t tid;
00155 
00156   /* Process ID - thread group ID in kernel speak.  */
00157   pid_t pid;
00158 
00159   /* List of robust mutexes the thread is holding.  */
00160 #ifdef __PTHREAD_MUTEX_HAVE_PREV
00161   void *robust_prev;
00162   struct robust_list_head robust_head;
00163 
00164   /* The list above is strange.  It is basically a double linked list
00165      but the pointer to the next/previous element of the list points
00166      in the middle of the object, the __next element.  Whenever
00167      casting to __pthread_list_t we need to adjust the pointer
00168      first.  */
00169 # define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
00170 
00171 # define ENQUEUE_MUTEX_BOTH(mutex, val)                                     \
00172   do {                                                               \
00173     __pthread_list_t *next = (__pthread_list_t *)                           \
00174       ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul)   \
00175        - QUEUE_PTR_ADJUST);                                          \
00176     next->__prev = (void *) &mutex->__data.__list.__next;                   \
00177     mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF,               \
00178                                            robust_head.list);        \
00179     mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head;       \
00180     THREAD_SETMEM (THREAD_SELF, robust_head.list,                           \
00181                  (void *) (((uintptr_t) &mutex->__data.__list.__next)       \
00182                           | val));                                   \
00183   } while (0)
00184 # define DEQUEUE_MUTEX(mutex) \
00185   do {                                                               \
00186     __pthread_list_t *next = (__pthread_list_t *)                           \
00187       ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul)          \
00188        - QUEUE_PTR_ADJUST);                                          \
00189     next->__prev = mutex->__data.__list.__prev;                             \
00190     __pthread_list_t *prev = (__pthread_list_t *)                           \
00191       ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul)          \
00192        - QUEUE_PTR_ADJUST);                                          \
00193     prev->__next = mutex->__data.__list.__next;                             \
00194     mutex->__data.__list.__prev = NULL;                                     \
00195     mutex->__data.__list.__next = NULL;                                     \
00196   } while (0)
00197 #else
00198   union
00199   {
00200     __pthread_slist_t robust_list;
00201     struct robust_list_head robust_head;
00202   };
00203 
00204 # define ENQUEUE_MUTEX_BOTH(mutex, val)                                     \
00205   do {                                                               \
00206     mutex->__data.__list.__next                                             \
00207       = THREAD_GETMEM (THREAD_SELF, robust_list.__next);                    \
00208     THREAD_SETMEM (THREAD_SELF, robust_list.__next,                         \
00209                  (void *) (((uintptr_t) &mutex->__data.__list) | val));     \
00210   } while (0)
00211 # define DEQUEUE_MUTEX(mutex) \
00212   do {                                                               \
00213     __pthread_slist_t *runp = (__pthread_slist_t *)                         \
00214       (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
00215     if (runp == &mutex->__data.__list)                                      \
00216       THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next);        \
00217     else                                                             \
00218       {                                                                     \
00219        __pthread_slist_t *next = (__pthread_slist_t *)               \
00220          (((uintptr_t) runp->__next) & ~1ul);                               \
00221        while (next != &mutex->__data.__list)                                \
00222          {                                                           \
00223            runp = next;                                              \
00224            next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
00225          }                                                           \
00226                                                                      \
00227        runp->__next = next->__next;                                         \
00228        mutex->__data.__list.__next = NULL;                                  \
00229       }                                                                     \
00230   } while (0)
00231 #endif
00232 #define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
00233 #define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
00234 
00235   /* List of cleanup buffers.  */
00236   struct _pthread_cleanup_buffer *cleanup;
00237 
00238   /* Unwind information.  */
00239   struct pthread_unwind_buf *cleanup_jmp_buf;
00240 #define HAVE_CLEANUP_JMP_BUF
00241 
00242   /* Flags determining processing of cancellation.  */
00243   int cancelhandling;
00244   /* Bit set if cancellation is disabled.  */
00245 #define CANCELSTATE_BIT            0
00246 #define CANCELSTATE_BITMASK 0x01
00247   /* Bit set if asynchronous cancellation mode is selected.  */
00248 #define CANCELTYPE_BIT             1
00249 #define CANCELTYPE_BITMASK  0x02
00250   /* Bit set if canceling has been initiated.  */
00251 #define CANCELING_BIT              2
00252 #define CANCELING_BITMASK   0x04
00253   /* Bit set if canceled.  */
00254 #define CANCELED_BIT        3
00255 #define CANCELED_BITMASK    0x08
00256   /* Bit set if thread is exiting.  */
00257 #define EXITING_BIT         4
00258 #define EXITING_BITMASK            0x10
00259   /* Bit set if thread terminated and TCB is freed.  */
00260 #define TERMINATED_BIT             5
00261 #define TERMINATED_BITMASK  0x20
00262   /* Bit set if thread is supposed to change XID.  */
00263 #define SETXID_BIT          6
00264 #define SETXID_BITMASK             0x40
00265   /* Mask for the rest.  Helps the compiler to optimize.  */
00266 #define CANCEL_RESTMASK            0xffffff80
00267 
00268 #define CANCEL_ENABLED_AND_CANCELED(value) \
00269   (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK            \
00270               | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
00271 #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
00272   (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK    \
00273               | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK))     \
00274    == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
00275 
00276   /* Flags.  Including those copied from the thread attribute.  */
00277   int flags;
00278 
00279   /* We allocate one block of references here.  This should be enough
00280      to avoid allocating any memory dynamically for most applications.  */
00281   struct pthread_key_data
00282   {
00283     /* Sequence number.  We use uintptr_t to not require padding on
00284        32- and 64-bit machines.  On 64-bit machines it helps to avoid
00285        wrapping, too.  */
00286     uintptr_t seq;
00287 
00288     /* Data pointer.  */
00289     void *data;
00290   } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
00291 
00292   /* Two-level array for the thread-specific data.  */
00293   struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE];
00294 
00295   /* Flag which is set when specific data is set.  */
00296   bool specific_used;
00297 
00298   /* True if events must be reported.  */
00299   bool report_events;
00300 
00301   /* True if the user provided the stack.  */
00302   bool user_stack;
00303 
00304   /* True if thread must stop at startup time.  */
00305   bool stopped_start;
00306 
00307   /* The parent's cancel handling at the time of the pthread_create
00308      call.  This might be needed to undo the effects of a cancellation.  */
00309   int parent_cancelhandling;
00310 
00311   /* Lock to synchronize access to the descriptor.  */
00312   int lock;
00313 
00314   /* Lock for synchronizing setxid calls.  */
00315   int setxid_futex;
00316 
00317 #if HP_TIMING_AVAIL
00318   /* Offset of the CPU clock at start thread start time.  */
00319   hp_timing_t cpuclock_offset;
00320 #endif
00321 
00322   /* If the thread waits to join another one the ID of the latter is
00323      stored here.
00324 
00325      In case a thread is detached this field contains a pointer of the
00326      TCB if the thread itself.  This is something which cannot happen
00327      in normal operation.  */
00328   struct pthread *joinid;
00329   /* Check whether a thread is detached.  */
00330 #define IS_DETACHED(pd) ((pd)->joinid == (pd))
00331 
00332   /* The result of the thread function.  */
00333   void *result;
00334 
00335   /* Scheduling parameters for the new thread.  */
00336   struct sched_param schedparam;
00337   int schedpolicy;
00338 
00339   /* Start position of the code to be executed and the argument passed
00340      to the function.  */
00341   void *(*start_routine) (void *);
00342   void *arg;
00343 
00344   /* Debug state.  */
00345   td_eventbuf_t eventbuf;
00346   /* Next descriptor with a pending event.  */
00347   struct pthread *nextevent;
00348 
00349 #ifdef HAVE_FORCED_UNWIND
00350   /* Machine-specific unwind info.  */
00351   struct _Unwind_Exception exc;
00352 #endif
00353 
00354   /* If nonzero pointer to area allocated for the stack and its
00355      size.  */
00356   void *stackblock;
00357   size_t stackblock_size;
00358   /* Size of the included guard area.  */
00359   size_t guardsize;
00360   /* This is what the user specified and what we will report.  */
00361   size_t reported_guardsize;
00362 
00363   /* Thread Priority Protection data.  */
00364   struct priority_protection_data *tpp;
00365 
00366   /* Resolver state.  */
00367   struct __res_state res;
00368 
00369   /* This member must be last.  */
00370   char end_padding[];
00371 
00372 #define PTHREAD_STRUCT_END_PADDING \
00373   (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
00374 } __attribute ((aligned (TCB_ALIGNMENT)));
00375 
00376 
00377 #endif /* descr.h */