123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #include <assert.h>
- #include <errno.h>
- #include <inttypes.h>
- #include <stdio.h>
- #include <stdio_ext.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/resource.h>
- #include "pthreadP.h"
- #include <lowlevellock.h>
- #include <ldsodefs.h>
- int
- pthread_getattr_np (
- pthread_t thread_id,
- pthread_attr_t *attr)
- {
- struct pthread *thread = (struct pthread *) thread_id;
- struct pthread_attr *iattr = (struct pthread_attr *) attr;
- int ret = 0;
- lll_lock (thread->lock, LLL_PRIVATE);
-
- memcpy (&iattr->schedparam, &thread->schedparam,
- sizeof (struct sched_param));
- iattr->schedpolicy = thread->schedpolicy;
-
- iattr->flags = thread->flags;
-
- if (IS_DETACHED (thread))
- iattr->flags |= ATTR_FLAG_DETACHSTATE;
-
- iattr->guardsize = thread->reported_guardsize;
-
- if (__builtin_expect (thread->stackblock != NULL, 1))
- {
- iattr->stacksize = thread->stackblock_size;
- iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
- }
- else
- {
-
-
- struct rlimit rl;
-
- FILE *fp = fopen ("/proc/self/maps", "rc");
- if (fp == NULL)
- ret = errno;
-
- else
- {
- if (getrlimit (RLIMIT_STACK, &rl) != 0)
- ret = errno;
- else
- {
-
- __fsetlocking (fp, FSETLOCKING_BYCALLER);
-
- ret = ENOENT;
- char *line = NULL;
- size_t linelen = 0;
- uintptr_t last_to = 0;
- while (! feof_unlocked (fp))
- {
- if (getdelim (&line, &linelen, '\n', fp) <= 0)
- break;
- uintptr_t from;
- uintptr_t to;
- if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
- continue;
- if (from <= (uintptr_t) __libc_stack_end
- && (uintptr_t) __libc_stack_end < to)
- {
-
- iattr->stacksize = rl.rlim_cur;
- iattr->stackaddr = (void *) to;
-
- if ((size_t) iattr->stacksize
- > (size_t) iattr->stackaddr - last_to)
- iattr->stacksize = (size_t) iattr->stackaddr - last_to;
-
- ret = 0;
- break;
- }
- last_to = to;
- }
- free (line);
- }
- fclose (fp);
- }
- }
- iattr->flags |= ATTR_FLAG_STACKADDR;
- if (ret == 0)
- {
- size_t size = 16;
- cpu_set_t *cpuset = NULL;
- do
- {
- size <<= 1;
- void *newp = realloc (cpuset, size);
- if (newp == NULL)
- {
- ret = ENOMEM;
- break;
- }
- cpuset = (cpu_set_t *) newp;
- ret = __pthread_getaffinity_np (thread_id, size, cpuset);
- }
-
- while (ret == EINVAL && size < 1024 * 1024);
- if (ret == 0)
- {
- iattr->cpuset = cpuset;
- iattr->cpusetsize = size;
- }
- else
- {
- free (cpuset);
- if (ret == ENOSYS)
- {
-
- ret = 0;
- iattr->cpuset = NULL;
- iattr->cpusetsize = 0;
- }
- }
- }
- lll_unlock (thread->lock, LLL_PRIVATE);
- return ret;
- }
|