Skip to content

Commit 9cda534

Browse files
committed
don't use threading module, there's to many issues still with our threads and weakrefs
1 parent 4531919 commit 9cda534

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

graalpython/com.oracle.graal.python.test/src/graalpytest.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#!/usr/bin/env mx python
4141
import _io
4242
import sys
43+
import time
44+
import _thread
4345

4446
os = sys.modules.get("posix", sys.modules.get("nt", None))
4547
if os is None:
@@ -51,17 +53,48 @@
5153

5254
verbose = False
5355

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
5467

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)
6598

6699

67100
def dump_truffle_ast(func):
@@ -84,7 +117,7 @@ def __init__(self):
84117

85118
def run_safely(self, func, print_immediately=False):
86119
if verbose:
87-
with result_lock:
120+
with print_lock:
88121
print(u"\n\t\u21B3 ", func.__name__, " ", end="")
89122
try:
90123
func()
@@ -118,14 +151,9 @@ def run_test(self, func):
118151
else:
119152
def do_run():
120153
r = self.run_safely(func)
121-
with result_lock:
154+
with print_lock:
122155
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)
129157

130158
def success(self):
131159
self.passed += 1
@@ -338,8 +366,7 @@ def run(self):
338366
self.failed += testcase.failed
339367
if verbose:
340368
print()
341-
for i, t in enumerate(threads):
342-
t.join(timeout=0)
369+
ThreadPool.shutdown()
343370
print("\n\nRan %d tests (%d passes, %d failures)" % (self.passed + self.failed, self.passed, self.failed))
344371
for e in self.exceptions:
345372
print(e)

0 commit comments

Comments
 (0)