From 36a5cd505ac4f82afc4269e369aa657203a3893d Mon Sep 17 00:00:00 2001 From: Dmitry Kovba Date: Wed, 6 Aug 2025 18:29:07 -0700 Subject: [PATCH] Replace public computed properties with locks in setters (#242) This PR replaces public computed properties with setters in the `NATNetworkInterface` class with constants to avoid using locks in the setters. A new instance of `NATNetworkInterface` should be created to change these values. --- Makefile | 6 +- .../NATNetworkInterface.swift | 65 ++++--------------- 2 files changed, 15 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index 9f67f283..d154a611 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Version and build configuration variables +# Build configuration variables # The default version ID 0.0.0 indicates a local development build or PRB BUILD_CONFIGURATION ?= debug @@ -42,10 +42,10 @@ release: all .PHONY: containerization containerization: @echo Building containerization binaries... - @mkdir -p bin @$(SWIFT) build -c $(BUILD_CONFIGURATION) @echo Copying containerization binaries... + @mkdir -p bin @install "$(BUILD_BIN_DIR)/cctl" ./bin/ @install "$(BUILD_BIN_DIR)/containerization-integration" ./bin/ @@ -147,7 +147,7 @@ cleancontent: .PHONY: clean clean: - @echo Cleaning the build files... + @echo Cleaning build files... @rm -rf bin/ @rm -rf _site/ @rm -rf _serve/ diff --git a/Sources/Containerization/NATNetworkInterface.swift b/Sources/Containerization/NATNetworkInterface.swift index e648802e..825d3cc0 100644 --- a/Sources/Containerization/NATNetworkInterface.swift +++ b/Sources/Containerization/NATNetworkInterface.swift @@ -26,47 +26,12 @@ import Synchronization /// container/virtual machine. @available(macOS 26, *) public final class NATNetworkInterface: Interface, Sendable { - public var address: String { - get { - state.withLock { $0.address } - } - set { - state.withLock { $0.address = newValue } - } - - } - - public var gateway: String? { - get { - state.withLock { $0.gateway } - } - set { - state.withLock { $0.gateway = newValue } - } - } + public let address: String + public let gateway: String? + public let macAddress: String? @available(macOS 26, *) - public var reference: vmnet_network_ref { - state.withLock { $0.reference } - } - - public var macAddress: String? { - get { - state.withLock { $0.macAddress } - } - set { - state.withLock { $0.macAddress = newValue } - } - } - - private struct State { - var address: String - var gateway: String? - var reference: vmnet_network_ref! - var macAddress: String? - } - - private let state: Mutex + public nonisolated(unsafe) let reference: vmnet_network_ref! @available(macOS 26, *) public init( @@ -75,13 +40,10 @@ public final class NATNetworkInterface: Interface, Sendable { reference: sending vmnet_network_ref, macAddress: String? = nil ) { - let state = State( - address: address, - gateway: gateway, - reference: reference, - macAddress: macAddress - ) - self.state = Mutex(state) + self.address = address + self.gateway = gateway + self.macAddress = macAddress + self.reference = reference } @available(macOS, obsoleted: 26, message: "Use init(address:gateway:reference:macAddress:) instead") @@ -90,13 +52,10 @@ public final class NATNetworkInterface: Interface, Sendable { gateway: String?, macAddress: String? = nil ) { - let state = State( - address: address, - gateway: gateway, - reference: nil, - macAddress: macAddress - ) - self.state = Mutex(state) + self.address = address + self.gateway = gateway + self.macAddress = macAddress + self.reference = nil } }