Skip to content
Draft
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
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ list(APPEND _Foundation_common_build_flags
"-Wno-switch"
"-fblocks")

if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI")
list(APPEND _Foundation_common_build_flags
"-DDEPLOYMENT_ENABLE_LIBDISPATCH"
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
endif()

if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
list(APPEND _Foundation_common_build_flags
"-fconstant-cfstrings"
Expand Down Expand Up @@ -245,10 +239,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
list(APPEND _Foundation_swift_build_flags "SHELL:-Xcc -D${def}")
list(APPEND _Foundation_common_build_flags "-D${def}")
endforeach()
else()
# Assume we have threads on other platforms
list(APPEND _Foundation_swift_build_flags
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
Expand Down
17 changes: 0 additions & 17 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@

import PackageDescription

let platformsWithThreads: [Platform] = [
.iOS,
.macOS,
.tvOS,
.watchOS,
.macCatalyst,
.driverKit,
.android,
.linux,
.windows,
]

var dispatchIncludeFlags: [CSetting] = []
if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] {
dispatchIncludeFlags.append(.unsafeFlags([
Expand Down Expand Up @@ -92,10 +80,8 @@ let coreFoundationBuildSettings: [CSetting] = [
.define("DEBUG", .when(configuration: .debug)),
.define("CF_BUILDING_CF"),
.define("CF_WINDOWS_EXECUTABLE_INITIALIZER", .when(platforms: [.windows])), // Ensure __CFInitialize is run even when statically linked into an executable
.define("DEPLOYMENT_ENABLE_LIBDISPATCH", .when(platforms: platformsWithThreads)),
.define("DEPLOYMENT_RUNTIME_SWIFT"),
.define("HAVE_STRUCT_TIMESPEC"),
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)),
.define("_GNU_SOURCE", .when(platforms: [.linux, .android])),
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
.unsafeFlags([
Expand Down Expand Up @@ -124,9 +110,7 @@ let interfaceBuildSettings: [CSetting] = [
.headerSearchPath("../CoreFoundation/internalInclude"),
.define("DEBUG", .when(configuration: .debug)),
.define("CF_BUILDING_CF"),
.define("DEPLOYMENT_ENABLE_LIBDISPATCH"),
.define("HAVE_STRUCT_TIMESPEC"),
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)),
.define("_GNU_SOURCE", .when(platforms: [.linux, .android])),
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
.unsafeFlags([
Expand All @@ -149,7 +133,6 @@ let interfaceBuildSettings: [CSetting] = [

let swiftBuildSettings: [SwiftSetting] = [
.define("DEPLOYMENT_RUNTIME_SWIFT"),
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"),
.swiftLanguageMode(.v6),
.unsafeFlags([
"-Xfrontend",
Expand Down
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 !TARGET_OS_WASI || 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 !TARGET_OS_WASI || 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 !TARGET_OS_WASI || 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 !TARGET_OS_WASI || 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 !TARGET_OS_WASI || 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
Loading