Skip to content

Commit b52ed57

Browse files
committed
Reworking the threading and multiprocessing modules.
1 parent 7e8a981 commit b52ed57

File tree

12 files changed

+712
-79
lines changed

12 files changed

+712
-79
lines changed

ext_mod/threading/common/inc/multiprocessing.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#ifndef __MULTIPROCESSING_H__
33
#define __MULTIPROCESSING_H__
44

5-
uint8_t mp_get_current_process_core(void);
6-
uint8_t mp_get_process_core(thread_t *thread);
7-
uint8_t mp_get_cpu_count(void);
5+
#include "thread_thread.h"
86

9-
extern mp_obj_t *processes;
10-
extern uint8_t process_count;
7+
uint8_t mp_get_current_process_core(void); // needs to be defined in port
8+
uint8_t mp_get_process_core(thread_t *thread); // needs to be defined in port
9+
uint8_t mp_get_cpu_count(void); // needs to be defined in port
10+
void multiprocessing_init(void); // needs to be defined in port
1111

12-
void multiprocessing_init(void);
13-
extern mp_obj_t processes[2];
12+
extern mp_obj_t *processes; // needs to be defined in port
13+
extern uint8_t process_count; // needs to be defined in port
1414

1515
#endif /* __MULTIPROCESSING_H__ */

ext_mod/threading/common/inc/thread_event.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@
66
#ifndef __THREAD_EVENT_H__
77
#define __THREAD_EVENT_H__
88

9-
typedef struct _thread_event_t thread_event_t;
9+
typedef struct _thread_event_t thread_event_t; // needs to be defined in port
1010

1111
typedef struct _mp_obj_thread_event_t {
1212
mp_obj_base_t base;
1313
thread_event_t event;
14-
1514
bool is_set;
16-
1715
} mp_obj_thread_event_t;
1816

19-
20-
void threading_event_set(thread_event_t *event);
21-
bool threading_event_isset(thread_event_t *event);
22-
void threading_event_clear(thread_event_t *event);
23-
void threading_event_wait(thread_event_t *event, int32_t wait_ms);
24-
void threading_event_init(thread_event_t *event);
25-
void threading_event_delete(thread_event_t *event);
17+
void threading_event_set(thread_event_t *event); // needs to be defined in port
18+
bool threading_event_isset(thread_event_t *event); // needs to be defined in port
19+
void threading_event_clear(thread_event_t *event); // needs to be defined in port
20+
void threading_event_wait(thread_event_t *event, int32_t wait_ms); // needs to be defined in port
21+
void threading_event_init(thread_event_t *event); // needs to be defined in port
22+
void threading_event_delete(thread_event_t *event); // needs to be defined in port
2623

2724
#endif
2825

ext_mod/threading/common/inc/thread_lock.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66
#ifndef __THREAD_LOCK_H__
77
#define __THREAD_LOCK_H__
88

9-
#include "threading_common.h"
10-
11-
typedef struct _thread_lock_t thread_lock_t;
9+
typedef struct _thread_lock_t thread_lock_t; // needs to be defined in port
1210

1311
typedef struct _mp_obj_lock_t {
1412
mp_obj_base_t base;
1513
thread_lock_t lock;
1614
volatile bool locked;
1715
} mp_obj_lock_t;
1816

19-
int threading_lock_acquire(thread_lock_t *lock, int32_t wait_ms);
20-
void threading_lock_release(thread_lock_t *lock);
21-
void threading_lock_init(thread_lock_t *lock);
22-
void threading_lock_delete(thread_lock_t *lock);
17+
int threading_lock_acquire(thread_lock_t *lock, int32_t wait_ms); // needs to be defined in port
18+
void threading_lock_release(thread_lock_t *lock); // needs to be defined in port
19+
void threading_lock_init(thread_lock_t *lock); // needs to be defined in port
20+
void threading_lock_delete(thread_lock_t *lock); // needs to be defined in port
2321

2422
#endif

ext_mod/threading/common/inc/thread_rlock.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifndef __THREAD_RLOCK_H__
1111
#define __THREAD_RLOCK_H__
1212

13-
typedef struct _thread_rlock_t thread_rlock_t;
13+
typedef struct _thread_rlock_t thread_rlock_t; // needs to be defined in port
1414

