@@ -12,6 +12,7 @@ annotated by François Pinard, and converted to C by Raymond Hettinger.
12
12
13
13
#include "Python.h"
14
14
#include "pycore_list.h" // _PyList_ITEMS(), _PyList_AppendTakeRef()
15
+ #include "pycore_pyatomic_ft_wrappers.h"
15
16
16
17
#include "clinic/_heapqmodule.c.h"
17
18
@@ -59,8 +60,8 @@ siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
59
60
arr = _PyList_ITEMS (heap );
60
61
parent = arr [parentpos ];
61
62
newitem = arr [pos ];
62
- arr [parentpos ] = newitem ;
63
- arr [pos ] = parent ;
63
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [parentpos ], newitem ) ;
64
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [pos ], parent ) ;
64
65
pos = parentpos ;
65
66
}
66
67
return 0 ;
@@ -108,8 +109,8 @@ siftup(PyListObject *heap, Py_ssize_t pos)
108
109
/* Move the smaller child up. */
109
110
tmp1 = arr [childpos ];
110
111
tmp2 = arr [pos ];
111
- arr [childpos ] = tmp2 ;
112
- arr [pos ] = tmp1 ;
112
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [childpos ], tmp2 ) ;
113
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [pos ], tmp1 ) ;
113
114
pos = childpos ;
114
115
}
115
116
/* Bubble it up to its final resting place (by sifting its parents down). */
@@ -172,8 +173,9 @@ heappop_internal(PyObject *heap, int siftup_func(PyListObject *, Py_ssize_t))
172
173
if (!n )
173
174
return lastelt ;
174
175
returnitem = PyList_GET_ITEM (heap , 0 );
175
- PyList_SET_ITEM (heap , 0 , lastelt );
176
- if (siftup_func ((PyListObject * )heap , 0 )) {
176
+ PyListObject * list = _PyList_CAST (heap );
177
+ FT_ATOMIC_STORE_PTR_RELAXED (list -> ob_item [0 ], lastelt );
178
+ if (siftup_func (list , 0 )) {
177
179
Py_DECREF (returnitem );
178
180
return NULL ;
179
181
}
@@ -208,8 +210,9 @@ heapreplace_internal(PyObject *heap, PyObject *item, int siftup_func(PyListObjec
208
210
}
209
211
210
212
returnitem = PyList_GET_ITEM (heap , 0 );
211
- PyList_SET_ITEM (heap , 0 , Py_NewRef (item ));
212
- if (siftup_func ((PyListObject * )heap , 0 )) {
213
+ PyListObject * list = _PyList_CAST (heap );
214
+ FT_ATOMIC_STORE_PTR_RELAXED (list -> ob_item [0 ], Py_NewRef (item ));
215
+ if (siftup_func (list , 0 )) {
213
216
Py_DECREF (returnitem );
214
217
return NULL ;
215
218
}
@@ -284,8 +287,9 @@ _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
284
287
}
285
288
286
289
returnitem = PyList_GET_ITEM (heap , 0 );
287
- PyList_SET_ITEM (heap , 0 , Py_NewRef (item ));
288
- if (siftup ((PyListObject * )heap , 0 )) {
290
+ PyListObject * list = _PyList_CAST (heap );
291
+ FT_ATOMIC_STORE_PTR_RELAXED (list -> ob_item [0 ], Py_NewRef (item ));
292
+ if (siftup (list , 0 )) {
289
293
Py_DECREF (returnitem );
290
294
return NULL ;
291
295
}
@@ -437,8 +441,8 @@ siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
437
441
arr = _PyList_ITEMS (heap );
438
442
parent = arr [parentpos ];
439
443
newitem = arr [pos ];
440
- arr [parentpos ] = newitem ;
441
- arr [pos ] = parent ;
444
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [parentpos ], newitem ) ;
445
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [pos ], parent ) ;
442
446
pos = parentpos ;
443
447
}
444
448
return 0 ;
@@ -486,8 +490,8 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
486
490
/* Move the smaller child up. */
487
491
tmp1 = arr [childpos ];
488
492
tmp2 = arr [pos ];
489
- arr [childpos ] = tmp2 ;
490
- arr [pos ] = tmp1 ;
493
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [childpos ], tmp2 ) ;
494
+ FT_ATOMIC_STORE_PTR_RELAXED ( arr [pos ], tmp1 ) ;
491
495
pos = childpos ;
492
496
}
493
497
/* Bubble it up to its final resting place (by sifting its parents down). */
@@ -621,8 +625,9 @@ _heapq_heappushpop_max_impl(PyObject *module, PyObject *heap, PyObject *item)
621
625
}
622
626
623
627
returnitem = PyList_GET_ITEM (heap , 0 );
624
- PyList_SET_ITEM (heap , 0 , Py_NewRef (item ));
625
- if (siftup_max ((PyListObject * )heap , 0 ) < 0 ) {
628
+ PyListObject * list = _PyList_CAST (heap );
629
+ FT_ATOMIC_STORE_PTR_RELAXED (list -> ob_item [0 ], Py_NewRef (item ));
630
+ if (siftup_max (list , 0 ) < 0 ) {
626
631
Py_DECREF (returnitem );
627
632
return NULL ;
628
633
}
0 commit comments