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.
This commit is contained in:
Damien George 2016-04-21 12:38:22 +00:00
parent 2d5ea38b49
commit 3545ef8bb4
6 changed files with 65 additions and 38 deletions

View File

@ -2,10 +2,6 @@
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
try:
import utime as time
except ImportError:
import time
import _thread
def foo():
@ -16,9 +12,19 @@ def thread_entry():
foo()
except ValueError:
pass
with lock:
global n_finished
n_finished += 1
for i in range(4):
lock = _thread.allocate_lock()
n_thread = 4
n_finished = 0
# spawn threads
for i in range(n_thread):
_thread.start_new_thread(thread_entry, ())
time.sleep(0.2)
# busy wait for threads to finish
while n_finished < n_thread:
pass
print('done')

View File

@ -2,20 +2,20 @@
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
try:
import utime as time
except ImportError:
import time
import _thread
def thread_entry():
tid = _thread.get_ident()
print('thread', type(tid) == int, tid != 0, tid != tid_main)
global finished
finished = True
tid_main = _thread.get_ident()
print('main', type(tid_main) == int, tid_main != 0)
finished = False
_thread.start_new_thread(thread_entry, ())
time.sleep(0.2)
while not finished:
pass
print('done')

View File

@ -2,10 +2,6 @@
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
try:
import utime as time
except ImportError:
import time
import _thread
def foo(i):
@ -14,11 +10,22 @@ def foo(i):
def thread_entry(n, tup):
for i in tup:
foo(i)
with lock:
global n_finished
n_finished += 1
lock = _thread.allocate_lock()
n_thread = 2
n_finished = 0
# the shared data structure
tup = (1, 2, 3, 4)
_thread.start_new_thread(thread_entry, (100, tup))
_thread.start_new_thread(thread_entry, (100, tup))
# wait for threads to finish
time.sleep(0.2)
# spawn threads
for i in range(n_thread):
_thread.start_new_thread(thread_entry, (100, tup))
# busy wait for threads to finish
while n_finished < n_thread:
pass
print(tup)

View File

@ -3,10 +3,6 @@
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
try:
import utime as time
except ImportError:
import time
import _thread
def foo(lst, i):
@ -15,11 +11,22 @@ def foo(lst, i):
def thread_entry(n, lst, idx):
for i in range(n):
foo(lst, idx)
with lock:
global n_finished
n_finished += 1
lock = _thread.allocate_lock()
n_thread = 2
n_finished = 0
# the shared data structure
lst = [0, 0]
_thread.start_new_thread(thread_entry, (10, lst, 0))
_thread.start_new_thread(thread_entry, (20, lst, 1))
# wait for threads to finish
time.sleep(0.2)
# spawn threads
for i in range(n_thread):
_thread.start_new_thread(thread_entry, ((i + 1) * 10, lst, i))
# busy wait for threads to finish
while n_finished < n_thread:
pass
print(lst)

View File

@ -3,10 +3,6 @@
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
import sys
try:
import utime as time
except ImportError:
import time
import _thread
# different implementations have different minimum sizes
@ -20,6 +16,9 @@ def foo():
def thread_entry():
foo()
with lock:
global n_finished
n_finished += 1
# test set/get of stack size
print(_thread.stack_size())
@ -27,10 +26,16 @@ print(_thread.stack_size(sz))
print(_thread.stack_size() == sz)
print(_thread.stack_size())
lock = _thread.allocate_lock()
n_thread = 2
n_finished = 0
# set stack size and spawn a few threads
_thread.stack_size(sz)
for i in range(2):
for i in range(n_thread):
_thread.start_new_thread(thread_entry, ())
time.sleep(0.2)
# busy wait for threads to finish
while n_finished < n_thread:
pass
print('done')

View File

@ -2,10 +2,6 @@
#
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
try:
import utime as time
except ImportError:
import time
import _thread
def foo():
@ -16,8 +12,14 @@ def thread_entry():
foo()
except RuntimeError:
print('RuntimeError')
global finished
finished = True
finished = False
_thread.start_new_thread(thread_entry, ())
time.sleep(0.2)
# busy wait for thread to finish
while not finished:
pass
print('done')