Skip to content

Commit 7404ca9

Browse files
committed
[Threading] Fix 32-bit Linux.
`swift::once` needs to be 32-bit on 32-bit Linux, which means we have to revert to using a global lock. rdar://94831034
1 parent 863ab00 commit 7404ca9

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

include/swift/Threading/Impl/Linux.h

+3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ inline void lazy_mutex_unsafe_unlock(lazy_mutex_handle &handle) {
135135

136136
struct once_t {
137137
std::atomic<std::int32_t> flag;
138+
#if defined(__LP64__) || defined(_LP64)
139+
// On 32-bit Linux we can't have the lock, so we'll be less efficient
138140
linux::ulock_t lock;
141+
#endif
139142
};
140143

141144
void once_slow(once_t &predicate, void (*fn)(void *), void *context);

lib/Threading/Linux.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class MainThreadRememberer {
3636

3737
MainThreadRememberer rememberer;
3838

39+
#if !defined(__LP64__) && !defined(_LP64)
40+
pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
41+
#endif
42+
3943
#pragma clang diagnostic pop
4044

4145
} // namespace
@@ -49,12 +53,21 @@ bool swift::threading_impl::thread_is_main() {
4953

5054
void swift::threading_impl::once_slow(once_t &predicate, void (*fn)(void *),
5155
void *context) {
56+
// On 32-bit Linux we can't have per-once locks
57+
#if defined(__LP64__) || defined(_LP64)
5258
linux::ulock_lock(&predicate.lock);
59+
#else
60+
pthread_mutex_lock(&once_mutex);
61+
#endif
5362
if (predicate.flag.load(std::memory_order_acquire) == 0) {
5463
fn(context);
5564
predicate.flag.store(-1, std::memory_order_release);
5665
}
66+
#if defined(__LP64__) || defined(_LP64)
5767
linux::ulock_unlock(&predicate.lock);
68+
#else
69+
pthread_mutex_unlock(&once_mutex);
70+
#endif
5871
}
5972

6073
llvm::Optional<swift::threading_impl::stack_bounds>

0 commit comments

Comments
 (0)