Skip to content

Commit c91dab9

Browse files
committed
adding threading and multiprocessing modules
1 parent a153432 commit c91dab9

29 files changed

+2460
-482
lines changed

builder/esp32.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,14 @@ def read_file(file):
845845
def update_mpthreadport():
846846
data = read_file(MPTHREADPORT_PATH)
847847

848+
'''
849+
#else
850+
851+
void FREERTOS_TASK_DELETE_HOOK(void *tcb) {
852+
}
853+
854+
'''
855+
848856
if '_CORE_ID' not in data:
849857
data = data.replace('MP_TASK_COREID', '_CORE_ID')
850858

@@ -900,6 +908,19 @@ def update_mpconfigboard():
900908
def update_mpconfigport():
901909
data = read_file(MPCONFIGPORT_PATH)
902910

911+
'''
912+
#if MICROPY_PY_THREAD
913+
#define MICROPY_EVENT_POLL_HOOK \
914+
do { \
915+
extern void mp_handle_pending(bool); \
916+
mp_handle_pending(true); \
917+
MICROPY_PY_SOCKET_EVENTS_HANDLER \
918+
MP_THREAD_GIL_EXIT(); \
919+
ulTaskNotifyTake(pdFALSE, 1); \
920+
MP_THREAD_GIL_ENTER(); \
921+
} while (0);
922+
'''
923+
903924
if 'MP_USB_OTG' in data:
904925
data = data.rsplit('\n\n#define MP_USB_OTG', 1)[0]
905926

@@ -997,8 +1018,51 @@ def update_mphalport():
9971018
write_file(MPHALPORT_PATH, data)
9981019

9991020

1021+
def update_gc():
1022+
gc_path = 'lib/micropython/py/gc.c'
1023+
1024+
data = read_file(gc_path)
1025+
1026+
'''
1027+
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
1028+
mp_thread_mutex_init(&MP_STATE_MEM(gc_mutex));
1029+
#endif
1030+
'''
1031+
write_file(gc_path, data)
1032+
1033+
1034+
def update_gccollect():
1035+
1036+
gccollect_path = 'lib/micropython/ports/esp32/gccollect.c'
1037+
1038+
data = read_file(gccollect_path)
1039+
1040+
'''
1041+
#if MICROPY_PY_THREAD
1042+
mp_thread_gc_others();
1043+
#endif
1044+
'''
1045+
write_file(gccollect_path, data)
1046+
1047+
10001048
def update_main():
10011049
data = read_file(MAIN_PATH)
1050+
1051+
1052+
'''
1053+
#if MICROPY_PY_THREAD
1054+
mp_thread_init(pxTaskGetStackStart(NULL), MICROPY_TASK_STACK_SIZE / sizeof(uintptr_t));
1055+
#endif
1056+
1057+
1058+
1059+
#if MICROPY_PY_THREAD
1060+
mp_thread_deinit();
1061+
#endif
1062+
1063+
'''
1064+
1065+
10021066
data = data.replace(
10031067
'#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG',
10041068
'#if MP_USB_SERIAL_JTAG'

ext_mod/multiprocess/inc/common.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "freertos/task.h"
3+
#include "freertos/semphr.h"
4+
#include "freertos/queue.h"
5+
6+
#ifndef __COMMON_H__
7+
8+
#define __COMMON_H__
9+
10+
typedef struct _mp_mutex_t {
11+
SemaphoreHandle_t handle;
12+
StaticSemaphore_t buffer;
13+
} mp_mutex_t;
14+
15+
int lock_acquire(mp_mutex_t *mutex, int wait);
16+
void lock_release(mp_mutex_t *mutex);
17+
void lock_init(mp_mutex_t *mutex);
18+
19+
int rlock_acquire(mp_mutex_t *mutex, int wait);
20+
void rlock_release(mp_mutex_t *mutex);
21+
void rlock_init(mp_mutex_t *mutex);
22+
23+
24+
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
#include "freertos/FreeRTOS.h"
4+
#include "freertos/task.h"
5+
#include "freertos/semphr.h"
6+
#include "freertos/queue.h"
7+
#include "../inc/common.h"
8+
#include "threading.h"
9+
10+
11+
#ifndef __MULTIPROCESS_H__
12+
#define __MULTIPROCESS_H__
13+
14+
typedef struct _mp_obj_process_t {
15+
mp_obj_base_t base;
16+
TaskHandle_t id;
17+
TaskHandle_t **threads;
18+
uint8_t num_threads;
19+
} mp_obj_process_t;
20+
21+
extern mp_obj_process_t *mp_processes[2];
22+
23+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "freertos/task.h"
3+
#include "freertos/semphr.h"
4+
#include "freertos/queue.h"
5+
#include "../inc/common.h"
6+
7+
#ifndef __MULTIPROCESS_LOCK_H__
8+
#define __MULTIPROCESS_LOCK_H__
9+
10+
typedef struct _mp_obj_process_lock_t {
11+
mp_obj_base_t base;
12+
mp_mutex_t mutex;
13+
volatile bool locked;
14+
} mp_obj_process_lock_t;
15+
16+
#endif

ext_mod/multiprocess/inc/multiprocess_process.h

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "freertos/task.h"
3+
#include "freertos/semphr.h"
4+
#include "freertos/queue.h"
5+
#include "../inc/common.h"
6+
7+
#ifndef __MULTIPROCESS_RLOCK_H__
8+
#define __MULTIPROCESS_RLOCK_H__
9+
10+
typedef struct _mp_obj_process_rlock_t {
11+
mp_obj_base_t base;
12+
mp_mutex_t mutex;
13+
volatile bool locked;
14+
volatile int count;
15+
} mp_obj_process_rlock_t;
16+
17+
#endif

0 commit comments

Comments
 (0)