Are rawhid_recv() and rawhid_send() thread safe or "thread usable"?

Status
Not open for further replies.
Hi,

My reading of the windows version of the the rawhid code (hid_WINDOWS.c) leaves me uncertain about whether rawhid_send() and rawhid_recv() are thread safe. I do see critical sections/mutexs, but enough global state that I wouldn't want to assume thread-safety.

So, question #1 is, Are these functions safe to call from multiple threads (where each thread manages IO to a specific device)?

Question #2 concerns whether there's any point if having multiple threads. Since there's a mutex surrounding blocking IO calls (in both send and recv functions), I would think that as soon as thread A grabs the mutex and then starts to wait for IO, all other threads will block if they call the same function--even if data were available immediately from their devices.

In other words, IO waiting in one thread for one device, will cause all threads to wait/block.

Do I have this correct?

Thanks,
Dan
 
Gentle bump...

I'd imagine that this is important to folks other than me; since non-blocking IO has bugs on Windows rawhid, reading from devices in separate threads is probably the only viable implementation for working with multiple devices.
 
There is a single critical section, so only a single thread can receive for a single device at a time. At the same time, it's not really good enough for thread safety - you can't safely re-enumerate devices. So I don't know why the critical section is there in the first place.

The code closes all file handles when it enumerates devices - probably not what you want (you can lose messages).

\\

Look at signal11/hidapi:
https://github.com/signal11/hidapi

You may want to look at the Issues and pull requests, it has its own share of bugs.

windows/hid.c + hidapi/hidapi.h is all you need, you don't have to use their build system.

Synchronization is up to you. If you only use each device from a single thread, nothing should be required for device access. Enumeration and device usage is decoupled (the path that you can get from the enumeration is unique for each HID interface).
 
Thanks a ton tni. Was having a bit of trouble understanding the purpose of the critical section as well.

Coincidentally, I decided to give hidapi a try in the interim, but hadn't thought of checking the issues. Again, thanks for the help.
 
Status
Not open for further replies.
Back
Top