Skip to content

Commit

Permalink
Updated with upstream suggestions in libusb/hidapi#582
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jun 1, 2023
1 parent 0ad089d commit f082c68
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
9 changes: 2 additions & 7 deletions src/hidapi/libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1672,14 +1672,9 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
else if (milliseconds > 0) {
/* Non-blocking, but called with timeout. */
int res;
struct timespec ts;
hidapi_timespec ts;
hidapi_thread_gettime(&ts);
ts.tv_sec += milliseconds / 1000;
ts.tv_nsec += (milliseconds % 1000) * 1000000;
if (ts.tv_nsec >= 1000000000L) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000L;
}
hidapi_thread_addtime(&ts, milliseconds);

while (!dev->input_reports && !dev->shutdown_thread) {
res = hidapi_thread_cond_timedwait(&dev->thread_state, &ts);
Expand Down
25 changes: 18 additions & 7 deletions src/hidapi/libusb/hidapi_thread_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
libusb/hidapi Team
Copyright 2022, All Rights Reserved.
Sam Lantinga
Copyright 2023, All Rights Reserved.
At the discretion of the user of this library,
this software may be licensed under the terms of the
Expand Down Expand Up @@ -66,15 +68,13 @@ static int pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
++(barrier->count);
if(barrier->count >= barrier->trip_count)
{
if(barrier->count >= barrier->trip_count) {
barrier->count = 0;
pthread_cond_broadcast(&barrier->cond);
pthread_mutex_unlock(&barrier->mutex);
return 1;
}
else
{
else {
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
pthread_mutex_unlock(&barrier->mutex);
return 0;
Expand All @@ -85,6 +85,8 @@ static int pthread_barrier_wait(pthread_barrier_t *barrier)

#define HIDAPI_THREAD_TIMED_OUT ETIMEDOUT

typedef struct timespec hidapi_timespec;

typedef struct
{
pthread_t thread;
Expand Down Expand Up @@ -126,7 +128,7 @@ static void hidapi_thread_cond_wait(hidapi_thread_state *state)
pthread_cond_wait(&state->condition, &state->mutex);
}

static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, struct timespec *ts)
static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
{
return pthread_cond_timedwait(&state->condition, &state->mutex, ts);
}
Expand Down Expand Up @@ -156,8 +158,17 @@ static void hidapi_thread_join(hidapi_thread_state *state)
pthread_join(state->thread, NULL);
}

static void hidapi_thread_gettime(struct timespec *ts)
static void hidapi_thread_gettime(hidapi_timespec *ts)
{
clock_gettime(CLOCK_REALTIME, ts);
}

static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
{
ts->tv_sec += milliseconds / 1000;
ts->tv_nsec += (milliseconds % 1000) * 1000000;
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
ts->tv_nsec -= 1000000000L;
}
}
20 changes: 10 additions & 10 deletions src/hidapi/libusb/hidapi_thread_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ static int SDL_WaitThreadBarrier(SDL_ThreadBarrier *barrier)

#define HIDAPI_THREAD_TIMED_OUT SDL_MUTEX_TIMEDOUT

typedef Uint64 hidapi_timespec;

typedef struct
{
SDL_Thread *thread;
Expand Down Expand Up @@ -123,16 +125,12 @@ static void hidapi_thread_cond_wait(hidapi_thread_state *state)
SDL_WaitCondition(state->condition, state->mutex);
}

static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, struct timespec *ts)
static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
{
Uint64 end_time;
Sint64 timeout_ns;
Sint32 timeout_ms;

end_time = ts->tv_sec;
end_time *= 1000000000L;
end_time += ts->tv_nsec;
timeout_ns = (Sint64)(end_time - SDL_GetTicksNS());
timeout_ns = (Sint64)(*ts - SDL_GetTicksNS());
if (timeout_ns <= 0) {
timeout_ms = 0;
} else {
Expand Down Expand Up @@ -189,10 +187,12 @@ static void hidapi_thread_join(hidapi_thread_state *state)
SDL_WaitThread(state->thread, NULL);
}

static void hidapi_thread_gettime(struct timespec *ts)
static void hidapi_thread_gettime(hidapi_timespec *ts)
{
Uint64 ns = SDL_GetTicksNS();
*ts = SDL_GetTicksNS();
}

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

0 comments on commit f082c68

Please sign in to comment.