Skip to content

Commit 27cc077

Browse files
committed
py: Add basic _thread module, with ability to start a new thread.
1 parent 330165a commit 27cc077

File tree

9 files changed

+286
-0
lines changed

9 files changed

+286
-0
lines changed

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ extern const mp_obj_module_t mp_module_micropython;
9494
extern const mp_obj_module_t mp_module_ustruct;
9595
extern const mp_obj_module_t mp_module_sys;
9696
extern const mp_obj_module_t mp_module_gc;
97+
extern const mp_obj_module_t mp_module_thread;
9798

9899
extern const mp_obj_dict_t mp_module_builtins_globals;
99100

py/modthread.c

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "py/runtime.h"
31+
#include "py/stackctrl.h"
32+
33+
#if MICROPY_PY_THREAD
34+
35+
#include "py/mpthread.h"
36+
37+
#if 0 // print debugging info
38+
#define DEBUG_PRINT (1)
39+
#define DEBUG_printf DEBUG_printf
40+
#else // don't print debugging info
41+
#define DEBUG_PRINT (0)
42+
#define DEBUG_printf(...) (void)0
43+
#endif
44+
45+
/****************************************************************/
46+
// _thread module
47+
48+
STATIC mp_obj_t mod_thread_get_ident(void) {
49+
return mp_obj_new_int_from_uint((uintptr_t)mp_thread_get_state());
50+
}
51+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_get_ident_obj, mod_thread_get_ident);
52+
53+
typedef struct _thread_entry_args_t {
54+
mp_obj_t fun;
55+
size_t n_args;
56+
size_t n_kw;
57+
const mp_obj_t *args;
58+
} thread_entry_args_t;
59+
60+
STATIC void *thread_entry(void *args_in) {
61+
thread_entry_args_t *args = (thread_entry_args_t*)args_in;
62+
63+
mp_state_thread_t ts;
64+
mp_thread_set_state(&ts);
65+
66+
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
67+
mp_stack_set_limit(16 * 1024); // fixed stack limit for now
68+
69+
// TODO set more thread-specific state here:
70+
// mp_pending_exception? (root pointer)
71+
// cur_exception (root pointer)
72+
// dict_locals? (root pointer) uPy doesn't make a new locals dict for functions, just for classes, so it's different to CPy
73+
74+
DEBUG_printf("[thread] start ts=%p args=%p stack=%p\n", &ts, &args, MP_STATE_THREAD(stack_top));
75+
76+
nlr_buf_t nlr;
77+
if (nlr_push(&nlr) == 0) {
78+
mp_call_function_n_kw(args->fun, args->n_args, args->n_kw, args->args);
79+
nlr_pop();
80+
} else {
81+
// uncaught exception
82+
// check for SystemExit
83+
if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) {
84+
// swallow exception silently
85+
} else {
86+
// print exception out
87+
mp_printf(&mp_plat_print, "Unhandled exception in thread started by ");
88+
mp_obj_print_helper(&mp_plat_print, args->fun, PRINT_REPR);
89+
mp_printf(&mp_plat_print, "\n");
90+
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
91+
}
92+
}
93+
94+
DEBUG_printf("[thread] finish ts=%p\n", &ts);
95+
96+
return NULL;
97+
}
98+
99+
STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args) {
100+
mp_uint_t pos_args_len;
101+
mp_obj_t *pos_args_items;
102+
mp_obj_get_array(args[1], &pos_args_len, &pos_args_items);
103+
thread_entry_args_t *th_args = m_new_obj(thread_entry_args_t);
104+
th_args->fun = args[0];
105+
if (n_args == 2) {
106+
// just position arguments
107+
th_args->n_args = pos_args_len;
108+
th_args->n_kw = 0;
109+
th_args->args = pos_args_items;
110+
} else {
111+
// positional and keyword arguments
112+
if (mp_obj_get_type(args[2]) != &mp_type_dict) {
113+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "expecting a dict for keyword args"));
114+
}
115+
mp_map_t *map = &((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[2]))->map;
116+
th_args->n_args = pos_args_len;
117+
th_args->n_kw = map->used;
118+
mp_obj_t *all_args = m_new(mp_obj_t, th_args->n_args + 2 * th_args->n_kw);
119+
memcpy(all_args, pos_args_items, pos_args_len * sizeof(mp_obj_t));
120+
for (size_t i = 0, n = pos_args_len; i < map->alloc; ++i) {
121+
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
122+
all_args[n++] = map->table[i].key;
123+
all_args[n++] = map->table[i].value;
124+
}
125+
}
126+
th_args->args = all_args;
127+
}
128+
// TODO implement setting thread stack size
129+
mp_thread_create(thread_entry, th_args);
130+
return mp_const_none;
131+
}
132+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread);
133+
134+
STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = {
135+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__thread) },
136+
{ MP_ROM_QSTR(MP_QSTR_get_ident), MP_ROM_PTR(&mod_thread_get_ident_obj) },
137+
{ MP_ROM_QSTR(MP_QSTR_start_new_thread), MP_ROM_PTR(&mod_thread_start_new_thread_obj) },
138+
};
139+
140+
STATIC MP_DEFINE_CONST_DICT(mp_module_thread_globals, mp_module_thread_globals_table);
141+
142+
const mp_obj_module_t mp_module_thread = {
143+
.base = { &mp_type_module },
144+
.name = MP_QSTR__thread,
145+
.globals = (mp_obj_dict_t*)&mp_module_thread_globals,
146+
};
147+
148+
#endif // MICROPY_PY_THREAD

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,11 @@ typedef double mp_float_t;
824824
#define MICROPY_PY_UERRNO (0)
825825
#endif
826826

