Skip to content

Commit 2a60c88

Browse files
committed
Implement Mutex in Synchronization
1 parent 083fdaa commit 2a60c88

File tree

10 files changed

+675
-10
lines changed

10 files changed

+675
-10
lines changed

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,42 @@ endfunction()
18231823
# Presence of a build flavor requires SWIFT_MODULE_DEPENDS_MACCATALYST to be
18241824
# defined and have values.
18251825
#
1826+
# SWIFT_SOURCES_DEPENDS_MACOS
1827+
# Sources that are built when this library is being built for macOS
1828+
#
1829+
# SWIFT_SOURCES_DEPENDS_IOS
1830+
# Sources that are built when this library is being built for iOS
1831+
#
1832+
# SWIFT_SOURCES_DEPENDS_TVOS
1833+
# Sources that are built when this library is being built for macOS
1834+
#
1835+
# SWIFT_SOURCES_DEPENDS_WATCHOS
1836+
# Sources that are built when this library is being built for macOS
1837+
#
1838+
# SWIFT_SOURCES_DEPENDS_FREESTANDING
1839+
# Sources that are built when this library is being built for macOS
1840+
#
1841+
# SWIFT_SOURCES_DEPENDS_FREEBSD
1842+
# Sources that are built when this library is being built for macOS
1843+
#
1844+
# SWIFT_SOURCES_DEPENDS_OPENBSD
1845+
# Sources that are built when this library is being built for macOS
1846+
#
1847+
# SWIFT_SOURCES_DEPENDS_LINUX
1848+
# Sources that are built when this library is being built for macOS
1849+
#
1850+
# SWIFT_SOURCES_DEPENDS_CYGWIN
1851+
# Sources that are built when this library is being built for macOS
1852+
#
1853+
# SWIFT_SOURCES_DEPENDS_HAIKU
1854+
# Sources that are built when this library is being built for macOS
1855+
#
1856+
# SWIFT_SOURCES_DEPENDS_WASI
1857+
# Sources that are built when this library is being built for macOS
1858+
#
1859+
# SWIFT_SOURCES_DEPENDS_WINDOWS
1860+
# Sources that are built when this library is being built for macOS
1861+
#
18261862
# source1 ...
18271863
# Sources to add into this library.
18281864
function(add_swift_target_library name)
@@ -1898,7 +1934,19 @@ function(add_swift_target_library name)
18981934
TARGET_SDKS
18991935
SWIFT_COMPILE_FLAGS_MACCATALYST
19001936
SWIFT_MODULE_DEPENDS_MACCATALYST
1901-
SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED)
1937+
SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED
1938+
SWIFT_SOURCES_DEPENDS_MACOS
1939+
SWIFT_SOURCES_DEPENDS_IOS
1940+
SWIFT_SOURCES_DEPENDS_TVOS
1941+
SWIFT_SOURCES_DEPENDS_WATCHOS
1942+
SWIFT_SOURCES_DEPENDS_FREESTANDING
1943+
SWIFT_SOURCES_DEPENDS_FREEBSD
1944+
SWIFT_SOURCES_DEPENDS_OPENBSD
1945+
SWIFT_SOURCES_DEPENDS_LINUX
1946+
SWIFT_SOURCES_DEPENDS_CYGWIN
1947+
SWIFT_SOURCES_DEPENDS_HAIKU
1948+
SWIFT_SOURCES_DEPENDS_WASI
1949+
SWIFT_SOURCES_DEPENDS_WINDOWS)
19021950

19031951
cmake_parse_arguments(SWIFTLIB
19041952
"${SWIFTLIB_options}"
@@ -2169,6 +2217,33 @@ function(add_swift_target_library name)
21692217
list(APPEND swiftlib_link_flags_all "-Xlinker" "-ignore_auto_link")
21702218
endif()
21712219

2220+
# Append SDK specific sources to the full list of sources
2221+
if(sdk STREQUAL "OSX")
2222+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_MACOS})
2223+
elseif(sdk STREQUAL "IOS" OR sdk STREQUAL "IOS_SIMULATOR")
2224+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_IOS})
2225+
elseif(sdk STREQUAL "TVOS" OR sdk STREQUAL "TVOS_SIMULATOR")
2226+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_TVOS})
2227+
elseif(sdk STREQUAL "WATCHOS" OR sdk STREQUAL "WATCHOS_SIMULATOR")
2228+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WATCHOS})
2229+
elseif(sdk STREQUAL "FREESTANDING")
2230+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_FREESTANDING})
2231+
elseif(sdk STREQUAL "FREEBSD")
2232+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_FREEBSD})
2233+
elseif(sdk STREQUAL "OPENBSD")
2234+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_OPENBSD})
2235+
elseif(sdk STREQUAL "LINUX" OR sdk STREQUAL "ANDROID")
2236+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_LINUX})
2237+
elseif(sdk STREQUAL "CYGWIN")
2238+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_CYGWIN})
2239+
elseif(sdk STREQUAL "HAIKU")
2240+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_HAIKU})
2241+
elseif(sdk STREQUAL "WASI")
2242+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WASI})
2243+
elseif(sdk STREQUAL "WINDOWS")
2244+
list(APPEND SWIFTLIB_SOURCES ${SWIFTLIB_SWIFT_SOURCES_DEPENDS_WINDOWS})
2245+
endif()
2246+
21722247
# We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in
21732248
# swift_common_standalone_build_config_llvm within
21742249
# SwiftSharedCMakeConfig.cmake, where it was added by a call to

