td_thr_tsd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include "thread_dbP.h"
  18. td_err_e
  19. td_thr_tsd (const td_thrhandle_t *th, const thread_key_t tk, void **data)
  20. {
  21. td_err_e err;
  22. psaddr_t tk_seq, level1, level2, seq, value;
  23. void *copy;
  24. uint32_t pthread_key_2ndlevel_size, idx1st, idx2nd;
  25. LOG ("td_thr_tsd");
  26. /* Get the key entry. */
  27. err = DB_GET_VALUE (tk_seq, th->th_ta_p, __pthread_keys, tk);
  28. if (err == TD_NOAPLIC)
  29. return TD_BADKEY;
  30. if (err != TD_OK)
  31. return err;
  32. /* Fail if this key is not at all used. */
  33. if (((uintptr_t) tk_seq & 1) == 0)
  34. return TD_BADKEY;
  35. /* This makes sure we have the size information on hand. */
  36. err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p, 0, pthread_key_data_level2,
  37. data, 1);
  38. if (err != TD_OK)
  39. return err;
  40. /* Compute the indeces. */
  41. pthread_key_2ndlevel_size
  42. = DB_DESC_NELEM (th->th_ta_p->ta_field_pthread_key_data_level2_data);
  43. idx1st = tk / pthread_key_2ndlevel_size;
  44. idx2nd = tk % pthread_key_2ndlevel_size;
  45. /* Now fetch the first level pointer. */
  46. err = DB_GET_FIELD (level1, th->th_ta_p, th->th_unique, pthread,
  47. specific, idx1st);
  48. if (err == TD_NOAPLIC)
  49. return TD_DBERR;
  50. if (err != TD_OK)
  51. return err;
  52. /* Check the pointer to the second level array. */
  53. if (level1 == 0)
  54. return TD_NOTSD;
  55. /* Locate the element within the second level array. */
  56. err = DB_GET_FIELD_ADDRESS (level2, th->th_ta_p,
  57. level1, pthread_key_data_level2, data, idx2nd);
  58. if (err == TD_NOAPLIC)
  59. return TD_DBERR;
  60. if (err != TD_OK)
  61. return err;
  62. /* Now copy in that whole structure. */
  63. err = DB_GET_STRUCT (copy, th->th_ta_p, level2, pthread_key_data);
  64. if (err != TD_OK)
  65. return err;
  66. /* Check whether the data is valid. */
  67. err = DB_GET_FIELD_LOCAL (seq, th->th_ta_p, copy, pthread_key_data, seq, 0);
  68. if (err != TD_OK)
  69. return err;
  70. if (seq != tk_seq)
  71. return TD_NOTSD;
  72. /* Finally, fetch the value. */
  73. err = DB_GET_FIELD_LOCAL (value, th->th_ta_p, copy, pthread_key_data,
  74. data, 0);
  75. if (err == TD_OK)
  76. *data = value;
  77. return err;
  78. }