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 force the use of the
bytecode emitter for the busy functions.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2025-07-13 22:49:15 +10:00
parent b15065b95e
commit b070765427
16 changed files with 34 additions and 15 deletions

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
# the shared bytearray # the shared bytearray
@@ -36,7 +37,7 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
# check bytearray has correct contents # check bytearray has correct contents
print(len(ba)) print(len(ba))

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
# the shared dict # the shared dict
@@ -38,7 +39,7 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
# check dict has correct contents # check dict has correct contents
print(sorted(di.items())) print(sorted(di.items()))

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
@@ -40,7 +41,7 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
# check user instance has correct contents # check user instance has correct contents
print(user.a, user.b, user.c) print(user.a, user.b, user.c)

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
# the shared list # the shared list
@@ -39,7 +40,7 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
# check list has correct contents # check list has correct contents
li.sort() li.sort()

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
# the shared set # the shared set
@@ -33,7 +34,7 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
# check set has correct contents # check set has correct contents
print(sorted(se)) print(sorted(se))

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
@@ -24,5 +25,5 @@ _thread.start_new_thread(thread_entry, ())
# busy wait for thread to finish # busy wait for thread to finish
while not finished: while not finished:
pass time.sleep(0)
print("done") print("done")

View File

@@ -27,6 +27,8 @@ def task(x):
n += 1 n += 1
# This function must always use the bytecode emitter so it bounces the GIL when running.
@micropython.bytecode
def thread(): def thread():
while thread_run: while thread_run:
try: try:
@@ -46,7 +48,7 @@ for i in range(8):
# Wait up to 10 seconds for 10000 tasks to be scheduled. # Wait up to 10 seconds for 10000 tasks to be scheduled.
t = time.ticks_ms() t = time.ticks_ms()
while n < _NUM_TASKS and time.ticks_diff(time.ticks_ms(), t) < _TIMEOUT_MS: while n < _NUM_TASKS and time.ticks_diff(time.ticks_ms(), t) < _TIMEOUT_MS:
pass time.sleep(0)
# Stop all threads. # Stop all threads.
thread_run = False thread_run = False

View File

@@ -7,6 +7,7 @@
import _thread import _thread
import sys import sys
from time import ticks_ms, ticks_diff, sleep_ms from time import ticks_ms, ticks_diff, sleep_ms
import micropython
done = False done = False
@@ -21,6 +22,8 @@ if sys.platform in ("win32", "linux", "darwin"):
MAX_DELTA = 100 MAX_DELTA = 100
# This function must always use the bytecode emitter so the VM can bounce the GIL when running.
@micropython.bytecode
def busy_thread(): def busy_thread():
while not done: while not done:
pass pass

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
@@ -34,5 +35,5 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
print("done") print("done")

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import gc import gc
import _thread import _thread
@@ -44,6 +45,6 @@ n_thread += 1
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
print(n_correct == n_finished) print(n_correct == n_finished)

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
@@ -27,6 +28,6 @@ print("main", type(tid_main) == int, tid_main != 0)
new_tid = _thread.start_new_thread(thread_entry, ()) new_tid = _thread.start_new_thread(thread_entry, ())
while not finished: while not finished:
pass time.sleep(0)
print("done", type(new_tid) == int, new_tid == tid) print("done", type(new_tid) == int, new_tid == tid)

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
lock = _thread.allocate_lock() lock = _thread.allocate_lock()
@@ -26,4 +27,4 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)

View File

@@ -2,6 +2,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
@@ -40,5 +41,5 @@ n_thread += 1
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
print(tup) print(tup)

View File

@@ -3,6 +3,7 @@
# #
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import time
import _thread import _thread
@@ -31,5 +32,5 @@ for i in range(n_thread):
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
print(lst) print(lst)

View File

@@ -3,6 +3,7 @@
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import sys import sys
import time
import _thread import _thread
# different implementations have different minimum sizes # different implementations have different minimum sizes
@@ -51,5 +52,5 @@ _thread.stack_size()
# busy wait for threads to finish # busy wait for threads to finish
while n_finished < n_thread: while n_finished < n_thread:
pass time.sleep(0)
print("done") print("done")

View File

@@ -5,6 +5,7 @@
# This is a regression test for https://github.com/micropython/micropython/issues/15230 # This is a regression test for https://github.com/micropython/micropython/issues/15230
# on rp2, but doubles as a general property to test across all ports. # on rp2, but doubles as a general property to test across all ports.
import sys import sys
import time
import _thread import _thread
try: try:
@@ -38,7 +39,7 @@ StdinWaiter().wait_stdin(1000)
# have run yet. The actual delay is <20ms but spinning here instead of # have run yet. The actual delay is <20ms but spinning here instead of
# sleep(0.1) means the test can run on MP builds without float support. # sleep(0.1) means the test can run on MP builds without float support.
while not thread_waiter.is_done(): while not thread_waiter.is_done():
pass time.sleep(0)
# The background thread should have completed its wait by now. # The background thread should have completed its wait by now.
print(thread_waiter.is_done()) print(thread_waiter.is_done())