Skip to content

Commit 3545ef8

Browse files
committed
tests/thread: Remove need to sleep to wait for completion in some tests.
Use a lock and a counter instead, and busy wait for all threads to complete. This makes test run faster and they no longer rely on the time module.
1 parent 2d5ea38 commit 3545ef8

File tree

6 files changed

+65
-38
lines changed

6 files changed

+65
-38
lines changed

tests/thread/thread_exc1.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
#
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

5-
try:
6-
import utime as time
7-
except ImportError:
8-
import time
95
import _thread
106

117
def foo():
@@ -16,9 +12,19 @@ def thread_entry():
1612
foo()
1713
except ValueError:
1814
pass
15+
with lock:
16+
global n_finished
17+
n_finished += 1
1918

20-
for i in range(4):
19+
lock = _thread.allocate_lock()
20+
n_thread = 4
21+
n_finished = 0
22+
23+
# spawn threads
24+
for i in range(n_thread):
2125
_thread.start_new_thread(thread_entry, ())
2226

23-
time.sleep(0.2)
27+
# busy wait for threads to finish
28+
while n_finished < n_thread:
29+
pass
2430
print('done')

tests/thread/thread_ident1.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
#
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

5-
try:
6-
import utime as time
7-
except ImportError:
8-
import time
95
import _thread
106

117
def thread_entry():
128
tid = _thread.get_ident()
139
print('thread', type(tid) == int, tid != 0, tid != tid_main)
10+
global finished
11+
finished = True
1412

1513
tid_main = _thread.get_ident()
1614
print('main', type(tid_main) == int, tid_main != 0)
1715

16+
finished = False
1817
_thread.start_new_thread(thread_entry, ())
1918

20-
time.sleep(0.2)
19+
while not finished:
20+
pass
2121
print('done')

tests/thread/thread_shared1.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
#
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

5-
try:
6-
import utime as time
7-
except ImportError:
8-
import time
95
import _thread
106

117
def foo(i):
@@ -14,11 +10,22 @@ def foo(i):
1410
def thread_entry(n, tup):
1511
for i in tup:
1612
foo(i)
13+
with lock:
14+
global n_finished
15+
n_finished += 1
1716

17+
lock = _thread.allocate_lock()
18+
n_thread = 2
19+
n_finished = 0
20+
21+
# the shared data structure
1822
tup = (1, 2, 3, 4)
19-
_thread.start_new_thread(thread_entry, (100, tup))
20-
_thread.start_new_thread(thread_entry, (100, tup))
2123

22-
# wait for threads to finish
23-
time.sleep(0.2)
24+
# spawn threads
25+
for i in range(n_thread):
26+
_thread.start_new_thread(thread_entry, (100, tup))
27+
28+
# busy wait for threads to finish
29+
while n_finished < n_thread:
30+
pass
2431
print(tup)

tests/thread/thread_shared2.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
#
44
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
55

6-
try:
7-
import utime as time
8-
except ImportError:
9-
import time
106
import _thread
117

128
def foo(lst, i):
@@ -15,11 +11,22 @@ def foo(lst, i):
1511
def thread_entry(n, lst, idx):
1612
for i in range(n):
1713
foo(lst, idx)
14+
with lock:
15+
global n_finished
16+
n_finished += 1
1817

18+
lock = _thread.allocate_lock()
19+
n_thread = 2
20+
n_finished = 0
21+
22+
# the shared data structure
1923
lst = [0, 0]
20-
_thread.start_new_thread(thread_entry, (10, lst, 0))
21-
_thread.start_new_thread(thread_entry, (20, lst, 1))
2224

23-
# wait for threads to finish
24-
time.sleep(0.2)
25+
# spawn threads
26+
for i in range(n_thread):
27+
_thread.start_new_thread(thread_entry, ((i + 1) * 10, lst, i))
28+
29+
# busy wait for threads to finish
30+
while n_finished < n_thread:
31+
pass
2532
print(lst)

tests/thread/thread_stacksize1.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import sys
6-
try:
7-
import utime as time
8-
except ImportError:
9-
import time
106
import _thread
117

128
# different implementations have different minimum sizes
@@ -20,17 +16,26 @@ def foo():
2016

2117
def thread_entry():
2218
foo()
19+
with lock:
20+
global n_finished
21+
n_finished += 1
2322

2423
# test set/get of stack size
2524
print(_thread.stack_size())
2625
print(_thread.stack_size(sz))
2726
print(_thread.stack_size() == sz)
2827
print(_thread.stack_size())
2928

29+
lock = _thread.allocate_lock()
30+
n_thread = 2
31+
n_finished = 0
32+
3033
# set stack size and spawn a few threads
3134
_thread.stack_size(sz)
32-
for i in range(2):
35+
for i in range(n_thread):
3336
_thread.start_new_thread(thread_entry, ())
3437

35-
time.sleep(0.2)
38+
# busy wait for threads to finish
39+
while n_finished < n_thread:
40+
pass
3641
print('done')

tests/thread/thread_stress_recurse.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
#
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

5-
try:
6-
import utime as time
7-
except ImportError:
8-
import time
95
import _thread
106

117
def foo():
@@ -16,8 +12,14 @@ def thread_entry():
1612
foo()
1713
except RuntimeError:
1814
print('RuntimeError')
15+
global finished
16+
finished = True
17+
18+
finished = False
1919

2020
_thread.start_new_thread(thread_entry, ())
2121

22-
time.sleep(0.2)
22+
# busy wait for thread to finish
23+
while not finished:
24+
pass
2325
print('done')

0 commit comments

Comments
 (0)