Back to index

glibc  2.9
td_thr_tsd.c
Go to the documentation of this file.
00001 /* Get a thread-specific data pointer for a thread.
00002    Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include "thread_dbP.h"
00022 #include <linuxthreads/internals.h>
00023 
00024 
00025 td_err_e
00026 td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
00027 {
00028   struct _pthread_descr_struct pds;
00029   struct pthread_key_struct *keys = th->th_ta_p->keys;
00030   struct pthread_key_struct key;
00031   int pthread_keys_max = th->th_ta_p->pthread_keys_max;
00032   int pthread_key_2ndlevel_size = th->th_ta_p->pthread_key_2ndlevel_size;
00033   unsigned int idx1st;
00034   unsigned int idx2nd;
00035   void *p;
00036 
00037   LOG ("td_thr_tsd");
00038 
00039   /* If there is no thread descriptor there cannot be any thread
00040      specific data.  */
00041   if (th->th_unique == NULL)
00042     return TD_BADKEY;
00043 
00044   /* Get the thread descriptor.  */
00045   if (ps_pdread (th->th_ta_p->ph, th->th_unique, &pds,
00046                sizeof (struct _pthread_descr_struct)) != PS_OK)
00047     return TD_ERR;   /* XXX Other error value?  */
00048 
00049   /* Check correct value of key.  */
00050   if (tk >= pthread_keys_max)
00051     return TD_BADKEY;
00052 
00053   /* Get the key entry.  */
00054   if (ps_pdread (th->th_ta_p->ph, &keys[tk], &key,
00055                sizeof (struct pthread_key_struct)) != PS_OK)
00056     return TD_ERR;   /* XXX Other error value?  */
00057 
00058   /* Fail if this key is not at all used.  */
00059   if (! key.in_use)
00060     return TD_BADKEY;
00061 
00062   /* Compute the indeces.  */
00063   idx1st = tk / pthread_key_2ndlevel_size;
00064   idx2nd = tk % pthread_key_2ndlevel_size;
00065 
00066   /* Check the pointer to the second level array.  */
00067   if (pds.p_specific[idx1st] == NULL)
00068     return TD_NOTSD;
00069 
00070   /* Now get the real key.
00071      XXX I don't know whether it's correct but there is currently no
00072      easy way to determine whether a key was never set or the value
00073      is NULL.  We return an error whenever the value is NULL.  */
00074   if (ps_pdread (th->th_ta_p->ph, &pds.p_specific[idx1st][idx2nd], &p,
00075                sizeof (void *)) != PS_OK)
00076     return TD_ERR;
00077 
00078   if (p != NULL)
00079     *data = p;
00080 
00081   return p != NULL ? TD_OK : TD_NOTSD;
00082 }