From 344643740a95c2e3bcdc2286c3310527bcbb5844 Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Mon, 5 May 2014 17:07:55 +0200 Subject: [PATCH] libroot/x86_64: minor improvements in TLS code * less inline asm * std::atomic<> instead of obsolete atomic_*() --- src/system/libroot/os/arch/x86_64/tls.cpp | 54 +++++++++++------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/system/libroot/os/arch/x86_64/tls.cpp b/src/system/libroot/os/arch/x86_64/tls.cpp index 15925c282f..48be9078fc 100644 --- a/src/system/libroot/os/arch/x86_64/tls.cpp +++ b/src/system/libroot/os/arch/x86_64/tls.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2014, Paweł Dziepak, pdziepak@quarnos.org. * Copyright 2012, Alex Smith, alex@alex-smith.me.uk. * Distributed under the terms of the MIT License. */ @@ -8,6 +9,8 @@ # define _NO_INLINE_ASM 1 #endif +#include + #include #include @@ -22,54 +25,49 @@ struct tls_index { }; -static int32 gNextSlot = TLS_FIRST_FREE_SLOT; +static std::atomic gNextSlot(TLS_FIRST_FREE_SLOT); + + +static inline void** +get_tls() +{ + void** tls; + __asm__ __volatile__ ("movq %%fs:0, %0" : "=r" (tls)); + return tls; +} int32 -tls_allocate(void) +tls_allocate() { - int32 next = atomic_add(&gNextSlot, 1); - if (next >= TLS_MAX_KEYS) - return B_NO_MEMORY; + if (gNextSlot < TLS_MAX_KEYS) { + auto next = gNextSlot++; + if (next < TLS_MAX_KEYS) + return next; + } - return next; + return B_NO_MEMORY; } void* -tls_get(int32 _index) +tls_get(int32 index) { - int64 index = _index; - void* ret; - - __asm__ __volatile__ ( - "movq %%fs:(, %1, 8), %0" - : "=r" (ret) : "r" (index)); - return ret; + return get_tls()[index]; } void** -tls_address(int32 _index) +tls_address(int32 index) { - int64 index = _index; - void** ret; - - __asm__ __volatile__ ( - "movq %%fs:0, %0\n\t" - "leaq (%0, %1, 8), %0\n\t" - : "=&r" (ret) : "r" (index)); - return ret; + return get_tls() + index; } void -tls_set(int32 _index, void* value) +tls_set(int32 index, void* value) { - int64 index = _index; - __asm__ __volatile__ ( - "movq %1, %%fs:(, %0, 8)" - : : "r" (index), "r" (value)); + get_tls()[index] = value; }