Skip to content

Commit 1d9514b

Browse files
committed
selecting TLS support
1 parent 0ebf6f6 commit 1d9514b

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

quaddtype/meson.build

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,44 @@ if openmp_dep.found()
3030
dependencies += openmp_dep
3131
endif
3232

33+
# Thread-local storage detection (borrowed from NumPy)
34+
optional_variable_attributes = [
35+
['thread_local', 'HAVE_THREAD_LOCAL'], # C23
36+
['_Thread_local', 'HAVE__THREAD_LOCAL'], # C11/C17
37+
['__thread', 'HAVE___THREAD'], # GCC/Clang
38+
['__declspec(thread)', 'HAVE___DECLSPEC_THREAD_'] # MSVC
39+
]
40+
41+
if not is_variable('cdata')
42+
cdata = configuration_data()
43+
endif
44+
45+
foreach optional_attr: optional_variable_attributes
46+
attr = optional_attr[0]
47+
code = '''
48+
#pragma GCC diagnostic error "-Wattributes"
49+
#pragma clang diagnostic error "-Wattributes"
50+
51+
int @0@ foo;
52+
53+
int main() {
54+
return 0;
55+
}
56+
'''.format(attr)
57+
58+
if c.compiles(code, name: optional_attr[0])
59+
cdata.set10(optional_attr[1], true)
60+
message('Thread-local storage support found: @0@'.format(attr))
61+
endif
62+
endforeach
63+
64+
configure_file(
65+
output: 'quaddtype_config.h',
66+
configuration: cdata
67+
)
68+
69+
build_includes = include_directories('.')
70+
3371
includes = include_directories(
3472
[
3573
incdir_numpy,
@@ -84,5 +122,5 @@ py.extension_module('_quaddtype_main',
84122
dependencies: dependencies,
85123
install: true,
86124
subdir: 'numpy_quaddtype',
87-
include_directories: includes
125+
include_directories: [includes, build_includes]
88126
)

quaddtype/numpy_quaddtype/src/dragon4.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Modifications are specific to support the SLEEF_QUAD
1111
#include <sleef.h>
1212
#include <sleefquad.h>
1313

14+
#include "quaddtype_config.h"
15+
16+
1417
#define PY_ARRAY_UNIQUE_SYMBOL QuadPrecType_ARRAY_API
1518
#define PY_UFUNC_UNIQUE_SYMBOL QuadPrecType_UFUNC_API
1619
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
@@ -22,6 +25,15 @@ Modifications are specific to support the SLEEF_QUAD
2225
#include "dtype.h"
2326
#include "scalar.h"
2427

28+
29+
#if !defined(HAVE_THREAD_LOCAL) && !defined(HAVE__THREAD_LOCAL) && \
30+
!defined(HAVE___THREAD) && !defined(HAVE___DECLSPEC_THREAD_) && \
31+
!defined(__cplusplus)
32+
#warning "No thread-local storage support detected! NPY_TLS will be empty, causing thread safety issues."
33+
#else
34+
#warning "NPY_TLS Thread-local storage support detected."
35+
#endif
36+
2537
#if 0
2638
#define DEBUG_ASSERT(stmnt) assert(stmnt)
2739
#else

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@
1515
#include "scalar_ops.h"
1616
#include "dragon4.h"
1717

18-
#ifdef Py_GIL_DISABLED
19-
static PyMutex scalar_mutex = {0};
20-
#endif
2118

2219
QuadPrecisionObject *
2320
QuadPrecision_raw_new(QuadBackendType backend)
2421
{
2522
QuadPrecisionObject *new;
26-
#ifdef Py_GIL_DISABLED
27-
PyMutex_Lock(&scalar_mutex);
28-
#endif
2923
new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type);
30-
#ifdef Py_GIL_DISABLED
31-
PyMutex_Unlock(&scalar_mutex);
32-
#endif
3324

3425
if (!new)
3526
return NULL;
@@ -196,28 +187,19 @@ QuadPrecision_str(QuadPrecisionObject *self)
196187
static PyObject *
197188
QuadPrecision_repr(QuadPrecisionObject *self)
198189
{
199-
#ifdef Py_GIL_DISABLED
200-
PyMutex_Lock(&scalar_mutex);
201-
#endif
202190
PyObject *str = QuadPrecision_str(self);
203191
if (str == NULL) {
204192
return NULL;
205193
}
206194
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
207195
PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str);
208196
Py_DECREF(str);
209-
#ifdef Py_GIL_DISABLED
210-
PyMutex_Unlock(&scalar_mutex);
211-
#endif
212197
return res;
213198
}
214199

215200
static PyObject *
216201
QuadPrecision_repr_dragon4(QuadPrecisionObject *self)
217202
{
218-
#ifdef Py_GIL_DISABLED
219-
PyMutex_Lock(&scalar_mutex);
220-
#endif
221203
Dragon4_Options opt = {.scientific = 1,
222204
.digit_mode = DigitMode_Unique,
223205
.cutoff_mode = CutoffMode_TotalLength,
@@ -247,9 +229,6 @@ QuadPrecision_repr_dragon4(QuadPrecisionObject *self)
247229
const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble";
248230
PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str);
249231
Py_DECREF(str);
250-
#ifdef Py_GIL_DISABLED
251-
PyMutex_Unlock(&scalar_mutex);
252-
#endif
253232
return res;
254233
}
255234

0 commit comments

Comments
 (0)