1515
typedef struct _mp_obj_rlock_t {
1616
mp_obj_base_t base;
@@ -19,9 +19,9 @@
1919
volatile int count;
2020
} mp_obj_rlock_t;
2121

22-
int threading_rlock_acquire(thread_rlock_t *rlock, int32_t wait_ms);
23-
void threading_rlock_release(thread_rlock_t *rlock);
24-
void threading_rlock_init(thread_rlock_t *rlock);
25-
void threading_rlock_delete(thread_rlock_t *rlock);
22+
int threading_rlock_acquire(thread_rlock_t *rlock, int32_t wait_ms); // needs to be defined in port
23+
void threading_rlock_release(thread_rlock_t *rlock); // needs to be defined in port
24+
void threading_rlock_init(thread_rlock_t *rlock); // needs to be defined in port
25+
void threading_rlock_delete(thread_rlock_t *rlock); // needs to be defined in port
2626

2727
#endif

ext_mod/threading/common/inc/thread_semaphore.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@
88

99
#include "threading_common.h"
1010

11-
typedef struct _thread_semphamore_t thread_semphamore_t;
11+
typedef struct _thread_semphamore_t thread_semphamore_t; // needs to be defined in port
1212

1313
typedef struct _mp_obj_thread_semaphore_t {
1414
mp_obj_base_t base;
1515
thread_semphamore_t sem;
1616
uint16_t start_value;
17-
volatile uint16_t value;
18-
volatile uint16_t waiting;
1917
} mp_obj_thread_semaphore_t;
2018

21-
22-
uint16_t threading_semphamore_get_count(thread_semphamore_t *sem);
23-
bool threading_semphamore_acquire(thread_semphamore_t *sem, int32_t wait_ms);
24-
void threading_semphamore_release(thread_semphamore_t *sem);
25-
void threading_semphamore_init(thread_event_t *mutex);
26-
void threading_semphamore_delete(thread_event_t *mutex);
19+
uint16_t threading_semphamore_get_count(thread_semphamore_t *sem); // needs to be defined in port
20+
bool threading_semphamore_acquire(thread_semphamore_t *sem, int32_t wait_ms); // needs to be defined in port
21+
void threading_semphamore_release(thread_semphamore_t *sem); // needs to be defined in port
22+
void threading_semphamore_init(thread_semphamore_t *sem); // needs to be defined in port
23+
void threading_semphamore_delete(thread_semphamore_t *sem); // needs to be defined in port
2724

2825
#endif

ext_mod/threading/common/inc/thread_thread.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
#include "py/obj.h"
33
#include "py/runtime.h"
44

5-
#include "thread_common.h"
65

76
#ifndef __THREAD_THREAD_H__
87
#define __THREAD_THREAD_H__
98

10-
typedef struct _thread_t thread_t;
9+
typedef struct _thread_t thread_t; // needs to be defined in port
1110

1211
typedef struct _thread_entry_args_t {
1312
mp_obj_dict_t *dict_locals;
@@ -38,7 +37,7 @@
3837

3938
} mp_obj_thread_t;
4039

41-
mp_uint_t threading_create_thread(mp_obj_thread_t *self);
42-
void threading_delete_thread(thread_t *thread);
40+
mp_uint_t threading_create_thread(mp_obj_thread_t *self); // needs to be defined in port
41+
void threading_delete_thread(thread_t *thread); // needs to be defined in port
4342

4443
#endif

ext_mod/threading/common/inc/threading.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,27 @@
55
#ifndef __THREADING_H__
66
#define __THREADING_H__
77

8+
#define THREAD_UNUSED(x) ((void)x)
9+
810
#include "thread_thread.h"
911
#include "thread_lock.h"
1012
#include "thread_rlock.h"
1113
#include "thread_semphamore.h"
1214
#include "thread_event.h"
1315

14-
#define THREAD_UNUSED(x) ((void)x)
15-
16-
void threading_init(void *stack, uint32_t stack_len)
17-
void threading_deinit(void)
18-
void threading_gc_others(void)
16+
void threading_init(void *stack, uint32_t stack_len); // needs to be defined in port
17+
void threading_deinit(void); // needs to be defined in port
18+
void threading_gc_others(void); // needs to be defined in port
1919

2020
typedef void *(*thread_entry_cb_t)(mp_obj_thread_t *self);
2121

