Updated with upstream suggestions in https://github.com/libusb/hidapi/pull/582
This commit is contained in:
parent
0ad089de21
commit
f082c68b2f
@ -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);
|
||||
|
@ -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
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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);
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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 {
|
||||
@ -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->tv_sec = ns / 1000000000L;
|
||||
ts->tv_nsec = ns % 1000000000L;
|
||||
*ts = SDL_GetTicksNS();
|
||||
}
|
||||
|
||||
static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
|
||||
{
|
||||
*ts += SDL_MS_TO_NS(milliseconds);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user