Skip to content

Commit 2b4feee

Browse files
authored
gh-122581: Use parser mutex in default build for subinterpreters (gh-142959)
1 parent 7607712 commit 2b4feee

File tree

3 files changed

+9
-28
lines changed

3 files changed

+9
-28
lines changed

Include/internal/pycore_parser.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ extern "C" {
1414
#include "pycore_pyarena.h" // PyArena
1515

1616
_Py_DECLARE_STR(empty, "")
17-
#if defined(Py_DEBUG) && defined(Py_GIL_DISABLED)
1817
#define _parser_runtime_state_INIT \
1918
{ \
20-
.mutex = {0}, \
2119
.dummy_name = { \
2220
.kind = Name_kind, \
2321
.v.Name.id = &_Py_STR(empty), \
@@ -28,20 +26,6 @@ _Py_DECLARE_STR(empty, "")
2826
.end_col_offset = 0, \
2927
}, \
3028
}
31-
#else
32-
#define _parser_runtime_state_INIT \
33-
{ \
34-
.dummy_name = { \
35-
.kind = Name_kind, \
36-
.v.Name.id = &_Py_STR(empty), \
37-
.v.Name.ctx = Load, \
38-
.lineno = 1, \
39-
.col_offset = 0, \
40-
.end_lineno = 1, \
41-
.end_col_offset = 0, \
42-
}, \
43-
}
44-
#endif
4529

4630
extern struct _mod* _PyParser_ASTFromString(
4731
const char *str,

Include/internal/pycore_runtime_structs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ struct _fileutils_state {
7777
struct _parser_runtime_state {
7878
#ifdef Py_DEBUG
7979
long memo_statistics[_PYPEGEN_NSTATISTICS];
80-
#ifdef Py_GIL_DISABLED
8180
PyMutex mutex;
82-
#endif
8381
#else
8482
int _not_used;
8583
#endif

Parser/pegen.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
#include "pycore_pystate.h" // _PyThreadState_GET()
44
#include "pycore_parser.h" // _PYPEGEN_NSTATISTICS
55
#include "pycore_pyerrors.h" // PyExc_IncompleteInputError
6-
#include "pycore_runtime.h" // _PyRuntime
6+
#include "pycore_runtime.h" // _PyRuntime
77
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal
8-
#include "pycore_pyatomic_ft_wrappers.h"
98
#include <errcode.h>
109

1110
#include "lexer/lexer.h"
@@ -303,11 +302,11 @@ _PyPegen_fill_token(Parser *p)
303302
void
304303
_PyPegen_clear_memo_statistics(void)
305304
{
306-
FT_MUTEX_LOCK(&_PyRuntime.parser.mutex);
305+
PyMutex_Lock(&_PyRuntime.parser.mutex);
307306
for (int i = 0; i < NSTATISTICS; i++) {
308307
memo_statistics[i] = 0;
309308
}
310-
FT_MUTEX_UNLOCK(&_PyRuntime.parser.mutex);
309+
PyMutex_Unlock(&_PyRuntime.parser.mutex);
311310
}
312311

313312
PyObject *
@@ -318,22 +317,22 @@ _PyPegen_get_memo_statistics(void)
318317
return NULL;
319318
}
320319

321-
FT_MUTEX_LOCK(&_PyRuntime.parser.mutex);
320+
PyMutex_Lock(&_PyRuntime.parser.mutex);
322321
for (int i = 0; i < NSTATISTICS; i++) {
323322
PyObject *value = PyLong_FromLong(memo_statistics[i]);
324323
if (value == NULL) {
325-
FT_MUTEX_UNLOCK(&_PyRuntime.parser.mutex);
324+
PyMutex_Unlock(&_PyRuntime.parser.mutex);
326325
Py_DECREF(ret);
327326
return NULL;
328327
}
329328
// PyList_SetItem borrows a reference to value.
330329
if (PyList_SetItem(ret, i, value) < 0) {
331-
FT_MUTEX_UNLOCK(&_PyRuntime.parser.mutex);
330+
PyMutex_Unlock(&_PyRuntime.parser.mutex);
332331
Py_DECREF(ret);
333332
return NULL;
334333
}
335334
}
336-
FT_MUTEX_UNLOCK(&_PyRuntime.parser.mutex);
335+
PyMutex_Unlock(&_PyRuntime.parser.mutex);
337336
return ret;
338337
}
339338
#endif
@@ -359,9 +358,9 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
359358
if (count <= 0) {
360359
count = 1;
361360
}
362-
FT_MUTEX_LOCK(&_PyRuntime.parser.mutex);
361+
PyMutex_Lock(&_PyRuntime.parser.mutex);
363362
memo_statistics[type] += count;
364-
FT_MUTEX_UNLOCK(&_PyRuntime.parser.mutex);
363+
PyMutex_Unlock(&_PyRuntime.parser.mutex);
365364
}
366365
#endif
367366
p->mark = m->mark;

0 commit comments

Comments
 (0)