2016-04-20 10:23:55 -04:00
|
|
|
# test setting the thread stack size
|
|
|
|
#
|
|
|
|
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
|
2020-06-18 05:19:14 -04:00
|
|
|
try:
|
|
|
|
import usys as sys
|
|
|
|
except ImportError:
|
|
|
|
import sys
|
2016-04-20 10:23:55 -04:00
|
|
|
import _thread
|
|
|
|
|
|
|
|
# different implementations have different minimum sizes
|
2020-03-22 22:26:08 -04:00
|
|
|
if sys.implementation.name == "micropython":
|
2016-04-20 10:23:55 -04:00
|
|
|
sz = 2 * 1024
|
|
|
|
else:
|
2020-05-01 06:28:00 -04:00
|
|
|
sz = 512 * 1024
|
2016-04-20 10:23:55 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2016-04-20 10:23:55 -04:00
|
|
|
def foo():
|
|
|
|
pass
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2016-04-20 10:23:55 -04:00
|
|
|
def thread_entry():
|
|
|
|
foo()
|
2016-04-21 08:38:22 -04:00
|
|
|
with lock:
|
|
|
|
global n_finished
|
|
|
|
n_finished += 1
|
2016-04-20 10:23:55 -04:00
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2016-05-31 06:54:09 -04:00
|
|
|
# reset stack size to default
|
|
|
|
_thread.stack_size()
|
|
|
|
|
2016-04-20 10:23:55 -04:00
|
|
|
# test set/get of stack size
|
|
|
|
print(_thread.stack_size())
|
|
|
|
print(_thread.stack_size(sz))
|
|
|
|
print(_thread.stack_size() == sz)
|
|
|
|
print(_thread.stack_size())
|
|
|
|
|
2016-04-21 08:38:22 -04:00
|
|
|
lock = _thread.allocate_lock()
|
|
|
|
n_thread = 2
|
|
|
|
n_finished = 0
|
|
|
|
|
2016-04-20 10:23:55 -04:00
|
|
|
# set stack size and spawn a few threads
|
|
|
|
_thread.stack_size(sz)
|
2016-04-21 08:38:22 -04:00
|
|
|
for i in range(n_thread):
|
2016-04-20 10:23:55 -04:00
|
|
|
_thread.start_new_thread(thread_entry, ())
|
|
|
|
|
2017-02-01 01:21:35 -05:00
|
|
|
# reset stack size to default (for subsequent scripts on baremetal)
|
|
|
|
_thread.stack_size()
|
|
|
|
|
2016-04-21 08:38:22 -04:00
|
|
|
# busy wait for threads to finish
|
|
|
|
while n_finished < n_thread:
|
|
|
|
pass
|
2020-03-22 22:26:08 -04:00
|
|
|
print("done")
|