Skip to content

Commit 7e8a981

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

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1818
-3272
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef __MULTIPROCESSING_H__
3+
#define __MULTIPROCESSING_H__
4+
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);
8+
9+
extern mp_obj_t *processes;
10+
extern uint8_t process_count;
11+
12+
void multiprocessing_init(void);
13+
extern mp_obj_t processes[2];
14+
15+
#endif /* __MULTIPROCESSING_H__ */
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// micropython includes
2+
#include "py/obj.h"
3+
#include "py/runtime.h"
4+
5+
6+
#ifndef __THREAD_EVENT_H__
7+
#define __THREAD_EVENT_H__
8+
9+
typedef struct _thread_event_t thread_event_t;
10+
11+
typedef struct _mp_obj_thread_event_t {
12+
mp_obj_base_t base;
13+
thread_event_t event;
14+
15+
bool is_set;
16+
17+
} mp_obj_thread_event_t;
18+
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);
26+
27+
#endif
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// micropython includes
2+
#include "py/obj.h"
3+
#include "py/runtime.h"
4+
5+
6+
#ifndef __THREAD_LOCK_H__
7+
#define __THREAD_LOCK_H__
8+
9+
#include "threading_common.h"
10+
11+
typedef struct _thread_lock_t thread_lock_t;
12+
13+
typedef struct _mp_obj_lock_t {
14+
mp_obj_base_t base;
15+
thread_lock_t lock;
16+
volatile bool locked;
17+
} mp_obj_lock_t;
18+
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);
23+
24+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// micropython includes
2+
#include "py/obj.h"
3+
#include "py/runtime.h"
4+
5+
#include "freertos/FreeRTOS.h"
6+
#include "freertos/semphr.h"
7+
8+
#include "thread_common.h"
9+
10+
#ifndef __THREAD_RLOCK_H__
11+
#define __THREAD_RLOCK_H__
12+
13+
typedef struct _thread_rlock_t thread_rlock_t;
14+
15+
typedef struct _mp_obj_rlock_t {
16+
mp_obj_base_t base;
17+
thread_rlock_t rlock;
18+
volatile bool locked;
19+
volatile int count;
20+
} mp_obj_rlock_t;
21+
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);
26+
27+
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// micropython includes
2+
#include "py/obj.h"
3+
#include "py/runtime.h"
4+
5+
6+
#ifndef __THREAD_SEMAPHORE_H__
7+
#define __THREAD_SEMAPHORE_H__
8+
9+
#include "threading_common.h"
10+
11+
typedef struct _thread_semphamore_t thread_semphamore_t;
12+
13+
typedef struct _mp_obj_thread_semaphore_t {
14+
mp_obj_base_t base;
15+
thread_semphamore_t sem;
16+
uint16_t start_value;
17+
volatile uint16_t value;
18+
volatile uint16_t waiting;
19+
} mp_obj_thread_semaphore_t;
20+
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);
27+
28+
#endif

ext_mod/threading/esp32/common/inc/thread_thread.h renamed to ext_mod/threading/common/inc/thread_thread.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
#include "py/obj.h"
33
#include "py/runtime.h"
44

5-
#include "freertos/idf_additions.h"
6-
#include "freertos/task.h"
7-
#include "freertos/semphr.h"
8-
95
#include "thread_common.h"
106

117
#ifndef __THREAD_THREAD_H__
128
#define __THREAD_THREAD_H__
139

10+
typedef struct _thread_t thread_t;
11+
1412
typedef struct _thread_entry_args_t {
1513
mp_obj_dict_t *dict_locals;
1614
mp_obj_dict_t *dict_globals;
@@ -21,9 +19,9 @@
2119
mp_obj_t args[];
2220
} thread_entry_args_t;
2321

24-
typedef struct _mp_obj_thread_thread_t {
22+
typedef struct _mp_obj_thread_t {
2523
mp_obj_base_t base;
26-
TaskHandle_t id;
24+
thread_t thread;
2725
mp_obj_t ident;
2826

2927
mp_obj_t name;
@@ -36,15 +34,11 @@
3634
void *arg; // thread Python args, a GC root pointer
3735
void *stack; // pointer to the stack
3836
size_t stack_len; // number of words in the stack
39-
struct _mp_obj_thread_thread_t *next;
40-
41-
} mp_obj_thread_thread_t;
42-
37+
struct _mp_obj_thread_t *next;
4338

44-
extern const mp_obj_fun_builtin_fixed_t thread_start_obj;
45-
extern const mp_obj_fun_builtin_fixed_t thread_is_alive_obj;
39+
} mp_obj_thread_t;
4640

