Skip to content

Commit

Permalink
C11 Threads: add non-standard function to get the current thread name.
Browse files Browse the repository at this point in the history
DVS128/DAVIS: set better thread names for the data acquisition thread, as well as the libusb internal threads.
  • Loading branch information
llongi committed Aug 19, 2016
1 parent 1ce4943 commit 856740a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/c11threads_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,26 @@ static inline int thrd_set_name(const char *name) {
#endif
}

static inline int thrd_get_name(char *name, size_t maxNameLength) {
#if defined(OS_LINUX)
(void)(maxNameLength); // UNUSED FOR LINUX.

if (prctl(PR_GET_NAME, name) != 0) {
return (thrd_error);
}

return (thrd_success);
#elif defined(OS_MACOSX)
if (pthread_getname_np(pthread_self(), name, maxNameLength) != 0) {
return (thrd_error);
}

return (thrd_success);
#else
return (thrd_error);
#endif
}

// NON STANDARD!
static inline int thrd_set_priority(int priority) {
#if defined(OS_LINUX)
Expand Down
20 changes: 19 additions & 1 deletion src/davis_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,25 @@ bool davisCommonOpen(davisHandle handle, uint16_t VID, uint16_t PID, uint8_t DID

atomic_thread_fence(memory_order_release);

// Set device thread name. Maximum length of 15 chars due to Linux limitations.
snprintf(state->deviceThreadName, 15 + 1, "%s ID-%" PRIu16, deviceName, deviceID);
state->deviceThreadName[15] = '\0';

// Search for device and open it.
// Initialize libusb using a separate context for each device.
// This is to correctly support one thread per device.
if ((errno = libusb_init(&state->deviceContext)) != LIBUSB_SUCCESS) {
// libusb may create its own threads at this stage, so we temporarly set
// a different thread name.
char originalThreadName[15 + 1]; // +1 for terminating NUL character.
thrd_get_name(originalThreadName, 15);
originalThreadName[15] = '\0';

thrd_set_name(state->deviceThreadName);
errno = libusb_init(&state->deviceContext);

thrd_set_name(originalThreadName);

if (errno != LIBUSB_SUCCESS) {
caerLog(CAER_LOG_CRITICAL, __func__, "Failed to initialize libusb context. Error: %d.", errno);
return (false);
}
Expand Down Expand Up @@ -3829,6 +3844,9 @@ static int davisDataAcquisitionThread(void *inPtr) {

caerLog(CAER_LOG_DEBUG, handle->info.deviceString, "Initializing data acquisition thread ...");

// Set thread name.
thrd_set_name(state->deviceThreadName);

// Reset configuration update, so as to not re-do work afterwards.
atomic_store(&state->dataAcquisitionThreadConfigUpdate, 0);

Expand Down
1 change: 1 addition & 0 deletions src/davis_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct davis_state {
void (*dataShutdownNotify)(void *ptr);
void *dataShutdownUserPtr;
// USB Device State
char deviceThreadName[15 + 1]; // +1 for terminating NUL character.
libusb_context *deviceContext;
libusb_device_handle *deviceHandle;
// USB Transfer Settings
Expand Down
21 changes: 19 additions & 2 deletions src/dvs128.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,26 @@ caerDeviceHandle dvs128Open(uint16_t deviceID, uint8_t busNumberRestrict, uint8_

atomic_thread_fence(memory_order_release);

// Set device thread name. Maximum length of 15 chars due to Linux limitations.
snprintf(state->deviceThreadName, 15 + 1, "%s ID-%" PRIu16, DVS_DEVICE_NAME, deviceID);
state->deviceThreadName[15] = '\0';

// Search for device and open it.
// Initialize libusb using a separate context for each device.
// This is to correctly support one thread per device.
if ((errno = libusb_init(&state->deviceContext)) != LIBUSB_SUCCESS) {
free(handle);
// libusb may create its own threads at this stage, so we temporarly set
// a different thread name.
char originalThreadName[15 + 1]; // +1 for terminating NUL character.
thrd_get_name(originalThreadName, 15);
originalThreadName[15] = '\0';

thrd_set_name(state->deviceThreadName);
errno = libusb_init(&state->deviceContext);

thrd_set_name(originalThreadName);

if (errno != LIBUSB_SUCCESS) {
free(handle);
caerLog(CAER_LOG_CRITICAL, __func__, "Failed to initialize libusb context. Error: %d.", errno);
return (NULL);
}
Expand Down Expand Up @@ -1239,6 +1253,9 @@ static int dvs128DataAcquisitionThread(void *inPtr) {

caerLog(CAER_LOG_DEBUG, handle->info.deviceString, "Initializing data acquisition thread ...");

// Set thread name.
thrd_set_name(state->deviceThreadName);

// Reset configuration update, so as to not re-do work afterwards.
atomic_store(&state->dataAcquisitionThreadConfigUpdate, 0);

Expand Down
1 change: 1 addition & 0 deletions src/dvs128.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct dvs128_state {
void (*dataShutdownNotify)(void *ptr);
void *dataShutdownUserPtr;
// USB Device State
char deviceThreadName[15 + 1]; // +1 for terminating NUL character.
libusb_context *deviceContext;
libusb_device_handle *deviceHandle;
// USB Transfer Settings
Expand Down

0 comments on commit 856740a

Please sign in to comment.