-
Notifications
You must be signed in to change notification settings - Fork 396
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
hidapi/windows: thread local error messages #688
base: master
Are you sure you want to change the base?
Conversation
While this is an interesting idea to solve the current issue, there is unresolved issue with it: |
Oh you're right, I'm working on a fix. |
I've added a global list which contains the thread local allocations. When a thread wants to allocate memory for a thread local variable, it adds it to this list. The list is cleared as necessary. Accesses to the list are protected with a critical section for thread safety. The only scenarios this global list is accessed are :
One caveat :
|
I've implemented my suggestion for the caveat I mentioned. |
Thanks for the implementation! I totaly get your motivation for this change, but I have in mind a few doubts about this:
I was even thinkg about an alternative implementation for this - instead of having a gloal list of thread-local vairables - have a list of per-device error_strings linked to a current thread-id. That should be a bit more simpler. Another alternative is to introduce a |
While this is true, there is currently no way to have async read and write on a device without having to modify hidapi directly because of the current error message implementation. Right now you have to process them one by one or you risk a crash/undefined behavior. Having some thread safety guarantees is not a bad thing either. (the user still have to do their part, for example, the user must call hid_exit only when all threads are done using hidapi)
Understandable!
Shouldn't be too hard as long as we find an equivalent to DllMain. I could put the tls code in its own files to avoid code duplication.
I made an attempt here (not tested!) to see what it would look like. It's not much simplier imo (you have tls_get instead of tls_register mostly). And you have a potential lock contention each time error function are called (which is every times to set the error to null).
Yeah, it's unfortunate. At the end of the day, you're the one who has the say. If you want to take a cautious approach, you could also make a separate branch (like for the connection callback) to check the stability over time and for those who need this. |
I've implemented thread local error messages on Windows to fix the double free crash when you have a Read and a Write happening in 2 different threads on the same device or when there is a thread for Read/Write for each device.
Need testing with multiple devices per thread since I only have 1 device per thread in my setup.