mirror of
https://github.com/fluencelabs/musl
synced 2025-06-30 07:02:41 +00:00
simplify pthread_attr_t stack/guard size representation
previously, the pthread_attr_t object was always initialized all-zero, and stack/guard size were represented as differences versus their defaults. this required lots of confusing offset arithmetic everywhere they were used. instead, have pthread_attr_init fill in the default values, and work with absolute sizes everywhere.
This commit is contained in:
@ -7,7 +7,7 @@ int pthread_attr_getdetachstate(const pthread_attr_t *a, int *state)
|
|||||||
}
|
}
|
||||||
int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size)
|
int pthread_attr_getguardsize(const pthread_attr_t *restrict a, size_t *restrict size)
|
||||||
{
|
{
|
||||||
*size = a->_a_guardsize + DEFAULT_GUARD_SIZE;
|
*size = a->_a_guardsize;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,14 +39,14 @@ int pthread_attr_getstack(const pthread_attr_t *restrict a, void **restrict addr
|
|||||||
{
|
{
|
||||||
if (!a->_a_stackaddr)
|
if (!a->_a_stackaddr)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
*size = a->_a_stacksize + DEFAULT_STACK_SIZE;
|
*size = a->_a_stacksize;
|
||||||
*addr = (void *)(a->_a_stackaddr - *size);
|
*addr = (void *)(a->_a_stackaddr - *size);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size)
|
int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size)
|
||||||
{
|
{
|
||||||
*size = a->_a_stacksize + DEFAULT_STACK_SIZE;
|
*size = a->_a_stacksize;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,5 +3,7 @@
|
|||||||
int pthread_attr_init(pthread_attr_t *a)
|
int pthread_attr_init(pthread_attr_t *a)
|
||||||
{
|
{
|
||||||
*a = (pthread_attr_t){0};
|
*a = (pthread_attr_t){0};
|
||||||
|
a->_a_stacksize = DEFAULT_STACK_SIZE;
|
||||||
|
a->_a_guardsize = DEFAULT_GUARD_SIZE;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
int pthread_attr_setguardsize(pthread_attr_t *a, size_t size)
|
int pthread_attr_setguardsize(pthread_attr_t *a, size_t size)
|
||||||
{
|
{
|
||||||
if (size > SIZE_MAX/8) return EINVAL;
|
if (size > SIZE_MAX/8) return EINVAL;
|
||||||
a->_a_guardsize = size - DEFAULT_GUARD_SIZE;
|
a->_a_guardsize = size;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,6 @@ int pthread_attr_setstack(pthread_attr_t *a, void *addr, size_t size)
|
|||||||
{
|
{
|
||||||
if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL;
|
if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL;
|
||||||
a->_a_stackaddr = (size_t)addr + size;
|
a->_a_stackaddr = (size_t)addr + size;
|
||||||
a->_a_stacksize = size - DEFAULT_STACK_SIZE;
|
a->_a_stacksize = size;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,6 @@ int pthread_attr_setstacksize(pthread_attr_t *a, size_t size)
|
|||||||
{
|
{
|
||||||
if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL;
|
if (size-PTHREAD_STACK_MIN > SIZE_MAX/4) return EINVAL;
|
||||||
a->_a_stackaddr = 0;
|
a->_a_stackaddr = 0;
|
||||||
a->_a_stacksize = size - DEFAULT_STACK_SIZE;
|
a->_a_stacksize = size;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -208,7 +208,7 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
|
|||||||
|
|
||||||
if (attr._a_stackaddr) {
|
if (attr._a_stackaddr) {
|
||||||
size_t need = libc.tls_size + __pthread_tsd_size;
|
size_t need = libc.tls_size + __pthread_tsd_size;
|
||||||
size = attr._a_stacksize + DEFAULT_STACK_SIZE;
|
size = attr._a_stacksize;
|
||||||
stack = (void *)(attr._a_stackaddr & -16);
|
stack = (void *)(attr._a_stackaddr & -16);
|
||||||
stack_limit = (void *)(attr._a_stackaddr - size);
|
stack_limit = (void *)(attr._a_stackaddr - size);
|
||||||
/* Use application-provided stack for TLS only when
|
/* Use application-provided stack for TLS only when
|
||||||
@ -223,8 +223,8 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
|
|||||||
guard = 0;
|
guard = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
guard = ROUND(DEFAULT_GUARD_SIZE + attr._a_guardsize);
|
guard = ROUND(attr._a_guardsize);
|
||||||
size = guard + ROUND(DEFAULT_STACK_SIZE + attr._a_stacksize
|
size = guard + ROUND(attr._a_stacksize
|
||||||
+ libc.tls_size + __pthread_tsd_size);
|
+ libc.tls_size + __pthread_tsd_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ int pthread_getattr_np(pthread_t t, pthread_attr_t *a)
|
|||||||
a->_a_detach = !!t->detached;
|
a->_a_detach = !!t->detached;
|
||||||
if (t->stack) {
|
if (t->stack) {
|
||||||
a->_a_stackaddr = (uintptr_t)t->stack;
|
a->_a_stackaddr = (uintptr_t)t->stack;
|
||||||
a->_a_stacksize = t->stack_size - DEFAULT_STACK_SIZE;
|
a->_a_stacksize = t->stack_size;
|
||||||
} else {
|
} else {
|
||||||
char *p = (void *)libc.auxv;
|
char *p = (void *)libc.auxv;
|
||||||
size_t l = PAGE_SIZE;
|
size_t l = PAGE_SIZE;
|
||||||
@ -17,7 +17,7 @@ int pthread_getattr_np(pthread_t t, pthread_attr_t *a)
|
|||||||
a->_a_stackaddr = (uintptr_t)p;
|
a->_a_stackaddr = (uintptr_t)p;
|
||||||
while (mremap(p-l-PAGE_SIZE, PAGE_SIZE, 2*PAGE_SIZE, 0)==MAP_FAILED && errno==ENOMEM)
|
while (mremap(p-l-PAGE_SIZE, PAGE_SIZE, 2*PAGE_SIZE, 0)==MAP_FAILED && errno==ENOMEM)
|
||||||
l += PAGE_SIZE;
|
l += PAGE_SIZE;
|
||||||
a->_a_stacksize = l - DEFAULT_STACK_SIZE;
|
a->_a_stacksize = l;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user