Skip to content

Commit 526493b

Browse files
committed
tests/thread: Allow thread tests to pass with the native emitter.
The native emitter will not release/bounce the GIL when running code, so if it runs tight loops then no other threads get a chance to run (if the GIL is enabled). So for the thread tests, explicitly include a call to `time.sleep(0)` (or equivalent) to bounce the GIL and give other threads a chance to run. For some tests (eg `thread_coop.py`) the whole point of the test is to test that the GIL is correctly bounced. So for those cases for the use of the bytecode emitter for the busy functions. Signed-off-by: Damien George <damien@micropython.org>
1 parent 5ce2146 commit 526493b

16 files changed

+45
-26
lines changed

tests/thread/mutate_bytearray.py

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

5+
import time
56
import _thread
67

78
# the shared bytearray
@@ -36,7 +37,7 @@ def th(n, lo, hi):
3637

3738
# busy wait for threads to finish
3839
while n_finished < n_thread:
39-
pass
40+
time.sleep(0)
4041

4142
# check bytearray has correct contents
4243
print(len(ba))

tests/thread/mutate_dict.py

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

5+
import time
56
import _thread
67

78
# the shared dict
@@ -36,9 +37,9 @@ def th(n, lo, hi):
3637
for i in range(n_thread):
3738
_thread.start_new_thread(th, (30, i * 300, (i + 1) * 300))
3839

39-
# busy wait for threads to finish
40+
# wait for threads to finish
4041
while n_finished < n_thread:
41-
pass
42+
time.sleep(0)
4243

4344
# check dict has correct contents
4445
print(sorted(di.items()))

tests/thread/mutate_instance.py

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

5+
import time
56
import _thread
67

78

@@ -38,9 +39,9 @@ def th(n, lo, hi):
3839
for i in range(n_thread):
3940
_thread.start_new_thread(th, (n_repeat, i * n_range, (i + 1) * n_range))
4041

41-
# busy wait for threads to finish
42+
# wait for threads to finish
4243
while n_finished < n_thread:
43-
pass
44+
time.sleep(0)
4445

4546
# check user instance has correct contents
4647
print(user.a, user.b, user.c)

tests/thread/mutate_list.py

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

5+
import time
56
import _thread
67

78
# the shared list
@@ -37,9 +38,9 @@ def th(n, lo, hi):
3738
for i in range(n_thread):
3839
_thread.start_new_thread(th, (4, i * 60, (i + 1) * 60))
3940

40-
# busy wait for threads to finish
41+
# wait for threads to finish
4142
while n_finished < n_thread:
42-
pass
43+
time.sleep(0)
4344

4445
# check list has correct contents
4546
li.sort()

tests/thread/mutate_set.py

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

5+
import time
56
import _thread
67

78
# the shared set
@@ -31,9 +32,9 @@ def th(n, lo, hi):
3132
for i in range(n_thread):
3233
_thread.start_new_thread(th, (50, i * 500, (i + 1) * 500))
3334

34-
# busy wait for threads to finish
35+
# wait for threads to finish
3536
while n_finished < n_thread:
36-
pass
37+
time.sleep(0)
3738

3839
# check set has correct contents
3940
print(sorted(se))

tests/thread/stress_recurse.py

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

5+
import time
56
import _thread
67

78

@@ -22,7 +23,7 @@ def thread_entry():
2223

2324
_thread.start_new_thread(thread_entry, ())
2425

25-
# busy wait for thread to finish
26+
# wait for thread to finish
2627
while not finished:
27-
pass
28+
time.sleep(0)
2829
print("done")

tests/thread/stress_schedule.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def task(x):
2727
n += 1
2828

2929

30+
# This function must always use the bytecode emitter so it bounces the GIL when running.
31+
@micropython.bytecode
3032
def thread():
3133
while thread_run:
3234
try:
@@ -46,7 +48,7 @@ def thread():
4648
# Wait up to 10 seconds for 10000 tasks to be scheduled.
4749
t = time.ticks_ms()
4850
while n < _NUM_TASKS and time.ticks_diff(time.ticks_ms(), t) < _TIMEOUT_MS:
49-
pass
51+
time.sleep(0)
5052

5153
# Stop all threads.
5254
thread_run = False

tests/thread/thread_coop.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import _thread
88
import sys
99
from time import ticks_ms, ticks_diff, sleep_ms
10+
import micropython
1011

1112

1213
done = False
@@ -21,6 +22,8 @@
2122
MAX_DELTA = 100
2223

2324

25+
# This function must always use the bytecode emitter so the VM can bounce the GIL when running.
26+
@micropython.bytecode
2427
def busy_thread():
2528
while not done:
2629
pass

tests/thread/thread_exc1.py

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

5+
import time
56
import _thread
67

78

@@ -32,7 +33,7 @@ def thread_entry():
3233
except OSError:
3334
pass
3435

35-
# busy wait for threads to finish
36+
# wait for threads to finish
3637
while n_finished < n_thread:
37-
pass
38+
time.sleep(0)
3839
print("done")

tests/thread/thread_gc1.py

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

5+
import time
56
import gc
67
import _thread
78

@@ -42,8 +43,8 @@ def thread_entry(n):
4243
thread_entry(10)
4344
n_thread += 1
4445

45-
# busy wait for threads to finish
46+
# wait for threads to finish
4647
while n_finished < n_thread:
47-
pass
48+
time.sleep(0)
4849

4950
print(n_correct == n_finished)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy