Skip to content

Commit eef6f00

Browse files
[wasm] Use pthread API for NSLock for wasip1-threads target (#5293)
* [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. * [wasm] Make `Thread` APIs available on WASI target Recent wasi-libc versions include pthreads support for wasip1-threads target and a stub implementation for wasip1 target.
1 parent 040e214 commit eef6f00

File tree

3 files changed

+50
-52
lines changed

3 files changed

+50
-52
lines changed

Sources/CoreFoundation/CFPlatform.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,43 +1695,37 @@ typedef struct _CFThreadSpecificData {
16951695
__stdcall
16961696
#endif
16971697
static void _CFThreadSpecificDestructor(void *ctx) {
1698-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
16991698
#if TARGET_OS_WIN32
17001699
_CFThreadSpecificData *data = (_CFThreadSpecificData *)ctx;
17011700
FlsSetValue(data->key, NULL);
17021701
swift_release(data->value);
17031702
free(data);
1704-
#else
1703+
#elif !TARGET_OS_WASI || defined(_REENTRANT)
17051704
swift_release(ctx);
17061705
#endif
1707-
#endif
17081706
}
17091707

17101708
_CFThreadSpecificKey _CFThreadSpecificKeyCreate() {
1711-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
17121709
_CFThreadSpecificKey key;
17131710
#if TARGET_OS_WIN32
17141711
key = FlsAlloc(_CFThreadSpecificDestructor);
1715-
#else
1712+
#elif !TARGET_OS_WASI || defined(_REENTRANT)
17161713
pthread_key_create(&key, &_CFThreadSpecificDestructor);
1717-
#endif
1718-
return key;
17191714
#else
1720-
return 0;
1715+
key = 0;
17211716
#endif
1717+
return key;
17221718
}
17231719

17241720
CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key) {
1725-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
17261721
#if TARGET_OS_WIN32
17271722
_CFThreadSpecificData *data = (_CFThreadSpecificData *)FlsGetValue(key);
17281723
if (data == NULL) {
17291724
return NULL;
17301725
}
17311726
return data->value;
1732-
#else
1727+
#elif !TARGET_OS_WASI || defined(_REENTRANT)
17331728
return (CFTypeRef)pthread_getspecific(key);
1734-
#endif
17351729
#else
17361730
return NULL;
17371731
#endif
@@ -1741,7 +1735,6 @@ void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
17411735
// Intentionally not calling `swift_release` for previous value.
17421736
// OperationQueue uses these API (through NSThreadSpecific), and balances
17431737
// retain count manually.
1744-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
17451738
#if TARGET_OS_WIN32
17461739
free(FlsGetValue(key));
17471740

@@ -1758,20 +1751,17 @@ void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
17581751
}
17591752

17601753
FlsSetValue(key, data);
1761-
#else
1754+
#elif !TARGET_OS_WASI || defined(_REENTRANT)
17621755
if (value != NULL) {
17631756
swift_retain((void *)value);
17641757
pthread_setspecific(key, value);
17651758
} else {
17661759
pthread_setspecific(key, NULL);
17671760
}
17681761
#endif
1769-
#else
1770-
#endif
17711762
}
17721763

17731764
_CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context) {
1774-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
17751765
#if TARGET_OS_WIN32
17761766
DWORD dwCreationFlags = 0;
17771767
DWORD dwStackSize = 0;
@@ -1787,18 +1777,16 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
17871777
return (_CFThreadRef)_beginthreadex(NULL, dwStackSize,
17881778
(_beginthreadex_proc_type)startfn,
17891779
context, dwCreationFlags, NULL);
1790-
#else
1780+
#elif !TARGET_OS_WASI || defined(_REENTRANT)
17911781
_CFThreadRef thread;
17921782
pthread_create(&thread, &attrs, startfn, context);
17931783
return thread;
1794-
#endif
17951784
#else
17961785
return NULL;
17971786
#endif
17981787
}
17991788

18001789
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name) {
1801-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
18021790
#if TARGET_OS_MAC
18031791
if (pthread_equal(pthread_self(), thread)) {
18041792
return pthread_setname_np(name);
@@ -1828,15 +1816,14 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
18281816
#elif TARGET_OS_BSD
18291817
pthread_set_name_np(thread, name);
18301818
return 0;
1831-
#endif
1832-
#else
1819+
#elif TARGET_OS_WASI
1820+
// No thread naming support in WASI
18331821
return -1;
18341822
#endif
18351823
}
18361824

18371825
// `buf` must be null-terminated
18381826
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
1839-
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
18401827
#if TARGET_OS_MAC
18411828
return pthread_getname_np(pthread_self(), buf, length);
18421829
#elif TARGET_OS_ANDROID
@@ -1882,11 +1869,11 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
18821869
LocalFree(pszThreadDescription);
18831870

18841871
return 0;
1885-
#endif
1886-
return -1;
1887-
#else
1872+
#elif TARGET_OS_WASI
1873+
// No thread naming support in WASI
18881874
return -1;
18891875
#endif
1876+
return -1;
18901877
}
18911878

18921879
CF_EXPORT char **_CFEnviron(void) {

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)