Define and use the CAMLthread_local
macro for TLS variables
#12811
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current way of declaring Thread-Local Storage for variables,
__thread
, isn't available under MSVC.To declare a variable in Thread-Local Storage (TLS):
__thread
storage class keyword;__declspec(thread)
extended storage-class modifier;_Thread_local
storage-class specifier, and thethread_local
macro defined in the (optionally provided)<threads.h>
header;thread_local
storage-class specifier as a keyword, and removes_Thread_local
;thread_local
storage-class specifier as a keyword;<threads.h>
, they must define__STDC_NO_THREADS__
. Neither MinGW nor MSVC provide the header or define the macro (non-compliant compilers…).Adding
#define __thread __declspec(thread)
for MSVC wouldn't be standard-compliant, as identifiers beginning with an underscore are reserved.Although it would be nice to use
thread_local
everywhere, if we use it in a public header we risk it may conflict with a previous declaration from a user. That's unlikely, because users not having a sane definition ofthread_local
as a macro would expose themselves to the same compatibility problems, and their code will start breaking in C23 when it becomes a keyword.If we had private headers we could also have used
thread_local
internally, and have a compatibility definition for our public headers.We take the disruptive approach of using standard keywords instead of compilers extensions, and the conservative approach of hiding them below the new
CAMLthread_local
macro.In the
caml/misc.h
header, we assume that the file is being compiled at least in C11 or C++11. We already use the_Atomic
keyword in public headers, introduced in C11. C++11 is also required for atomics.