Skip to content

Commit ecfd199

Browse files
committed
Take a critical section around dict specialization
1 parent feb7d34 commit ecfd199

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Python/specialize.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "opcode.h"
44

55
#include "pycore_code.h"
6+
#include "pycore_critical_section.h"
67
#include "pycore_descrobject.h" // _PyMethodWrapper_Type
78
#include "pycore_dict.h" // DICT_KEYS_UNICODE
89
#include "pycore_function.h" // _PyFunction_GetVersionForCurrentState()
@@ -966,14 +967,15 @@ analyze_descriptor(PyTypeObject *type, PyObject *name, PyObject **descr, unsigne
966967
}
967968

968969
static int
969-
specialize_inline_values_access(
970+
specialize_inline_values_access_lock_held(
970971
PyObject *owner, _Py_CODEUNIT *instr, PyTypeObject *type,
971972
unsigned int tp_version,
972973
PyObject *name, int base_op, int values_op)
973974
{
974975
PyDictKeysObject *keys = ((PyHeapTypeObject *)type)->ht_cached_keys;
975976
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
976977
assert(PyUnicode_CheckExact(name));
978+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(owner);
977979
Py_ssize_t index = _PyDictKeys_StringLookup(keys, name);
978980
assert (index != DKIX_ERROR);
979981
if (index == DKIX_EMPTY) {
@@ -994,13 +996,16 @@ specialize_inline_values_access(
994996
}
995997

996998
static int
997-
specialize_managed_dict_access(
999+
specialize_managed_dict_access_lock_held(
9981000
PyObject *owner, _Py_CODEUNIT *instr, PyTypeObject *type,
9991001
unsigned int tp_version,
10001002
PyObject *name, int base_op, int hint_op)
10011003
{
10021004
PyDictObject *dict = _PyObject_GetManagedDict(owner);
10031005
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
1006+
1007+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(owner);
1008+
10041009
if (dict == NULL || !PyDict_CheckExact(dict)) {
10051010
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_NO_DICT);
10061011
return 0;
@@ -1039,17 +1044,21 @@ specialize_dict_access(
10391044
SPECIALIZATION_FAIL(base_op, SPEC_FAIL_ATTR_NOT_MANAGED_DICT);
10401045
return 0;
10411046
}
1047+
int result;
1048+
Py_BEGIN_CRITICAL_SECTION(owner);
10421049
if (type->tp_flags & Py_TPFLAGS_INLINE_VALUES &&
10431050
_PyObject_InlineValues(owner)->valid &&
10441051
!(base_op == STORE_ATTR && _PyObject_GetManagedDict(owner) != NULL))
10451052
{
1046-
return specialize_inline_values_access(
1053+
result = specialize_inline_values_access_lock_held(
10471054
owner, instr, type, tp_version, name, base_op, values_op);
10481055
}
10491056
else {
1050-
return specialize_managed_dict_access(
1057+
result = specialize_managed_dict_access_lock_held(
10511058
owner, instr, type, tp_version, name, base_op, hint_op);
10521059
}
1060+
Py_END_CRITICAL_SECTION();
1061+
return result;
10531062
}
10541063

10551064
static int

0 commit comments

Comments
 (0)