td_ta_map_id2thr.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Map thread ID to thread handle.
  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/internals.h>
  19. td_err_e
  20. td_ta_map_id2thr (const td_thragent_t *ta, pthread_t pt, td_thrhandle_t *th)
  21. {
  22. struct pthread_handle_struct phc;
  23. struct _pthread_descr_struct pds;
  24. int pthread_threads_max;
  25. LOG ("td_ta_map_id2thr");
  26. /* Test whether the TA parameter is ok. */
  27. if (! ta_ok (ta))
  28. return TD_BADTA;
  29. /* Make the following expression a bit smaller. */
  30. pthread_threads_max = ta->pthread_threads_max;
  31. /* We can compute the entry in the handle array we want. */
  32. if (ps_pdread (ta->ph, ta->handles + pt % pthread_threads_max, &phc,
  33. sizeof (struct pthread_handle_struct)) != PS_OK)
  34. return TD_ERR; /* XXX Other error value? */
  35. /* Test whether this entry is in use. */
  36. if (phc.h_descr == NULL)
  37. {
  38. if (pt % pthread_threads_max == 0)
  39. {
  40. /* The initial thread always exists but the thread library
  41. might not yet be initialized. */
  42. th->th_ta_p = (td_thragent_t *) ta;
  43. th->th_unique = NULL;
  44. return TD_OK;
  45. }
  46. return TD_BADTH;
  47. }
  48. /* Next test: get the descriptor to see whether this is not an old
  49. thread handle. */
  50. if (ps_pdread (ta->ph, phc.h_descr, &pds,
  51. sizeof (struct _pthread_descr_struct)) != PS_OK)
  52. return TD_ERR; /* XXX Other error value? */
  53. if (pds.p_tid != pt)
  54. return TD_BADTH;
  55. if (pds.p_terminated != 0)
  56. return TD_NOTHR;
  57. /* Create the `td_thrhandle_t' object. */
  58. th->th_ta_p = (td_thragent_t *) ta;
  59. th->th_unique = phc.h_descr;
  60. return TD_OK;
  61. }