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