Skip to content

Commit df95f52

Browse files
committed
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.
1 parent eef4f13 commit df95f52

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

cc3200/mpthreadport.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,37 @@ STATIC void freertos_entry(void *arg) {
107107
}
108108
}
109109

110-
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
110+
void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
111111
// store thread entry function into a global variable so we can access it
112112
ext_thread_entry = entry;
113113

114-
if (stack_size == 0) {
115-
stack_size = 2048; // default stack size
114+
if (*stack_size == 0) {
115+
*stack_size = 4096; // default stack size
116+
} else if (*stack_size < 2048) {
117+
*stack_size = 2048; // minimum stack size
116118
}
117119

118120
mp_thread_mutex_lock(&thread_mutex, 1);
119121

120122
// create thread
121-
StackType_t *stack = m_new(StackType_t, stack_size / sizeof(StackType_t));
123+
StackType_t *stack = m_new(StackType_t, *stack_size / sizeof(StackType_t));
122124
StaticTask_t *task_buf = m_new(StaticTask_t, 1);
123-
TaskHandle_t id = xTaskCreateStatic(freertos_entry, "Thread", stack_size / sizeof(void*), arg, 2, stack, task_buf);
125+
TaskHandle_t id = xTaskCreateStatic(freertos_entry, "Thread", *stack_size / sizeof(void*), arg, 2, stack, task_buf);
124126
if (id == NULL) {
125127
mp_thread_mutex_unlock(&thread_mutex);
126128
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, "can't create thread"));
127129
}
128130

131+
// adjust stack_size to provide room to recover from hitting the limit
132+
*stack_size -= 512;
133+
129134
// add thread to linked list of all threads
130135
thread_t *th = m_new_obj(thread_t);
131136
th->id = id;
132137
th->ready = 0;
133138
th->arg = arg;
134139
th->stack = stack;
135-
th->stack_len = stack_size / sizeof(StackType_t);
140+
th->stack_len = *stack_size / sizeof(StackType_t);
136141
th->next = thread;
137142
thread = th;
138143

py/modthread.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) {
160160
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_stack_size_obj, 0, 1, mod_thread_stack_size);
161161

162162
typedef struct _thread_entry_args_t {
163+
size_t stack_size;
163164
mp_obj_t fun;
164165
size_t n_args;
165166
size_t n_kw;
@@ -175,7 +176,7 @@ STATIC void *thread_entry(void *args_in) {
175176
mp_thread_set_state(&ts);
176177

177178
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
178-
mp_stack_set_limit(16 * 1024); // fixed stack limit for now
179+
mp_stack_set_limit(args->stack_size);
179180

180181
MP_THREAD_GIL_ENTER();
181182

@@ -256,11 +257,14 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
256257
th_args->n_args = pos_args_len;
257258
memcpy(th_args->args, pos_args_items, pos_args_len * sizeof(mp_obj_t));
258259

260+
// set the stack size to use
261+
th_args->stack_size = thread_stack_size;
262+
259263
// set the function for thread entry
260264
th_args->fun = args[0];
261265

262266
// spawn the thread!
263-
mp_thread_create(thread_entry, th_args, thread_stack_size);
267+
mp_thread_create(thread_entry, th_args, &th_args->stack_size);
264268

265269
return mp_const_none;
266270
}

py/mpthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct _mp_state_thread_t;
4040

4141
struct _mp_state_thread_t *mp_thread_get_state(void);
4242
void mp_thread_set_state(void *state);
43-
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size);
43+
void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size);
4444
void mp_thread_start(void);
4545
void mp_thread_finish(void);
4646
void mp_thread_mutex_init(mp_thread_mutex_t *mutex);

unix/mpthreadport.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ void mp_thread_start(void) {
133133
pthread_mutex_unlock(&thread_mutex);
134134
}
135135

136-
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
137-
// default stack size is 8k machine-words
138-
if (stack_size == 0) {
139-
stack_size = 8192 * BYTES_PER_WORD;
136+
void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
137+
// default stack size is 8k machine-words, minimum is 2k
138+
if (*stack_size == 0) {
139+
*stack_size = 8192 * BYTES_PER_WORD;
140+
} else if (*stack_size < 2048 * BYTES_PER_WORD) {
141+
*stack_size = 2048 * BYTES_PER_WORD;
140142
}
141143

142144
// set thread attributes
@@ -145,7 +147,7 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
145147
if (ret != 0) {
146148
goto er;
147149
}
148-
ret = pthread_attr_setstacksize(&attr, stack_size);
150+
ret = pthread_attr_setstacksize(&attr, *stack_size);
149151
if (ret != 0) {
150152
goto er;
151153
}
@@ -160,6 +162,9 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
160162
goto er;
161163
}
162164

165+
// adjust stack_size to provide room to recover from hitting the limit
166+
*stack_size -= 1024 * BYTES_PER_WORD;
167+
163168
// add thread to linked list of all threads
164169
thread_t *th = malloc(sizeof(thread_t));
165170
th->id = id;

0 commit comments

Comments
 (0)