From ba56d0a513eda9b1f3242b57fcaa238f63014529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Thu, 7 Mar 2019 21:09:56 +0100 Subject: [PATCH] pthread: add pthread_getattr_np. non standard extension, but widely available to obtain attributes for a thread. also provides an alias for pthread_getattr_np which is the bsd equivalent. Change-Id: I26ef8245ed2537186f48c8b8bdf3e42b03e70892 Reviewed-on: https://review.haiku-os.org/c/1172 Reviewed-by: Adrien Destugues --- headers/compatibility/gnu/pthread.h | 32 +++++++++++++++++ .../libroot/posix/pthread/pthread_attr.c | 36 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 headers/compatibility/gnu/pthread.h diff --git a/headers/compatibility/gnu/pthread.h b/headers/compatibility/gnu/pthread.h new file mode 100644 index 0000000000..b1699125cd --- /dev/null +++ b/headers/compatibility/gnu/pthread.h @@ -0,0 +1,32 @@ +/* + * Copyright 2019 Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the Haiku License. + */ +#ifndef _GNU_PTHREAD_H_ +#define _GNU_PTHREAD_H_ + + +#include_next + + +#ifdef _GNU_SOURCE + + +#ifdef __cplusplus +extern "C" { +#endif + + +extern int pthread_getattr_np(pthread_t thread, pthread_attr_t* attr); + + +#ifdef __cplusplus +} +#endif + + +#endif + + +#endif /* _GNU_PTHREAD_H_ */ + diff --git a/src/system/libroot/posix/pthread/pthread_attr.c b/src/system/libroot/posix/pthread/pthread_attr.c index 9ae6a3e8c4..ac6894508b 100644 --- a/src/system/libroot/posix/pthread/pthread_attr.c +++ b/src/system/libroot/posix/pthread/pthread_attr.c @@ -14,9 +14,13 @@ #include +#include #include +int __pthread_getattr_np(pthread_t thread, pthread_attr_t *_attr); + + int pthread_attr_init(pthread_attr_t *_attr) { @@ -235,3 +239,35 @@ pthread_attr_setstack(pthread_attr_t *_attr, void *stackaddr, return 0; } + +int +__pthread_getattr_np(pthread_t thread, pthread_attr_t *_attr) +{ + pthread_attr *attr; + status_t status; + thread_info info; + + if (_attr == NULL || (attr = *_attr) == NULL) + return B_BAD_VALUE; + + status = _kern_get_thread_info(thread->id, &info); + if (status == B_BAD_THREAD_ID) + return ESRCH; + + if ((thread->flags & THREAD_DETACHED) != 0) + attr->detach_state = PTHREAD_CREATE_DETACHED; + else + attr->detach_state = PTHREAD_CREATE_JOINABLE; + attr->sched_priority = info.priority; + attr->stack_address = info.stack_base; + attr->stack_size = (size_t)info.stack_end - (size_t)info.stack_base; + // not in thread_info + attr->guard_size = 0; + + return 0; +} + + +B_DEFINE_WEAK_ALIAS(__pthread_getattr_np, pthread_getattr_np); +B_DEFINE_WEAK_ALIAS(__pthread_getattr_np, pthread_attr_get_np); +