Back to index

glibc  2.9
pthread_getattr_np.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <inttypes.h>
00023 #include <stdio.h>
00024 #include <stdio_ext.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <sys/resource.h>
00028 #include "pthreadP.h"
00029 #include <lowlevellock.h>
00030 #include <ldsodefs.h>
00031 
00032 
00033 int
00034 pthread_getattr_np (thread_id, attr)
00035      pthread_t thread_id;
00036      pthread_attr_t *attr;
00037 {
00038   struct pthread *thread = (struct pthread *) thread_id;
00039   struct pthread_attr *iattr = (struct pthread_attr *) attr;
00040   int ret = 0;
00041 
00042   lll_lock (thread->lock, LLL_PRIVATE);
00043 
00044   /* The thread library is responsible for keeping the values in the
00045      thread desriptor up-to-date in case the user changes them.  */
00046   memcpy (&iattr->schedparam, &thread->schedparam,
00047          sizeof (struct sched_param));
00048   iattr->schedpolicy = thread->schedpolicy;
00049 
00050   /* Clear the flags work.  */
00051   iattr->flags = thread->flags;
00052 
00053   /* The thread might be detached by now.  */
00054   if (IS_DETACHED (thread))
00055     iattr->flags |= ATTR_FLAG_DETACHSTATE;
00056 
00057   /* This is the guardsize after adjusting it.  */
00058   iattr->guardsize = thread->reported_guardsize;
00059 
00060   /* The sizes are subject to alignment.  */
00061   if (__builtin_expect (thread->stackblock != NULL, 1))
00062     {
00063       iattr->stacksize = thread->stackblock_size;
00064       iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
00065     }
00066   else
00067     {
00068       /* No stack information available.  This must be for the initial
00069         thread.  Get the info in some magical way.  */
00070       assert (abs (thread->pid) == thread->tid);
00071 
00072       /* Stack size limit.  */
00073       struct rlimit rl;
00074 
00075       /* The safest way to get the top of the stack is to read
00076         /proc/self/maps and locate the line into which
00077         __libc_stack_end falls.  */
00078       FILE *fp = fopen ("/proc/self/maps", "rc");
00079       if (fp == NULL)
00080        ret = errno;
00081       /* We need the limit of the stack in any case.  */
00082       else
00083        {
00084          if (getrlimit (RLIMIT_STACK, &rl) != 0)
00085            ret = errno;
00086          else
00087            {
00088              /* We need no locking.  */
00089              __fsetlocking (fp, FSETLOCKING_BYCALLER);
00090 
00091              /* Until we found an entry (which should always be the case)
00092                mark the result as a failure.  */
00093              ret = ENOENT;
00094 
00095              char *line = NULL;
00096              size_t linelen = 0;
00097              uintptr_t last_to = 0;
00098 
00099              while (! feof_unlocked (fp))
00100               {
00101                 if (__getdelim (&line, &linelen, '\n', fp) <= 0)
00102                   break;
00103 
00104                 uintptr_t from;
00105                 uintptr_t to;
00106                 if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
00107                   continue;
00108                 if (from <= (uintptr_t) __libc_stack_end
00109                     && (uintptr_t) __libc_stack_end < to)
00110                   {
00111                     /* Found the entry.  Now we have the info we need.  */
00112                     iattr->stacksize = rl.rlim_cur;
00113                     iattr->stackaddr = (void *) to;
00114 
00115                     /* The limit might be too high.  */
00116                     if ((size_t) iattr->stacksize
00117                        > (size_t) iattr->stackaddr - last_to)
00118                      iattr->stacksize = (size_t) iattr->stackaddr - last_to;
00119 
00120                     /* We succeed and no need to look further.  */
00121                     ret = 0;
00122                     break;
00123                   }
00124                 last_to = to;
00125               }
00126 
00127              free (line);
00128            }
00129 
00130          fclose (fp);
00131        }
00132     }
00133 
00134   iattr->flags |= ATTR_FLAG_STACKADDR;
00135 
00136   if (ret == 0)
00137     {
00138       size_t size = 16;
00139       cpu_set_t *cpuset = NULL;
00140 
00141       do
00142        {
00143          size <<= 1;
00144 
00145          void *newp = realloc (cpuset, size);
00146          if (newp == NULL)
00147            {
00148              ret = ENOMEM;
00149              break;
00150            }
00151          cpuset = (cpu_set_t *) newp;
00152 
00153          ret = __pthread_getaffinity_np (thread_id, size, cpuset);
00154        }
00155       /* Pick some ridiculous upper limit.  Is 8 million CPUs enough?  */
00156       while (ret == EINVAL && size < 1024 * 1024);
00157 
00158       if (ret == 0)
00159        {
00160          iattr->cpuset = cpuset;
00161          iattr->cpusetsize = size;
00162        }
00163       else
00164        {
00165          free (cpuset);
00166          if (ret == ENOSYS)
00167            {    
00168              /* There is no such functionality.  */
00169              ret = 0;
00170              iattr->cpuset = NULL;
00171              iattr->cpusetsize = 0;
00172            }
00173        }
00174     }
00175 
00176   lll_unlock (thread->lock, LLL_PRIVATE);
00177 
00178   return ret;
00179 }