py/gc: Make memory manager and garbage collector thread safe.
By using a single, global mutex, all memory-related functions (alloc,
free, realloc, collect, etc) are made thread safe. This means that only
one thread can be in such a function at any one time.
diff --git a/py/mpstate.h b/py/mpstate.h
index 18bec09..f1f8fd9 100644
--- a/py/mpstate.h
+++ b/py/mpstate.h
@@ -29,6 +29,7 @@
#include <stdint.h>
#include "py/mpconfig.h"
+#include "py/mpthread.h"
#include "py/misc.h"
#include "py/nlr.h"
#include "py/obj.h"
@@ -80,6 +81,11 @@
#if MICROPY_PY_GC_COLLECT_RETVAL
size_t gc_collected;
#endif
+
+ #if MICROPY_PY_THREAD
+ // This is a global mutex used to make the GC thread-safe.
+ mp_thread_mutex_t gc_mutex;
+ #endif
} mp_state_mem_t;
// This structure hold runtime and VM information. It includes a section