Skip to content

Commit 43cc1f9

Browse files
committed
[Threading] Use llvm::Optional<> rather than making a zero lower bound special.
While most systems aren't going to have their stack bottom at zero, using llvm::Optional<> here is cleaner. rdar://90776105
1 parent 3a5f103 commit 43cc1f9

File tree

11 files changed

+51
-32
lines changed

11 files changed

+51
-32
lines changed

include/swift/Threading/Impl/C11.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
#include <atomic>
2121
#include <cstdint>
22+
2223
#include <threads.h>
2324

25+
#include "llvm/ADT/Optional.h"
26+
2427
#include "swift/Threading/Errors.h"
2528

2629
namespace swift {
@@ -55,9 +58,8 @@ bool thread_is_main();
5558
inline bool threads_same(thread_id a, thread_id b) {
5659
return ::thrd_equal(a, b);
5760
}
58-
inline stack_bounds thread_get_current_stack_bounds() {
59-
stack_bounds zero = { nullptr, nullptr };
60-
return zero;
61+
inline llvm::Optional<stack_bounds> thread_get_current_stack_bounds() {
62+
return {};
6163
}
6264

6365
// .. Mutex support ..........................................................

include/swift/Threading/Impl/Darwin.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <os/lock.h>
2222
#include <pthread.h>
2323

24+
#include "llvm/ADT/Optional.h"
25+
2426
#include "swift/Threading/Errors.h"
2527

2628
namespace swift {
@@ -38,7 +40,7 @@ inline bool threads_same(thread_id a, thread_id b) {
3840
return ::pthread_equal(a, b);
3941
}
4042

41-
inline stack_bounds thread_get_current_stack_bounds() {
43+
inline llvm::Optional<stack_bounds> thread_get_current_stack_bounds() {
4244
stack_bounds result;
4345
pthread_t thread = pthread_self();
4446

include/swift/Threading/Impl/Linux.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <pthread.h>
2222

2323
#include <atomic>
24+
#include <optional>
25+
26+
#include "llvm/ADT/Optional.h"
2427

2528
#include "swift/Threading/Errors.h"
2629

@@ -61,7 +64,7 @@ inline bool threads_same(thread_id a, thread_id b) {
6164
return ::pthread_equal(a, b);
6265
}
6366

64-
stack_bounds thread_get_current_stack_bounds();
67+
llvm::Optional<stack_bounds> thread_get_current_stack_bounds();
6568

6669
// .. Mutex support ..........................................................
6770

include/swift/Threading/Impl/Nothreads.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef SWIFT_THREADING_IMPL_NOTHREADS_H
1818
#define SWIFT_THREADING_IMPL_NOTHREADS_H
1919

20+
#include "llvm/ADT/Optional.h"
21+
2022
namespace swift {
2123
namespace threading_impl {
2224

@@ -27,9 +29,8 @@ using thread_id = unsigned;
2729
inline thread_id thread_get_current() { return 0; }
2830
inline bool thread_is_main() { return true; }
2931
inline bool threads_same(thread_id a, thread_id b) { return a == b; }
30-
inline stack_bounds thread_get_current_stack_bounds() {
31-
stack_bounds zero = { nullptr, nullptr };
32-
return zero;
32+
inline llvm::Optional<stack_bounds> thread_get_current_stack_bounds() {
33+
return {};
3334
}
3435

3536
// .. Mutex support ..........................................................

include/swift/Threading/Impl/Pthreads.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <atomic>
2424
#include <cstdint>
2525

26+
#include "llvm/ADT/Optional.h"
27+
2628
#include "swift/Threading/Errors.h"
2729

2830
namespace swift {
@@ -60,7 +62,7 @@ inline bool threads_same(thread_id a, thread_id b) {
6062
return ::pthread_equal(a, b);
6163
}
6264

63-
stack_bounds thread_get_current_stack_bounds();
65+
llvm::Optional<stack_bounds> thread_get_current_stack_bounds();
6466

6567
// .. Mutex support ..........................................................
6668

include/swift/Threading/Impl/Win32.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include <atomic>
2323

24+
#include "llvm/ADT/Optional.h"
25+
2426
namespace swift {
2527
namespace threading_impl {
2628

@@ -31,7 +33,7 @@ using thread_id = ::DWORD;
3133
inline thread_id thread_get_current() { return ::GetCurrentThreadId(); }
3234
bool thread_is_main();
3335
inline bool threads_same(thread_id a, thread_id b) { return a == b; }
34-
stack_bounds thread_get_current_stack_bounds();
36+
llvm::Optional<stack_bounds> thread_get_current_stack_bounds();
3537

3638
// .. Mutex support ..........................................................
3739

include/swift/Threading/Thread.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef SWIFT_THREADING_THREAD_H
2020
#define SWIFT_THREADING_THREAD_H
2121

22+
#include "llvm/ADT/Optional.h"
23+
2224
#include "Impl.h"
2325

2426
namespace swift {
@@ -68,7 +70,7 @@ class Thread {
6870
}
6971

7072
// Retrieve the bounds of the current thread's stack
71-
static StackBounds stackBounds() {
73+
static llvm::Optional<StackBounds> stackBounds() {
7274
return threading_impl::thread_get_current_stack_bounds();
7375
}
7476
};

lib/Threading/Linux.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,25 @@ void swift::threading_impl::once_slow(once_t &predicate, void (*fn)(void *),
5757
linux::ulock_unlock(&predicate.lock);
5858
}
5959

60-
swift::threading_impl::stack_bounds
60+
llvm::Optional<swift::threading_impl::stack_bounds>
6161
swift::threading_impl::thread_get_current_stack_bounds() {
62-
stack_bounds result = { nullptr, nullptr };
6362
pthread_attr_t attr;
6463
size_t size = 0;
6564
void *begin = nullptr;
6665

6766
if (!pthread_getattr_np(pthread_self(), &attr)) {
6867
if (!pthread_attr_getstack(&attr, &begin, &size)) {
69-
result.low = begin;
70-
result.high = (char *)begin + size;
68+
stack_bounds result = { begin, (char *)begin + size };
69+
70+
pthread_attr_destroy(&attr);
71+
72+
return result;
7173
}
7274

7375
pthread_attr_destroy(&attr);
7476
}
7577

76-
return result;
78+
return {};
7779
}
7880

7981
#endif // SWIFT_THREADING_LINUX

lib/Threading/Pthreads.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -77,41 +77,43 @@ void swift::threading_impl::once_slow(once_t &predicate, void (*fn)(void *),
7777
}
7878

7979
#if defined(__OpenBSD__)
80-
swift::threading_impl::stack_bounds
80+
llvm::Optional<swift::threading_impl::stack_bounds>
8181
swift::threading_impl::thread_get_current_stack_bounds() {
82-
stack_bounds result = { nullptr, nullptr };
8382
stack_t sinfo;
8483

8584
if (!pthread_stackseg_np(pthread_self(), &sinfo)) {
86-
result.low = (char *)sinfo.ss_sp - sinfo.ss_size;
87-
result.high = sinfo.ss_sp;
85+
stack_bounds result = {
86+
(char *)sinfo.ss_sp - sinfo.ss_size,
87+
sinfo.ss_sp
88+
};
89+
return result;
8890
}
8991

90-
return result;
92+
return {};
9193
}
9294
#else
93-
swift::threading_impl::stack_bounds
95+
llvm::Optional<swift::threading_impl::stack_bounds>
9496
swift::threading_impl::thread_get_current_stack_bounds() {
95-
stack_bounds result = { nullptr, nullptr };
9697
pthread_attr_t attr;
9798
size_t size = 0;
9899
void *begin = nullptr;
99100

100101
#if defined(__FreeBSD__)
101102
if (pthread_attr_init(&attr))
102-
return result;
103+
return {};
103104
#endif
104105

105106
if (!pthread_getattr_np(pthread_self(), &attr)) {
106107
if (!pthread_attr_getstack(&attr, &begin, &size)) {
107-
result.low = begin;
108-
result.high = (char *)begin + size;
108+
stack_bounds result = { begin, (char *)begin + size };
109+
pthread_attr_destroy(&attr);
110+
return result;
109111
}
110112

111113
pthread_attr_destroy(&attr);
112114
}
113115

114-
return result;
116+
return {};
115117
}
116118
#endif
117119

lib/Threading/Win32.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void swift::threading_impl::once_slow(once_t &predicate, void (*fn)(void *),
9797
#endif
9898
}
9999

100-
swift::threading_impl::stack_bounds
100+
llvm::Optional<swift::threading_impl::stack_bounds>
101101
swift::threading_impl::thread_get_current_stack_bounds() {
102102
#if _WIN32_WINNT >= 0x0602
103103
ULONG_PTR lowLimit = 0;

stdlib/public/stubs/Stubs.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,11 @@ __swift_bool swift_stdlib_isStackAllocationSafe(__swift_size_t byteCount,
535535

536536
__swift_bool _swift_stdlib_getCurrentStackBounds(__swift_uintptr_t *outBegin,
537537
__swift_uintptr_t *outEnd) {
538-
swift::Thread::StackBounds bounds = swift::Thread::stackBounds();
539-
if (!bounds.low)
538+
llvm::Optional<swift::Thread::StackBounds> bounds =
539+
swift::Thread::stackBounds();
540+
if (!bounds)
540541
return false;
541-
*outBegin = (uintptr_t)bounds.low;
542-
*outEnd = (uintptr_t)bounds.high;
542+
*outBegin = (uintptr_t)bounds->low;
543+
*outEnd = (uintptr_t)bounds->high;
543544
return true;
544545
}

0 commit comments

Comments
 (0)