Skip to content

[Build][Runtime] Add a new threading library. #59287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0cf687a
[Build][Runtime] Replace SWIFT_STDLIB_SINGLE_THREADED_RUNTIME.
al45tair Mar 25, 2022
66b9d21
[Runtime] Remove all use of read/write locks.
al45tair Mar 29, 2022
f5bdb85
[Threading] Create new threading library and use it.
al45tair Apr 15, 2022
2ba80e1
[Unittests] Make the Threading unit tests work.
al45tair Apr 20, 2022
785f4a2
[Unittests] Add more threading library tests.
al45tair Apr 20, 2022
e934cbc
[Threading] Fix case sensitivity bug.
al45tair Apr 20, 2022
a699633
[Threading] Auto-detect threading in the preprocessor.
al45tair Apr 20, 2022
e0d23ed
[Threading] A couple of header file fixes.
al45tair Apr 20, 2022
6d1b6db
[Threading] Fix the Linux build.
al45tair Apr 21, 2022
84083af
[Build] Move initialization of SWIFT_HOST_VARIANT_SDK further up.
al45tair Apr 21, 2022
8615983
[Build] Fix static linking.
al45tair Apr 22, 2022
02b3431
[Build] Fix the name of the threading package setting.
al45tair Apr 22, 2022
4eca62a
[Threading][Windows] Fix missing close brace.
al45tair Apr 22, 2022
9d5736e
[Threading][Windows] Fix another Windows niggle.
al45tair Apr 22, 2022
d76f80d
[Threading][Windows] More header file fixes.
al45tair Apr 22, 2022
a7e42d0
[Threading][Windows] Undefine Yield and ERROR.
al45tair Apr 26, 2022
05cccf9
[Runtime][Windows] Need to link with Synchronization.lib.
al45tair Apr 26, 2022
2e28ac4
[Threading][Windows] Don't include <windows.h>
al45tair Apr 27, 2022
66f6eb6
[Runtime][Windows] A couple of files need to include <windows.h>
al45tair Apr 27, 2022
14a4bd4
[Concurrency][Threading] Remove use of platform thread functions.
al45tair Apr 27, 2022
d6cff94
[Threading][Windows] Try to avoid declaring _RTL_SRWLOCK
al45tair Apr 28, 2022
e305a7d
[Concurrency][Windows] Add a couple of includes for Windows.
al45tair Apr 28, 2022
ef2e40a
[Concurrency][Windows] Remove unnecessary includes.
al45tair Apr 28, 2022
3bfc7e0
[Threading][Windows] Link the unit test against Synchronization.lib
al45tair Apr 28, 2022
210b772
[UnitTests][Windows] Link with Synchronization.lib
al45tair Apr 28, 2022
f3a412d
[Threading] Remove thread_get_main().
al45tair Apr 28, 2022
0e9318c
[Threading] Put everything through git clang-format.
al45tair Apr 28, 2022
c7c1c1b
[Threading] Fix some problems with the C11 threading code.
al45tair Apr 29, 2022
eb4c81d
[Threading] Don't use TLS keys as template arguments.
al45tair May 4, 2022
b5c8b79
[Threading] Move stack bounds fetching into the threading library.
al45tair May 6, 2022
4825578
[Concurrency] Remove redundant include.
al45tair May 6, 2022
dc809f8
[Threading] A few include tweaks.
al45tair May 6, 2022
6962758
[Threading] Add the ability to use any C++ callable in swift::once().
al45tair May 24, 2022
668bcbc
[Threading] Fix a few things following rebase.
al45tair May 24, 2022
c401cc3
[Threading] Use llvm::Optional<> rather than making a zero lower boun…
al45tair May 24, 2022
33f103f
[Build] Fix some issues with the standalone library build.
al45tair May 31, 2022
124581a
[Build][Concurrency] Fix Concurrency build to work for Linux.
al45tair Jun 1, 2022
aa0265b
[Threading] Use raw gettid syscall for older glibc compatibility
kateinoigakukun Jun 2, 2022
c46d4af
[Build] Explicitly include threading library when not building stdlib.
al45tair Jun 6, 2022
ede4010
[BackDeployConcurrency] Fix to use the new threading library.
al45tair Jun 7, 2022
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
200 changes: 107 additions & 93 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,95 @@ else()
any compiler host sources written in Swift")
endif()

