Skip to content

Commit f082c68

Browse files
committed
Updated with upstream suggestions in libusb/hidapi#582
1 parent 0ad089d commit f082c68

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/hidapi/libusb/hid.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,14 +1672,9 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
16721672
else if (milliseconds > 0) {
16731673
/* Non-blocking, but called with timeout. */
16741674
int res;
1675-
struct timespec ts;
1675+
hidapi_timespec ts;
16761676
hidapi_thread_gettime(&ts);
1677-
ts.tv_sec += milliseconds / 1000;
1678-
ts.tv_nsec += (milliseconds % 1000) * 1000000;
1679-
if (ts.tv_nsec >= 1000000000L) {
1680-
ts.tv_sec++;
1681-
ts.tv_nsec -= 1000000000L;
1682-
}
1677+
hidapi_thread_addtime(&ts, milliseconds);
16831678

16841679
while (!dev->input_reports && !dev->shutdown_thread) {
16851680
res = hidapi_thread_cond_timedwait(&dev->thread_state, &ts);

src/hidapi/libusb/hidapi_thread_pthread.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
88
libusb/hidapi Team
99
10-
Copyright 2022, All Rights Reserved.
10+
Sam Lantinga
11+
12+
Copyright 2023, All Rights Reserved.
1113
1214
At the discretion of the user of this library,
1315
this software may be licensed under the terms of the
@@ -66,15 +68,13 @@ static int pthread_barrier_wait(pthread_barrier_t *barrier)
6668
{
6769
pthread_mutex_lock(&barrier->mutex);
6870
++(barrier->count);
69-
if(barrier->count >= barrier->trip_count)
70-
{
71+
if(barrier->count >= barrier->trip_count) {
7172
barrier->count = 0;
7273
pthread_cond_broadcast(&barrier->cond);
7374
pthread_mutex_unlock(&barrier->mutex);
7475
return 1;
7576
}
76-
else
77-
{
77+
else {
7878
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
7979
pthread_mutex_unlock(&barrier->mutex);
8080
return 0;
@@ -85,6 +85,8 @@ static int pthread_barrier_wait(pthread_barrier_t *barrier)
8585

8686
#define HIDAPI_THREAD_TIMED_OUT ETIMEDOUT
8787

88+
typedef struct timespec hidapi_timespec;
89+
8890
typedef struct
8991
{
9092
pthread_t thread;
@@ -126,7 +128,7 @@ static void hidapi_thread_cond_wait(hidapi_thread_state *state)
126128
pthread_cond_wait(&state->condition, &state->mutex);
127129
}
128130

129-
static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, struct timespec *ts)
131+
static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
130132
{
131133
return pthread_cond_timedwait(&state->condition, &state->mutex, ts);
132134
}
@@ -156,8 +158,17 @@ static void hidapi_thread_join(hidapi_thread_state *state)
156158
pthread_join(state->thread, NULL);
157159
}
158160

159-
static void hidapi_thread_gettime(struct timespec *ts)
161+
static void hidapi_thread_gettime(hidapi_timespec *ts)
160162
{
161163
clock_gettime(CLOCK_REALTIME, ts);
162164
}
163165

166+
static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
167+
{
168+
ts->tv_sec += milliseconds / 1000;
169+
ts->tv_nsec += (milliseconds % 1000) * 1000000;
170+
if (ts->tv_nsec >= 1000000000L) {
171+
ts->tv_sec++;
172+
ts->tv_nsec -= 1000000000L;
173+
}
174+
}

src/hidapi/libusb/hidapi_thread_sdl.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ static int SDL_WaitThreadBarrier(SDL_ThreadBarrier *barrier)
7676

7777
#define HIDAPI_THREAD_TIMED_OUT SDL_MUTEX_TIMEDOUT
7878

79+
typedef Uint64 hidapi_timespec;
80+
7981
typedef struct
8082
{
8183
SDL_Thread *thread;
@@ -123,16 +125,12 @@ static void hidapi_thread_cond_wait(hidapi_thread_state *state)
123125
SDL_WaitCondition(state->condition, state->mutex);
124126
}
125127

126-
static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, struct timespec *ts)
128+
static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
127129
{
128-
Uint64 end_time;
129130
Sint64 timeout_ns;
130131
Sint32 timeout_ms;
131132

132-
end_time = ts->tv_sec;
133-
end_time *= 1000000000L;
134-
end_time += ts->tv_nsec;
135-
timeout_ns = (Sint64)(end_time - SDL_GetTicksNS());
133+
timeout_ns = (Sint64)(*ts - SDL_GetTicksNS());
136134
if (timeout_ns <= 0) {
137135
timeout_ms = 0;
138136
} else {
@@ -189,10 +187,12 @@ static void hidapi_thread_join(hidapi_thread_state *state)
189187
SDL_WaitThread(state->thread, NULL);
190188
}
191189

192-
static void hidapi_thread_gettime(struct timespec *ts)
190+
static void hidapi_thread_gettime(hidapi_timespec *ts)
193191
{
194-
Uint64 ns = SDL_GetTicksNS();
192+
*ts = SDL_GetTicksNS();
193+
}
195194

196-
ts->tv_sec = ns / 1000000000L;
197-
ts->tv_nsec = ns % 1000000000L;
195+
static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
196+
{
197+
*ts += SDL_MS_TO_NS(milliseconds);
198198
}

0 commit comments

Comments
 (0)