Skip to content

Commit cf440d8

Browse files
[wasm] Use pthread API for NSLock for wasip1-threads target
`SWIFT_CORELIBS_FOUNDATION_HAS_THREADS` is not defined for all WASI targets now but we do have pthread support for wasip1-threads target. `_runtime(_multithreaded)` check is more appropriate here to detect multithreaded runtime support. We can remove `SWIFT_CORELIBS_FOUNDATION_HAS_THREADS` definition later.
1 parent 265274a commit cf440d8

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Sources/Foundation/NSLock.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
5959
#endif
6060

6161
public override init() {
62-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
62+
#if !_runtime(_multithreaded)
6363
// noop on no thread platforms
6464
#elseif os(Windows)
6565
InitializeSRWLock(mutex)
@@ -75,7 +75,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
7575
}
7676

7777
deinit {
78-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
78+
#if !_runtime(_multithreaded)
7979
// noop on no thread platforms
8080
#elseif os(Windows)
8181
// SRWLocks do not need to be explicitly destroyed
@@ -91,7 +91,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
9191

9292
@available(*, noasync, message: "Use async-safe scoped locking instead")
9393
open func lock() {
94-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
94+
#if !_runtime(_multithreaded)
9595
// noop on no thread platforms
9696
#elseif os(Windows)
9797
AcquireSRWLockExclusive(mutex)
@@ -102,7 +102,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
102102

103103
@available(*, noasync, message: "Use async-safe scoped locking instead")
104104
open func unlock() {
105-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
105+
#if !_runtime(_multithreaded)
106106
// noop on no thread platforms
107107
#elseif os(Windows)
108108
ReleaseSRWLockExclusive(mutex)
@@ -122,7 +122,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
122122

123123
@available(*, noasync, message: "Use async-safe scoped locking instead")
124124
open func `try`() -> Bool {
125-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
125+
#if !_runtime(_multithreaded)
126126
// noop on no thread platforms
127127
return true
128128
#elseif os(Windows)
@@ -134,7 +134,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
134134

135135
@available(*, noasync, message: "Use async-safe scoped locking instead")
136136
open func lock(before limit: Date) -> Bool {
137-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
137+
#if !_runtime(_multithreaded)
138138
// noop on no thread platforms
139139
#elseif os(Windows)
140140
if TryAcquireSRWLockExclusive(mutex) != 0 {
@@ -146,7 +146,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
146146
}
147147
#endif
148148

149-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
149+
#if !_runtime(_multithreaded)
150150
// noop on no thread platforms
151151
return true
152152
#elseif os(macOS) || os(iOS) || os(Windows)
@@ -170,7 +170,7 @@ extension NSLock {
170170
}
171171
}
172172

173-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
173+
#if _runtime(_multithreaded)
174174
open class NSConditionLock : NSObject, NSLocking, @unchecked Sendable {
175175
internal var _cond = NSCondition()
176176
internal var _value: Int
@@ -282,7 +282,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
282282

283283
public override init() {
284284
super.init()
285-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
285+
#if !_runtime(_multithreaded)
286286
// noop on no thread platforms
287287
#elseif os(Windows)
288288
InitializeCriticalSection(mutex)
@@ -312,7 +312,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
312312
}
313313

314314
deinit {
315-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
315+
#if !_runtime(_multithreaded)
316316
// noop on no thread platforms
317317
#elseif os(Windows)
318318
DeleteCriticalSection(mutex)
@@ -328,7 +328,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
328328

329329
@available(*, noasync, message: "Use async-safe scoped locking instead")
330330
open func lock() {
331-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
331+
#if !_runtime(_multithreaded)
332332
// noop on no thread platforms
333333
#elseif os(Windows)
334334
EnterCriticalSection(mutex)
@@ -339,7 +339,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
339339

340340
@available(*, noasync, message: "Use async-safe scoped locking instead")
341341
open func unlock() {
342-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
342+
#if !_runtime(_multithreaded)
343343
// noop on no thread platforms
344344
#elseif os(Windows)
345345
LeaveCriticalSection(mutex)
@@ -359,7 +359,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
359359

360360
@available(*, noasync, message: "Use async-safe scoped locking instead")
361361
open func `try`() -> Bool {
362-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
362+
#if !_runtime(_multithreaded)
363363
// noop on no thread platforms
364364
return true
365365
#elseif os(Windows)
@@ -371,7 +371,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
371371

372372
@available(*, noasync, message: "Use async-safe scoped locking instead")
373373
open func lock(before limit: Date) -> Bool {
374-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
374+
#if !_runtime(_multithreaded)
375375
// noop on no thread platforms
376376
#elseif os(Windows)
377377
if TryEnterCriticalSection(mutex) {
@@ -383,7 +383,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
383383
}
384384
#endif
385385

386-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
386+
#if !_runtime(_multithreaded)
387387
// noop on no thread platforms
388388
return true
389389
#elseif os(macOS) || os(iOS) || os(Windows)
@@ -404,7 +404,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
404404
internal var cond = _ConditionVariablePointer.allocate(capacity: 1)
405405

406406
public override init() {
407-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
407+
#if !_runtime(_multithreaded)
408408
// noop on no thread platforms
409409
#elseif os(Windows)
410410
InitializeSRWLock(mutex)
@@ -416,7 +416,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
416416
}
417417

418418
deinit {
419-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
419+
#if !_runtime(_multithreaded)
420420
// noop on no thread platforms
421421
#elseif os(Windows)
422422
// SRWLock do not need to be explicitly destroyed
@@ -432,7 +432,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
432432

433433
@available(*, noasync, message: "Use async-safe scoped locking instead")
434434
open func lock() {
435-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
435+
#if !_runtime(_multithreaded)
436436
// noop on no thread platforms
437437
#elseif os(Windows)
438438
AcquireSRWLockExclusive(mutex)
@@ -443,7 +443,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
443443

444444
@available(*, noasync, message: "Use async-safe scoped locking instead")
445445
open func unlock() {
446-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
446+
#if !_runtime(_multithreaded)
447447
// noop on no thread platforms
448448
#elseif os(Windows)
449449
ReleaseSRWLockExclusive(mutex)
@@ -454,7 +454,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
454454

455455
@available(*, noasync, message: "Use async-safe scoped locking instead")
456456
open func wait() {
457-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
457+
#if !_runtime(_multithreaded)
458458
// noop on no thread platforms
459459
#elseif os(Windows)
460460
SleepConditionVariableSRW(cond, mutex, WinSDK.INFINITE, 0)
@@ -465,7 +465,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
465465

466466
@available(*, noasync, message: "Use async-safe scoped locking instead")
467467
open func wait(until limit: Date) -> Bool {
468-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
468+
#if !_runtime(_multithreaded)
469469
// noop on no thread platforms
470470
return true
471471
#elseif os(Windows)
@@ -480,7 +480,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
480480

481481
@available(*, noasync, message: "Use async-safe scoped locking instead")
482482
open func signal() {
483-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
483+
#if !_runtime(_multithreaded)
484484
// noop on no thread platforms
485485
#elseif os(Windows)
486486
WakeConditionVariable(cond)
@@ -490,7 +490,7 @@ open class NSCondition: NSObject, NSLocking, @unchecked Sendable {
490490
}
491491

492492
open func broadcast() {
493-
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
493+
#if !_runtime(_multithreaded)
494494
// noop on no thread platforms
495495
#elseif os(Windows)
496496
WakeAllConditionVariable(cond)

0 commit comments

Comments
 (0)