Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS hid.c created a separate thread for runloop #321

Closed
mcuee opened this issue Aug 12, 2021 · 5 comments
Closed

macOS hid.c created a separate thread for runloop #321

mcuee opened this issue Aug 12, 2021 · 5 comments
Labels
macOS Related to macOS backend question Information is requested

Comments

@mcuee
Copy link
Member

mcuee commented Aug 12, 2021

Ref: cvuchener/hidpp#17 (comment)

cvuchener (author of hidpp) seems to say this may not be a necessary and he thinks there is a potentially better way to use mach ports.

I do not understand this well. Just wondering why the macOS codes create a seperate thread for runloop.

@mcuee mcuee added question Information is requested macOS Related to macOS backend labels Aug 12, 2021
@Youw
Copy link
Member

Youw commented Aug 12, 2021

HIDAPI needs a runloop to function. A runloop blocks the thread execution where it is invoked, and only processes "asyncronous" messages sent to objects that "live" in that runloop. Each macOS HID object has to live in a runloop as it is the only way to get HID interruptions (maybe some more, can't recall at the moment).

In theory - yes, HIDAPI could leave without a separate thread, but only if a calling code guarantees that in each thread where hid_init/hid_enumerate/hid_open (maybe not all of those, I'm not sure, but it doesn't really matter) is called - there is a runloop created and that it will be active for the whole hid_device lifetime.

This makes a limitation on the usage of the HIDAPI, and makes it really hard to use HIDAPI in simple UNIX-like console application, which knows nothing of the environment they run in (macOS)/runloops/etc.

@Youw
Copy link
Member

Youw commented Aug 12, 2021

What is done right now - a thread is created per-device.
In theory this can be optimised to create a thread per-context (we have only one context as per current implementation) for all devices.
But I think that is as much as hidapi could go to preserve the API and simple usage.

@noah-nuebling
Copy link

I’m currently working on the macOS port for HIDPP and my understanding is that, in the case of HIDPP all interaction with the device is blocking, and not asynchronous. That’s why it was unnecessary to create a separate thread to run the runLoop on.

As soon as you want any asynchronous interaction with the device like having a Device::registerCallback(callback) function that returns right away, you’d need a separate thread.

@Youw
Copy link
Member

Youw commented Aug 12, 2021

As soon as you want any asynchronous interaction with the device

and HIDAPI does that 1, 2

@mcuee
Copy link
Member Author

mcuee commented Aug 13, 2021

Close this as it is answered.

@mcuee mcuee closed this as completed Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macOS Related to macOS backend question Information is requested
Projects
None yet
Development

No branches or pull requests

3 participants