Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,16 +1215,16 @@ static void* callback_thread(void* user_data)
{
(void) user_data;

/* 5 msec timeout seems reasonable; don't set too low to avoid high CPU usage */
/* This timeout only affects how much time it takes to stop the thread */
hidapi_timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 5000000;

hidapi_thread_mutex_lock(&hid_hotplug_context.callback_thread);

/* We stop the thread if by the moment there are no events left in the queue there are no callbacks left */
while (1) {
/* 5 msec timeout seems reasonable; don't set too low to avoid high CPU usage */
/* This timeout only affects how much time it takes to stop the thread */
hidapi_timespec ts;
hidapi_thread_gettime(&ts);
hidapi_thread_addtime(&ts, 5);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd set it to 1 second - I've been using 1 second in multiple projects, and seems reasonable
5 second to exit the thread (effectively - to quit the application in some cases) seem a bit too much

Copy link
Copy Markdown
Contributor Author

@k1-801 k1-801 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the call signature, the number is in milliseconds, not whole seconds.
image

When it comes to milliseconds, it doesn't really matter what number is set there, as execution at single milliseconds scale is likely limited by OS scheduler, not the actual number set here, so long as it's long enough to actually go idle.

A whole second here would be too long (at least to my taste), and checking every single millisecond seems a bit excessive. It can be changed later on to 20 or 40 or even 100 milliseconds if needed (i.e. if this thread makes any noticeable CPU load).

Copy link
Copy Markdown
Contributor Author

@k1-801 k1-801 Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, the patch seems to work:

image

("wrapped HID devices" in this context means HID devices that use libusb backend specifically)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While modern processors are fast-enough practically not to notice wait/idle/check every 5ms, my experience tells it is a bit too often, I'd make it 100ms then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increased the timeout to 100


/* Make the tread fall asleep and wait for a condition to wake it up */
hidapi_thread_cond_timedwait(&hid_hotplug_context.callback_thread, &ts);

Expand Down
Loading