casaroli commented on code in PR #20723: URL: https://github.com/apache/nuttx/pull/20723#discussion_r4577568275
########## libs/libc/tls/tls_emutls.c: ########## @@ -0,0 +1,279 @@ +/**************************************************************************** + * libs/libc/tls/tls_emutls.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <nuttx/atomic.h> +#include <nuttx/mutex.h> +#include <nuttx/tls.h> + +#include "libc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* loc.offset of a control object is as wide as a pointer */ + +#if UINTPTR_MAX > UINT32_MAX +# define emutls_load(p) atomic64_read_acquire(p) +# define emutls_store(p, v) atomic64_set_release(p, v) +#else +# define emutls_load(p) atomic_read_acquire(p) +# define emutls_store(p, v) atomic_set_release(p, v) +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control object that GCC emits for a __thread variable when the + * target has no native TLS, with the types of libgcc's emutls.c. + */ + +struct __emutls_object +{ + emutls_word_t size; + emutls_word_t align; + union + { + emutls_pointer_t offset; + FAR void *ptr; + } loc; + FAR void *templ; +}; + +/* The copies of the variables that one thread has used. The TLS info of + * the thread points to it. + */ + +struct emutls_array_s +{ + uintptr_t size; + FAR void *data[1]; +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* The index of a variable is global: in a flat build all task groups share + * this data, but each variable has its own index. + */ + +static mutex_t g_emutls_lock = NXMUTEX_INITIALIZER; +static uintptr_t g_emutls_count; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: emutls_alloc + * + * Description: + * Allocate the copy of a variable for the calling thread, initialized + * from the template of the variable, or zeroed if it has none. + * + ****************************************************************************/ + +static FAR void *emutls_alloc(FAR struct __emutls_object *obj) +{ + size_t align = obj->align > sizeof(FAR void *) ? + obj->align : sizeof(FAR void *); + FAR void *ptr; + + ptr = lib_memalign(align, obj->size); + if (ptr == NULL) + { + abort(); + } + + if (obj->templ != NULL) + { + memcpy(ptr, obj->templ, obj->size); + } + else + { + memset(ptr, 0, obj->size); + } + + return ptr; +} + +/**************************************************************************** + * Name: emutls_getslot + * + * Description: + * Return the entry for index in the array of the calling thread. + * + ****************************************************************************/ + +static FAR void **emutls_getslot(FAR struct tls_info_s *tls, + uintptr_t index) +{ + FAR struct emutls_array_s *array = tls->tl_emutls; + FAR struct emutls_array_s *newarray; + uintptr_t oldsize = array != NULL ? array->size : 0; + uintptr_t newsize; + + if (index < oldsize) + { + return &array->data[index]; + } + + newsize = index + 16; + newarray = lib_realloc(array, sizeof(struct emutls_array_s) + + (newsize - 1) * sizeof(FAR void *)); + if (newarray == NULL) + { + abort(); + } + + memset(&newarray->data[oldsize], 0, + (newsize - oldsize) * sizeof(FAR void *)); + newarray->size = newsize; + tls->tl_emutls = newarray; + g_tls_emutls_destruct = tls_emutls_destruct; + return &newarray->data[index]; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: __wrap___emutls_get_address + * + * Description: + * Return the address of the calling thread's copy of a __thread + * variable. libgcc's __emutls_get_address() keeps a single copy when + * libgcc is built without thread support; --wrap sends the calls here. + * + ****************************************************************************/ + +FAR void *__wrap___emutls_get_address(FAR void *arg) +{ + FAR struct __emutls_object *obj = arg; + FAR void **slot; + uintptr_t index; + + /* loc.offset is the index plus one, or 0 before the first use */ + + index = emutls_load(&obj->loc.offset); Review Comment: Done. ########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) + ---help--- + GCC and clang call __emutls_get_address() for __thread variables + when the target has no native TLS, or with -femulated-tls. libc + then gives one copy of each variable per thread, and the link + sends the calls to it with --wrap. Not for AVR: 8-bit AVR has + no atomic pointer loads, and AVR32 was not tested. The sim has Review Comment: Done. ########## libs/libc/tls/Kconfig: ########## @@ -5,6 +5,23 @@ menu "Thread Local Storage (TLS)" +config TLS_EMUTLS + bool "Per-thread copies of emulated TLS variables" + default y + depends on (ARCH_TOOLCHAIN_GNU && !ARCH_TOOLCHAIN_ARMCLANG && !ARCH_TOOLCHAIN_GHS && !ARCH_AVR && !ARCH_SIM) || (ARCH_SIM && SIM_TOOLCHAIN_GCC && !HOST_MACOS && !WINDOWS_NATIVE) + ---help--- + GCC and clang call __emutls_get_address() for __thread variables + when the target has no native TLS, or with -femulated-tls. libc + then gives one copy of each variable per thread, and the link + sends the calls to it with --wrap. Not for AVR: 8-bit AVR has + no atomic pointer loads, and AVR32 was not tested. The sim has Review Comment: Done. ########## boards/arm/stm32g4/b-g431b-esc1/configs/nsh/defconfig: ########## @@ -8,6 +8,7 @@ # CONFIG_ARCH_FPU is not set # CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_DISABLE_PS is not set +# CONFIG_TLS_EMUTLS is not set Review Comment: Done. ########## boards/arm/stm32g4/nucleo-g431kb/configs/nsh/defconfig: ########## @@ -8,6 +8,7 @@ # CONFIG_ARCH_FPU is not set # CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_DISABLE_PS is not set +# CONFIG_TLS_EMUTLS is not set Review Comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
