-
Notifications
You must be signed in to change notification settings - Fork 460
A bit of documentation details on callback API #784
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
Open
Youw
wants to merge
1
commit into
connection-callback
Choose a base branch
from
connection-doc
base: connection-callback
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,6 +303,11 @@ extern "C" { | |
| This callback may be called by an internal event thread and as such it is | ||
| recommended the callback do minimal processing before returning. | ||
|
|
||
| It is NOT safe to call hid_open/hid_open_path or any other global/non-device | ||
| functions from within this callback. If you need to open | ||
| or close a device in response to a hotplug event, queue the | ||
| device path and handle it on your own application thread. | ||
|
|
||
| hidapi will call this function later, when a matching event had happened on | ||
| a matching device. | ||
|
|
||
|
|
@@ -315,6 +320,8 @@ extern "C" { | |
|
|
||
| @param callback_handle The hid_hotplug_callback_handle callback handle. | ||
| @param device The hid_device_info of device this event occurred on event that occurred. | ||
| The device pointer is only valid for the duration of the callback. | ||
| If you need the information later, copy the relevant fields before returning. | ||
| @param event Event that occurred. | ||
| @param user_data User data provided when this callback was registered. | ||
| (Optionally NULL). | ||
|
|
@@ -358,6 +365,9 @@ extern "C" { | |
|
|
||
| This function is safe to call from within a hotplug callback. | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently, not really, here is one unresolved issue: #674 (comment) |
||
|
|
||
| It is safe to call this function on an already deregistered | ||
| callback handle - the call will be a no-op returning 0. | ||
|
|
||
| @ingroup API | ||
|
|
||
| @param callback_handle The handle of the callback to deregister. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally don't see any reason why opening or closing a device would be unsafe in a callback. There aren't any intersections between the hotplug subsystem and the code responsible for opening the devices, so there should be no race conditions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What can I say...
For starters, check: https://github.com/libusb/hidapi/wiki/Multi%E2%80%90threading-Notes
HIDAPI is not, and never been a thread-safe API. New
register/deregisterfunctions are not thread-safe as well.It may be safe, only in case of all calls to
hid_open_path(and others), including the ones called from within a callback function - are all correctly serialized. But for that:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, so long as no other thread opens any device, there is nothing wrong with opening a device from a callback, right?
If so, this limitation is not any different from restricting using this call from any other 2 threads, and should be then worded accordingly :)
As in "It is NOT safe to call hid_open/hid_open_path from within a callback IF any other thread may be calling those functions at the same time (concurrently). It may be safe to do so if it is known that no other thread will be calling hid_open/hid_open_path, otherwise opening the device should be queued, and the device path should be copied by the application to be used and opened later in the queue."
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are well-known approaches to how the wording for multithreading safety should look like.
I'll get back to this later, one I re-check that it is really safe, and in which cases.