td_thr_tsd.c 2.5 KB

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