Documentation
¶
Overview ¶
Command machportkevent demonstrates waiting on Mach ports with kqueue's EVFILT_MACHPORT filter.
It builds a small request/response service: a worker goroutine owns a port set, registers it with a kqueue, and blocks in kevent64 until a message arrives on any member port. Clients send Mach messages to individual ports in that set. One wait covers every port, so adding a port costs nothing at the wait site.
Run it:
go run ./examples/kernel/machportkevent go run ./examples/kernel/machportkevent -ports=8 -messages=4
Why a dedicated thread ¶
This wait cannot be handed to the Go runtime's network poller. The poller only ever registers EVFILT_READ, EVFILT_WRITE and EVFILT_USER, and a kqueue descriptor has no read(2) interface for it to use, so a blocking kevent64 would stall the P it runs on. runtime.LockOSThread gives the wait a thread of its own, which is what the netpoller would otherwise have avoided.
The trade is real: a blocked thread costs a thread, but it costs no CPU while idle, unlike polling. Whether that is the right trade depends on how often messages arrive relative to how long the process can afford to spin.