1 /* Get a thread-specific data pointer for a thread.
2    Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19 
20 #include "thread_dbP.h"
21 
22 
23 td_err_e
td_thr_tsd(const td_thrhandle_t * th,const thread_key_t tk,void ** data)24 td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
25 {
26   struct _pthread_descr_struct pds;
27   struct pthread_key_struct *keys = th->th_ta_p->keys;
28   struct pthread_key_struct key;
29   int pthread_keys_max = th->th_ta_p->pthread_keys_max;
30   int pthread_key_2ndlevel_size = th->th_ta_p->pthread_key_2ndlevel_size;
31   unsigned int idx1st;
32   unsigned int idx2nd;
33   void *p;
34 
35   LOG ("td_thr_tsd");
36 
37   /* If there is no thread descriptor there cannot be any thread
38      specific data.  */
39   if (th->th_unique == NULL)
40     return TD_BADKEY;
41 
42   /* Get the thread descriptor.  */
43   if (ps_pdread (th->th_ta_p->ph, th->th_unique, &pds,
44 		 sizeof (struct _pthread_descr_struct)) != PS_OK)
45     return TD_ERR;	/* XXX Other error value?  */
46 
47   /* Check correct value of key.  */
48   if (tk >= pthread_keys_max)
49     return TD_BADKEY;
50 
51   /* Get the key entry.  */
52   if (ps_pdread (th->th_ta_p->ph, &keys[tk], &key,
53 		 sizeof (struct pthread_key_struct)) != PS_OK)
54     return TD_ERR;	/* XXX Other error value?  */
55 
56   /* Fail if this key is not at all used.  */
57   if (! key.in_use)
58     return TD_BADKEY;
59 
60   /* Compute the indeces.  */
61   idx1st = tk / pthread_key_2ndlevel_size;
62   idx2nd = tk % pthread_key_2ndlevel_size;
63 
64   /* Check the pointer to the second level array.  */
65   if (pds.p_specific[idx1st] == NULL)
66     return TD_NOTSD;
67 
68   /* Now get the real key.
69      XXX I don't know whether it's correct but there is currently no
70      easy way to determine whether a key was never set or the value
71      is NULL.  We return an error whenever the value is NULL.  */
72   if (ps_pdread (th->th_ta_p->ph, &pds.p_specific[idx1st][idx2nd], &p,
73 		 sizeof (void *)) != PS_OK)
74     return TD_ERR;
75 
76   if (p != NULL)
77     *data = p;
78 
79   return p != NULL ? TD_OK : TD_NOTSD;
80 }
81