gh-123358: Use _PyStackRef in LOAD_DEREF (gh-130064)

Concurrent accesses from multiple threads to the same `cell` object did not
scale well in the free-threaded build. Use `_PyStackRef` and optimistically
avoid locking to improve scaling.

With the locks around cell reads gone, some of the free threading tests were
prone to starvation: the readers were able to run in a tight loop and the
writer threads weren't scheduled frequently enough to make timely progress.
Adjust the tests to avoid this.

Co-authored-by: Donghee Na <donghee.na@python.org>
This commit is contained in:
Sam Gross 2025-03-26 12:08:20 -04:00 committed by GitHub
parent 1b8bb1ed0c
commit 3d4ac1a2c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 90 additions and 44 deletions

View file

@ -8,7 +8,7 @@ import weakref
from sys import monitoring
from test.support import threading_helper
from threading import Thread, _PyRLock
from threading import Thread, _PyRLock, Barrier
from unittest import TestCase
@ -194,7 +194,9 @@ class SetProfileMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
@threading_helper.requires_working_threading()
class MonitoringMisc(MonitoringTestMixin, TestCase):
def register_callback(self):
def register_callback(self, barrier):
barrier.wait()
def callback(*args):
pass
@ -206,8 +208,9 @@ class MonitoringMisc(MonitoringTestMixin, TestCase):
def test_register_callback(self):
self.refs = []
threads = []
for i in range(50):
t = Thread(target=self.register_callback)
barrier = Barrier(5)
for i in range(5):
t = Thread(target=self.register_callback, args=(barrier,))
t.start()
threads.append(t)