Skip to content

Commit 4cec63a

Browse files
committed
py: Implement a simple global interpreter lock.
This makes the VM/runtime thread safe, at the cost of not being able to run code in parallel.
1 parent 1f54ad2 commit 4cec63a

File tree

6 files changed

+35
-0
lines changed

6 files changed

+35
-0
lines changed

py/modthread.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ typedef struct _thread_entry_args_t {
146146
} thread_entry_args_t;
147147

148148
STATIC void *thread_entry(void *args_in) {
149+
// Execution begins here for a new thread. We do not have the GIL.
150+
149151
thread_entry_args_t *args = (thread_entry_args_t*)args_in;
150152

151153
mp_state_thread_t ts;
@@ -154,6 +156,8 @@ STATIC void *thread_entry(void *args_in) {
154156
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
155157
mp_stack_set_limit(16 * 1024); // fixed stack limit for now
156158

159+
MP_THREAD_GIL_ENTER();
160+
157161
// signal that we are set up and running
158162
mp_thread_start();
159163

@@ -188,6 +192,8 @@ STATIC void *thread_entry(void *args_in) {
188192
// signal that we are finished
189193
mp_thread_finish();
190194

195+
MP_THREAD_GIL_EXIT();
196+
191197
return NULL;
192198
}
193199

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,12 @@ typedef double mp_float_t;
829829
#define MICROPY_PY_THREAD (0)
830830
#endif
831831

832+
// Whether to make the VM/runtime thread-safe using a global lock
833+
// If not enabled then thread safety must be provided at the Python level
834+
#ifndef MICROPY_PY_THREAD_GIL
835+
#define MICROPY_PY_THREAD_GIL (MICROPY_PY_THREAD)
836+
#endif
837+
832838
// Extended modules
833839

834840
#ifndef MICROPY_PY_UCTYPES

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ typedef struct _mp_state_vm_t {
175175
#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
176176
mp_int_t mp_emergency_exception_buf_size;
177177
#endif
178+
179+
#if MICROPY_PY_THREAD_GIL
180+
// This is a global mutex used to make the VM/runtime thread-safe.
181+
mp_thread_mutex_t gil_mutex;
182+
#endif
178183
} mp_state_vm_t;
179184

180185
// This structure holds state that is specific to a given thread.

py/mpthread.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
struct _mp_state_thread_t;
4040

41+
#if MICROPY_PY_THREAD_GIL
42+
#define MP_THREAD_GIL_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(gil_mutex), 1)
43+
#define MP_THREAD_GIL_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(gil_mutex))
44+
#else
45+
#define MP_THREAD_GIL_ENTER()
46+
#define MP_THREAD_GIL_EXIT()
47+
#endif
48+
4149
struct _mp_state_thread_t *mp_thread_get_state(void);
4250
void mp_thread_set_state(void *state);
4351
void mp_thread_create(void *(*entry)(void*), void *arg, size_t stack_size);

py/runtime.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ void mp_init(void) {
9191
// start with no extensions to builtins
9292
MP_STATE_VM(mp_module_builtins_override_dict) = NULL;
9393
#endif
94+
95+
#if MICROPY_PY_THREAD_GIL
96+
mp_thread_mutex_init(&MP_STATE_VM(gil_mutex));
97+
#endif
98+
99+
MP_THREAD_GIL_ENTER();
94100
}
95101

96102
void mp_deinit(void) {

py/vm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,10 @@ unwind_jump:;
12631263
RAISE(obj);
12641264
}
12651265

1266+
// TODO make GIL release more efficient
1267+
MP_THREAD_GIL_EXIT();
1268+
MP_THREAD_GIL_ENTER();
1269+
12661270
} // for loop
12671271

12681272
} else {

0 commit comments

Comments
 (0)