# A convenience pattern to match Darwin platforms. Example:
# if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
# ...
# endif()
set(SWIFT_DARWIN_VARIANTS "^(macosx|iphoneos|iphonesimulator|appletvos|appletvsimulator|watchos|watchsimulator)")
set(SWIFT_DARWIN_EMBEDDED_VARIANTS "^(iphoneos|iphonesimulator|appletvos|appletvsimulator|watchos|watchsimulator)")

# A convenient list to match Darwin SDKs. Example:
# if("${SWIFT_HOST_VARIANT_SDK}" IN_LIST SWIFT_DARWIN_PLATFORMS)
# ...
# endif()
set(SWIFT_DARWIN_PLATFORMS "IOS" "IOS_SIMULATOR" "TVOS" "TVOS_SIMULATOR" "WATCHOS" "WATCHOS_SIMULATOR" "OSX")

set(SWIFT_APPLE_PLATFORMS ${SWIFT_DARWIN_PLATFORMS})
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple")
list(APPEND SWIFT_APPLE_PLATFORMS "FREESTANDING")
if(SWIFT_FREESTANDING_IS_DARWIN)
list(APPEND SWIFT_DARWIN_PLATFORMS "FREESTANDING")
endif()
endif()

# If SWIFT_HOST_VARIANT_SDK not given, try to detect from the CMAKE_SYSTEM_NAME.
if(SWIFT_HOST_VARIANT_SDK)
set(SWIFT_HOST_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
else()
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(SWIFT_HOST_VARIANT_SDK_default "LINUX")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
set(SWIFT_HOST_VARIANT_SDK_default "FREEBSD")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
set(SWIFT_HOST_VARIANT_SDK_default "OPENBSD")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
set(SWIFT_HOST_VARIANT_SDK_default "CYGWIN")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(SWIFT_HOST_VARIANT_SDK_default "WINDOWS")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Haiku")
set(SWIFT_HOST_VARIANT_SDK_default "HAIKU")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
set(SWIFT_HOST_VARIANT_SDK_default "ANDROID")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(SWIFT_HOST_VARIANT_SDK_default "OSX")
else()
message(FATAL_ERROR "Unable to detect SDK for host system: ${CMAKE_SYSTEM_NAME}")
endif()
endif()

# If SWIFT_HOST_VARIANT_ARCH not given, try to detect from the CMAKE_SYSTEM_PROCESSOR.
if(SWIFT_HOST_VARIANT_ARCH)
set(SWIFT_HOST_VARIANT_ARCH_default "${SWIFT_HOST_VARIANT_ARCH}")
else()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
set(SWIFT_HOST_VARIANT_ARCH_default "x86_64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64")
if(SWIFT_HOST_VARIANT_SDK_default STREQUAL OSX)
set(SWIFT_HOST_VARIANT_ARCH_default "arm64")
else()
set(SWIFT_HOST_VARIANT_ARCH_default "aarch64")
endif()
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
set(SWIFT_HOST_VARIANT_ARCH_default "s390x")
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "armv5|armv5te")
set(SWIFT_HOST_VARIANT_ARCH_default "armv5")
# FIXME: Only matches v6l/v7l - by far the most common variants
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
set(SWIFT_HOST_VARIANT_ARCH_default "armv6")
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "armv7l|armv7-a")
set(SWIFT_HOST_VARIANT_ARCH_default "armv7")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
set(SWIFT_HOST_VARIANT_ARCH_default "itanium")
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "(x86|i686)")
set(SWIFT_HOST_VARIANT_ARCH_default "i686")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32")
set(SWIFT_HOST_VARIANT_ARCH_default "wasm32")
else()
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
endif()

set(SWIFT_HOST_VARIANT_SDK "${SWIFT_HOST_VARIANT_SDK_default}" CACHE STRING
"Deployment sdk for Swift host tools (the compiler).")
set(SWIFT_HOST_VARIANT_ARCH "${SWIFT_HOST_VARIANT_ARCH_default}" CACHE STRING
"Deployment arch for Swift host tools (the compiler).")

#
# User-configurable options that control the inclusion and default build
# behavior for components which may not strictly be necessary (tools, examples,
Expand Down Expand Up @@ -103,6 +192,16 @@ option(SWIFT_STDLIB_ENABLE_UNICODE_DATA
NOTE: Disabling this will cause many String methods to crash."
TRUE)

include(Threading)

threading_package_default("${SWIFT_HOST_VARIANT_SDK}"
SWIFT_THREADING_PACKAGE_default)