827+
// Whether to provide "_thread" module
828+
#ifndef MICROPY_PY_THREAD
829+
#define MICROPY_PY_THREAD (0)
830+
#endif
831+
827832
// Extended modules
828833

829834
#ifndef MICROPY_PY_UCTYPES

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ extern mp_state_ctx_t mp_state_ctx;
201201
#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
202202
#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
203203

204+
#if MICROPY_PY_THREAD
205+
extern mp_state_thread_t *mp_thread_get_state(void);
206+
#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
207+
#else
204208
#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
209+
#endif
205210

206211
#endif // __MICROPY_INCLUDED_PY_MPSTATE_H__

py/mpthread.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef __MICROPY_INCLUDED_PY_MPTHREAD_H__
27+
#define __MICROPY_INCLUDED_PY_MPTHREAD_H__
28+
29+
#include "py/mpconfig.h"
30+
31+
#if MICROPY_PY_THREAD
32+
33+
#ifdef MICROPY_MPTHREADPORT_H
34+
#include MICROPY_MPTHREADPORT_H
35+
#else
36+
#include <mpthreadport.h>
37+
#endif
38+
39+
mp_state_thread_t *mp_thread_get_state(void);
40+
void mp_thread_set_state(void *state);
41+
void mp_thread_create(void *(*entry)(void*), void *arg);
42+
43+
#endif // MICROPY_PY_THREAD
44+
45+
#endif // __MICROPY_INCLUDED_PY_MPTHREAD_H__

py/nlrx64.S

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#define NLR_TOP (mp_state_ctx + NLR_TOP_OFFSET)
4242
#endif
4343

