@@ -385,7 +385,7 @@ static inline void _PyObject_GC_UNTRACK(
385
385
* where the reference count modification requires an atomic operation. This
386
386
* allows call sites to specialize for the immortal/local case.
387
387
*/
388
- static inline Py_ALWAYS_INLINE int
388
+ static inline int
389
389
_Py_TryIncrefFast (PyObject * op ) {
390
390
uint32_t local = _Py_atomic_load_uint32_relaxed (& op -> ob_ref_local );
391
391
local += 1 ;
@@ -394,6 +394,7 @@ _Py_TryIncrefFast(PyObject *op) {
394
394
return 1 ;
395
395
}
396
396
if (_Py_IsOwnedByCurrentThread (op )) {
397
+ _Py_INCREF_STAT_INC ();
397
398
_Py_atomic_store_uint32_relaxed (& op -> ob_ref_local , local );
398
399
#ifdef Py_REF_DEBUG
399
400
_Py_IncRefTotal (_PyInterpreterState_GET ());
@@ -403,12 +404,11 @@ _Py_TryIncrefFast(PyObject *op) {
403
404
return 0 ;
404
405
}
405
406
406
- static inline Py_ALWAYS_INLINE int
407
+ static inline int
407
408
_Py_TryIncRefShared (PyObject * op )
408
409
{
410
+ Py_ssize_t shared = _Py_atomic_load_ssize_relaxed (& op -> ob_ref_shared );
409
411
for (;;) {
410
- Py_ssize_t shared = _Py_atomic_load_ssize_relaxed (& op -> ob_ref_shared );
411
-
412
412
// If the shared refcount is zero and the object is either merged
413
413
// or may not have weak references, then we cannot incref it.
414
414
if (shared == 0 || shared == _Py_REF_MERGED ) {
@@ -422,6 +422,7 @@ _Py_TryIncRefShared(PyObject *op)
422
422
#ifdef Py_REF_DEBUG
423
423
_Py_IncRefTotal (_PyInterpreterState_GET ());
424
424
#endif
425
+ _Py_INCREF_STAT_INC ();
425
426
return 1 ;
426
427
}
427
428
}
@@ -447,10 +448,9 @@ _Py_TryAcquireObject(PyObject **src, PyObject *op)
447
448
/* Loads and increfs an object from ptr, which may contain a NULL value.
448
449
Safe with concurrent (atomic) updates to ptr.
449
450
NOTE: The writer must set maybe-weakref on the stored object! */
450
- static inline Py_ALWAYS_INLINE PyObject *
451
- _Py_XFetchRef (PyObject * * ptr )
451
+ static inline PyObject *
452
+ _Py_XGetRef (PyObject * * ptr )
452
453
{
453
- #ifdef Py_NOGIL
454
454
for (;;) {
455
455
PyObject * value = _Py_atomic_load_ptr (ptr );
456
456
if (value == NULL ) {
@@ -460,14 +460,11 @@ _Py_XFetchRef(PyObject **ptr)
460
460
return value ;
461
461
}
462
462
}
463
- #else
464
- return Py_XNewRef (* ptr );
465
- #endif
466
463
}
467
464
468
465
/* Attempts to loads and increfs an object from ptr. Returns NULL
469
466
on failure, which may be due to a NULL value or a concurrent update. */
470
- static inline Py_ALWAYS_INLINE PyObject *
467
+ static inline PyObject *
471
468
_Py_TryXFetchRef (PyObject * * ptr )
472
469
{
473
470
PyObject * value = _Py_atomic_load_ptr (ptr );
@@ -485,32 +482,21 @@ _Py_TryXFetchRef(PyObject **ptr)
485
482
static inline PyObject *
486
483
_Py_NewRefWithLock (PyObject * op )
487
484
{
488
- _Py_INCREF_STAT_INC ();
489
- uint32_t local = _Py_atomic_load_uint32_relaxed (& op -> ob_ref_local );
490
- local += 1 ;
491
- if (local == 0 ) {
485
+ if (_Py_TryIncrefFast (op )) {
492
486
return op ;
493
487
}
494
-
495
- #ifdef Py_REF_DEBUG
496
- _Py_IncRefTotal (_PyInterpreterState_GET ());
497
- #endif
498
- if (_Py_IsOwnedByCurrentThread (op )) {
499
- _Py_atomic_store_uint32_relaxed (& op -> ob_ref_local , local );
500
- }
501
- else {
502
- for (;;) {
503
- Py_ssize_t shared = _Py_atomic_load_ssize_relaxed (& op -> ob_ref_shared );
504
- Py_ssize_t new_shared = shared + (1 << _Py_REF_SHARED_SHIFT );
505
- if ((shared & _Py_REF_SHARED_FLAG_MASK ) == 0 ) {
506
- new_shared |= _Py_REF_MAYBE_WEAKREF ;
507
- }
508
- if (_Py_atomic_compare_exchange_ssize (
509
- & op -> ob_ref_shared ,
510
- & shared ,
511
- new_shared )) {
512
- return op ;
513
- }
488
+ _Py_INCREF_STAT_INC ();
489
+ for (;;) {
490
+ Py_ssize_t shared = _Py_atomic_load_ssize_relaxed (& op -> ob_ref_shared );
491
+ Py_ssize_t new_shared = shared + (1 << _Py_REF_SHARED_SHIFT );
492
+ if ((shared & _Py_REF_SHARED_FLAG_MASK ) == 0 ) {
493
+ new_shared |= _Py_REF_MAYBE_WEAKREF ;
494
+ }
495
+ if (_Py_atomic_compare_exchange_ssize (
496
+ & op -> ob_ref_shared ,
497
+ & shared ,
498
+ new_shared )) {
499
+ return op ;
514
500
}
515
501
}
516
502
return op ;
0 commit comments