Skip to content

Commit

Permalink
Merge pull request #127 from loffgren/no-tuple-api
Browse files Browse the repository at this point in the history
  • Loading branch information
glessard committed Jun 10, 2023
2 parents aabb8d0 + 7bcc01f commit 1cbb2cb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
23 changes: 2 additions & 21 deletions Sources/System/MachPort.swift
Expand Up @@ -7,7 +7,7 @@
See https://swift.org/LICENSE.txt for license information
*/

#if swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS))
#if false && swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS))

import Darwin.Mach

Expand All @@ -27,8 +27,7 @@ internal func _machPrecondition(
@available(/*System 1.3.0: macOS 9999, iOS 9999, watchOS 9999, tvOS 9999*/iOS 8, *)
@frozen
public enum Mach {
@_moveOnly
public struct Port<RightType: MachPortRight> {
public struct Port<RightType: MachPortRight>: ~Copyable {
@usableFromInline
internal var _name: mach_port_name_t

Expand Down Expand Up @@ -122,24 +121,6 @@ public enum Mach {
/// receiving end.
@frozen
public struct SendOnceRight: MachPortRight {}

/// Create a connected pair of rights, one receive, and one send.
///
/// This function will abort if the rights could not be created.
/// Callers may assert that valid rights are always returned.
public static func allocatePortRightPair() ->
(receive: Mach.Port<Mach.ReceiveRight>, send: Mach.Port<Mach.SendRight>) {
var name = mach_port_name_t(MACH_PORT_NULL)
let secret = mach_port_context_t(arc4random())
withUnsafeMutablePointer(to: &name) { name in
var options = mach_port_options_t()
options.flags = UInt32(MPO_INSERT_SEND_RIGHT);
withUnsafeMutablePointer(to: &options) { options in
_machPrecondition(mach_port_construct(mach_task_self_, options, secret, name))
}
}
return (Mach.Port(name: name, context: secret), Mach.Port(name: name))
}
}

extension Mach.Port where RightType == Mach.ReceiveRight {
Expand Down
21 changes: 7 additions & 14 deletions Tests/SystemTests/MachPortTests.swift
Expand Up @@ -7,11 +7,17 @@
See https://swift.org/LICENSE.txt for license information
*/

#if swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS))
#if false && swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS))

import XCTest
import Darwin.Mach

#if SYSTEM_PACKAGE
import SystemPackage
#else
import System
#endif

final class MachPortTests: XCTestCase {
func refCountForMachPortName(name:mach_port_name_t, kind:mach_port_right_t) -> mach_port_urefs_t {
var refCount:mach_port_urefs_t = 0
Expand Down Expand Up @@ -121,19 +127,6 @@ final class MachPortTests: XCTestCase {
}
}

func testMakePair() throws {
let (recv, send) = Mach.allocatePortRightPair()
XCTAssert(recv.makeSendCount == 1)
recv.withBorrowedName { rName in
send.withBorrowedName { sName in
XCTAssert(rName != 0xFFFFFFFF)
XCTAssert(rName != MACH_PORT_NULL)
// send and recv port names coalesce
XCTAssert(rName == sName)
}
}
}

func testCopySend() throws {
let recv = Mach.Port<Mach.ReceiveRight>()
let zero = recv.makeSendCount
Expand Down

0 comments on commit 1cbb2cb

Please sign in to comment.