stdlib/public/SwiftShims/swift/shims/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(sources
1717
SwiftStdbool.h
1818
SwiftStddef.h
1919
SwiftStdint.h
20+
SynchronizationShims.h
2021
System.h
2122
Target.h
2223
ThreadLocalStorage.h
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H
14+
#define SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H
15+
16+
#include "SwiftStdbool.h"
17+
#include "SwiftStdint.h"
18+
19+
#if defined(__linux__)
20+
#include <linux/futex.h>
21+
#include <sys/syscall.h>
22+
#include <unistd.h>
23+
24+
#define SWIFT_FUTEX_WAITERS FUTEX_WAITERS
25+
26+
static inline __swift_bool _swift_stdlib_wait(__swift_uint32_t *lock) {
27+
return syscall(SYS_futex, lock, FUTEX_LOCK_PI_PRIVATE,
28+
/* val */ 0, // this value is ignored by this futex op
29+
/* timeout */ NULL); // block indefinitely
30+
}
31+
32+
static inline __swift_bool _swift_stdlib_wake(__swift_uint32_t *lock) {
33+
return syscall(SYS_futex, lock, FUTEX_UNLOCK_PI_PRIVATE);
34+
}
35+
36+
#endif // defined(__linux__)
37+
38+
#endif // SWIFT_STDLIB_SYNCHRONIZATION_SHIMS_H

stdlib/public/SwiftShims/swift/shims/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ module SwiftOverlayShims {
3131
header "LibcOverlayShims.h"
3232
export *
3333
}
34+
35+
module SynchronizationShims {
36+
header "SynchronizationShims.h"
37+
export *
38+
}

stdlib/public/Synchronization/CMakeLists.txt

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
#===----------------------------------------------------------------------===#
1212

13-
set(swift_synchronization_sources
13+
set(SWIFT_SYNCHRONIZATION_ATOMIC_SOURCES
1414
Atomic.swift
1515
AtomicBool.swift
1616
AtomicFloats.swift
@@ -21,21 +21,86 @@ set(swift_synchronization_sources
2121
AtomicRepresentable.swift
2222
WordPair.swift
2323
)
24-
set(swift_synchronization_gyb_sources
24+
25+
set(SWIFT_SYNCHRONIZATION_SOURCES
26+
${SWIFT_SYNCHRONIZATION_ATOMIC_SOURCES}
27+
28+
Cell.swift
29+
Mutex/Mutex.swift
30+
)
31+
32+
set(SWIFT_SYNCHRONIZATION_GYB_SOURCES
2533
AtomicIntegers.swift.gyb
2634
AtomicStorage.swift.gyb
2735
)
28-
set(swift_synchronization_swift_compile_flags
36+
37+
# Darwin dependencies and sources
38+
39+
set(SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES)
40+
41+
if(SWIFT_BUILD_SDK_OVERLAY)
42+
set(SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES Darwin)
43+
endif()
44+
45+
set(SWIFT_SYNCHRONIZATION_DARWIN_SOURCES
46+
Mutex/DarwinImpl.swift
47+
)
48+
49+
# Linux dependencies and sources
50+
51+
set(SWIFT_SYNCHRONIZATION_LINUX_SOURCES
52+
Mutex/LinuxImpl.swift
53+
)
54+
55+
# Windows depdencies and sources
56+
57+
set(SWIFT_SYNCHRONIZATION_WINDOWS_DEPENDENCIES)
58+
59+
if(SWIFT_BUILD_SDK_OVERLAY)
60+
set(SWIFT_SYNCHRONIZATION_WINDOWS_DEPENDENCIES WinSDK)
61+
endif()
62+
63+
set(SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES
64+
Mutex/WindowsImpl.swift
65+
)
66+
67+
set(SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS
2968
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
3069
"-enable-builtin-module"
3170
"-enable-experimental-feature" "RawLayout"
3271
"-enable-experimental-feature" "StaticExclusiveOnly"
3372
)
3473

3574
add_swift_target_library(swiftSynchronization ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
36-
${swift_synchronization_sources}
37-
GYB_SOURCES ${swift_synchronization_gyb_sources}
38-
SWIFT_COMPILE_FLAGS ${swift_synchronization_swift_compile_flags}
75+
${SWIFT_SYNCHRONIZATION_SOURCES}
76+
77+
GYB_SOURCES
78+
${SWIFT_SYNCHRONIZATION_GYB_SOURCES}
79+
80+
SWIFT_SOURCES_DEPENDS_MACOS
81+
${SWIFT_SYNCHRONIZATION_DARWIN_SOURCES}
82+
SWIFT_SOURCES_DEPENDS_IOS
83+
${SWIFT_SYNCHRONIZATION_DARWIN_SOURCES}
84+
SWIFT_SOURCES_DEPENDS_TVOS
85+
${SWIFT_SYNCHRONIZATION_DARWIN_SOURCES}
86+
SWIFT_SOURCES_DEPENDS_WATCHOS
87+
${SWIFT_SYNCHRONIZATION_DARWIN_SOURCES}
88+
SWIFT_SOURCES_DEPENDS_LINUX
89+
${SWIFT_SYNCHRONIZATION_LINUX_SOURCES}
90+
SWIFT_SOURCES_DEPENDS_WINDOWS
91+
${SWIFT_SYNCHRONIZATION_WINDOWS_SOURCES}
92+
93+
SWIFT_MODULE_DEPENDS_OSX
94+
${SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES}
95+
SWIFT_MODULE_DEPENDS_IOS
96+
${SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES}
97+
SWIFT_MODULE_DEPENDS_TVOS
98+
${SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES}
99+
SWIFT_MODULE_DEPENDS_WATCHOS
100+
${SWIFT_SYNCHRONIZATION_DARWIN_DEPENDENCIES}
101+
102+
SWIFT_COMPILE_FLAGS
103+
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}
39104
LINK_FLAGS
40105
"${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
41106
INSTALL_IN_COMPONENT
@@ -68,11 +133,12 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
68133
ONLY_SWIFTMODULE
69134
IS_FRAGILE
70135

71-
${swift_synchronization_sources}
72-
GYB_SOURCES ${swift_synchronization_gyb_sources}
136+
${SWIFT_SYNCHRONIZATION_ATOMIC_SOURCES}
137+
GYB_SOURCES
138+
${SWIFT_SYNCHRONIZATION_GYB_SOURCES}
73139

74140
SWIFT_COMPILE_FLAGS
75-
${swift_synchronization_swift_compile_flags}
141+
${SWIFT_SYNCHRNOIZATION_SWIFT_FLAGS}
76142
-Xcc -D__MACH__ -Xcc -D__APPLE__ -Xcc -ffreestanding -enable-experimental-feature Embedded
77143

78144
MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Atomics open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Builtin
14+
15+
@available(SwiftStdlib 6.0, *)
16+
@frozen
17+
@usableFromInline
18+
@_rawLayout(like: Value)
19+
internal struct _Cell<Value: ~Copyable>: ~Copyable {
20+
@available(SwiftStdlib 6.0, *)
21+
@_alwaysEmitIntoClient
22+
@_transparent
23+
var rawAddress: Builtin.RawPointer {
24+
Builtin.unprotectedAddressOfBorrow(self)
25+
}
26+
27+
@available(SwiftStdlib 6.0, *)
28+
@_alwaysEmitIntoClient
29+
@_transparent
30+
init(_ initialValue: consuming Value) {
31+
Builtin.initialize(initialValue, rawAddress)
32+
}
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Atomics open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Builtin
14+
import Darwin
15+
16+
@available(SwiftStdlib 6.0, *)
17+
@frozen
18+
@usableFromInline
19+
@_staticExclusiveOnly
20+
internal struct _MutexHandle: ~Copyable {
21+
@usableFromInline
22+
let value: _Cell<os_unfair_lock>
23+
24+
@available(SwiftStdlib 6.0, *)
25+
@_alwaysEmitIntoClient
26+
@_transparent
27+
init() {
28+
value = _Cell(os_unfair_lock())
29+
}
30+
31+
@available(SwiftStdlib 6.0, *)
32+
@_alwaysEmitIntoClient
33+
@_transparent
34+
borrowing func lock() {
35+
os_unfair_lock_lock(os_unfair_lock_t(value.rawAddress))
36+
}
37+
38+
@available(SwiftStdlib 6.0, *)
39+
@_alwaysEmitIntoClient
40+
@_transparent
41+
borrowing func tryLock() -> Bool {
42+
os_unfair_lock_trylock(os_unfair_lock_t(value.rawAddress))
43+
}
44+
45+
@available(SwiftStdlib 6.0, *)
46+
@_alwaysEmitIntoClient
47+
@_transparent
48+
borrowing func unlock() {
49+
os_unfair_lock_unlock(os_unfair_lock_t(value.rawAddress))
50+
}
51+
}

0 commit comments

Comments
 (0)