From 355b5406fafe0d45285e70a0b01d5e674d6a2c16 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Mon, 28 Apr 2014 09:15:06 -0400 Subject: [PATCH] Make errno a thread-local variable. errno is supposed to be thread local and we do that here in a rather makeshift way by implementing a function that returns a pointer to the __errno variable in the thread block. If more serious TLS is required, perhaps it would be worthwhile implementing it using the linker and the "__thread" storage attribute. That's a bit overkill just for this. I'm also not liking what I saw the compiler producing. Change-Id: I03bc0bd6a89f6e3d6bae7653284ee01054614f9a Reviewed-on: http://gerrit.rockbox.org/803 Reviewed-by: Michael Sevakis Tested: Michael Sevakis --- apps/plugin.c | 2 +- apps/plugin.h | 6 +++--- firmware/kernel/include/thread.h | 1 + firmware/libc/errno.c | 6 +++++- firmware/libc/include/errno.h | 6 ++++-- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/plugin.c b/apps/plugin.c index 93779d7a6c..4bed707c11 100644 --- a/apps/plugin.c +++ b/apps/plugin.c @@ -684,7 +684,7 @@ static const struct plugin_api rockbox_api = { /* misc */ #if (CONFIG_PLATFORM & PLATFORM_NATIVE) - &errno, + __errno, #endif srand, rand, diff --git a/apps/plugin.h b/apps/plugin.h index 6a15f86919..fd4e468b91 100644 --- a/apps/plugin.h +++ b/apps/plugin.h @@ -160,12 +160,12 @@ void* plugin_get_buffer(size_t *buffer_size); #define PLUGIN_MAGIC 0x526F634B /* RocK */ /* increase this every time the api struct changes */ -#define PLUGIN_API_VERSION 229 +#define PLUGIN_API_VERSION 230 /* update this to latest version if a change to the api struct breaks backwards compatibility (and please take the opportunity to sort in any new function which are "waiting" at the end of the function table) */ -#define PLUGIN_MIN_API_VERSION 228 +#define PLUGIN_MIN_API_VERSION 230 /* plugin return codes */ /* internal returns start at 0x100 to make exit(1..255) work */ @@ -829,7 +829,7 @@ struct plugin_api { /* misc */ #if (CONFIG_PLATFORM & PLATFORM_NATIVE) - int* __errno; + int * (*__errno)(void); #endif void (*srand)(unsigned int seed); int (*rand)(void); diff --git a/firmware/kernel/include/thread.h b/firmware/kernel/include/thread.h index 9cc33b23ae..8c13b462e6 100644 --- a/firmware/kernel/include/thread.h +++ b/firmware/kernel/include/thread.h @@ -210,6 +210,7 @@ struct thread_entry volatile intptr_t retval; /* Return value from a blocked operation/ misc. use */ #endif + int __errno; /* Thread error number (errno tls) */ #ifdef HAVE_PRIORITY_SCHEDULING /* Priority summary of owned objects that support inheritance */ struct blocker *blocker; /* Pointer to blocker when this thread is blocked diff --git a/firmware/libc/errno.c b/firmware/libc/errno.c index 6e7bb62b51..2e3cd9083e 100644 --- a/firmware/libc/errno.c +++ b/firmware/libc/errno.c @@ -1 +1,5 @@ -int errno; +#include "thread.h" +int * __errno(void) +{ + return &thread_self_entry()->__errno; +} diff --git a/firmware/libc/include/errno.h b/firmware/libc/include/errno.h index 6a24a1938f..9df261db9f 100644 --- a/firmware/libc/include/errno.h +++ b/firmware/libc/include/errno.h @@ -10,10 +10,12 @@ #ifndef _SYS_ERRNO_H_ +extern int * __errno(void); + #ifdef PLUGIN -#define errno (*rb->__errno) +#define errno (*rb->__errno()) #else -extern int errno; +#define errno (*__errno()) #endif #define EPERM 1 /* Not super-user */