set(SWIFT_THREADING_PACKAGE "${SWIFT_THREADING_PACKAGE_default}"
CACHE STRING
"The threading package to use. Must be one of 'none', 'pthreads',
'darwin', 'linux', 'win32', 'c11'.")

option(SWIFT_BUILD_DYNAMIC_SDK_OVERLAY
"Build dynamic variants of the Swift SDK overlay"
TRUE)
Expand Down Expand Up @@ -671,27 +770,6 @@ include_directories(BEFORE
${SWIFT_INCLUDE_DIR}
)

# A convenience pattern to match Darwin platforms. Example:
# if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
# ...
# endif()
set(SWIFT_DARWIN_VARIANTS "^(macosx|iphoneos|iphonesimulator|appletvos|appletvsimulator|watchos|watchsimulator)")
set(SWIFT_DARWIN_EMBEDDED_VARIANTS "^(iphoneos|iphonesimulator|appletvos|appletvsimulator|watchos|watchsimulator)")

# A convenient list to match Darwin SDKs. Example:
# if("${SWIFT_HOST_VARIANT_SDK}" IN_LIST SWIFT_DARWIN_PLATFORMS)
# ...
# endif()
set(SWIFT_DARWIN_PLATFORMS "IOS" "IOS_SIMULATOR" "TVOS" "TVOS_SIMULATOR" "WATCHOS" "WATCHOS_SIMULATOR" "OSX")

set(SWIFT_APPLE_PLATFORMS ${SWIFT_DARWIN_PLATFORMS})
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple")
list(APPEND SWIFT_APPLE_PLATFORMS "FREESTANDING")
if(SWIFT_FREESTANDING_IS_DARWIN)
list(APPEND SWIFT_DARWIN_PLATFORMS "FREESTANDING")
endif()
endif()

# Configuration flags passed to all of our invocations of gyb. Try to
# avoid making up new variable names here if you can find a CMake
# variable that will do the job.
Expand All @@ -708,74 +786,6 @@ if(XCODE)
swift_common_xcode_cxx_config()
endif()

# If SWIFT_HOST_VARIANT_SDK not given, try to detect from the CMAKE_SYSTEM_NAME.
if(SWIFT_HOST_VARIANT_SDK)
set(SWIFT_HOST_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
else()
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(SWIFT_HOST_VARIANT_SDK_default "LINUX")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
set(SWIFT_HOST_VARIANT_SDK_default "FREEBSD")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
set(SWIFT_HOST_VARIANT_SDK_default "OPENBSD")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
set(SWIFT_HOST_VARIANT_SDK_default "CYGWIN")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(SWIFT_HOST_VARIANT_SDK_default "WINDOWS")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Haiku")
set(SWIFT_HOST_VARIANT_SDK_default "HAIKU")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
set(SWIFT_HOST_VARIANT_SDK_default "ANDROID")
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(SWIFT_HOST_VARIANT_SDK_default "OSX")
else()
message(FATAL_ERROR "Unable to detect SDK for host system: ${CMAKE_SYSTEM_NAME}")
endif()
endif()

# If SWIFT_HOST_VARIANT_ARCH not given, try to detect from the CMAKE_SYSTEM_PROCESSOR.
if(SWIFT_HOST_VARIANT_ARCH)
set(SWIFT_HOST_VARIANT_ARCH_default "${SWIFT_HOST_VARIANT_ARCH}")
else()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
set(SWIFT_HOST_VARIANT_ARCH_default "x86_64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64")
if(SWIFT_HOST_VARIANT_SDK_default STREQUAL OSX)
set(SWIFT_HOST_VARIANT_ARCH_default "arm64")
else()
set(SWIFT_HOST_VARIANT_ARCH_default "aarch64")
endif()
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
set(SWIFT_HOST_VARIANT_ARCH_default "s390x")
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "armv5|armv5te")
set(SWIFT_HOST_VARIANT_ARCH_default "armv5")
# FIXME: Only matches v6l/v7l - by far the most common variants
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
set(SWIFT_HOST_VARIANT_ARCH_default "armv6")
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "armv7l|armv7-a")
set(SWIFT_HOST_VARIANT_ARCH_default "armv7")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
set(SWIFT_HOST_VARIANT_ARCH_default "itanium")
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "(x86|i686)")
set(SWIFT_HOST_VARIANT_ARCH_default "i686")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32")
set(SWIFT_HOST_VARIANT_ARCH_default "wasm32")
else()
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
endif()