44+
// offset of nlr_top within mp_state_thread_t structure
45+
#define NLR_TOP_TH_OFF (0)
46+
4447
#if defined(_WIN32) || defined(__CYGWIN__)
4548
#define NLR_OS_WINDOWS
4649
#endif
@@ -77,9 +80,20 @@ _nlr_push:
7780
movq %r13, 56(%rdi) # store %r13 into nlr_buf
7881
movq %r14, 64(%rdi) # store %r14 into nlr_buf
7982
movq %r15, 72(%rdi) # store %r15 into nlr_buf
83+
84+
#if !MICROPY_PY_THREAD
8085
movq NLR_TOP(%rip), %rax # get last nlr_buf
8186
movq %rax, (%rdi) # store it
8287
movq %rdi, NLR_TOP(%rip) # stor new nlr_buf (to make linked list)
88+
#else
89+
movq %rdi, %rbp # since we make a call, must save rdi in rbp
90+
callq mp_thread_get_state # get mp_state_thread ptr into rax
91+
movq NLR_TOP_TH_OFF(%rax), %rsi # get thread.nlr_top (last nlr_buf)
92+
movq %rsi, (%rbp) # store it
93+
movq %rbp, NLR_TOP_TH_OFF(%rax) # store new nlr_buf (to make linked list)
94+
movq 24(%rbp), %rbp # restore rbp
95+
#endif
96+
8397
xorq %rax, %rax # return 0, normal return
8498
ret # return
8599
#if !(defined(__APPLE__) && defined(__MACH__))
@@ -97,9 +111,18 @@ nlr_pop:
97111
.globl _nlr_pop
98112
_nlr_pop:
99113
#endif
114+
115+
#if !MICROPY_PY_THREAD
100116
movq NLR_TOP(%rip), %rax # get nlr_top into %rax
101117
movq (%rax), %rax # load prev nlr_buf
102118
movq %rax, NLR_TOP(%rip) # store prev nlr_buf (to unlink list)
119+
#else
120+
callq mp_thread_get_state # get mp_state_thread ptr into rax
121+
movq NLR_TOP_TH_OFF(%rax), %rdi # get thread.nlr_top (last nlr_buf)
122+
movq (%rdi), %rdi # load prev nlr_buf
123+
movq %rdi, NLR_TOP_TH_OFF(%rax) # store prev nlr_buf (to unlink list)
124+
#endif
125+
103126
ret # return
104127
#if !(defined(__APPLE__) && defined(__MACH__))
105128
.size nlr_pop, .-nlr_pop
@@ -116,13 +139,28 @@ nlr_jump:
116139
.globl _nlr_jump
117140
_nlr_jump:
118141
#endif
142+
143+
#if !MICROPY_PY_THREAD
119144
movq %rdi, %rax # put return value in %rax
120145
movq NLR_TOP(%rip), %rdi # get nlr_top into %rdi
121146
test %rdi, %rdi # check for nlr_top being NULL
122147
je .fail # fail if nlr_top is NULL
123148
movq %rax, 8(%rdi) # store return value
124149
movq (%rdi), %rax # load prev nlr_buf
125150
movq %rax, NLR_TOP(%rip) # store prev nlr_buf (to unlink list)
151+
#else
152+
movq %rdi, %rbp # put return value in rbp
153+
callq mp_thread_get_state # get thread ptr in rax
154+
movq %rax, %rsi # put thread ptr in rsi
155+
movq %rbp, %rax # put return value to rax (for je .fail)
156+
movq NLR_TOP_TH_OFF(%rsi), %rdi # get thread.nlr_top in rdi
157+
test %rdi, %rdi # check for nlr_top being NULL
158+
je .fail # fail if nlr_top is NULL
159+
movq %rax, 8(%rdi) # store return value
160+
movq (%rdi), %rax # load prev nlr_buf
161+
movq %rax, NLR_TOP_TH_OFF(%rsi) # store prev nlr_buf (to unlink list)
162+
#endif
163+
126164
movq 72(%rdi), %r15 # load saved %r15
127165
movq 64(%rdi), %r14 # load saved %r14
128166
movq 56(%rdi), %r13 # load saved %r13

py/nlrx86.S

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
#define NLR_TOP (mp_state_ctx + NLR_TOP_OFFSET)
4343
#endif
4444

45+
// offset of nlr_top within mp_state_thread_t structure
46+
#define NLR_TOP_TH_OFF (0)
47+
4548
.file "nlr.s"
4649
.text
4750

