File tree Expand file tree Collapse file tree 1 file changed +20
-27
lines changed
Lib/test/test_free_threading Expand file tree Collapse file tree 1 file changed +20
-27
lines changed Original file line number Diff line number Diff line change 3
3
import heapq
4
4
5
5
from enum import Enum
6
- from threading import Thread , Barrier
6
+ from threading import Thread , Barrier , Lock
7
7
from random import shuffle , randint
8
8
9
- from test .support import threading_helper , Py_GIL_DISABLED
9
+ from test .support import threading_helper
10
10
from test import test_heapq
11
11
12
12
@@ -178,39 +178,32 @@ def heapreplace_max_func(max_heap, replace_items):
178
178
self .assertEqual (len (max_heap ), OBJECT_COUNT )
179
179
self .test_heapq .check_max_invariant (max_heap )
180
180
181
- @unittest .skipUnless (Py_GIL_DISABLED , 'only used to test under free-threaded build' )
182
181
def test_lock_free_list_read (self ):
183
182
n , n_threads = 1_000 , 10
184
183
l = []
185
184
barrier = Barrier (n_threads * 2 )
186
185
187
- def writer ():
188
- barrier .wait ()
189
- for i in range (n ):
190
- heapq .heappush (l , 1 )
191
- heapq .heappop (l )
186
+ count = 0
187
+ lock = Lock ()
188
+
189
+ def worker ():
190
+ with lock :
191
+ nonlocal count
192
+ x = count
193
+ count += 1
192
194
193
- def reader ():
194
195
barrier .wait ()
195
196
for i in range (n ):
196
- try :
197
- l [0 ]
198
- except IndexError :
199
- pass
200
-
201
- import threading
202
- threads = []
203
- with threading_helper .catch_threading_exception () as cm :
204
- for _ in range (n_threads ):
205
- t1 = threading .Thread (target = writer )
206
- t2 = threading .Thread (target = reader )
207
- threads .append (t1 )
208
- threads .append (t2 )
209
- t1 .start ()
210
- t2 .start ()
211
-
212
- for t in threads :
213
- t .join ()
197
+ if x % 2 :
198
+ heapq .heappush (l , 1 )
199
+ heapq .heappop (l )
200
+ else :
201
+ try :
202
+ l [0 ]
203
+ except IndexError :
204
+ pass
205
+
206
+ self .run_concurrently (worker , (), n_threads * 2 )
214
207
215
208
@staticmethod
216
209
def is_sorted_ascending (lst ):
You can’t perform that action at this time.
0 commit comments