td_thr_tsd.c 2.7 KB

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