|
15 | 15 | #include "scalar_ops.h" |
16 | 16 | #include "dragon4.h" |
17 | 17 |
|
| 18 | +#ifdef Py_GIL_DISABLED |
| 19 | +static PyMutex scalar_mutex = {0}; |
| 20 | +#endif |
| 21 | + |
18 | 22 | QuadPrecisionObject * |
19 | 23 | QuadPrecision_raw_new(QuadBackendType backend) |
20 | 24 | { |
21 | | - QuadPrecisionObject *new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type); |
| 25 | + QuadPrecisionObject *new; |
| 26 | +#ifdef Py_GIL_DISABLED |
| 27 | + PyMutex_Lock(&scalar_mutex); |
| 28 | +#endif |
| 29 | + new = PyObject_New(QuadPrecisionObject, &QuadPrecision_Type); |
| 30 | +#ifdef Py_GIL_DISABLED |
| 31 | + PyMutex_Unlock(&scalar_mutex); |
| 32 | +#endif |
| 33 | + |
22 | 34 | if (!new) |
23 | 35 | return NULL; |
24 | 36 | new->backend = backend; |
@@ -184,19 +196,28 @@ QuadPrecision_str(QuadPrecisionObject *self) |
184 | 196 | static PyObject * |
185 | 197 | QuadPrecision_repr(QuadPrecisionObject *self) |
186 | 198 | { |
| 199 | +#ifdef Py_GIL_DISABLED |
| 200 | + PyMutex_Lock(&scalar_mutex); |
| 201 | +#endif |
187 | 202 | PyObject *str = QuadPrecision_str(self); |
188 | 203 | if (str == NULL) { |
189 | 204 | return NULL; |
190 | 205 | } |
191 | 206 | const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble"; |
192 | 207 | PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str); |
193 | 208 | Py_DECREF(str); |
| 209 | +#ifdef Py_GIL_DISABLED |
| 210 | + PyMutex_Unlock(&scalar_mutex); |
| 211 | +#endif |
194 | 212 | return res; |
195 | 213 | } |
196 | 214 |
|
197 | 215 | static PyObject * |
198 | 216 | QuadPrecision_repr_dragon4(QuadPrecisionObject *self) |
199 | 217 | { |
| 218 | +#ifdef Py_GIL_DISABLED |
| 219 | + PyMutex_Lock(&scalar_mutex); |
| 220 | +#endif |
200 | 221 | Dragon4_Options opt = {.scientific = 1, |
201 | 222 | .digit_mode = DigitMode_Unique, |
202 | 223 | .cutoff_mode = CutoffMode_TotalLength, |
@@ -226,6 +247,9 @@ QuadPrecision_repr_dragon4(QuadPrecisionObject *self) |
226 | 247 | const char *backend_str = (self->backend == BACKEND_SLEEF) ? "sleef" : "longdouble"; |
227 | 248 | PyObject *res = PyUnicode_FromFormat("QuadPrecision('%S', backend='%s')", str, backend_str); |
228 | 249 | Py_DECREF(str); |
| 250 | +#ifdef Py_GIL_DISABLED |
| 251 | + PyMutex_Unlock(&scalar_mutex); |
| 252 | +#endif |
229 | 253 | return res; |
230 | 254 | } |
231 | 255 |
|
|
0 commit comments