IONotificationPortGetRunLoopSource isn't as generic as you might think
Of IONotificationPortGetRunLoopSource, its header says only: "Returns a CFRunLoopSource to be used to listen for notifications." You might think from this generic description that the function would be useful for listening to any old notification. However, it calls CFMachPortCreateWithPort, passing an internal callback function which works well if the only notifications you care about are the ones you can create with high-level functions such as IOServiceAddMatchingNotification. If you want to field any other notification, you're out of luck, because that internal callback function will most likely ignore or misinterpret the 'type' field of your message header. So, rather than IONotificationPortGetRunLoopSource, call CFMachPortCreateWithPort and CFMachPortCreateRunLoopSource.
CFMachPortCreateWithPort performs a copy
For reasons I have not yet attempted to determine, CFMachPortCreateWithPort is backed by a cache whose key is the mach_port_t which the client provides. The function first searches this cache for previously created instances of CFMachPortRef with the specified mach_port_t. If one is present, the function passes the CFMachPortRef to CFRetain and returns it. A Create function thus returns a copy despite its name. If no matching CFMachPortRef is present in the cache, CFMachPortCreateWithPort creates one and adds it to the cache before returning it.
Subscribe to:
Comments (Atom)