From 856740a94ea585734beccf84086034d6418a1803 Mon Sep 17 00:00:00 2001 From: Luca Longinotti Date: Fri, 19 Aug 2016 11:39:46 +0200 Subject: [PATCH] C11 Threads: add non-standard function to get the current thread name. DVS128/DAVIS: set better thread names for the data acquisition thread, as well as the libusb internal threads. --- src/c11threads_posix.h | 20 ++++++++++++++++++++ src/davis_common.c | 20 +++++++++++++++++++- src/davis_common.h | 1 + src/dvs128.c | 21 +++++++++++++++++++-- src/dvs128.h | 1 + 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/c11threads_posix.h b/src/c11threads_posix.h index 99f24b61..6efb2ca2 100644 --- a/src/c11threads_posix.h +++ b/src/c11threads_posix.h @@ -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) diff --git a/src/davis_common.c b/src/davis_common.c index 71638296..54f5dcb7 100644 --- a/src/davis_common.c +++ b/src/davis_common.c @@ -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); } @@ -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); diff --git a/src/davis_common.h b/src/davis_common.h index be9d673a..cf07615b 100644 --- a/src/davis_common.h +++ b/src/davis_common.h @@ -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 diff --git a/src/dvs128.c b/src/dvs128.c index 7967fcb3..e659677a 100644 --- a/src/dvs128.c +++ b/src/dvs128.c @@ -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); } @@ -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); diff --git a/src/dvs128.h b/src/dvs128.h index 4c69ac34..ef645923 100644 --- a/src/dvs128.h +++ b/src/dvs128.h @@ -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