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