[Threading] Move stack bounds fetching into the threading library.

Fetching the current stack bounds is done using threading functions, so
should be in the threading library.

rdar://90776105
This commit is contained in:
Alastair Houghton
2022-05-06 10:59:48 +01:00
parent eb4c81d60e
commit b5c8b79556
12 changed files with 134 additions and 82 deletions

View File

@@ -16,6 +16,10 @@
#if SWIFT_THREADING_PTHREADS
#if defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h>
#endif
#include "swift/Threading/Impl/Pthreads.h"
#include "swift/Threading/Errors.h"
@@ -72,4 +76,43 @@ void swift::threading_impl::once_slow(once_t &predicate, void (*fn)(void *),
pthread_mutex_unlock(&onceMutex);
}
#if defined(__OpenBSD__)
swift::threading_impl::stack_bounds
swift::threading_impl::thread_get_current_stack_bounds() {
stack_bounds result = { nullptr, nullptr };
stack_t sinfo;
if (!pthread_stackseg_np(pthread_self(), &sinfo)) {
result.low = (char *)sinfo.ss_sp - sinfo.ss_size;
result.high = sinfo.ss_sp;
}
return result;
}
#else
swift::threading_impl::stack_bounds
swift::threading_impl::thread_get_current_stack_bounds() {
stack_bounds result = { nullptr, nullptr };
pthread_attr_t attr;
size_t size = 0;
void *begin = nullptr;
#if defined(__FreeBSD__)
if (pthread_attr_init(&attr))
return result;
#endif
if (!pthread_getattr_np(pthread_self(), &attr)) {
if (!pthread_attr_getstack(&attr, &begin, &size)) {
result.low = begin;
result.high = (char *)begin + size;
}
pthread_attr_destroy(&attr);
}
return result;
}
#endif
#endif // SWIFT_THREADING_PTHREADS