Back to index

glibc  2.9
td_ta_map_id2thr.c
Go to the documentation of this file.
00001 /* Map thread ID to thread handle.
00002    Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include "thread_dbP.h"
00022 #include <linuxthreads/internals.h>
00023 
00024 
00025 td_err_e
00026 td_ta_map_id2thr (const td_thragent_t *ta, pthread_t pt, td_thrhandle_t *th)
00027 {
00028   struct pthread_handle_struct phc;
00029   struct _pthread_descr_struct pds;
00030   int pthread_threads_max;
00031 
00032   LOG ("td_ta_map_id2thr");
00033 
00034   /* Test whether the TA parameter is ok.  */
00035   if (! ta_ok (ta))
00036     return TD_BADTA;
00037 
00038   /* Make the following expression a bit smaller.  */
00039   pthread_threads_max = ta->pthread_threads_max;
00040 
00041   /* We can compute the entry in the handle array we want.  */
00042   if (ps_pdread (ta->ph, ta->handles + pt % pthread_threads_max, &phc,
00043                sizeof (struct pthread_handle_struct)) != PS_OK)
00044     return TD_ERR;   /* XXX Other error value?  */
00045 
00046   /* Test whether this entry is in use.  */
00047   if (phc.h_descr == NULL)
00048     {
00049       if (pt % pthread_threads_max == 0)
00050        {
00051          /* The initial thread always exists but the thread library
00052             might not yet be initialized.  */
00053          th->th_ta_p = (td_thragent_t *) ta;
00054          th->th_unique = NULL;
00055 
00056          return TD_OK;
00057        }
00058 
00059       return TD_BADTH;
00060     }
00061 
00062   /* Next test: get the descriptor to see whether this is not an old
00063      thread handle.  */
00064   if (ps_pdread (ta->ph, phc.h_descr, &pds,
00065                sizeof (struct _pthread_descr_struct)) != PS_OK)
00066     return TD_ERR;   /* XXX Other error value?  */
00067 
00068   if (pds.p_tid != pt)
00069     return TD_BADTH;
00070 
00071   if (pds.p_terminated != 0)
00072     return TD_NOTHR;
00073 
00074   /* Create the `td_thrhandle_t' object.  */
00075   th->th_ta_p = (td_thragent_t *) ta;
00076   th->th_unique = phc.h_descr;
00077 
00078   return TD_OK;
00079 }