@@ -65,9 +68,20 @@ nlr_push:
6568
mov %ebx, 20(%edx) # store %bx into nlr_buf+20
6669
mov %edi, 24(%edx) # store %di into nlr_buf
6770
mov %esi, 28(%edx) # store %si into nlr_buf
71+
72+
#if !MICROPY_PY_THREAD
6873
mov NLR_TOP, %eax # load nlr_top
6974
mov %eax, (%edx) # store it
7075
mov %edx, NLR_TOP # stor new nlr_buf (to make linked list)
76+
#else
77+
// to check: stack is aligned to 16-byte boundary before this call
78+
call mp_thread_get_state # get mp_state_thread ptr into eax
79+
mov 4(%esp), %edx # load nlr_buf argument into edx (edx clobbered by call)
80+
mov NLR_TOP_TH_OFF(%eax), %ecx # get thread.nlr_top (last nlr_buf)
81+
mov %ecx, (%edx) # store it
82+
mov %edx, NLR_TOP_TH_OFF(%eax) # store new nlr_buf (to make linked list)
83+
#endif
84+
7185
xor %eax, %eax # return 0, normal return
7286
ret # return
7387
#if !defined(NLR_OS_WINDOWS)
@@ -86,9 +100,18 @@ _nlr_pop:
86100
.type nlr_pop, @function
87101
nlr_pop:
88102
#endif
103+
104+
#if !MICROPY_PY_THREAD
89105
mov NLR_TOP, %eax # load nlr_top
90106
mov (%eax), %eax # load prev nlr_buf
91107
mov %eax, NLR_TOP # store nlr_top (to unlink list)
108+
#else
109+
call mp_thread_get_state # get mp_state_thread ptr into eax
110+
mov NLR_TOP_TH_OFF(%eax), %ecx # get thread.nlr_top (last nlr_buf)
111+
mov (%ecx), %ecx # load prev nlr_buf
112+
mov %ecx, NLR_TOP_TH_OFF(%eax) # store prev nlr_buf (to unlink list)
113+
#endif
114+
92115
ret # return
93116
#if !defined(NLR_OS_WINDOWS)
94117
.size nlr_pop, .-nlr_pop
@@ -106,6 +129,8 @@ _nlr_jump:
106129
.type nlr_jump, @function
107130
nlr_jump:
108131
#endif
132+
133+
#if !MICROPY_PY_THREAD
109134
mov NLR_TOP, %edx # load nlr_top
110135
test %edx, %edx # check for nlr_top being NULL
111136
#if defined(NLR_OS_WINDOWS)
@@ -117,6 +142,21 @@ nlr_jump:
117142
mov %eax, 4(%edx) # store return value
118143
mov (%edx), %eax # load prev nlr_top
119144
mov %eax, NLR_TOP # store nlr_top (to unlink list)
145+
#else
146+
call mp_thread_get_state # get mp_state_thread ptr into eax
147+
mov NLR_TOP_TH_OFF(%eax), %edx # get thread.nlr_top (last nlr_buf)
148+
test %edx, %edx # check for nlr_top being NULL
149+
#if defined(NLR_OS_WINDOWS)
150+
je _nlr_jump_fail # fail if nlr_top is NULL
151+
#else
152+
je nlr_jump_fail # fail if nlr_top is NULL
153+
#endif
154+
mov 4(%esp), %ecx # load return value
155+
mov %ecx, 4(%edx) # store return value
156+
mov (%edx), %ecx # load prev nlr_top
157+
mov %ecx, NLR_TOP_TH_OFF(%eax) # store nlr_top (to unlink list)
158+
#endif
159+
120160
mov 28(%edx), %esi # load saved %si
121161
mov 24(%edx), %edi # load saved %di
122162
mov 20(%edx), %ebx # load saved %bx

py/objmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
160160
#if MICROPY_PY_GC && MICROPY_ENABLE_GC
161161
{ MP_ROM_QSTR(MP_QSTR_gc), MP_ROM_PTR(&mp_module_gc) },
162162
#endif
163+
#if MICROPY_PY_THREAD
164+
{ MP_ROM_QSTR(MP_QSTR__thread), MP_ROM_PTR(&mp_module_thread) },
165+
#endif
163166

164167
// extmod modules
165168

0 commit comments

Comments
 (0)