10000 Atomic assigns · python/cpython@0d9fef7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d9fef7

Browse files
committed
Atomic assigns
1 parent 113687a commit 0d9fef7

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

Objects/dictobject.c

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,20 @@ As a consequence of this, split keys have a maximum size of 16.
115115
#define PyDict_MINSIZE 8
116116

117117
#include "Python.h"
118-
#include "pycore_bitutils.h" // _Py_bit_length
119-
#include "pycore_call.h" // _PyObject_CallNoArgs()
120-
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
121-
#include "pycore_code.h" // stats
122-
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION, Py_END_CRITICAL_SECTION
123-
#include "pycore_dict.h" // export _PyDict_SizeOf()
124-
#include "pycore_freelist.h" // _PyFreeListState_GET()
125-
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
126-
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
127-
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
128-
#include "pycore_pystate.h" // _PyThreadState_GET()
129-
#include "pycore_setobject.h" // _PySet_NextEntry()
130-
#include "stringlib/eq.h" // unicode_eq()
118+
#include "pycore_bitutils.h" // _Py_bit_length
119+
#include "pycore_call.h" // _PyObject_CallNoArgs()
120+
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
121+
#include "pycore_code.h" // stats
122+
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION, Py_END_CRITICAL_SECTION
123+
#include "pycore_dict.h" // export _PyDict_SizeOf()
124+
#include "pycore_freelist.h" // _PyFreeListState_GET()
125+
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
126+
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_STORE_PTR_RELAXED
127+
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
128+
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
129+
#include "pycore_pystate.h" // _PyThreadState_GET()
130+
#include "pycore_setobject.h" // _PySet_NextEntry()
131+
#include "stringlib/eq.h" // unicode_eq
131132

132133
#include <stdbool.h>
133134

@@ -234,6 +235,13 @@ set_values(PyDictObject *mp, PyDictValues *values)
234235

235236
#endif
236237

238+
#define STORE_KEY(ep, key) FT_ATOMIC_STORE_PTR_RELAXED(ep->me_key, key)
239+
#define STORE_VALUE(ep, value) FT_ATOMIC_STORE_PTR_RELAXED(ep->me_value, value)
240+
#define STORE_SPLIT_VALUE(mp, idx, value) FT_ATOMIC_STORE_PTR_RELAXED(mp->ma_values->values[idx], value)
241+
#define STORE_HASH(ep, hash) FT_ATOMIC_STORE_SSIZE_RELAXED(ep->me_hash, hash)
242+
#define STORE_KEYS_USABLE(keys, usable) FT_ATOMIC_STORE_SSIZE_RELAXED(keys->dk_usable, usable)
243+
#define STORE_KEYS_NENTRIES(keys, nentries) FT_ATOMIC_STORE_SSIZE_RELAXED(keys->dk_nentries, nentries)
244+
237245
#define PERTURB_SHIFT 5
238246

239247
/*
@@ -1605,7 +1613,6 @@ insert_into_splitdictkeys(PyDictKeysObject *keys, PyObject *name)
16051613
return ix;
16061614
}
16071615

1608-
16091616
static inline int
16101617
insert_combined_dict(PyInterpreterState *interp, PyDictObject *mp,
16111618
Py_hash_t hash, PyObject *key, PyObject *value)
@@ -1623,18 +1630,18 @@ insert_combined_dict(PyInterpreterState *interp, PyDictObject *mp,
16231630
if (DK_IS_UNICODE(mp->ma_keys)) {
16241631
PyDictUnicodeEntry *ep;
16251632
ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
1626-
ep->me_key = key;
1627-
ep->me_value = value;
1633+
STORE_KEY(ep, key);
1634+
STORE_VALUE(ep, value);
16281635
}
16291636
else {
16301637
PyDictKeyEntry *ep;
16311638
ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
1632-
ep->me_key = key;
1633-
ep->me_hash = hash;
1634-
ep->me_value = value;
1639+
STORE_KEY(ep, key);
1640+
STORE_VALUE(ep, value);
1641+
STORE_HASH(ep, hash);
16351642
}
1636-
mp->ma_keys->dk_usable--;
1637-
mp->ma_keys->dk_nentries++;
1643+
STORE_KEYS_USABLE(mp->ma_keys, mp->ma_keys->dk_usable - 1);
1644+
STORE_KEYS_NENTRIES(mp->ma_keys, mp->ma_keys->dk_nentries + 1);
16381645
assert(mp->ma_keys->dk_usable >= 0);
16391646
return 0;
16401647
}
@@ -1666,7 +1673,7 @@ insert_split_dict(PyInterpreterState *interp, PyDictObject *mp,
16661673
Py_ssize_t index = keys->dk_nentries;
16671674
_PyDictValues_AddToInsertionOrder(mp->ma_values, index);
16681675
assert (mp->ma_values->values[index] == NULL);
1669-
mp->ma_values->values[index] = value;
1676+
STORE_SPLIT_VALUE(mp, index, value);
16701677

16711678
split_keys_entry_added(keys);
16721679
assert(keys->dk_usable >= 0);
@@ -2491,15 +2498,15 @@ delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
24912498
if (DK_IS_UNICODE(mp->ma_keys)) {
24922499
PyDictUnicodeEntry *ep = &DK_UNICODE_ENTRIES(mp->ma_keys)[ix];
24932500
old_key = ep->me_key;
2494-
ep->me_key = NULL;
2495-
ep->me_value = NULL;
2501+
STORE_KEY(ep, NULL);
2502+
STORE_VALUE(ep, NULL);
24962503
}
24972504
else {
24982505
PyDictKeyEntry *ep = &DK_ENTRIES(mp->ma_keys)[ix];
24992506
old_key = ep->me_key;
2500-
ep->me_key = NULL;
2501-
ep->me_value = NULL;
2502-
ep->me_hash = 0;
2507+
STORE_KEY(ep, NULL);
2508+
STORE_VALUE(ep, NULL);
2509+
STORE_HASH(ep, 0);
25032510
}
25042511
Py_DECREF(old_key);
25052512
}

0 commit comments

Comments
 (0)
0