Skip to content

Commit ca926e4

Browse files
committed
[Threading][Windows] Try to avoid declaring _RTL_SRWLOCK
Declaring _RTL_SRWLOCK ourselves causes clashes with <windows.h>. Rather than doing that, declare an equivalent struct, and some overloads. rdar://90776105
1 parent 8910f0e commit ca926e4

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

include/swift/Threading/Impl/Win32.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,31 @@ inline bool threads_same(thread_id a, thread_id b) { return a == b; }
3535

3636
// .. Mutex support ..........................................................
3737

38-
using mutex_handle = ::SRWLOCK;
38+
using mutex_handle = SWIFT_SRWLOCK;
3939

4040
inline void mutex_init(mutex_handle &handle, bool checked=false) {
4141
handle = SRWLOCK_INIT;
4242
}
4343
inline void mutex_destroy(mutex_handle &handle) { }
4444

4545
inline void mutex_lock(mutex_handle &handle) {
46-
::AcquireSRWLockExclusive(&handle);
46+
AcquireSRWLockExclusive(&handle);
4747
}
4848
inline void mutex_unlock(mutex_handle &handle) {
49-
::ReleaseSRWLockExclusive(&handle);
49+
ReleaseSRWLockExclusive(&handle);
5050
}
5151
inline bool mutex_try_lock(mutex_handle &handle) {
52-
return !!::TryAcquireSRWLockExclusive(&handle);
52+
return !!TryAcquireSRWLockExclusive(&handle);
5353
}
5454

5555
inline void mutex_unsafe_lock(mutex_handle &handle) {
56-
::AcquireSRWLockExclusive(&handle);
56+
AcquireSRWLockExclusive(&handle);
5757
}
5858
inline void mutex_unsafe_unlock(mutex_handle &handle) {
59-
::ReleaseSRWLockExclusive(&handle);
59+
ReleaseSRWLockExclusive(&handle);
6060
}
6161

62-
using lazy_mutex_handle = ::SRWLOCK;
62+
using lazy_mutex_handle = SWIFT_SRWLOCK;
6363

6464
// We don't need to be lazy here because Win32 has SRWLOCK_INIT.
6565
inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
@@ -68,20 +68,20 @@ inline constexpr lazy_mutex_handle lazy_mutex_initializer() {
6868
inline void lazy_mutex_destroy(lazy_mutex_handle &handle) { }
6969

7070
inline void lazy_mutex_lock(lazy_mutex_handle &handle) {
71-
::AcquireSRWLockExclusive(&handle);
71+
AcquireSRWLockExclusive(&handle);
7272
}
7373
inline void lazy_mutex_unlock(lazy_mutex_handle &handle) {
74-
::ReleaseSRWLockExclusive(&handle);
74+
ReleaseSRWLockExclusive(&handle);
7575
}
7676
inline bool lazy_mutex_try_lock(lazy_mutex_handle &handle) {
77-
return !!::TryAcquireSRWLockExclusive(&handle);
77+
return !!TryAcquireSRWLockExclusive(&handle);
7878
}
7979

8080
inline void lazy_mutex_unsafe_lock(lazy_mutex_handle &handle) {
81-
::AcquireSRWLockExclusive(&handle);
81+
AcquireSRWLockExclusive(&handle);
8282
}
8383
inline void lazy_mutex_unsafe_unlock(lazy_mutex_handle &handle) {
84-
::ReleaseSRWLockExclusive(&handle);
84+
ReleaseSRWLockExclusive(&handle);
8585
}
8686

8787
// .. Once ...................................................................

include/swift/Threading/Impl/Win32/Win32Defs.h

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,8 @@ typedef unsigned long DWORD;
3939

4040
typedef VOID (NTAPI* PFLS_CALLBACK_FUNCTION)(PVOID lpFlsData);
4141

42-
// We can't define this struct if <winnt.h> already did so
43-
#ifndef _WINNT_
44-
struct _RTL_SRWLOCK {
45-
PVOID Ptr;
46-
};
47-
#endif // _WINNT_
48-
49-
typedef struct _RTL_SRWLOCK RTL_SRWLOCK, *PRTL_SRWLOCK;
50-
typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
42+
typedef struct _RTL_SRWLOCK *PRTL_SRWLOCK;
43+
typedef PRTL_SRWLOCK PSRWLOCK;
5144

5245
// These have to be #defines, to avoid problems with <windows.h>
5346
#define RTL_SRWLOCK_INIT {0}
@@ -68,4 +61,31 @@ extern "C" {
6861
WINBASEAPI BOOL WINAPI FlsFree(DWORD dwFlsIndex);
6962
}
7063

64+
namespace swift {
65+
namespace threading_impl {
66+
67+
// We do this because we can't declare _RTL_SRWLOCK here in case someone
68+
// later includes <windows.h>
69+
struct SWIFT_SRWLOCK {
70+
PVOID Ptr;
71+
};
72+
73+
typedef SWIFT_SRWLOCK *PSWIFT_SRWLOCK;
74+
75+
inline VOID InitializeSRWLock(PSWIFT_SRWLOCK SRWLock) {
76+
::InitializeSRWLock(reinterpret_cast<PSRWLOCK>(SRWLock));
77+
}
78+
inline VOID ReleaseSRWLockExclusive(PSWIFT_SRWLOCK SRWLock) {
79+
::ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(SRWLock));
80+
}
81+
inline VOID AcquireSRWLockExclusive(PSWIFT_SRWLOCK SRWLock) {
82+
::AcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(SRWLock));
83+
}
84+
inline BOOLEAN TryAcquireSRWLockExclusive(PSWIFT_SRWLOCK SRWLock) {
85+
return ::TryAcquireSRWLockExclusive(reinterpret_cast<PSRWLOCK>(SRWLock));
86+
}
87+
88+
}
89+
}
90+
7191
#endif // SWIFT_THREADING_IMPL_WIN32_DEFS_H

0 commit comments

Comments
 (0)