Skip to content

Commit e288ba1

Browse files
authored
Fix high CPU usage in callback thread due to incorrect timespec
1 parent 12a30f1 commit e288ba1

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

libusb/hid.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,16 +1215,16 @@ static void* callback_thread(void* user_data)
12151215
{
12161216
(void) user_data;
12171217

1218-
/* 5 msec timeout seems reasonable; don't set too low to avoid high CPU usage */
1219-
/* This timeout only affects how much time it takes to stop the thread */
1220-
hidapi_timespec ts;
1221-
ts.tv_sec = 0;
1222-
ts.tv_nsec = 5000000;
1223-
12241218
hidapi_thread_mutex_lock(&hid_hotplug_context.callback_thread);
12251219

12261220
/* We stop the thread if by the moment there are no events left in the queue there are no callbacks left */
12271221
while (1) {
1222+
/* 5 msec timeout seems reasonable; don't set too low to avoid high CPU usage */
1223+
/* This timeout only affects how much time it takes to stop the thread */
1224+
hidapi_timespec ts;
1225+
hidapi_thread_gettime(&ts);
1226+
hidapi_thread_addtime(&ts, 5);
1227+
12281228
/* Make the tread fall asleep and wait for a condition to wake it up */
12291229
hidapi_thread_cond_timedwait(&hid_hotplug_context.callback_thread, &ts);
12301230

0 commit comments

Comments
 (0)