td_thr_tsd.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Get a thread-specific data pointer for a thread.
  2. Copyright (C) 1999,2001,2002,2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 1999.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "thread_dbP.h"
  17. td_err_e
  18. td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
  19. {
  20. td_err_e err;
  21. psaddr_t tk_seq, level1, level2, seq, value;
  22. void *copy;
  23. uint32_t pthread_key_2ndlevel_size, idx1st, idx2nd;
  24. LOG ("td_thr_tsd");
  25. /* Get the key entry. */
  26. err = DB_GET_VALUE (tk_seq, th->th_ta_p, __pthread_keys, tk);
  27. if (err == TD_NOAPLIC)
  28. return TD_BADKEY;
  29. if (err != TD_OK)
  30. return err;
  31. /* Fail if this key is not at all used. */
  32. if (((uintptr_t) tk_seq & 1) == 0)
  33. return TD_BADKEY;
  34. /* This makes sure we have the size information on hand. */
  35. err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p, 0, pthread_key_data_level2,
  36. data, 1);
  37. if (err != TD_OK)
  38. return err;
  39. /* Compute the indeces. */
  40. pthread_key_2ndlevel_size
  41. = DB_DESC_NELEM (th->th_ta_p->ta_field_pthread_key_data_level2_data);
  42. idx1st = tk / pthread_key_2ndlevel_size;
  43. idx2nd = tk % pthread_key_2ndlevel_size;
  44. /* Now fetch the first level pointer. */
  45. err = DB_GET_FIELD (level1, th->th_ta_p, th->th_unique, pthread,
  46. specific, idx1st);
  47. if (err == TD_NOAPLIC)
  48. return TD_DBERR;
  49. if (err != TD_OK)
  50. return err;
  51. /* Check the pointer to the second level array. */
  52. if (level1 == 0)
  53. return TD_NOTSD;
  54. /* Locate the element within the second level array. */
  55. err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p,
  56. level1, pthread_key_data_level2, data, idx2nd);
  57. if (err == TD_NOAPLIC)
  58. return TD_DBERR;
  59. if (err != TD_OK)
  60. return err;
  61. /* Now copy in that whole structure. */
  62. err = DB_GET_STRUCT (copy, th->th_ta_p, level2, pthread_key_data);
  63. if (err != TD_OK)
  64. return err;
  65. /* Check whether the data is valid. */
  66. err = DB_GET_FIELD_LOCAL (seq, th->th_ta_p, copy, pthread_key_data, seq, 0);
  67. if (err != TD_OK)
  68. return err;
  69. if (seq != tk_seq)
  70. return TD_NOTSD;
  71. /* Finally, fetch the value. */
  72. err = DB_GET_FIELD_LOCAL (value, th->th_ta_p, copy, pthread_key_data,
  73. data, 0);
  74. if (err == TD_OK)
  75. *data = value;
  76. return err;
  77. }