td_thr_tsd.c 2.6 KB

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