47-
void *thread_entry_cb(mp_obj_thread_thread_t *th);
48-
void thread_attr_func(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
41+
mp_uint_t threading_create_thread(mp_obj_thread_t *self);
42+
void threading_delete_thread(thread_t *thread);
4943

5044
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// micropython includes
2+
#include "py/obj.h"
3+
#include "py/runtime.h"
4+
5+
#ifndef __THREADING_H__
6+
#define __THREADING_H__
7+
8+
#include "thread_thread.h"
9+
#include "thread_lock.h"
10+
#include "thread_rlock.h"
11+
#include "thread_semphamore.h"
12+
#include "thread_event.h"
13+
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)
19+
20+
typedef void *(*thread_entry_cb_t)(mp_obj_thread_t *self);
21+
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)
26+
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;
31+
32+
#endif /*__THREADING_H__ */
Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
#include "freertos/FreeRTOS.h"
2-
#include "freertos/task.h"
3-
#include "freertos/semphr.h"
4-
#include "freertos/queue.h"
5-
6-
7-
#include "thread_common.h"
8-
#include "thread_thread.h"
9-
10-
#include "threading.h"
11-
12-
#include "multiprocessing.h"
13-
#include "multiprocessing_event.h"
14-
#include "multiprocessing_lock.h"
15-
#include "multiprocessing_process.h"
16-
#include "multiprocessing_rlock.h"
17-
#include "multiprocessing_semaphore.h"
18-
19-
201
mp_obj_t processes[2];
212

223

234
void multiprocessing_init(void)
245
{
256
mp_obj_thread_thread_t * main_thread = (mp_obj_thread_thread_t *)MP_OBJ_TO_PTR(threading_main_thread());
26-
uint16_t curr_core_id = (uint16_t)xTaskGetCoreID(main_thread->id);
7+
uint8_t curr_core_id = mp_get_process_core(&main_thread->thread);
278
processes[curr_core_id] = MP_OBJ_FROM_PTR(main_thread);
289
}
2910

@@ -32,23 +13,23 @@ static mp_obj_t multiprocessing_active_children(void)
3213
{
3314
mp_obj_t list = mp_obj_new_list(0, NULL);
3415

35-
uint16_t core_id = (uint16_t)xTaskGetCoreID(xTaskGetCurrentTaskHandle());
36-
uint16_t task_core_id;
16+
uint8_t core_id = mp_get_current_process_core();
17+
uint8_t task_core_id;
3718

38-
lock_acquire(&t_mutex, 1);
19+
threading_lock_acquire(&t_mutex, 1);
3920

4021
for (mp_obj_thread_thread_t *th = t_thread; th != NULL; th = th->next) {
4122
if (!th->is_alive) {
4223
continue;
4324
}
44-
task_core_id = (uint16_t)xTaskGetCoreID(th->id);
25+
task_core_id = mp_get_process_core(&th->thread);
4526

4627
if (task_core_id == core_id) {
4728
mp_obj_list_append(list, MP_OBJ_FROM_PTR(th));
4829
}
4930
}
5031

51-
lock_release(&t_mutex);
32+
threading_lock_release(&t_mutex);
5233
return list;
5334
}
5435

@@ -57,15 +38,15 @@ static MP_DEFINE_CONST_FUN_OBJ_0(multiprocessing_active_children_obj, multiproce
5738

5839
static mp_obj_t multiprocessing_cpu_count(void)
5940
{
60-
return mp_obj_new_int(2);
41+
return mp_obj_new_int_truncated(mp_get_cpu_count());
6142
}
6243

6344
static MP_DEFINE_CONST_FUN_OBJ_0(multiprocessing_cpu_count_obj, multiprocessing_cpu_count);
6445

6546

6647
static mp_obj_t multiprocessing_current_process(void)
6748
{
68-
uint16_t core_id = (uint16_t)xTaskGetCoreID(xTaskGetCurrentTaskHandle());
49+
uint8_t core_id = mp_get_current_process_core();
6950
return processes[core_id];
7051
}
7152

@@ -75,13 +56,9 @@ static MP_DEFINE_CONST_FUN_OBJ_0(multiprocessing_current_process_obj, multiproce
7556
static mp_obj_t multiprocessing_parent_process(void)
7657
{
7758
mp_obj_t main_thread = threading_main_thread();
78-
uint16_t core_id = (uint16_t)xTaskGetCoreID(xTaskGetCurrentTaskHandle());
59+
uint8_t core_id = mp_get_current_process_core();
7960

80-
if (processes[core_id] == main_thread) {
81-
return mp_const_none;
82-
} else {
83-
return main_thread;
84-
}
61+
return processes[core_id];
8562
}
8663

8764
static MP_DEFINE_CONST_FUN_OBJ_0(multiprocessing_parent_process_obj, multiprocessing_parent_process);
@@ -108,6 +85,4 @@ const mp_obj_module_t mp_module_multiprocessing = {
10885
.globals = (mp_obj_dict_t *)&mp_module_multiprocessing_globals,
10986
};
11087

111-
MP_REGISTER_MODULE(MP_QSTR_multiprocessing, mp_module_multiprocessing);
112-
113-
88+
MP_REGISTER_MODULE(MP_QSTR_multiprocessing, mp_module_multiprocessing);

0 commit comments

Comments
 (0)