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(_:_:_:) and add it to a run loop with CFRunLoopAddSource(_:_:_:).

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 object, which can send arbitrary data.

Mach ports only support communication on the local machine. For network communication, you have to use a CFSocket object.


Configuring a CFMachPort Object

func CFMachPortInvalidate(CFMachPort!)

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

func CFMachPortCreateRunLoopSource(CFAllocator!, CFMachPort!, CFIndex)

Creates a CFRunLoopSource object for a CFMachPort object.

func CFMachPortSetInvalidationCallBack(CFMachPort!, CFMachPortInvalidationCallBack!)

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

Examining a CFMachPort Object

func CFMachPortGetContext(CFMachPort!, UnsafeMutablePointer<CFMachPortContext>!)

Returns the context information for a CFMachPort object.

func CFMachPortGetInvalidationCallBack(CFMachPort!)

Returns the invalidation callback function for a CFMachPort object.

func CFMachPortGetPort(CFMachPort!)

Returns the native Mach port represented by a CFMachPort object.

func CFMachPortIsValid(CFMachPort!)

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

Getting the CFMachPort Type ID

func CFMachPortGetTypeID()

Returns the type identifier for the CFMachPort opaque type.



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


Callback invoked when a CFMachPort object is invalidated.

Data Types


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


A reference to a CFMachPort object.