1/* Get a thread-specific data pointer for a thread.
2 Copyright (C) 1999-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.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 <stdint.h>
21#include "thread_dbP.h"
22
23
24td_err_e
25td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
26{
27 td_err_e err;
28 psaddr_t tk_seq, level1, level2, seq, value;
29 void *copy;
30 uint32_t pthread_key_2ndlevel_size, idx1st, idx2nd;
31
32 LOG ("td_thr_tsd");
33
34 /* Get the key entry. */
35 err = DB_GET_VALUE (tk_seq, th->th_ta_p, __pthread_keys, tk);
36 if (err == TD_NOAPLIC)
37 return TD_BADKEY;
38 if (err != TD_OK)
39 return err;
40
41 /* Fail if this key is not at all used. */
42 if (((uintptr_t) tk_seq & 1) == 0)
43 return TD_BADKEY;
44
45 /* This makes sure we have the size information on hand. */
46 err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p, 0, pthread_key_data_level2,
47 data, 1);
48 if (err != TD_OK)
49 return err;
50
51 /* Compute the indeces. */
52 pthread_key_2ndlevel_size
53 = DB_DESC_NELEM (th->th_ta_p->ta_field_pthread_key_data_level2_data);
54 idx1st = tk / pthread_key_2ndlevel_size;
55 idx2nd = tk % pthread_key_2ndlevel_size;
56
57 /* Now fetch the first level pointer. */
58 err = DB_GET_FIELD (level1, th->th_ta_p, th->th_unique, pthread,
59 specific, idx1st);
60 if (err == TD_NOAPLIC)
61 return TD_DBERR;
62 if (err != TD_OK)
63 return err;
64
65 /* Check the pointer to the second level array. */
66 if (level1 == 0)
67 return TD_NOTSD;
68
69 /* Locate the element within the second level array. */
70 err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p,
71 level1, pthread_key_data_level2, data, idx2nd);
72 if (err == TD_NOAPLIC)
73 return TD_DBERR;
74 if (err != TD_OK)
75 return err;
76
77 /* Now copy in that whole structure. */
78 err = DB_GET_STRUCT (copy, th->th_ta_p, level2, pthread_key_data);
79 if (err != TD_OK)
80 return err;
81
82 /* Check whether the data is valid. */
83 err = DB_GET_FIELD_LOCAL (seq, th->th_ta_p, copy, pthread_key_data, seq, 0);
84 if (err != TD_OK)
85 return err;
86 if (seq != tk_seq)
87 return TD_NOTSD;
88
89 /* Finally, fetch the value. */
90 err = DB_GET_FIELD_LOCAL (value, th->th_ta_p, copy, pthread_key_data,
91 data, 0);
92 if (err == TD_OK)
93 *data = value;
94
95 return err;
96}
97