Skip to content

Commit b011018

Browse files
authored
Replace LockedState with Mutex (swiftlang#1840)
1 parent e78fc09 commit b011018

File tree

50 files changed

+394
-681
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+394
-681
lines changed

Sources/FoundationEssentials/AttributedString/AttributeScope.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
internal import Synchronization
14+
1315
// Developers can also add the attributes to pre-defined scopes of attributes, which are used to provide type information to the encoding and decoding of AttributedString values, as well as allow for dynamic member lookup in Runs of AttributedStrings.
1416
// Example, where ForegroundColor is an existing AttributedStringKey:
1517
// struct MyAttributes : AttributeScope {
@@ -121,7 +123,7 @@ fileprivate struct LoadedScopeCache : Sendable {
121123
}
122124
}
123125

124-
fileprivate let _loadedScopeCache = LockedState(initialState: LoadedScopeCache())
126+
fileprivate let _loadedScopeCache = Mutex(LoadedScopeCache())
125127

126128
extension AttributeScopes {
127129
@_spi(AttributedStringDefaultScopes)

Sources/FoundationEssentials/AttributedString/AttributedString+IndexValidity.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if canImport(Synchronization)
1413
internal import Synchronization
15-
#endif
1614

1715
extension AttributedString.Guts {
1816
typealias Version = UInt
19-
20-
#if canImport(Synchronization)
17+
2118
private static let _nextVersion = Atomic<Version>(0)
22-
#else
23-
private static let _nextVersion = LockedState<Version>(initialState: 0)
24-
#endif
2519

2620
static func createNewVersion() -> Version {
27-
#if canImport(Synchronization)
2821
_nextVersion.wrappingAdd(1, ordering: .relaxed).oldValue
29-
#else
30-
_nextVersion.withLock { value in
31-
defer {
32-
value &+= 1
33-
}
34-
return value
35-
}
36-
#endif
3722
}
3823

3924
func incrementVersion() {

Sources/FoundationEssentials/AttributedString/AttributedString.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal import _RopeModule
1818
internal import _FoundationCollections
1919
#endif
2020

21+
internal import Synchronization
22+
2123
@dynamicMemberLookup
2224
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
2325
public struct AttributedString : Sendable {
@@ -30,12 +32,9 @@ public struct AttributedString : Sendable {
3032

3133
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
3234
extension AttributedString {
33-
internal static let currentIdentity = LockedState(initialState: 0)
35+
internal static let currentIdentity = Atomic(0)
3436
internal static var _nextModifyIdentity : Int {
35-
currentIdentity.withLock { identity in
36-
identity += 1
37-
return identity
38-
}
37+
currentIdentity.wrappingAdd(1, ordering: .relaxed).newValue
3938
}
4039
}
4140

Sources/FoundationEssentials/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ add_library(FoundationEssentials
2323
DateInterval.swift
2424
FoundationEssentials.swift
2525
IndexPath.swift
26-
LockedState.swift
2726
Logging.swift
27+
Mutex+ExtendedLifetime.swift
2828
OutputBuffer.swift
2929
Platform.swift
3030
Progress+Stub.swift

Sources/FoundationEssentials/Calendar/Calendar_Cache.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal import _ForSwiftFoundation
1515
import CoreFoundation
1616
#endif
1717

18+
internal import Synchronization
19+
1820
#if FOUNDATION_FRAMEWORK && canImport(_FoundationICU)
1921
internal func _calendarICUClass() -> _CalendarProtocol.Type? {
2022
_CalendarICU.self
@@ -41,9 +43,9 @@ struct CalendarCache : Sendable, ~Copyable {
4143

4244
// The values stored in these two locks do not depend upon each other, so it is safe to access them with separate locks. This helps avoids contention on a single lock.
4345

44-
private let _current = LockedState<(any _CalendarProtocol)?>(initialState: nil)
45-
private let _fixed = LockedState<[Calendar.Identifier: any _CalendarProtocol]>(initialState: [:])
46-
46+
private let _current = Mutex<(any _CalendarProtocol)?>(nil)
47+
private let _fixed = Mutex<[Calendar.Identifier: any _CalendarProtocol]>([:])
48+
4749
fileprivate init() {
4850
}
4951

Sources/FoundationEssentials/FileManager/SwiftFileManager.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#if !FOUNDATION_FRAMEWORK
1414

15+
internal import Synchronization
16+
1517
public struct FileAttributeType : Hashable, RawRepresentable, Sendable {
1618
public let rawValue: String
1719
public init(rawValue: String) {
@@ -179,7 +181,7 @@ extension FileManager {
179181
open class FileManager : @unchecked Sendable {
180182
// Sendable note: _impl may only be mutated in `init`
181183
private var _impl: _FileManagerImpl
182-
private let _lock = LockedState<State>(initialState: .init(delegate: nil))
184+
private let _lock = Mutex(State(delegate: nil))
183185
private let isDefault: Bool
184186

185187
private static let _default = FileManager(default: true)

Sources/FoundationEssentials/Formatting/FormatterCache.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
package struct FormatterCache<Format : Hashable & Sendable, FormattingType: Sendable>: Sendable {
13+
internal import Synchronization
14+
15+
package struct FormatterCache<Format : Hashable & Sendable, FormattingType: Sendable>: Sendable, ~Copyable {
1416
let countLimit = 100
1517

16-
private let _lock: LockedState<[Format: FormattingType]>
18+
private let _lock: Mutex<[Format: FormattingType]>
1719
package func formatter(for config: Format, creator: () throws -> FormattingType) rethrows -> FormattingType {
1820
let existed = _lock.withLock { cache in
1921
return cache [config]
@@ -43,12 +45,12 @@ package struct FormatterCache<Format : Hashable & Sendable, FormattingType: Send
4345
}
4446

4547
package subscript(key: Format) -> FormattingType? {
46-
_lock.withLock {
48+
_lock.withLockExtendingLifetimeOfState {
4749
$0[key]
4850
}
4951
}
5052

5153
package init() {
52-
_lock = LockedState(initialState: [Format: FormattingType]())
54+
_lock = Mutex([Format: FormattingType]())
5355
}
5456
}

Sources/FoundationEssentials/JSON/BufferView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ extension BufferView /*where Element: BitwiseCopyable*/ {
7575
}
7676
}
7777

78+
extension BufferView: Sendable where Element: Sendable {}
79+
7880
//MARK: Sequence
7981

8082
extension BufferView: Sequence {
@@ -94,9 +96,6 @@ extension BufferView: Sequence {
9496
}
9597
}
9698

97-
@available(*, unavailable)
98-
extension BufferView: Sendable {}
99-
10099
extension BufferView where Element: Equatable {
101100

102101
internal func elementsEqual(_ other: Self) -> Bool {

Sources/FoundationEssentials/JSON/BufferViewIndex.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal struct BufferViewIndex<Element> {
2424
}
2525
}
2626

27+
extension BufferViewIndex: @unchecked Sendable where Element: Sendable {}
28+
2729
extension BufferViewIndex: Equatable {}
2830

2931
extension BufferViewIndex: Hashable {}
@@ -48,6 +50,3 @@ extension BufferViewIndex: Comparable {
4850
lhs._rawValue < rhs._rawValue
4951
}
5052
}
51-
52-
@available(*, unavailable)
53-
extension BufferViewIndex: Sendable {}

0 commit comments

Comments
 (0)