Skip to content

Allow overriding USE_COMPILER_TLS (formerly HAS_COMPILER_TLS). #1726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions driver/others/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,26 +471,27 @@ struct alloc_t {
for an auxiliary tracking structure. */
static const int allocation_block_size = BUFFER_SIZE + sizeof(struct alloc_t);

#if !defined(USE_COMPILER_TLS)
/* Clang supports TLS from version 2.8 */
#if defined(__clang__) && __clang_major__ > 2 || \
(__clang_minor__ == 2 || __clang_minor__ == 8)
#define HAS_COMPILER_TLS
#define USE_COMPILER_TLS 1
#endif

/* GCC supports TLS from version 4.1 */
#if !defined(__clang__) && defined(__GNUC__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
#define HAS_COMPILER_TLS
#define USE_COMPILER_TLS 1
#endif

/* MSVC supports TLS from version 2005 */
#if defined(_MSC_VER) && _MSC_VER >= 1400
#define HAS_COMPILER_TLS
#define USE_COMPILER_TLS 1
#endif

/* Versions of XCode before 8 did not properly support TLS */
#if defined(__apple_build_version__) && __apple_build_version__ < 8000042
#undef HAS_COMPILER_TLS
#define USE_COMPILER_TLS 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have this condition match while also have a previous one match ?
This would mean a previous line (like 478) does #define USE_COMPILER_TLS 1 and then this one redefine it to 0 causing a compiler warning or error.
Maybe just add a #undef USE_COMPILER_TLS before this line.

#endif

/* Android NDK's before version 12b did not support TLS */
Expand All @@ -501,17 +502,18 @@ static const int allocation_block_size = BUFFER_SIZE + sizeof(struct alloc_t);
#if defined(__ANDROID__) && defined(__clang__) && defined(__NDK_MAJOR__) && \
defined(__NDK_MINOR__) && \
((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
#undef HAS_COMPILER_TLS
#define USE_COMPILER_TLS 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: possible redefinition of USE_COMPILER_TLS as __clang__ is checked and will cause that define to be set to 1 at line 478.

#endif
#endif
#endif /* !USE_COMPILER_TLS */

/* Holds pointers to allocated memory */
#if defined(SMP) && !defined(USE_OPENMP_UNUSED)
/* This is the number of threads than can be spawned by the server, which is the
server plus the number of threads in the thread pool */
# define MAX_ALLOCATING_THREADS MAX_CPU_NUMBER * 2 * MAX_PARALLEL_NUMBER * 2
static int next_memory_table_pos = 0;
# if defined(HAS_COMPILER_TLS)
# if USE_COMPILER_TLS
/* Use compiler generated thread-local-storage */
static int THREAD_LOCAL local_memory_table_pos = 0;
# else
Expand All @@ -521,7 +523,7 @@ static DWORD local_storage_key;
# else
static pthread_key_t local_storage_key;
# endif /* defined(OS_WINDOWS) */
# endif /* defined(HAS_COMPILER_TLS) */
# endif /* USE_COMPILER_TLS */
#else
/* There is only one allocating thread when in single-threaded mode and when using OpenMP */
# define MAX_ALLOCATING_THREADS 1
Expand All @@ -545,26 +547,26 @@ static BLASULONG alloc_lock = 0UL;
/* Returns a pointer to the start of the per-thread memory allocation data */
static __inline struct alloc_t ** get_memory_table() {
#if defined(SMP) && !defined(USE_OPENMP_UNUSED)
# if !defined(HAS_COMPILER_TLS)
# if !USE_COMPILER_TLS
# if defined(OS_WINDOWS)
int local_memory_table_pos = (int)::TlsGetValue(local_storage_key);
# else
int local_memory_table_pos = (int)pthread_getspecific(local_storage_key);
# endif /* defined(OS_WINDOWS) */
# endif /* !defined(HAS_COMPILER_TLS) */
# endif /* !USE_COMPILER_TLS */
if (!local_memory_table_pos) {
LOCK_COMMAND(&alloc_lock);
local_memory_table_pos = next_memory_table_pos++;
if (next_memory_table_pos > MAX_ALLOCATING_THREADS)
printf("OpenBLAS : Program will terminate because you tried to start too many threads.\n");
UNLOCK_COMMAND(&alloc_lock);
# if !defined(HAS_COMPILER_TLS)
# if !USE_COMPILER_TLS
# if defined(OS_WINDOWS)
::TlsSetValue(local_storage_key, (void*)local_memory_table_pos);
# else
pthread_setspecific(local_storage_key, (void*)local_memory_table_pos);
# endif /* defined(OS_WINDOWS) */
# endif /* !defined(HAS_COMPILER_TLS) */
# endif /* !USE_COMPILER_TLS */
}
return local_memory_table[local_memory_table_pos];
#else
Expand Down Expand Up @@ -1072,13 +1074,13 @@ static volatile int memory_initialized = 0;
static void blas_memory_init(){
#if defined(SMP) && !defined(USE_OPENMP_UNUSED)
next_memory_table_pos = 0;
# if !defined(HAS_COMPILER_TLS)
# if !USE_COMPILER_TLS
# if defined(OS_WINDOWS)
local_storage_key = ::TlsAlloc();
# else
pthread_key_create(&local_storage_key, NULL);
# endif /* defined(OS_WINDOWS) */
# endif /* defined(HAS_COMPILER_TLS) */
# endif /* USE_COMPILER_TLS */
#endif /* defined(SMP) && !defined(USE_OPENMP) */
memset(local_memory_table, 0, sizeof(local_memory_table));
}
Expand Down