summaryrefslogtreecommitdiffstatshomepage
path: root/unix/mpthreadport.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-05-30 16:56:51 +0100
committerDamien George <damien.p.george@gmail.com>2016-06-28 11:28:51 +0100
commitdf95f52583e0f5e3f2a74f7461bb00e2f24b3079 (patch)
treebcde53b237e60eeba2b0fd5e1e21e023d3c614fb /unix/mpthreadport.c
parenteef4f13a3390dc88902563acb047f0439eff0caf (diff)
downloadmicropython-df95f52583e0f5e3f2a74f7461bb00e2f24b3079.tar.gz
micropython-df95f52583e0f5e3f2a74f7461bb00e2f24b3079.zip
py/modthread: Allow to properly set the stack limit of a thread.
We rely on the port setting and adjusting the stack size so there is enough room to recover from hitting the stack limit.
Diffstat (limited to 'unix/mpthreadport.c')
-rw-r--r--unix/mpthreadport.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/unix/mpthreadport.c b/unix/mpthreadport.c
index 336db3aa92..e5cfe7a669 100644
--- a/unix/mpthreadport.c
+++ b/unix/mpthreadport.c
@@ -133,10 +133,12 @@ void mp_thread_start(void) {
pthread_mutex_unlock(&thread_mutex);
}
-void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
- // default stack size is 8k machine-words
- if (stack_size == 0) {
- stack_size = 8192 * BYTES_PER_WORD;
+void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
+ // default stack size is 8k machine-words, minimum is 2k
+ if (*stack_size == 0) {
+ *stack_size = 8192 * BYTES_PER_WORD;
+ } else if (*stack_size < 2048 * BYTES_PER_WORD) {
+ *stack_size = 2048 * BYTES_PER_WORD;
}
// set thread attributes
@@ -145,7 +147,7 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
if (ret != 0) {
goto er;
}
- ret = pthread_attr_setstacksize(&attr, stack_size);
+ ret = pthread_attr_setstacksize(&attr, *stack_size);
if (ret != 0) {
goto er;
}
@@ -160,6 +162,9 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
goto er;
}
+ // adjust stack_size to provide room to recover from hitting the limit
+ *stack_size -= 1024 * BYTES_PER_WORD;
+
// add thread to linked list of all threads
thread_t *th = malloc(sizeof(thread_t));
th->id = id;