|
64 | 64 | #include <android/api-level.h>
|
65 | 65 | #endif
|
66 | 66 |
|
67 |
| -#if defined(__FreeBSD__) || defined(__OpenBSD__) |
68 |
| -#include <pthread_np.h> |
69 |
| -#endif |
70 |
| - |
71 | 67 | #include "swift/Runtime/Debug.h"
|
72 | 68 | #include "swift/Runtime/SwiftDtoa.h"
|
73 | 69 | #include "swift/Basic/Lazy.h"
|
74 | 70 |
|
| 71 | +#include "swift/Threading/Thread.h" |
| 72 | + |
75 | 73 | #include "SwiftShims/LibcShims.h"
|
76 | 74 | #include "SwiftShims/RuntimeShims.h"
|
77 | 75 | #include "SwiftShims/RuntimeStubs.h"
|
@@ -537,84 +535,10 @@ __swift_bool swift_stdlib_isStackAllocationSafe(__swift_size_t byteCount,
|
537 | 535 |
|
538 | 536 | __swift_bool _swift_stdlib_getCurrentStackBounds(__swift_uintptr_t *outBegin,
|
539 | 537 | __swift_uintptr_t *outEnd) {
|
540 |
| -#if SWIFT_THREADING_NONE |
541 |
| - // This platform does not support threads, so the API we'd call to get stack |
542 |
| - // bounds (i.e. libpthread) is not going to be usable. |
543 |
| - return false; |
544 |
| - |
545 |
| -#elif SWIFT_THREADING_DARWIN |
546 |
| - pthread_t thread = pthread_self(); |
547 |
| - |
548 |
| - // On Apple platforms, pthread_get_stackaddr_np() gets the address of the |
549 |
| - // *end* of the stack (i.e. the highest address in stack space), *NOT* the |
550 |
| - // address of the *base* of the stack (the lowest address). |
551 |
| - void *end = pthread_get_stackaddr_np(thread); |
552 |
| - if (!end) { |
553 |
| - return false; |
554 |
| - } |
555 |
| - *outEnd = (uintptr_t)end; |
556 |
| - *outBegin = *outEnd - pthread_get_stacksize_np(thread); |
557 |
| - return true; |
558 |
| - |
559 |
| -#elif SWIFT_THREADING_C11 |
560 |
| - // We don't know any way to do this for C11 threads |
561 |
| - return false; |
562 |
| - |
563 |
| -#elif SWIFT_THREADING_WIN32 |
564 |
| - |
565 |
| -#if _WIN32_WINNT >= 0x0602 |
566 |
| - ULONG_PTR lowLimit = 0; |
567 |
| - ULONG_PTR highLimit = 0; |
568 |
| - GetCurrentThreadStackLimits(&lowLimit, &highLimit); |
569 |
| - *outBegin = lowLimit; |
570 |
| - *outEnd = highLimit; |
571 |
| - return true; |
572 |
| -#else |
573 |
| - // Need _WIN32_WINNT to be 0x0602 or higher to use |
574 |
| - // GetCurrentThreadStackLimits(). We could use VirtualQuery() instead, |
575 |
| - // and give it the address of a page we know is on the stack? |
576 |
| - return false; |
577 |
| -#endif |
578 |
| - |
579 |
| -#elif SWIFT_THREADING_PTHREADS || SWIFT_THREADING_LINUX |
580 |
| - |
581 |
| -#if defined(__OpenBSD__) |
582 |
| - stack_t sinfo; |
583 |
| - if (pthread_stackseg_np(pthread_self(), &sinfo) != 0) { |
| 538 | + swift::Thread::StackBounds bounds = swift::Thread::stackBounds(); |
| 539 | + if (!bounds.low) |
584 | 540 | return false;
|
585 |
| - } |
586 |
| - |
587 |
| - *outBegin = (uintptr_t)sinfo.ss_sp - sinfo.ss_size; |
588 |
| - *outEnd = (uintptr_t)sinfo.ss_sp; |
| 541 | + *outBegin = (uintptr_t)bounds.low; |
| 542 | + *outEnd = (uintptr_t)bounds.high; |
589 | 543 | return true;
|
590 |
| -#elif defined(__FreeBSD__) || defined(__ANDROID__) || defined(__linux__) |
591 |
| - pthread_attr_t attr; |
592 |
| - |
593 |
| -#if defined(__FreeBSD__) |
594 |
| - if (0 != pthread_attr_init(&attr) || 0 != pthread_attr_get_np(pthread_self(), &attr)) { |
595 |
| - return false; |
596 |
| - } |
597 |
| -#else |
598 |
| - if (0 != pthread_getattr_np(pthread_self(), &attr)) { |
599 |
| - return false; |
600 |
| - } |
601 |
| -#endif |
602 |
| - |
603 |
| - void *begin = nullptr; |
604 |
| - size_t size = 0; |
605 |
| - bool success = (0 == pthread_attr_getstack(&attr, &begin, &size)); |
606 |
| - |
607 |
| - *outBegin = (uintptr_t)begin; |
608 |
| - *outEnd = *outBegin + size; |
609 |
| - |
610 |
| - pthread_attr_destroy(&attr); |
611 |
| - return success; |
612 |
| -#else |
613 |
| -#warning Please teach _swift_stdlib_getCurrentStackBounds() about your platform |
614 |
| - return false; |
615 |
| -#endif |
616 |
| - |
617 |
| -#else |
618 |
| -# error Unknown threading package selected; please teach _swift_stdlib_getCurrentStackBounds() what to do. |
619 |
| -#endif |
620 | 544 | }
|
0 commit comments