Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#include "../src/core/hidapi_input_ring.h"
#include "hidapi_libusb_report_descriptor.h"

/* A custom thread model (HIDAPI_THREAD_MODEL_INCLUDE) must provide the same
interface as hidapi_thread_pthread.h. In particular, hidapi_thread_create()
returns int: 0 on success, non-zero if the thread could not be started. */
#ifndef HIDAPI_THREAD_MODEL_INCLUDE
#define HIDAPI_THREAD_MODEL_INCLUDE "hidapi_thread_pthread.h"
#endif
Expand Down Expand Up @@ -1465,7 +1468,12 @@ static int hidapi_initialize_device(hid_device *dev, const struct libusb_interfa
goto err_release_interface;
}

hidapi_thread_create(&dev->thread_state, read_thread, dev);
if (hidapi_thread_create(&dev->thread_state, read_thread, dev) != 0) {
/* Nothing would ever release the startup barrier below, and there is
no thread to join: fail the open and undo what was set up above. */
LOG("hidapi_initialize_device: couldn't start the read thread\n");
goto err_release_interface;
}

/* Wait here for the read thread to be initialized. */
hidapi_thread_barrier_wait(&dev->thread_state);
Expand Down
7 changes: 5 additions & 2 deletions libusb/hidapi_thread_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ static void hidapi_thread_barrier_wait(hidapi_thread_state *state)
pthread_barrier_wait(&state->barrier);
}

static void hidapi_thread_create(hidapi_thread_state *state, void *(*func)(void*), void *func_arg)
/* Starts `func` on a new thread. Returns 0 on success, or a non-zero value when
the thread could not be started (in which case no thread was started and
`state->thread` must not be joined). */
static int hidapi_thread_create(hidapi_thread_state *state, void *(*func)(void*), void *func_arg)
{
pthread_create(&state->thread, NULL, func, func_arg);
return pthread_create(&state->thread, NULL, func, func_arg);
}

static void hidapi_thread_join(hidapi_thread_state *state)
Expand Down
Loading