td_thr_tsd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.
  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. struct _pthread_descr_struct pds;
  22. struct pthread_key_struct *keys = th->th_ta_p->keys;
  23. struct pthread_key_struct key;
  24. int pthread_keys_max = th->th_ta_p->pthread_keys_max;
  25. int pthread_key_2ndlevel_size = th->th_ta_p->pthread_key_2ndlevel_size;
  26. unsigned int idx1st;
  27. unsigned int idx2nd;
  28. void *p;
  29. LOG ("td_thr_tsd");
  30. /* If there is no thread descriptor there cannot be any thread
  31. specific data. */
  32. if (th->th_unique == NULL)
  33. return TD_BADKEY;
  34. /* Get the thread descriptor. */
  35. if (ps_pdread (th->th_ta_p->ph, th->th_unique, &pds,
  36. sizeof (struct _pthread_descr_struct)) != PS_OK)
  37. return TD_ERR; /* XXX Other error value? */
  38. /* Check correct value of key. */
  39. if (tk >= pthread_keys_max)
  40. return TD_BADKEY;
  41. /* Get the key entry. */
  42. if (ps_pdread (th->th_ta_p->ph, &keys[tk], &key,
  43. sizeof (struct pthread_key_struct)) != PS_OK)
  44. return TD_ERR; /* XXX Other error value? */
  45. /* Fail if this key is not at all used. */
  46. if (! key.in_use)
  47. return TD_BADKEY;
  48. /* Compute the indeces. */
  49. idx1st = tk / pthread_key_2ndlevel_size;
  50. idx2nd = tk % pthread_key_2ndlevel_size;
  51. /* Check the pointer to the second level array. */
  52. if (pds.p_specific[idx1st] == NULL)
  53. return TD_NOTSD;
  54. /* Now get the real key.
  55. XXX I don't know whether it's correct but there is currently no
  56. easy way to determine whether a key was never set or the value
  57. is NULL. We return an error whenever the value is NULL. */
  58. if (ps_pdread (th->th_ta_p->ph, &pds.p_specific[idx1st][idx2nd], &p,
  59. sizeof (void *)) != PS_OK)
  60. return TD_ERR;
  61. if (p != NULL)
  62. *data = p;
  63. return p != NULL ? TD_OK : TD_NOTSD;
  64. }