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

Mac OS X backend not thread safe #95

Open
LudovicRousseau opened this issue Jan 7, 2013 · 3 comments
Open

Mac OS X backend not thread safe #95

LudovicRousseau opened this issue Jan 7, 2013 · 3 comments

Comments

@LudovicRousseau
Copy link
Contributor

The Mac OS X backend uses a static variable static IOHIDManagerRef hid_mgr = 0x0;

The problem occurs with two threads are doing the sequence hid_init() [...] hid_exit()

The first thread to call hid_exit() will release hid_mgr and then the second thread will have problems.

For example I have a crash in hid_enumerate() on line:
CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);
because hid_mgr has been reset to NULL.

My workaround is to NOT call hid_exit() so hid_mgr is not released.

A proper solution would be to add a counter of use and really release hid_mgr when no other thread is using HIDAPI anymore.

@LudovicRousseau
Copy link
Contributor Author

This issue may be related to issues #72 and #45

@signal11
Copy link
Owner

signal11 commented Jan 7, 2013

hid_init() and hid_exit() are designed to be called at program startup and shutdown, respectively, to initialize the library. Two threads shouldn't be doing sequences of hid_init() ... hid_exit(). They only need to call hid_open*()/hid_close().

It could be beneficial to add what you're saying, but this can easily be worked around right now by using hid_init() as intended (above).

@LudovicRousseau
Copy link
Contributor Author

In my case HIDAPI is used in a library in charge of the HID devices enumeration. This library is then used by other libraries (with no knowledge of HIDAPI). The final application can be a browser like Firefox so I cannot use hid_init() and hid_exit() at the application startup and shutdown.

A long term strategy would be to use contexts managed by hid_init() and hid_exit() and used by the other hidapi calls.

No need to fix the problem right now. I have a workaround that works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants