1 /* Get a thread-specific data pointer for a thread.
2    Copyright (C) 1999-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <stdint.h>
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   td_err_e err;
27   psaddr_t tk_seq, level1, level2, seq, value;
28   void *copy;
29   uint32_t pthread_key_2ndlevel_size, idx1st, idx2nd;
30 
31   LOG ("td_thr_tsd");
32 
33   /* Get the key entry.  */
34   err = DB_GET_VALUE (tk_seq, th->th_ta_p, __pthread_keys, tk);
35   if (err == TD_NOAPLIC)
36     return TD_BADKEY;
37   if (err != TD_OK)
38     return err;
39 
40   /* Fail if this key is not at all used.  */
41   if (((uintptr_t) tk_seq & 1) == 0)
42     return TD_BADKEY;
43 
44   /* This makes sure we have the size information on hand.  */
45   err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p, 0, pthread_key_data_level2,
46 			      data, 1);
47   if (err != TD_OK)
48     return err;
49 
50   /* Compute the indices.  */
51   pthread_key_2ndlevel_size
52     = DB_DESC_NELEM (th->th_ta_p->ta_field_pthread_key_data_level2_data);
53   idx1st = tk / pthread_key_2ndlevel_size;
54   idx2nd = tk % pthread_key_2ndlevel_size;
55 
56   /* Now fetch the first level pointer.  */
57   err = DB_GET_FIELD (level1, th->th_ta_p, th->th_unique, pthread,
58 		      specific, idx1st);
59   if (err == TD_NOAPLIC)
60     return TD_DBERR;
61   if (err != TD_OK)
62     return err;
63 
64   /* Check the pointer to the second level array.  */
65   if (level1 == 0)
66     return TD_NOTSD;
67 
68   /* Locate the element within the second level array.  */
69   err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p,
70 			      level1, pthread_key_data_level2, data, idx2nd);
71   if (err == TD_NOAPLIC)
72     return TD_DBERR;
73   if (err != TD_OK)
74     return err;
75 
76   /* Now copy in that whole structure.  */
77   err = DB_GET_STRUCT (copy, th->th_ta_p, level2, pthread_key_data);
78   if (err != TD_OK)
79     return err;
80 
81   /* Check whether the data is valid.  */
82   err = DB_GET_FIELD_LOCAL (seq, th->th_ta_p, copy, pthread_key_data, seq, 0);
83   if (err != TD_OK)
84     return err;
85   if (seq != tk_seq)
86     return TD_NOTSD;
87 
88   /* Finally, fetch the value.  */
89   err = DB_GET_FIELD_LOCAL (value, th->th_ta_p, copy, pthread_key_data,
90 			    data, 0);
91   if (err == TD_OK)
92     *data = value;
93 
94   return err;
95 }
96