Skip to content

Commit c7fc8b3

Browse files
committed
refactor
1 parent 3375e70 commit c7fc8b3

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

quaddtype/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ srcs = [
175175
'numpy_quaddtype/src/umath/matmul.h',
176176
'numpy_quaddtype/src/umath/matmul.cpp',
177177
'numpy_quaddtype/src/constants.hpp',
178+
'numpy_quaddtype/src/lock.h',
179+
'numpy_quaddtype/src/lock.c',
178180
]
179181

180182
py.install_sources(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "lock.h"
2+
3+
#if PY_VERSION_HEX < 0x30d00b3
4+
PyThread_type_lock sleef_lock = NULL;
5+
#else
6+
PyMutex sleef_lock = {0};
7+
#endif
8+
9+
void init_sleef_locks(void)
10+
{
11+
#if PY_VERSION_HEX < 0x30d00b3
12+
sleef_lock = PyThread_allocate_lock();
13+
if (!sleef_lock) {
14+
PyErr_NoMemory();
15+
}
16+
#endif
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _QUADDTYPE_LOCK_H
2+
#define _QUADDTYPE_LOCK_H
3+
4+
#include <Python.h>
5+
6+
#if PY_VERSION_HEX < 0x30d00b3
7+
extern PyThread_type_lock sleef_lock;
8+
#define LOCK_SLEEF PyThread_acquire_lock(sleef_lock, WAIT_LOCK)
9+
#define UNLOCK_SLEEF PyThread_release_lock(sleef_lock)
10+
#else
11+
extern PyMutex sleef_lock;
12+
#define LOCK_SLEEF PyMutex_Lock(&sleef_lock)
13+
#define UNLOCK_SLEEF PyMutex_Unlock(&sleef_lock)
14+
#endif
15+
16+
void init_sleef_locks(void);
17+
18+
#endif // _QUADDTYPE_LOCK_H

quaddtype/numpy_quaddtype/src/quaddtype_main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "numpy/dtype_api.h"
1313
#include "numpy/ufuncobject.h"
1414

15+
#include "lock.h"
1516
#include "scalar.h"
1617
#include "dtype.h"
1718
#include "umath/umath.h"
@@ -96,6 +97,8 @@ PyInit__quaddtype_main(void)
9697
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
9798
#endif
9899

100+
init_sleef_locks();
101+
99102
if (init_quadprecision_scalar() < 0)
100103
goto error;
101104

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,14 @@
1515
#include "scalar_ops.h"
1616
#include "dragon4.h"
1717
#include "dtype.h"
18+
#include "lock.h"
1819

1920
// For IEEE 754 binary128 (quad precision), we need 36 decimal digits
2021
// to guarantee round-trip conversion (string -> parse -> equals original value)
2122
// Formula: ceil(1 + MANT_DIG * log10(2)) = ceil(1 + 113 * 0.30103) = 36
2223
// src: https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format
2324
#define SLEEF_QUAD_DECIMAL_DIG 36
2425

25-
#if PY_VERSION_HEX < 0x30d00b3
26-
static PyThread_type_lock sleef_lock;
27-
#define LOCK_SLEEF PyThread_acquire_lock(sleef_lock, WAIT_LOCK)
28-
#define UNLOCK_SLEEF PyThread_release_lock(sleef_lock)
29-
#else
30-
static PyMutex sleef_lock = {0};
31-
#define LOCK_SLEEF PyMutex_Lock(&sleef_lock)
32-
#define UNLOCK_SLEEF PyMutex_Unlock(&sleef_lock)
33-
#endif
34-
35-
36-
3726

3827
QuadPrecisionObject *
3928
QuadPrecision_raw_new(QuadBackendType backend)
@@ -655,13 +644,6 @@ PyTypeObject QuadPrecision_Type = {
655644
int
656645
init_quadprecision_scalar(void)
657646
{
658-
#if PY_VERSION_HEX < 0x30d00b3
659-
sleef_lock = PyThread_allocate_lock();
660-
if (sleef_lock == NULL) {
661-
PyErr_NoMemory();
662-
return -1;
663-
}
664-
#endif
665647
QuadPrecision_Type.tp_base = &PyFloatingArrType_Type;
666648
return PyType_Ready(&QuadPrecision_Type);
667649
}

0 commit comments

Comments
 (0)