mirror of
https://github.com/apple/container.git
synced 2026-09-21 15:15:36 +00:00
Transition to actors (#225)
Due to the reduced use of the macro, we can now fully transition to `Mutex`.
This commit is contained in:
@@ -44,7 +44,7 @@ package final class IndexedAddressAllocator<AddressType: CustomStringConvertible
|
||||
}
|
||||
}
|
||||
|
||||
private let stateGuard: Mutex<State>
|
||||
private let state: Mutex<State>
|
||||
|
||||
/// Create an allocator with specified size and index mappings.
|
||||
package init(
|
||||
@@ -57,11 +57,11 @@ package final class IndexedAddressAllocator<AddressType: CustomStringConvertible
|
||||
addressToIndex: addressToIndex,
|
||||
indexToAddress: indexToAddress
|
||||
)
|
||||
self.stateGuard = Mutex(state)
|
||||
self.state = Mutex(state)
|
||||
}
|
||||
|
||||
public func allocate() throws -> AddressType {
|
||||
try self.stateGuard.withLock { state in
|
||||
try self.state.withLock { state in
|
||||
guard state.enabled else {
|
||||
throw AllocatorError.allocatorDisabled
|
||||
}
|
||||
@@ -81,7 +81,7 @@ package final class IndexedAddressAllocator<AddressType: CustomStringConvertible
|
||||
}
|
||||
|
||||
package func reserve(_ address: AddressType) throws {
|
||||
try self.stateGuard.withLock { state in
|
||||
try self.state.withLock { state in
|
||||
guard state.enabled else {
|
||||
throw AllocatorError.allocatorDisabled
|
||||
}
|
||||
@@ -101,7 +101,7 @@ package final class IndexedAddressAllocator<AddressType: CustomStringConvertible
|
||||
}
|
||||
|
||||
package func release(_ address: AddressType) throws {
|
||||
try self.stateGuard.withLock { state in
|
||||
try self.state.withLock { state in
|
||||
guard let index = state.addressToIndex(address) else {
|
||||
throw AllocatorError.invalidAddress(address.description)
|
||||
}
|
||||
@@ -116,7 +116,7 @@ package final class IndexedAddressAllocator<AddressType: CustomStringConvertible
|
||||
}
|
||||
|
||||
package func disableAllocator() -> Bool {
|
||||
self.stateGuard.withLock { state in
|
||||
self.state.withLock { state in
|
||||
guard state.allocationCount == 0 else {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ package final class RotatingAddressAllocator: AddressAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
private let stateGuard: Mutex<State>
|
||||
private let state: Mutex<State>
|
||||
|
||||
/// Create an allocator with specified size and index mappings.
|
||||
package init(
|
||||
@@ -52,11 +52,11 @@ package final class RotatingAddressAllocator: AddressAllocator {
|
||||
addressToIndex: addressToIndex,
|
||||
indexToAddress: indexToAddress
|
||||
)
|
||||
self.stateGuard = Mutex(state)
|
||||
self.state = Mutex(state)
|
||||
}
|
||||
|
||||
public func allocate() throws -> AddressType {
|
||||
try self.stateGuard.withLock { state in
|
||||
try self.state.withLock { state in
|
||||
guard state.enabled else {
|
||||
throw AllocatorError.allocatorDisabled
|
||||
}
|
||||
@@ -77,7 +77,7 @@ package final class RotatingAddressAllocator: AddressAllocator {
|
||||
}
|
||||
|
||||
package func reserve(_ address: AddressType) throws {
|
||||
try self.stateGuard.withLock { state in
|
||||
try self.state.withLock { state in
|
||||
guard state.enabled else {
|
||||
throw AllocatorError.allocatorDisabled
|
||||
}
|
||||
@@ -97,7 +97,7 @@ package final class RotatingAddressAllocator: AddressAllocator {
|
||||
}
|
||||
|
||||
package func release(_ address: AddressType) throws {
|
||||
try self.stateGuard.withLock { state in
|
||||
try self.state.withLock { state in
|
||||
guard let index = (state.addressToIndex(address)) else {
|
||||
throw AllocatorError.invalidAddress(address.description)
|
||||
}
|
||||
@@ -113,7 +113,7 @@ package final class RotatingAddressAllocator: AddressAllocator {
|
||||
}
|
||||
|
||||
package func disableAllocator() -> Bool {
|
||||
self.stateGuard.withLock { state in
|
||||
self.state.withLock { state in
|
||||
guard state.allocationCount == 0 else {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// A declaration of the `@SendableProperty` macro.
|
||||
@attached(peer, names: arbitrary)
|
||||
@attached(accessor)
|
||||
public macro SendableProperty() = #externalMacro(module: "SendablePropertyMacros", type: "SendablePropertyMacro")
|
||||
@@ -1,20 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// A declaration of the `@SendablePropertyUnchecked` macro.
|
||||
@attached(peer, names: arbitrary)
|
||||
@attached(accessor)
|
||||
public macro SendablePropertyUnchecked() = #externalMacro(module: "SendablePropertyMacros", type: "SendablePropertyMacroUnchecked")
|
||||
@@ -1,41 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// `Synchronization` will be automatically imported with `SendableProperty`.
|
||||
@_exported import Synchronization
|
||||
|
||||
/// A synchronization primitive that protects shared mutable state via mutual exclusion.
|
||||
public final class Synchronized<T>: Sendable {
|
||||
private let lock: Mutex<State>
|
||||
|
||||
private struct State: @unchecked Sendable {
|
||||
var value: T
|
||||
}
|
||||
|
||||
/// Creates a new instance.
|
||||
/// - Parameter value: The initial value.
|
||||
public init(_ value: T) {
|
||||
self.lock = Mutex(State(value: value))
|
||||
}
|
||||
|
||||
/// Calls the given closure after acquiring the lock and returns its value.
|
||||
/// - Parameter body: The body of code to execute while the lock is held.
|
||||
public func withLock<R>(_ body: (inout T) throws -> R) rethrows -> R {
|
||||
try lock.withLock { state in
|
||||
try body(&state.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Errors that can be thrown by `@SendableProperty`.
|
||||
enum SendablePropertyError: CustomStringConvertible, Error {
|
||||
case unexpectedError
|
||||
case onlyApplicableToVar
|
||||
case notApplicableToType
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .unexpectedError: return "The macro encountered an unexpected error"
|
||||
case .onlyApplicableToVar: return "The macro can only be applied to a variable"
|
||||
case .notApplicableToType: return "The macro can't be applied to a variable of this type"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Foundation
|
||||
import SwiftCompilerPlugin
|
||||
import SwiftParser
|
||||
import SwiftSyntax
|
||||
import SwiftSyntaxBuilder
|
||||
import SwiftSyntaxMacros
|
||||
|
||||
/// A macro that allows to make a property of a supported type thread-safe keeping the `Sendable` conformance of the type.
|
||||
public struct SendablePropertyMacro: PeerMacro {
|
||||
private static let allowedTypes: Set<String> = [
|
||||
"Int", "UInt", "Int16", "UInt16", "Int32", "UInt32", "Int64", "UInt64", "Float", "Double", "Bool", "UnsafeRawPointer", "UnsafeMutableRawPointer", "UnsafePointer",
|
||||
"UnsafeMutablePointer",
|
||||
]
|
||||
|
||||
private static func checkPropertyType(in declaration: some DeclSyntaxProtocol) throws {
|
||||
guard let varDecl = declaration.as(VariableDeclSyntax.self),
|
||||
let binding = varDecl.bindings.first,
|
||||
let typeAnnotation = binding.typeAnnotation,
|
||||
let id = typeAnnotation.type.as(IdentifierTypeSyntax.self)
|
||||
else {
|
||||
// Nothing to check.
|
||||
return
|
||||
}
|
||||
|
||||
var typeName = id.name.text
|
||||
// Allow optionals of the allowed types.
|
||||
if typeName.prefix(9) == "Optional<" && typeName.suffix(1) == ">" {
|
||||
typeName = String(typeName.dropFirst(9).dropLast(1))
|
||||
}
|
||||
// Allow generics of the allowed types.
|
||||
if typeName.contains("<") {
|
||||
typeName = String(typeName.prefix { $0 != "<" })
|
||||
}
|
||||
|
||||
guard allowedTypes.contains(typeName) else {
|
||||
throw SendablePropertyError.notApplicableToType
|
||||
}
|
||||
}
|
||||
|
||||
/// The macro expansion that introduces a `Sendable`-conforming "peer" declaration for a thread-safe storage for the value of the given declaration of a variable.
|
||||
/// - Parameters:
|
||||
/// - node: The given attribute node.
|
||||
/// - declaration: The given declaration.
|
||||
/// - context: The macro expansion context.
|
||||
public static func expansion(
|
||||
of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext
|
||||
) throws -> [SwiftSyntax.DeclSyntax] {
|
||||
try checkPropertyType(in: declaration)
|
||||
return try SendablePropertyMacroUnchecked.expansion(of: node, providingPeersOf: declaration, in: context)
|
||||
}
|
||||
}
|
||||
|
||||
extension SendablePropertyMacro: AccessorMacro {
|
||||
/// The macro expansion that adds `Sendable`-conforming accessors to the given declaration of a variable.
|
||||
/// - Parameters:
|
||||
/// - node: The given attribute node.
|
||||
/// - declaration: The given declaration.
|
||||
/// - context: The macro expansion context.
|
||||
public static func expansion(
|
||||
of node: SwiftSyntax.AttributeSyntax, providingAccessorsOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext
|
||||
) throws -> [SwiftSyntax.AccessorDeclSyntax] {
|
||||
try checkPropertyType(in: declaration)
|
||||
return try SendablePropertyMacroUnchecked.expansion(of: node, providingAccessorsOf: declaration, in: context)
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Foundation
|
||||
import SwiftCompilerPlugin
|
||||
import SwiftParser
|
||||
import SwiftSyntax
|
||||
import SwiftSyntaxBuilder
|
||||
import SwiftSyntaxMacros
|
||||
|
||||
/// A macro that allows to make a property of a custom type thread-safe keeping the `Sendable` conformance of the type. This macro can be used with classes and enums. Avoid using it with structs, arrays, and dictionaries.
|
||||
public struct SendablePropertyMacroUnchecked: PeerMacro {
|
||||
private static func peerPropertyName(for propertyName: String) -> String {
|
||||
"_" + propertyName
|
||||
}
|
||||
|
||||
/// The macro expansion that introduces a `Sendable`-conforming "peer" declaration for a thread-safe storage for the value of the given declaration of a variable.
|
||||
/// - Parameters:
|
||||
/// - node: The given attribute node.
|
||||
/// - declaration: The given declaration.
|
||||
/// - context: The macro expansion context.
|
||||
public static func expansion(
|
||||
of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext
|
||||
) throws -> [SwiftSyntax.DeclSyntax] {
|
||||
guard let varDecl = declaration.as(VariableDeclSyntax.self),
|
||||
let binding = varDecl.bindings.first,
|
||||
let pattern = binding.pattern.as(IdentifierPatternSyntax.self)
|
||||
else {
|
||||
throw SendablePropertyError.onlyApplicableToVar
|
||||
}
|
||||
|
||||
let propertyName = pattern.identifier.text
|
||||
let hasInitializer = binding.initializer != nil
|
||||
let initializerValue = binding.initializer?.value.description ?? "nil"
|
||||
|
||||
var genericTypeAnnotation = ""
|
||||
if let typeAnnotation = binding.typeAnnotation {
|
||||
let typeName = typeAnnotation.type.description.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
||||
genericTypeAnnotation = "<\(typeName)\(hasInitializer ? "" : "?")>"
|
||||
}
|
||||
|
||||
let accessLevel = varDecl.modifiers.first(where: { ["open", "public", "internal", "fileprivate", "private"].contains($0.name.text) })?.name.text ?? "internal"
|
||||
|
||||
// Create a peer property
|
||||
let peerPropertyName = self.peerPropertyName(for: propertyName)
|
||||
let peerProperty: DeclSyntax =
|
||||
"""
|
||||
\(raw: accessLevel) let \(raw: peerPropertyName) = Synchronized\(raw: genericTypeAnnotation)(\(raw: initializerValue))
|
||||
"""
|
||||
return [peerProperty]
|
||||
}
|
||||
}
|
||||
|
||||
extension SendablePropertyMacroUnchecked: AccessorMacro {
|
||||
/// The macro expansion that adds `Sendable`-conforming accessors to the given declaration of a variable.
|
||||
/// - Parameters:
|
||||
/// - node: The given attribute node.
|
||||
/// - declaration: The given declaration.
|
||||
/// - context: The macro expansion context.
|
||||
public static func expansion(
|
||||
of node: SwiftSyntax.AttributeSyntax, providingAccessorsOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext
|
||||
) throws -> [SwiftSyntax.AccessorDeclSyntax] {
|
||||
guard let varDecl = declaration.as(VariableDeclSyntax.self),
|
||||
let binding = varDecl.bindings.first,
|
||||
let pattern = binding.pattern.as(IdentifierPatternSyntax.self)
|
||||
else {
|
||||
throw SendablePropertyError.onlyApplicableToVar
|
||||
}
|
||||
|
||||
let propertyName = pattern.identifier.text
|
||||
let hasInitializer = binding.initializer != nil
|
||||
|
||||
// Replace the property with an accessor
|
||||
let peerPropertyName = Self.peerPropertyName(for: propertyName)
|
||||
|
||||
let accessorGetter: AccessorDeclSyntax =
|
||||
"""
|
||||
get {
|
||||
\(raw: peerPropertyName).withLock { $0\(raw: hasInitializer ? "" : "!") }
|
||||
}
|
||||
"""
|
||||
let accessorSetter: AccessorDeclSyntax =
|
||||
"""
|
||||
set {
|
||||
\(raw: peerPropertyName).withLock { $0 = newValue }
|
||||
}
|
||||
"""
|
||||
|
||||
return [accessorGetter, accessorSetter]
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Copyright © 2025 Apple Inc. and the Containerization project authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import SwiftCompilerPlugin
|
||||
import SwiftSyntaxMacros
|
||||
|
||||
/// A plugin that registers the `SendablePropertyMacroUnchecked` and `SendablePropertyMacro`.
|
||||
@main
|
||||
struct SendablePropertyPlugin: CompilerPlugin {
|
||||
let providingMacros: [Macro.Type] = [
|
||||
SendablePropertyMacroUnchecked.self,
|
||||
SendablePropertyMacro.self,
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user