set(SWIFT_HOST_VARIANT_SDK "${SWIFT_HOST_VARIANT_SDK_default}" CACHE STRING
"Deployment sdk for Swift host tools (the compiler).")
set(SWIFT_HOST_VARIANT_ARCH "${SWIFT_HOST_VARIANT_ARCH_default}" CACHE STRING
"Deployment arch for Swift host tools (the compiler).")

# Which default linker to use. Prefer LLVM_USE_LINKER if it set, otherwise use
# our own defaults. This should only be possible in a unified (not stand alone)
# build environment.
Expand Down Expand Up @@ -1037,11 +1047,12 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
message(STATUS " Leak Detection Checker Entrypoints: ${SWIFT_RUNTIME_ENABLE_LEAK_CHECKER}")
message(STATUS "")

message(STATUS "Threading Package: ${SWIFT_THREADING_PACKAGE}")
message(STATUS "Differentiable Programming Support: ${SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING}")
message(STATUS "Concurrency Support: ${SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY}")
message(STATUS "Distributed Support: ${SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED}")
message(STATUS "String Processing Support: ${SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING}")
message(STATUS "Unicode Support: ${SWIFT_STDLIB_ENABLE_UNICODE_DATA}")
message(STATUS "Concurrency Support: ${SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY}")
message(STATUS "Distributed Support: ${SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED}")
message(STATUS "String Processing Support: ${SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING}")
message(STATUS "Unicode Support: ${SWIFT_STDLIB_ENABLE_UNICODE_DATA}")
message(STATUS "")
else()
message(STATUS "Not building Swift standard library, SDK overlays, and runtime")
Expand Down Expand Up @@ -1115,6 +1126,9 @@ endif()
if(SWIFT_BUILD_STDLIB)
add_subdirectory(stdlib)
else()
# Some of the things below depend on the threading library
add_subdirectory(stdlib/public/Threading)

if(SWIFT_BUILD_STDLIB_EXTRA_TOOLCHAIN_CONTENT)
add_subdirectory(stdlib/toolchain)
endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/caches/Runtime-WASI-wasm32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ set(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY YES CACHE BOOL "")
# build with the host compiler
set(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER YES CACHE BOOL "")

set(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME TRUE CACHE BOOL "")
set(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY TRUE CACHE BOOL "")
5 changes: 5 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include(SwiftXcodeSupport)
include(SwiftWindowsSupport)
include(SwiftAndroidSupport)
include(SwiftCXXUtils)
include(Threading)

