Update.
[glibc.git] / linuxthreads_db / td_thr_tsd.c
blobaa362fe68a788c2009a22ad38ac28f33345758e9
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.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include "thread_dbP.h"
22 #include <linuxthreads/internals.h>
25 td_err_e
26 td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
28 struct _pthread_descr_struct pds;
29 struct pthread_key_struct *keys = th->th_ta_p->keys;
30 struct pthread_key_struct key;
31 int pthread_keys_max = th->th_ta_p->pthread_keys_max;
32 int pthread_key_2ndlevel_size = th->th_ta_p->pthread_key_2ndlevel_size;
33 unsigned int idx1st;
34 unsigned int idx2nd;
35 void *p;
37 LOG ("td_thr_tsd");
39 /* Get the thread descriptor. */
40 if (ps_pdread (th->th_ta_p->ph, th->th_unique, &pds,
41 sizeof (struct _pthread_descr_struct)) != PS_OK)
42 return TD_ERR; /* XXX Other error value? */
44 /* Check correct value of key. */
45 if (tk >= pthread_keys_max)
46 return TD_BADKEY;
48 /* Get the key entry. */
49 if (ps_pdread (th->th_ta_p->ph, keys, &key,
50 sizeof (struct pthread_key_struct)) != PS_OK)
51 return TD_ERR; /* XXX Other error value? */
53 /* Fail if this key is not at all used. */
54 if (! key.in_use)
55 return TD_BADKEY;
57 /* Compute the indeces. */
58 idx1st = tk / pthread_key_2ndlevel_size;
59 idx2nd = tk % pthread_key_2ndlevel_size;
61 /* Check the pointer to the second level array. */
62 if (pds.p_specific[idx1st] == NULL)
63 return TD_NOTSD;
65 /* Now get the real key.
66 XXX I don't know whether it's correct but there is currently no
67 easy way to determine whether a key was never set or the value
68 is NULL. We return an error whenever the value is NULL. */
69 if (ps_pdread (th->th_ta_p->ph, &pds.p_specific[idx1st][idx2nd], &p,
70 sizeof (void *)) != PS_OK)
71 return TD_ERR;
73 if (p != NULL)
74 *data = p;
76 return p != NULL ? TD_OK : TD_NOTSD;