22-
mp_obj_t mp_get_main_thread(void);
23-
mp_obj_t mp_enumerate_threads(void);
24-
25-
uint32_t mp_get_current_thread_id(void)
22+
mp_obj_t mp_get_main_thread(void); // needs to be defined in port
23+
mp_obj_t mp_enumerate_threads(void); // needs to be defined in port
24+
uint32_t mp_get_current_thread_id(void); // needs to be defined in port
2625

27-
extern size_t thread_stack_size;
28-
extern mp_obj_thread_t *t_thread;
29-
extern thread_lock_t t_mutex;
30-
extern mp_obj_thread_t _main_thread;
26+
extern size_t thread_stack_size; // needs to be defined in port
27+
extern mp_obj_thread_t *t_thread; // needs to be defined in port
28+
extern thread_lock_t t_mutex; // needs to be defined in port
29+
extern mp_obj_thread_t _main_thread; // needs to be defined in port
3130

3231
#endif /*__THREADING_H__ */

ext_mod/threading/esp32/thread_port.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ void *thread_entry_cb(mp_obj_thread_t *self)
216216

217217

218218

219-
void threading_init(void *stack, uint32_t stack_len) {
219+
void threading_init(void *stack, uint32_t stack_len)
220+
{
220221
mp_thread_set_state(&mp_state_ctx.thread);
221222
// create the first entry in the linked list of all threads
222223
_main_thread.thread.handle = xTaskGetCurrentTaskHandle();
@@ -238,7 +239,8 @@ void threading_init(void *stack, uint32_t stack_len) {
238239
}
239240

240241

241-
void threading_deinit(void) {
242+
void threading_deinit(void)
243+
{
242244
for (;;) {
243245
// Find a task to delete
244246

@@ -266,8 +268,8 @@ void threading_deinit(void) {
266268
}
267269

268270

269-
270-
void threading_gc_others(void) {
271+
void threading_gc_others(void)
272+
{
271273
threading_lock_acquire(&t_mutex, 1);
272274

273275
for (mp_obj_thread_t *th = t_thread; th != NULL; th = th->next) {
@@ -285,7 +287,8 @@ void threading_gc_others(void) {
285287
}
286288

287289

288-
static void freertos_entry(void *arg) {
290+
static void freertos_entry(void *arg)
291+
{
289292
if (ext_threading_thread_entry) {
290293
mp_obj_thread_t *self = (mp_obj_thread_t *)arg;
291294
ext_threading_thread_entry(self);
@@ -297,7 +300,8 @@ static void freertos_entry(void *arg) {
297300
}
298301

299302

300-
mp_uint_t thread_create_ex(mp_obj_thread_t *self, int priority, char *name) {
303+
mp_uint_t thread_create_ex(mp_obj_thread_t *self, int priority, char *name)
304+
{
301305
// store thread entry function into a global variable so we can access it
302306
ext_threading_thread_entry = thread_entry_cb;
303307

@@ -335,7 +339,8 @@ mp_uint_t thread_create_ex(mp_obj_thread_t *self, int priority, char *name) {
335339
}
336340

337341

338-
mp_uint_t threading_create_thread(thread_t *self) {
342+
mp_uint_t threading_create_thread(thread_t *self)
343+
{
339344
return thread_create_ex(self, THREADING_PRIORITY, "mp_thread");
340345
}
341346

ext_mod/threading/esp32/thread_port.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
#ifndef __THREAD_PORT_H__
88
#define __THREAD_PORT_H__
99

10-
#include "threading.h"
11-
#include "multiprocessing.h"
12-
13-
#include "thread_thread.h"
14-
#include "thread_event.h"
15-
#include "thread_semphamore.h"
16-
#include "thread_lock.h"
17-
#include "thread_rlock.h"
18-
1910
struct _thread_lock_t {
2011
SemaphoreHandle_t handle;
2112
StaticSemaphore_t buffer;
@@ -40,4 +31,15 @@
4031
TaskHandle_t handle;
4132
}
4233

34+
void THREADING_FREERTOS_TASK_DELETE_HOOK(void *tcb);
35+
36+
#include "threading.h"
37+
#include "multiprocessing.h"
38+
39+
#include "thread_thread.h"
40+
#include "thread_event.h"
41+
#include "thread_semphamore.h"
42+
#include "thread_lock.h"
43+
#include "thread_rlock.h"
44+
4345
#endif /* __THREAD_PORT_H__ */

ext_mod/threading/threading.h

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)