function(_swift_gyb_target_sources target scope)
file(GLOB GYB_UNICODE_DATA ${SWIFT_SOURCE_DIR}/utils/UnicodeData/*)
Expand Down Expand Up @@ -308,6 +309,10 @@ function(_add_host_variant_c_compile_flags target)
$<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS>)
endif()

threading_package_name("${SWIFT_HOST_VARIANT_SDK}" _threading_package)
target_compile_definitions(${target} PRIVATE
"SWIFT_THREADING_${_threading_package}")

if(SWIFT_ANALYZE_CODE_COVERAGE)
target_compile_options(${target} PRIVATE
$<$<COMPILE_LANGUAGE:C,CXX,OBJC,OBJCXX>:-fprofile-instr-generate -fcoverage-mapping>)
Expand Down
11 changes: 8 additions & 3 deletions cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

include(AddSwift)
include(Threading)

add_custom_target(SwiftUnitTests)

Expand Down Expand Up @@ -47,11 +48,15 @@ function(add_swift_unittest test_dirname)
endif()

# some headers switch their inline implementations based on
# SWIFT_STDLIB_SINGLE_THREADED_RUNTIME definition
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and
# SWIFT_THREADING_PACKAGE definitions
if(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
target_compile_definitions("${test_dirname}" PRIVATE
SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
endif()
threading_package_name("${SWIFT_HOST_VARIANT_SDK}" _threading_package)
target_compile_definitions("${test_dirname}" PRIVATE
"SWIFT_THREADING_${_threading_package}")

if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
if(SWIFT_USE_LINKER)
Expand Down
29 changes: 29 additions & 0 deletions cmake/modules/Threading.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Get the default threading package for the platform
function(threading_package_default sdk out_var)
if(sdk IN_LIST SWIFT_DARWIN_PLATFORMS)
set("${out_var}" "darwin" PARENT_SCOPE)
elseif(sdk STREQUAL "LINUX")
set("${out_var}" "linux" PARENT_SCOPE)
elseif(sdk STREQUAL "WINDOWS")
set("${out_var}" "win32" PARENT_SCOPE)
elseif(sdk STREQUAL "WASI")
set("${out_var}" "none" PARENT_SCOPE)
else()
set("${out_var}" "pthreads" PARENT_SCOPE)
endif()
endfunction()

# Given the threading package, find the name for the preprocessor
# define that we need to make. Also deals with the default platform
# setting.
function(threading_package_name sdk out_var)
precondition(SWIFT_HOST_VARIANT_SDK)
precondition(SWIFT_DARWIN_PLATFORMS)

string(TOUPPER "${SWIFT_THREADING_PACKAGE}" package)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think setting this to SWIFT_THREADING_PACKAGE by default is a mistake, as it is set to the host platform by default. Since it's possible to cross-compile multiple non-host stdlibs, that means it will build all of them with the host threading library name.

I'm not sure how the couple lines just below are supposed to work either, how are you supposed to pass an empty string to build-script? I tried various things like --swift-threading-package="" and nothing worked. Even if it did, you only have threading_package_default() below taking one variable, when it needs two.

This just hit me because I cross-compile the Android armv7 SDK and it failed because it used pthreads, which doesn't support 32-bit here.

I was confused though because it built fine on the community Android armv7 CI, then I dug into it and it's because the host SDK there is LINUX so it applies that threading library to Android armv7 too, based on the default logic above. My own Android CI doesn't build for the linux host though, only for Android armv7 with the flags --cross-compile-hosts=android-armv7 --skip-local-build, so the host default logic chooses pthreads for the Android host and fails for armv7 only.

That suggests two fixes:

  1. Rethink the way the threading library is chosen by default for the stdlib, so you're not using the host threading library name for all stdlibs built.
  2. Add Android to the list of SDKs where a default threading library is chosen, and set a default.

Let me know what you think, @al45tair, you may want to get pthreads working for 32-bit too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making pthreads work for 32-bit is a fairly trivial fix, I think, and probably worth doing (possibly the right thing is to use uintptr_t instead of uint64_t for the type of the swift::once_t), though maybe we should be picking the linux setting on Android?

The swift-threading-package setting isn't presently supposed to be explicitly set to empty, which is why that doesn't work. It's there to let you override it and explicitly specify what you want, not the opposite (not sure why you'd need to do that). If you're looking at the headers and have seen the bit in the header file where it tries to guess, that has to be there because of lldb, rather than because I thought that was a good idea.

As for the cross compilation problem, could you file a separate issue for that? The threading code gets used both for the host and the target, so I think you're right that the automatic detection might indeed need to be tweaked for the target platform.

Copy link
Member

@finagolfin finagolfin Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should be picking the linux setting on Android?

I'm not sure: both appear to work on 64-bit platforms, so I will look into it and let you know.

The swift-threading-package setting isn't presently supposed to be explicitly set to empty, which is why that doesn't work.

I thought you explicitly included the empty string in this list of threading options to allow people to set it, but I think the bash check a couple lines above wouldn't allow it through anyway.

It's there to let you override it and explicitly specify what you want, not the opposite (not sure why you'd need to do that)

OK, this check that I mentioned will never be true then? I thought the intent was for it to signify "auto-detect" for each stdlib, but it currently doesn't do that either.

As for the cross compilation problem, could you file a separate issue for that?

Will doDone, #59568.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to fix the remaining 32-bit issues (@compnerd fixed it already for Windows): #59586

if(package STREQUAL "")
threading_package_default(package)
string(TOUPPER "${package}" package)
endif()
set("${out_var}" "${package}" PARENT_SCOPE)
endfunction()
16 changes: 16 additions & 0 deletions include/swift/Basic/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,20 @@
#define SWIFT_ASM_LABEL_WITH_PREFIX(STRING) \
SWIFT_ASM_LABEL_RAW(SWIFT_SYMBOL_PREFIX_STRING STRING)

// SWIFT_FORMAT(fmt,first) marks a function as taking a format string argument
// at argument `fmt`, with the first argument for the format string as `first`.
#if __has_attribute(format)
#define SWIFT_FORMAT(fmt, first) __attribute__((format(printf, fmt, first)))
#else
#define SWIFT_FORMAT(fmt, first)
#endif

// SWIFT_VFORMAT(fmt) marks a function as taking a format string argument at
// argument `fmt`, with the arguments in a `va_list`.
#if __has_attribute(format)
#define SWIFT_VFORMAT(fmt) __attribute__((format(printf, fmt, 0)))
#else
#define SWIFT_VFORMAT(fmt)
#endif

#endif // SWIFT_BASIC_COMPILER_H
Loading