Skip to content

Fix some Swift 6 concurrency issues in SWBUtil #501

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions Sources/SWBUtil/AsyncLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public actor ActorLock {
public init() {
}

public func withLock<T: Sendable, E>(_ body: @Sendable () async throws(E) -> T) async throws(E) -> T {
public func withLock<T: Sendable, E>(_ body: sending () async throws(E) -> T) async throws(E) -> T {
while busy {
await withCheckedContinuation { cc in
queue.append(cc)
Expand Down Expand Up @@ -47,7 +47,7 @@ public final class AsyncLockedValue<Wrapped: Sendable> {
}

@discardableResult @inlinable
public func withLock<Result: Sendable, E>(_ block: @Sendable (inout Wrapped) async throws(E) -> Result) async throws(E) -> Result {
public func withLock<Result: Sendable, E>(_ block: sending (inout Wrapped) async throws(E) -> Result) async throws(E) -> Result {
return try await lock.withLock { () throws(E) -> Result in try await block(&value) }
}
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/SWBUtil/HeavyCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import os
#endif

import Synchronization

/// Base protocol used for global operations over generic type.
private protocol _HeavyCacheBase: AnyObject {
/// Clear the cache.
Expand Down Expand Up @@ -60,7 +62,7 @@ public func withHeavyCacheGlobalState<T>(isolated: Bool = true, _ body: () throw
/// A cache designed for holding few, relatively heavy-weight objects.
///
/// This cache is specifically designed for holding a limited number of objects (usually less than 100) which are expensive enough to merit particular attention in terms of being purgeable under memory pressure, evictable in-mass, or cached with more complex parameters like time-to-live (TTL).
public final class HeavyCache<Key: Hashable, Value>: _HeavyCacheBase, KeyValueStorage, @unchecked Sendable {
public final class HeavyCache<Key: Hashable & Sendable, Value>: _HeavyCacheBase, KeyValueStorage, @unchecked Sendable {
public typealias HeavyCacheClock = SuspendingClock

/// Controls the non-deterministic eviction policy of the cache. Note that this is distinct from deterministic _pruning_ (due to TTL or size limits).
Expand All @@ -86,7 +88,7 @@ public final class HeavyCache<Key: Hashable, Value>: _HeavyCacheBase, KeyValueSt
}

/// The lock to protect shared instance state.
private let stateLock = Lock()
private let stateLock = SWBMutex(())

/// The underlying cache.
private let _cache: any HeavyCacheImpl<Key, Entry>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBUtil/InterningArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public typealias FrozenStringArena = FrozenInterningArena<String>
private let numberOfInternCalls = Statistic("InterningArena.numberOfInternCalls", "Number of calls to 'InterningArena.intern'")
private let numberOfItemsInterned = Statistic("InterningArena.numberOfItemsInterned", "Number of items interned in InterningArenas")

public final class InterningArena<T: Hashable> {
public final class InterningArena<T: Hashable & Sendable> {
public struct Handle: Hashable, Sendable {
fileprivate let value: UInt32

Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBUtil/Lock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ extension LockedValue where Value: ~Copyable {
extension LockedValue: @unchecked Sendable where Value: ~Copyable {
}

extension LockedValue {
extension LockedValue where Value: Sendable {
/// Sets the value of the wrapped value to `newValue` and returns the original value.
public func exchange(_ newValue: Value) -> Value {
withLock {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SWBUtil/Statistics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public final class Statistic: @unchecked Sendable, _StatisticBackend {
}
}

public protocol _StatisticBackend {
public protocol _StatisticBackend: Sendable {
var name: String { get }
var value: Int { get }
func increment(_ n: Int)
Expand Down
Loading