40
40
#!/usr/bin/env mx python
41
41
import _io
42
42
import sys
43
+ import time
44
+ import _thread
43
45
44
46
os = sys .modules .get ("posix" , sys .modules .get ("nt" , None ))
45
47
if os is None :
51
53
52
54
verbose = False
53
55
56
+ print_lock = _thread .RLock ()
57
+ class ThreadPool ():
58
+ cnt_lock = _thread .RLock ()
59
+ cnt = 0
60
+ if os .environ .get (b"ENABLE_THREADED_GRAALPYTEST" ) == b"true" :
61
+ maxcnt = min (os .cpu_count (), 16 )
62
+ sleep = time .sleep
63
+ print ("Running with %d threads" % maxcnt )
64
+ else :
65
+ sleep = lambda x : x
66
+ maxcnt = 1
54
67
55
- import threading
56
- import _thread
57
- result_lock = threading .RLock ()
58
- threads = []
59
- if os .environ .get (b"ENABLE_THREADED_GRAALPYTEST" ) == b"true" :
60
- thread_count = min (os .cpu_count (), 16 )
61
- print ("Running with %d threads" % thread_count )
62
- else :
63
- thread_count = 1
64
- thread_token = threading .Semaphore (thread_count )
68
+ @classmethod
69
+ def start (self , function ):
70
+ self .acquire_token ()
71
+ def runner ():
72
+ try :
73
+ function ()
74
+ finally :
75
+ self .release_token ()
76
+ _thread .start_new_thread (runner , ())
77
+ self .sleep (0.5 )
78
+
79
+ @classmethod
80
+ def acquire_token (self ):
81
+ while True :
82
+ with self .cnt_lock :
83
+ if self .cnt < self .maxcnt :
84
+ self .cnt += 1
85
+ break
86
+ self .sleep (1 )
87
+
88
+ @classmethod
89
+ def release_token (self ):
90
+ with self .cnt_lock :
91
+ self .cnt -= 1
92
+
93
+ @classmethod
94
+ def shutdown (self ):
95
+ self .sleep (2 )
96
+ while self .cnt > 0 :
97
+ self .sleep (2 )
65
98
66
99
67
100
def dump_truffle_ast (func ):
@@ -84,7 +117,7 @@ def __init__(self):
84
117
85
118
def run_safely (self , func , print_immediately = False ):
86
119
if verbose :
87
- with result_lock :
120
+ with print_lock :
88
121
print (u"\n \t \u21B3 " , func .__name__ , " " , end = "" )
89
122
try :
90
123
func ()
@@ -118,14 +151,9 @@ def run_test(self, func):
118
151
else :
119
152
def do_run ():
120
153
r = self .run_safely (func )
121
- with result_lock :
154
+ with print_lock :
122
155
self .success () if r else self .failure ()
123
- thread_token .release ()
124
-
125
- thread_token .acquire ()
126
- new_thread = threading .Thread (target = do_run )
127
- threads .append (new_thread )
128
- new_thread .start ()
156
+ ThreadPool .start (do_run )
129
157
130
158
def success (self ):
131
159
self .passed += 1
@@ -338,8 +366,7 @@ def run(self):
338
366
self .failed += testcase .failed
339
367
if verbose :
340
368
print ()
341
- for i , t in enumerate (threads ):
342
- t .join (timeout = 0 )
369
+ ThreadPool .shutdown ()
343
370
print ("\n \n Ran %d tests (%d passes, %d failures)" % (self .passed + self .failed , self .passed , self .failed ))
344
371
for e in self .exceptions :
345
372
print (e )
0 commit comments