Skip to content

Commit 707f98f

Browse files
committed
py/modthread: Add stack_size() function.
1 parent 3eb7a26 commit 707f98f

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

py/modthread.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,24 @@
4545
/****************************************************************/
4646
// _thread module
4747

48+
STATIC size_t thread_stack_size = 0;
49+
4850
STATIC mp_obj_t mod_thread_get_ident(void) {
4951
return mp_obj_new_int_from_uint((uintptr_t)mp_thread_get_state());
5052
}
5153
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_get_ident_obj, mod_thread_get_ident);
5254

55+
STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) {
56+
mp_obj_t ret = mp_obj_new_int_from_uint(thread_stack_size);
57+
if (n_args == 0) {
58+
thread_stack_size = 0;
59+
} else {
60+
thread_stack_size = mp_obj_get_int(args[0]);
61+
}
62+
return ret;
63+
}
64+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_stack_size_obj, 0, 1, mod_thread_stack_size);
65+
5366
typedef struct _thread_entry_args_t {
5467
mp_obj_t fun;
5568
size_t n_args;
@@ -126,15 +139,15 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
126139
}
127140
th_args->args = all_args;
128141
}
129-
// TODO implement setting thread stack size
130-
mp_thread_create(thread_entry, th_args);
142+
mp_thread_create(thread_entry, th_args, thread_stack_size);
131143
return mp_const_none;
132144
}
133145
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread);
134146

135147
STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = {
136148
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__thread) },
137149
{ MP_ROM_QSTR(MP_QSTR_get_ident), MP_ROM_PTR(&mod_thread_get_ident_obj) },
150+
{ MP_ROM_QSTR(MP_QSTR_stack_size), MP_ROM_PTR(&mod_thread_stack_size_obj) },
138151
{ MP_ROM_QSTR(MP_QSTR_start_new_thread), MP_ROM_PTR(&mod_thread_start_new_thread_obj) },
139152
};
140153

py/mpthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
mp_state_thread_t *mp_thread_get_state(void);
4040
void mp_thread_set_state(void *state);
41-
void mp_thread_create(void *(*entry)(void*), void *arg);
41+
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size);
4242

4343
#endif // MICROPY_PY_THREAD
4444

unix/mpthreadport.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,34 @@ void mp_thread_set_state(void *state) {
4646
pthread_setspecific(tls_key, state);
4747
}
4848

49-
void mp_thread_create(void *(*entry)(void*), void *arg) {
49+
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size) {
50+
// default stack size is 8k machine-words
51+
if (stack_size == 0) {
52+
stack_size = 8192 * BYTES_PER_WORD;
53+
}
54+
55+
// set thread attributes
56+
pthread_attr_t attr;
57+
int ret = pthread_attr_init(&attr);
58+
if (ret != 0) {
59+
goto er;
60+
}
61+
ret = pthread_attr_setstacksize(&attr, stack_size);
62+
if (ret != 0) {
63+
goto er;
64+
}
65+
66+
// create thread
5067
pthread_t id;
51-
pthread_create(&id, NULL, entry, arg);
68+
ret = pthread_create(&id, &attr, entry, arg);
69+
if (ret != 0) {
70+
goto er;
71+
}
72+
73+
return;
74+
75+
er:
76+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ret)));
5277
}
5378

5479
#endif // MICROPY_PY_THREAD

0 commit comments

Comments
 (0)