Skip to content

Commit a6c536b

Browse files
authored
Merge pull request swiftlang#70824 from etcwilde/ewilde/mutex-initializer
Use a macro to initialize lazy mutex
2 parents feae5f1 + 154e987 commit a6c536b

File tree

7 files changed

+13
-17
lines changed

7 files changed

+13
-17
lines changed

include/swift/Threading/Impl/C11.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ struct lazy_mutex_handle {
9595
std::int32_t once; // -1 = initialized, 0 = uninitialized, 1 = initializing
9696
};
9797

98-
inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
99-
return (lazy_mutex_handle){};
100-
}
98+
#define SWIFT_LAZY_MUTEX_INITIALIZER ((lazy_mutex_handle){})
99+
101100
inline void lazy_mutex_init(lazy_mutex_handle &handle) {
102101
// Sadly, we can't use call_once() for this as it doesn't have a context
103102
if (std::atomic_load_explicit((std::atomic<std::int32_t> *)&handle.once,

include/swift/Threading/Impl/Darwin.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ inline void mutex_unsafe_unlock(mutex_handle &handle) {
108108
using lazy_mutex_handle = ::os_unfair_lock;
109109

110110
// We don't need to be lazy here because Darwin has OS_UNFAIR_LOCK_INIT.
111-
inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
112-
return OS_UNFAIR_LOCK_INIT;
113-
}
111+
#define SWIFT_LAZY_MUTEX_INITIALIZER OS_UNFAIR_LOCK_INIT
112+
114113
inline void lazy_mutex_destroy(lazy_mutex_handle &handle) {}
115114

116115
inline void lazy_mutex_lock(lazy_mutex_handle &handle) {

include/swift/Threading/Impl/Linux.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ using lazy_mutex_handle = ::pthread_mutex_t;
110110

111111
// We don't actually need to be lazy here because pthreads has
112112
// PTHREAD_MUTEX_INITIALIZER.
113-
inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
114-
return PTHREAD_MUTEX_INITIALIZER;
115-
}
113+
#define SWIFT_LAZY_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
114+
116115
inline void lazy_mutex_destroy(lazy_mutex_handle &handle) {
117116
SWIFT_LINUXTHREADS_CHECK(::pthread_mutex_destroy(&handle));
118117
}

include/swift/Threading/Impl/Nothreads.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ inline void mutex_unsafe_unlock(mutex_handle &handle) {}
5050

5151
using lazy_mutex_handle = unsigned;
5252

53-
inline constexpr lazy_mutex_handle lazy_mutex_initializer() { return 0; }
53+
#define SWIFT_LAZY_MUTEX_INITIALIZER 0
54+
5455
inline void lazy_mutex_destroy(lazy_mutex_handle &handle) {}
5556
inline void lazy_mutex_lock(lazy_mutex_handle &handle) {}
5657
inline void lazy_mutex_unlock(lazy_mutex_handle &handle) {}

include/swift/Threading/Impl/Pthreads.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ using lazy_mutex_handle = ::pthread_mutex_t;
107107

108108
// We don't actually need to be lazy here because pthreads has
109109
// PTHREAD_MUTEX_INITIALIZER.
110-
inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
111-
return PTHREAD_MUTEX_INITIALIZER;
112-
}
110+
#define SWIFT_LAZY_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
111+
113112
inline void lazy_mutex_destroy(lazy_mutex_handle &handle) {
114113
SWIFT_PTHREADS_CHECK(::pthread_mutex_destroy(&handle));
115114
}

include/swift/Threading/Impl/Win32.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ inline void mutex_unsafe_unlock(mutex_handle &handle) {
6666
using lazy_mutex_handle = SWIFT_SRWLOCK;
6767

6868
// We don't need to be lazy here because Win32 has SRWLOCK_INIT.
69-
inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
70-
return SRWLOCK_INIT;
71-
}
69+
#define SWIFT_LAZY_MUTEX_INITIALIZER SRWLOCK_INIT
70+
7271
inline void lazy_mutex_destroy(lazy_mutex_handle &handle) {}
7372

7473
inline void lazy_mutex_lock(lazy_mutex_handle &handle) {

include/swift/Threading/Mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class LazyMutex {
144144
LazyMutex &operator=(LazyMutex &&) = delete;
145145

146146
public:
147-
constexpr LazyMutex() : Handle(threading_impl::lazy_mutex_initializer()) {}
147+
constexpr LazyMutex() : Handle(SWIFT_LAZY_MUTEX_INITIALIZER) {}
148148

149149
// No destructor; this is intentional; this class is for STATIC allocation
150150
// and you don't need to delete mutexes on termination.

0 commit comments

Comments
 (0)