Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions Sources/CoreFoundation/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -1715,43 +1715,37 @@ typedef struct _CFThreadSpecificData {
__stdcall
#endif
static void _CFThreadSpecificDestructor(void *ctx) {
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if TARGET_OS_WIN32
_CFThreadSpecificData *data = (_CFThreadSpecificData *)ctx;
FlsSetValue(data->key, NULL);
swift_release(data->value);
free(data);
#else
#elif defined(_REENTRANT)
swift_release(ctx);
#endif
#endif
}

_CFThreadSpecificKey _CFThreadSpecificKeyCreate() {
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
_CFThreadSpecificKey key;
#if TARGET_OS_WIN32
key = FlsAlloc(_CFThreadSpecificDestructor);
#else
#elif defined(_REENTRANT)
pthread_key_create(&key, &_CFThreadSpecificDestructor);
#endif
return key;
#else
return 0;
key = 0;
#endif
return key;
}

CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key) {
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if TARGET_OS_WIN32
_CFThreadSpecificData *data = (_CFThreadSpecificData *)FlsGetValue(key);
if (data == NULL) {
return NULL;
}
return data->value;
#else
#elif defined(_REENTRANT)
return (CFTypeRef)pthread_getspecific(key);
#endif
#else
return NULL;
#endif
Expand All @@ -1761,7 +1755,6 @@ void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
// Intentionally not calling `swift_release` for previous value.
// OperationQueue uses these API (through NSThreadSpecific), and balances
// retain count manually.
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if TARGET_OS_WIN32
free(FlsGetValue(key));

Expand All @@ -1778,20 +1771,17 @@ void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
}

FlsSetValue(key, data);
#else
#elif defined(_REENTRANT)
if (value != NULL) {
swift_retain((void *)value);
pthread_setspecific(key, value);
} else {
pthread_setspecific(key, NULL);
}
#endif
#else
#endif
}

_CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context) {
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if TARGET_OS_WIN32
DWORD dwCreationFlags = 0;
DWORD dwStackSize = 0;
Expand All @@ -1807,18 +1797,16 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
return (_CFThreadRef)_beginthreadex(NULL, dwStackSize,
(_beginthreadex_proc_type)startfn,
context, dwCreationFlags, NULL);
#else
#elif defined(_REENTRANT)
_CFThreadRef thread;
pthread_create(&thread, &attrs, startfn, context);
return thread;
#endif
#else
return NULL;
#endif
}

CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name) {
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if TARGET_OS_MAC
if (pthread_equal(pthread_self(), thread)) {
return pthread_setname_np(name);
Expand Down Expand Up @@ -1848,15 +1836,14 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
#elif TARGET_OS_BSD
pthread_set_name_np(thread, name);
return 0;
#endif
#else
#elif TARGET_OS_WASI
// No thread naming support in WASI
return -1;
#endif
}

// `buf` must be null-terminated
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if TARGET_OS_MAC
return pthread_getname_np(pthread_self(), buf, length);
#elif TARGET_OS_ANDROID
Expand Down Expand Up @@ -1902,11 +1889,11 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
LocalFree(pszThreadDescription);

return 0;
#endif
return -1;
#else
#elif TARGET_OS_WASI
// No thread naming support in WASI
return -1;
#endif
return -1;
}

CF_EXPORT char **_CFEnviron(void) {
Expand Down
46 changes: 23 additions & 23 deletions Sources/Foundation/NSLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ open class NSLock: NSObject, NSLocking, @unchecked Sendable {
#endif

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

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

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

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

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

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

#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if !_runtime(_multithreaded)
// noop on no thread platforms
return true
#elseif os(macOS) || os(iOS) || os(Windows)
Expand All @@ -170,7 +170,7 @@ extension NSLock {
}
}

#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if _runtime(_multithreaded)
open class NSConditionLock : NSObject, NSLocking, @unchecked Sendable {
internal var _cond = NSCondition()
internal var _value: Int
Expand Down Expand Up @@ -282,7 +282,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {

public override init() {
super.init()
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if !_runtime(_multithreaded)
// noop on no thread platforms
#elseif os(Windows)
InitializeCriticalSection(mutex)
Expand Down Expand Up @@ -312,7 +312,7 @@ open class NSRecursiveLock: NSObject, NSLocking, @unchecked Sendable {
}

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

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

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

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

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

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

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

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

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

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

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

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

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

open func broadcast() {
#if !SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
#if !_runtime(_multithreaded)
// noop on no thread platforms
#elseif os(Windows)
WakeAllConditionVariable(cond)
Expand Down
Loading