From aa859586a95c1548b406ee9f501af8c9df60e6a6 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Thu, 10 Sep 2020 22:52:58 +0300 Subject: [PATCH] Implement TLS for target_lo --- target_lo/Src/lo_os.c | 46 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/target_lo/Src/lo_os.c b/target_lo/Src/lo_os.c index 240139f03..14d79f0ac 100644 --- a/target_lo/Src/lo_os.c +++ b/target_lo/Src/lo_os.c @@ -195,4 +195,48 @@ BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) { xSemaphore->give_counter++; return pdTRUE; -} \ No newline at end of file +} + +#define TLS_ITEM_COUNT 1 +static pthread_key_t tls_keys[TLS_ITEM_COUNT]; +static pthread_once_t tls_keys_once = PTHREAD_ONCE_INIT; + +static void create_tls_keys() { + for (size_t i = 0; i < TLS_ITEM_COUNT; i++) { + pthread_key_create(&tls_keys[i], NULL); + } +} + +void* pvTaskGetThreadLocalStoragePointer( + TaskHandle_t xTaskToQuery, BaseType_t xIndex +) { + // Non-current task TLS access is not allowed + if (xTaskToQuery != NULL) { + return NULL; + } + + if (xIndex >= TLS_ITEM_COUNT) { + return NULL; + } + + pthread_once(&tls_keys_once, create_tls_keys); + + return pthread_getspecific(tls_keys[xIndex]); +} + +void vTaskSetThreadLocalStoragePointer( + TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue +) { + // Non-current task TLS access is not allowed + if (xTaskToSet != NULL) { + return; + } + + if (xIndex >= TLS_ITEM_COUNT) { + return; + } + + pthread_once(&tls_keys_once, create_tls_keys); + + pthread_setspecific(tls_keys[xIndex], pvValue); +}