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
|
2022-08-18 02:57:45 -04:00
|
|
|
|
|
|
|
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):
|
2021-05-09 22:44:47 -04:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
_thread.start_new_thread(thread_entry, ())
|
|
|
|
break
|
|
|
|
except OSError:
|
|
|
|
pass
|
2016-04-20 10:23:55 -04:00
|
|
|
|
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")
|