File tree Expand file tree Collapse file tree 6 files changed +44
-32
lines changed
Lib/test/test_free_threading Expand file tree Collapse file tree 6 files changed +44
-32
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ def writer_func(name):
7474 last = - 1
7575 while True :
7676 if CUR == last :
77+ time .sleep (0.001 )
7778 continue
7879 elif CUR == OBJECT_COUNT :
7980 break
Original file line number Diff line number Diff line change @@ -27,13 +27,13 @@ def set_func_annotation(f, b):
2727
2828@unittest .skipUnless (Py_GIL_DISABLED , "Enable only in FT build" )
2929class TestFTFuncAnnotations (TestCase ):
30- NUM_THREADS = 8
30+ NUM_THREADS = 4
3131
3232 def test_concurrent_read (self ):
3333 def f (x : int ) -> int :
3434 return x + 1
3535
36- for _ in range (100 ):
36+ for _ in range (10 ):
3737 with concurrent .futures .ThreadPoolExecutor (max_workers = self .NUM_THREADS ) as executor :
3838 b = Barrier (self .NUM_THREADS )
3939 futures = {executor .submit (get_func_annotation , f , b ): i for i in range (self .NUM_THREADS )}
@@ -54,7 +54,7 @@ def test_concurrent_write(self):
5454 def bar (x : int , y : float ) -> float :
5555 return y ** x
5656
57- for _ in range (100 ):
57+ for _ in range (10 ):
5858 with concurrent .futures .ThreadPoolExecutor (max_workers = self .NUM_THREADS ) as executor :
5959 b = Barrier (self .NUM_THREADS )
6060 futures = {executor .submit (set_func_annotation , bar , b ): i for i in range (self .NUM_THREADS )}
Original file line number Diff line number Diff line change @@ -35,24 +35,30 @@ def mutator_thread():
3535 pass
3636
3737 def test_get_referrers (self ):
38+ NUM_GC = 2
39+ NUM_MUTATORS = 4
40+
41+ b = threading .Barrier (NUM_GC + NUM_MUTATORS )
3842 event = threading .Event ()
3943
4044 obj = MyObj ()
4145
4246 def gc_thread ():
47+ b .wait ()
4348 for i in range (100 ):
4449 o = gc .get_referrers (obj )
4550 event .set ()
4651
4752 def mutator_thread ():
53+ b .wait ()
4854 while not event .is_set ():
4955 d1 = { "key" : obj }
5056 d2 = { "key" : obj }
5157 d3 = { "key" : obj }
5258 d4 = { "key" : obj }
5359
54- gcs = [Thread (target = gc_thread ) for _ in range (2 )]
55- mutators = [Thread (target = mutator_thread ) for _ in range (4 )]
60+ gcs = [Thread (target = gc_thread ) for _ in range (NUM_GC )]
61+ mutators = [Thread (target = mutator_thread ) for _ in range (NUM_MUTATORS )]
5662 with threading_helper .start_threads (gcs + mutators ):
5763 pass
5864
Original file line number Diff line number Diff line change @@ -20,22 +20,25 @@ class TestList(TestCase):
2020 def test_racing_iter_append (self ):
2121 l = []
2222
23- def writer_func ():
23+ barrier = Barrier (NTHREAD + 1 )
24+ def writer_func (l ):
25+ barrier .wait ()
2426 for i in range (OBJECT_COUNT ):
2527 l .append (C (i + OBJECT_COUNT ))
2628
27- def reader_func ():
29+ def reader_func (l ):
30+ barrier .wait ()
2831 while True :
2932 count = len (l )
3033 for i , x in enumerate (l ):
3134 self .assertEqual (x .v , i + OBJECT_COUNT )
3235 if count == OBJECT_COUNT :
3336 break
3437
35- writer = Thread (target = writer_func )
38+ writer = Thread (target = writer_func , args = ( l ,) )
3639 readers = []
3740 for x in range (NTHREAD ):
38- reader = Thread (target = reader_func )
41+ reader = Thread (target = reader_func , args = ( l ,) )
3942 readers .append (reader )
4043 reader .start ()
4144
@@ -47,11 +50,14 @@ def reader_func():
4750 def test_racing_iter_extend (self ):
4851 l = []
4952
53+ barrier = Barrier (NTHREAD + 1 )
5054 def writer_func ():
55+ barrier .wait ()
5156 for i in range (OBJECT_COUNT ):
5257 l .extend ([C (i + OBJECT_COUNT )])
5358
5459 def reader_func ():
60+ barrier .wait ()
5561 while True :
5662 count = len (l )
5763 for i , x in enumerate (l ):
Original file line number Diff line number Diff line change 88
99from sys import monitoring
1010from test .support import threading_helper
11- from threading import Thread , _PyRLock
11+ from threading import Thread , _PyRLock , Barrier
1212from unittest import TestCase
1313
1414
@@ -194,7 +194,9 @@ def during_threads(self):
194194
195195@threading_helper .requires_working_threading ()
196196class MonitoringMisc (MonitoringTestMixin , TestCase ):
197- def register_callback (self ):
197+ def register_callback (self , barrier ):
198+ barrier .wait ()
199+
198200 def callback (* args ):
199201 pass
200202
@@ -206,8 +208,9 @@ def callback(*args):
206208 def test_register_callback (self ):
207209 self .refs = []
208210 threads = []
209- for i in range (50 ):
210- t = Thread (target = self .register_callback )
211+ barrier = Barrier (5 )
212+ for i in range (5 ):
213+ t = Thread (target = self .register_callback , args = (barrier ,))
211214 t .start ()
212215 threads .append (t )
213216
Original file line number Diff line number Diff line change @@ -45,26 +45,20 @@ def test_attr_cache_consistency(self):
4545 class C :
4646 x = 0
4747
48- DONE = False
4948 def writer_func ():
50- for i in range (3000 ):
49+ for _ in range (3000 ):
5150 C .x
5251 C .x
5352 C .x += 1
54- nonlocal DONE
55- DONE = True
5653
5754 def reader_func ():
58- while True :
55+ for _ in range ( 3000 ) :
5956 # We should always see a greater value read from the type than the
6057 # dictionary
6158 a = C .__dict__ ['x' ]
6259 b = C .x
6360 self .assertGreaterEqual (b , a )
6461
65- if DONE :
66- break
67-
6862 self .run_one (writer_func , reader_func )
6963
7064 def test_attr_cache_consistency_subclass (self ):
@@ -74,26 +68,20 @@ class C:
7468 class D (C ):
7569 pass
7670
77- DONE = False
7871 def writer_func ():
79- for i in range (3000 ):
72+ for _ in range (3000 ):
8073 D .x
8174 D .x
8275 C .x += 1
83- nonlocal DONE
84- DONE = True
8576
8677 def reader_func ():
87- while True :
78+ for _ in range ( 3000 ) :
8879 # We should always see a greater value read from the type than the
8980 # dictionary
9081 a = C .__dict__ ['x' ]
9182 b = D .x
9283 self .assertGreaterEqual (b , a )
9384
94- if DONE :
95- break
96-
9785 self .run_one (writer_func , reader_func )
9886
9987 def test___class___modification (self ):
@@ -140,10 +128,18 @@ class ClassB(Base):
140128
141129
142130 def run_one (self , writer_func , reader_func ):
143- writer = Thread (target = writer_func )
131+ barrier = threading .Barrier (NTHREADS )
132+
133+ def wrap_target (target ):
134+ def wrapper ():
135+ barrier .wait ()
136+ target ()
137+ return wrapper
138+
139+ writer = Thread (target = wrap_target (writer_func ))
144140 readers = []
145- for x in range (30 ):
146- reader = Thread (target = reader_func )
141+ for x in range (NTHREADS - 1 ):
142+ reader = Thread (target = wrap_target ( reader_func ) )
147143 readers .append (reader )
148144 reader .start ()
149145
You can’t perform that action at this time.
0 commit comments