Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/erfadatextra.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
*/
#include "erfa.h"
#include "erfaextra.h"
#include "erfadatextra.h"

static eraLEAPSECOND *changes;
static int NDAT = -1;
static ERFA_THREAD_LOCAL eraLEAPSECOND *changes;
static ERFA_THREAD_LOCAL int NDAT = -1;


int eraGetLeapSeconds(eraLEAPSECOND **leapseconds)
Expand Down
26 changes: 26 additions & 0 deletions src/erfadatextra.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,30 @@
int eraDatini(const eraLEAPSECOND *builtin, int n_builtin,
eraLEAPSECOND **leapseconds);

/*
** For thread safety we want a way to define thread-local variables.
** If no TLS is available, fall back to ordinary storage and disable
** thread safety for those variables. This mostly follows the numpy
** specification (as of 8203da6e4cdad722664fa740c7a9514104483f07)
**
** The order is:
** C++ >= 11 and GCC C >= 23 uses thread_local
** GCC: 11 <= C < 23 uses _Thread_local
** Clang uses __thread
** MSVC uses _declspec(thread)
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be good to include in the comment where these definitions come from, in case things break later and we have to update or so. I notice that they are somewhat different from what is used by numpy

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I hadn't seen the numpy version, thanks for linking.

The differences in the conditions are that I missed C23 also has thread_local rather than _Thread_local and for cpp there is an explicit check of the version being > = C++11. I extended the comment to include more of this.

The main difference I see is the main checks are added in meson.build.
I tested this with erfa and it wasn't clear where this should go, I think for safety it would need to be in meson.build, configure.ac, and setup.py.
I'm happy to push a version with that, but it is quite a lot of repetition.
Do you think that would be better?

#if defined(__cplusplus) && __cplusplus >= 201103L
# define ERFA_THREAD_LOCAL thread_local
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
# define ERFA_THREAD_LOCAL thread_local
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
# define ERFA_THREAD_LOCAL _Thread_local
#elif defined(__GNUC__) || defined(__clang__)
# define ERFA_THREAD_LOCAL __thread
#elif defined(_MSC_VER)
# define ERFA_THREAD_LOCAL __declspec(thread)
#else
# define ERFA_THREAD_LOCAL
#endif

#endif