<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFMachPort",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@T@CFMachPortRef"
  },
  "title" : "CFMachPort"
}
-->

# CFMachPort

```
class CFMachPort
```

## Overview

A CFMachPort object is a wrapper for a native Mach port (`mach_port_t`). Mach ports are the native communication channel for the macOS kernel.

CFMachPort does not provide a function to send messages, so you primarily use a CFMachPort object if you need to listen to a Mach port that you obtained by other means. You can get a callback when a message arrives on the port or when the port becomes invalid, such as when the native port dies.

To listen for messages you need to create a run loop source with [`CFMachPortCreateRunLoopSource(_:_:_:)`](/documentation/CoreFoundation/CFMachPortCreateRunLoopSource(_:_:_:)) and add it to a run loop with [`CFRunLoopAddSource(_:_:_:)`](/documentation/CoreFoundation/CFRunLoopAddSource(_:_:_:)).

> Important:
> If you want to tear down the connection, you must invalidate the port (using ``doc://com.apple.corefoundation/documentation/CoreFoundation/CFMachPortInvalidate(_:)``) before releasing the runloop source and the Mach port object.

To send data, you must use the Mach APIs with the native Mach port, which is not described here. Alternatively, you can use a [`CFMessagePort`](/documentation/CoreFoundation/CFMessagePort) object, which can send arbitrary data.

Mach ports only support communication on the local machine. For network communication, you have to use a [`CFSocket`](/documentation/CoreFoundation/CFSocket) object.

## Topics

### Creating a CFMachPort Object

[`CFMachPortCreate(_:_:_:_:)`](/documentation/CoreFoundation/CFMachPortCreate(_:_:_:_:))

Creates a CFMachPort object with a new Mach port.

[`CFMachPortCreateWithPort(_:_:_:_:_:)`](/documentation/CoreFoundation/CFMachPortCreateWithPort(_:_:_:_:_:))

Creates a CFMachPort object for a pre-existing native Mach port.

### Configuring a CFMachPort Object

[`CFMachPortInvalidate(_:)`](/documentation/CoreFoundation/CFMachPortInvalidate(_:))

Invalidates a CFMachPort object, stopping it from receiving any more messages.

[`CFMachPortCreateRunLoopSource(_:_:_:)`](/documentation/CoreFoundation/CFMachPortCreateRunLoopSource(_:_:_:))

Creates a CFRunLoopSource object for a CFMachPort object.

[`CFMachPortSetInvalidationCallBack(_:_:)`](/documentation/CoreFoundation/CFMachPortSetInvalidationCallBack(_:_:))

Sets the callback function invoked when a CFMachPort object is invalidated.

### Examining a CFMachPort Object

[`CFMachPortGetContext(_:_:)`](/documentation/CoreFoundation/CFMachPortGetContext(_:_:))

Returns the context information for a CFMachPort object.

[`CFMachPortGetInvalidationCallBack(_:)`](/documentation/CoreFoundation/CFMachPortGetInvalidationCallBack(_:))

Returns the invalidation callback function for a CFMachPort object.

[`CFMachPortGetPort(_:)`](/documentation/CoreFoundation/CFMachPortGetPort(_:))

Returns the native Mach port represented by a CFMachPort object.

[`CFMachPortIsValid(_:)`](/documentation/CoreFoundation/CFMachPortIsValid(_:))

Returns a Boolean value that indicates whether a CFMachPort object is valid and able to receive messages.

### Getting the CFMachPort Type ID

[`CFMachPortGetTypeID()`](/documentation/CoreFoundation/CFMachPortGetTypeID())

Returns the type identifier for the CFMachPort opaque type.

### Callbacks

[`CFMachPortCallBack`](/documentation/CoreFoundation/CFMachPortCallBack)

Callback invoked to process a message received on a CFMachPort object.

[`CFMachPortInvalidationCallBack`](/documentation/CoreFoundation/CFMachPortInvalidationCallBack)

Callback invoked when a CFMachPort object is invalidated.

### Data Types

[`CFMachPortContext`](/documentation/CoreFoundation/CFMachPortContext)

A structure that contains program-defined data and callbacks with which you can configure a CFMachPort object’s behavior.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)