Skip to content

Commit 16f113f

Browse files
committed
it compiles!!!!
1 parent 84065f3 commit 16f113f

33 files changed

+780
-368
lines changed

README.md

Lines changed: 450 additions & 0 deletions
Large diffs are not rendered by default.

ext_mod/threading/common/inc/thread_common.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
#include "freertos/semphr.h"
88
#include "freertos/queue.h"
99

10-
#ifndef __COMMON_H__
11-
#define __COMMON_H__
10+
#ifndef __THREAD_COMMON_H__
11+
#define __THREAD_COMMON_H__
12+
13+
#include "esp_task.h"
1214

1315
#define THREAD_UNUSED(x) ((void)x)
16+
#define THREADING_MIN_STACK_SIZE (4 * 1024)
17+
#define THREADING_DEFAULT_STACK_SIZE (THREADING_MIN_STACK_SIZE + 1024)
18+
#define THREADING_PRIORITY (ESP_TASK_PRIO_MIN + 1)
1419

15-
#include "../../threading/threading_thread.h"
20+
#include "threading_thread.h"
1621

1722
typedef struct _threading_mutex_t {
1823
SemaphoreHandle_t handle;
@@ -27,15 +32,18 @@
2732
void rlock_release(threading_mutex_t *mutex);
2833
void rlock_init(threading_mutex_t *mutex);
2934

30-
typedef void *(*threading_thread_entry_cb_t)(mp_obj_thread_thread_t *);
35+
typedef void *(*threading_thread_entry_cb_t)(mp_obj_thread_thread_t *self);
3136

32-
mp_uint_t thread_create(threading_thread_entry_cb_t entry, mp_obj_thread_thread_t *th);
37+
mp_uint_t thread_create(threading_thread_entry_cb_t entry, mp_obj_thread_thread_t *self);
3338

3439
void threading_init(void *stack, uint32_t stack_len);
3540
void threading_deinit(void);
3641
void threading_gc_others(void);
3742

38-
estern size_t thread_stack_size;
43+
extern size_t thread_stack_size;
44+
extern mp_obj_thread_thread_t *t_thread;
45+
extern threading_mutex_t t_mutex;
46+
extern mp_obj_thread_thread_t _main_thread;
3947

4048
void THREADING_FREERTOS_TASK_DELETE_HOOK(void *tcb);
4149

ext_mod/threading/common/inc/thread_lock.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
} mp_obj_thread_lock_t;
1818

1919

20-
extern const mp_obj_fun_builtin_fixed_t threading_lock_locked_obj;
21-
extern const mp_obj_fun_builtin_fixed_t threading_lock_release_obj;
22-
extern const mp_obj_fun_builtin_var_t threading_lock__exit__obj;
23-
extern const mp_obj_fun_builtin_var_t threading_lock_acquire_obj;
24-
extern const mp_obj_fun_builtin_var_t threading_lock__enter__obj;
20+
extern const mp_obj_fun_builtin_fixed_t thread_lock_locked_obj;
21+
extern const mp_obj_fun_builtin_fixed_t thread_lock_release_obj;
22+
extern const mp_obj_fun_builtin_var_t thread_lock__exit__obj;
23+
extern const mp_obj_fun_builtin_var_t thread_lock_acquire_obj;
24+
extern const mp_obj_fun_builtin_var_t thread_lock__enter__obj;
25+
2526

2627
#endif

ext_mod/threading/common/inc/thread_rlock.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
volatile int count;
1818
} mp_obj_thread_rlock_t;
1919

20-
extern const mp_obj_fun_builtin_fixed_t rlock_locked_obj;
21-
extern const mp_obj_fun_builtin_fixed_t rlock_release_obj;
22-
extern const mp_obj_fun_builtin_var_t rlock__exit__obj;
23-
extern const mp_obj_fun_builtin_var_t rlock_acquire_obj;
24-
extern const mp_obj_fun_builtin_var_t rlock__enter__obj;
20+
extern const mp_obj_fun_builtin_fixed_t thread_rlock_locked_obj;
21+
extern const mp_obj_fun_builtin_fixed_t thread_rlock_release_obj;
22+
extern const mp_obj_fun_builtin_var_t thread_rlock__exit__obj;
23+
extern const mp_obj_fun_builtin_var_t thread_rlock_acquire_obj;
24+
extern const mp_obj_fun_builtin_var_t thread_rlock__enter__obj;
2525

2626
#endif

ext_mod/threading/common/inc/thread_semaphore.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef __THREAD_SEMAPHORE_H__
99
#define __THREAD_SEMAPHORE_H__
1010

11+
#include "thread_common.h"
12+
1113
typedef struct _mp_obj_thread_semaphore_t {
1214
mp_obj_base_t base;
1315
threading_mutex_t mutex;
@@ -19,10 +21,10 @@
1921

2022
void semaphore_attr_func(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
2123

22-
extern const mp_obj_fun_builtin_fixed_t semaphore__del__obj;
23-
extern const mp_obj_fun_builtin_var_t semaphore_acquire_obj;
24-
extern const mp_obj_fun_builtin_var_t semaphore__enter__obj;
25-
extern const mp_obj_fun_builtin_var_t semaphore_release_obj;
26-
extern const mp_obj_fun_builtin_var_t semaphore__exit__obj;
24+
extern const mp_obj_fun_builtin_fixed_t thread_semaphore__del__obj;
25+
extern const mp_obj_fun_builtin_var_t thread_semaphore_acquire_obj;
26+
extern const mp_obj_fun_builtin_var_t thread_semaphore__enter__obj;
27+
extern const mp_obj_fun_builtin_var_t thread_semaphore_release_obj;
28+
extern const mp_obj_fun_builtin_var_t thread_semaphore__exit__obj;
2729

2830
#endif

ext_mod/threading/common/inc/thread_thread.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
size_t n_args;
2020
size_t n_kw;
2121
mp_obj_t args[];
22-
bool is_alive;
2322
} thread_entry_args_t;
2423

2524
typedef struct _mp_obj_thread_thread_t {
@@ -29,10 +28,11 @@
2928

3029
mp_obj_t name;
3130

32-
thread_entry_args_t *th_args;
31+
thread_entry_args_t *call_args;
3332

3433
int ready; // whether the thread is ready and running
3534
int is_alive;
35+
uint8_t core_id;
3636
void *arg; // thread Python args, a GC root pointer
3737
void *stack; // pointer to the stack
3838
size_t stack_len; // number of words in the stack
@@ -44,7 +44,7 @@
4444
extern const mp_obj_fun_builtin_fixed_t thread_start_obj;
4545
extern const mp_obj_fun_builtin_fixed_t thread_is_alive_obj;
4646

47-
void *thread_entry_cb(threading_thread_entry_args_t *th);
47+
void *thread_entry_cb(mp_obj_thread_thread_t *th);
4848
void thread_attr_func(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
4949

5050
#endif
Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
// micropython includes
22
#include "py/obj.h"
33
#include "py/runtime.h"
4+
#include "py/gc.h"
45

56
#include "freertos/FreeRTOS.h"
67
#include "freertos/task.h"
78
#include "freertos/semphr.h"
89
#include "freertos/queue.h"
910

1011
#include "thread_common.h"
11-
#include "../../threading/threading_thread.h"
12+
#include "threading_thread.h"
1213

14+
/*
15+
#include "py/runtime.h"
16+
#include "py/mphal.h"
17+
#include "py/stackctrl.h"
18+
#include "py/mpthread.h"
19+
#include "mpthreadport.h"
20+
21+
#include "stdio.h"
22+
*/
1323

1424
int lock_acquire(threading_mutex_t *mutex, int wait)
1525
{
@@ -57,47 +67,47 @@ void rlock_init(threading_mutex_t *mutex)
5767

5868
// the mutex controls access to the linked list
5969
static threading_thread_entry_cb_t ext_threading_thread_entry = NULL;
60-
static threading_mutex_t thread_mutex;
61-
static mp_obj_thread_thread_t thread_entry0;
62-
static mp_obj_thread_thread_t *thread = NULL; // root pointer, handled by threading_gc_others
70+
threading_mutex_t t_mutex;
71+
mp_obj_thread_thread_t _main_thread;
72+
mp_obj_thread_thread_t *t_thread = NULL; // root pointer, handled by threading_gc_others
6373
size_t thread_stack_size = 0;
6474

6575

6676
void threading_init(void *stack, uint32_t stack_len) {
6777
mp_thread_set_state(&mp_state_ctx.thread);
6878
// create the first entry in the linked list of all threads
69-
thread_entry0.id = xTaskGetCurrentTaskHandle();
70-
71-
thread_entry0.ident = mp_obj_new_int_from_uint((mp_uint_t)thread_entry0.id);
72-
thread_entry0.ready = 1;
73-
thread_entry0.running = true;
74-
thread_entry0.arg = NULL;
75-
thread_entry0.stack = stack;
76-
thread_entry0.stack_len = stack_len;
77-
thread_entry0.next = NULL;
78-
mutex_init(&thread_mutex);
79+
_main_thread.id = xTaskGetCurrentTaskHandle();
80+
_main_thread.core_id = (uint8_t)xTaskGetCoreID(xTaskGetCurrentTaskHandle());
81+
_main_thread.ident = mp_obj_new_int_from_uint((mp_uint_t)_main_thread.id);
82+
_main_thread.ready = 1;
83+
_main_thread.is_alive = true;
84+
_main_thread.arg = NULL;
85+
_main_thread.stack = stack;
86+
_main_thread.stack_len = stack_len;
87+
_main_thread.next = NULL;
88+
lock_init(&t_mutex);
7989

8090
// memory barrier to ensure above data is committed
8191
__sync_synchronize();
8292

83-
// FREERTOS_TASK_DELETE_HOOK needs the thread ready after thread_mutex is ready
84-
thread = &thread_entry0;
93+
// FREERTOS_TASK_DELETE_HOOK needs the thread ready after t_mutex is ready
94+
t_thread = &_main_thread;
8595
}
8696

8797

8898
void threading_deinit(void) {
8999
for (;;) {
90100
// Find a task to delete
91101
TaskHandle_t id = NULL;
92-
mutex_lock(&thread_mutex, 1);
93-
for (mp_obj_thread_thread_t *th = thread; th != NULL; th = th->next) {
102+
lock_acquire(&t_mutex, 1);
103+
for (mp_obj_thread_thread_t *th = t_thread; th != NULL; th = th->next) {
94104
// Don't delete the current task
95105
if (th->id != xTaskGetCurrentTaskHandle()) {
96106
id = th->id;
97107
break;
98108
}
99109
}
100-
mutex_unlock(&thread_mutex);
110+
lock_release(&t_mutex);
101111

102112
if (id == NULL) {
103113
// No tasks left to delete
@@ -112,8 +122,8 @@ void threading_deinit(void) {
112122

113123

114124
void threading_gc_others(void) {
115-
mutex_lock(&thread_mutex, 1);
116-
for (mp_obj_thread_thread_t *th = thread; th != NULL; th = th->next) {
125+
lock_acquire(&t_mutex, 1);
126+
for (mp_obj_thread_thread_t *th = t_thread; th != NULL; th = th->next) {
117127
gc_collect_root((void **)&th, 1);
118128
gc_collect_root(&th->arg, 1); // probably not needed
119129
if (th->id == xTaskGetCurrentTaskHandle()) {
@@ -124,83 +134,81 @@ void threading_gc_others(void) {
124134
}
125135
gc_collect_root(th->stack, th->stack_len);
126136
}
127-
mutex_unlock(&thread_mutex);
137+
lock_release(&t_mutex);
128138
}
129139

130140

131141
static void threading_freertos_entry(void *arg) {
132142
if (ext_threading_thread_entry) {
133-
mp_obj_thread_thread_t * th = (mp_obj_thread_thread_t *)arg;
134-
ext_threading_thread_entry(th);
143+
mp_obj_thread_thread_t * self = (mp_obj_thread_thread_t *)arg;
144+
ext_threading_thread_entry(self);
135145
}
136146
vTaskDelete(NULL);
137147
for (;;) {;
138148
}
139149
}
140150

141151

142-
mp_uint_t thread_create_ex(threading_thread_entry_cb_t entry, mp_obj_thread_thread_t *th, int priority, char *name) {
152+
mp_uint_t thread_create_ex(threading_thread_entry_cb_t entry, mp_obj_thread_thread_t *self, int priority, char *name) {
143153
// store thread entry function into a global variable so we can access it
144154
ext_threading_thread_entry = entry;
145155

146-
if (th->call_args->stack_size == 0) {
147-
th->call_args->stack_size = THREADING_DEFAULT_STACK_SIZE; // default stack size
148-
} else if (th->call_args->stack_size < THREADING_MIN_STACK_SIZE) {
149-
th->call_args->stack_size = THREADING_MIN_STACK_SIZE; // minimum stack size
156+
if (self->call_args->stack_size == 0) {
157+
self->call_args->stack_size = THREADING_DEFAULT_STACK_SIZE; // default stack size
158+
} else if (self->call_args->stack_size < THREADING_MIN_STACK_SIZE) {
159+
self->call_args->stack_size = THREADING_MIN_STACK_SIZE; // minimum stack size
150160
}
151161

152-
// Allocate linked-list node (must be outside thread_mutex lock)
162+
// Allocate linked-list node (must be outside t_mutex lock)
153163

154-
mutex_lock(&thread_mutex, 1);
164+
lock_acquire(&t_mutex, 1);
155165

156-
BaseType_t core_id = xPortGetCoreID();
157166
// create thread
158-
BaseType_t result = xTaskCreatePinnedToCore(threading_freertos_entry, name, *stack_size / sizeof(StackType_t), th, priority, &th->id, core_id);
167+
BaseType_t result = xTaskCreatePinnedToCore(threading_freertos_entry, name, self->call_args->stack_size / sizeof(StackType_t), self, priority, &self->id, self->core_id);
159168

160169
if (result != pdPASS) {
161-
mutex_unlock(&thread_mutex);
170+
lock_release(&t_mutex);
162171
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("can't create thread"));
163172
}
164173

165174
// add thread to linked list of all threads
166-
th->ready = 0;
167-
th->arg = th->call_args;
168-
th->stack = pxTaskGetStackStart(th->id);
169-
th->stack_len = th->call_args->stack_size / sizeof(uintptr_t);
170-
th->next = thread;
171-
thread = th;
175+
self->ready = 0;
176+
self->stack = pxTaskGetStackStart(self->id);
177+
self->stack_len = self->call_args->stack_size / sizeof(uintptr_t);
178+
self->next = t_thread;
179+
t_thread = self;
172180

173181
// adjust the stack_size to provide room to recover from hitting the limit
174-
th->call_args->stack_size -= 1024;
182+
self->call_args->stack_size -= 1024;
175183

176-
mutex_unlock(&thread_mutex);
184+
lock_release(&t_mutex);
177185

178-
return (mp_uint_t)th->id;
186+
return (mp_uint_t)self->id;
179187
}
180188

181189

182-
mp_uint_t thread_create(threading_thread_entry_cb_t entry, mp_obj_thread_thread_t *th) {
183-
return thread_create_ex(entry, th, MP_THREAD_PRIORITY, "mp_thread");
190+
mp_uint_t thread_create(threading_thread_entry_cb_t entry, mp_obj_thread_thread_t *self) {
191+
return thread_create_ex(entry, self,THREADING_PRIORITY, "mp_thread");
184192
}
185193

186194

187195
void THREADING_FREERTOS_TASK_DELETE_HOOK(void *tcb) {
188-
if (thread == NULL) {
196+
if (t_thread == NULL) {
189197
// threading not yet initialised
190198
return;
191199
}
192200
mp_obj_thread_thread_t *prev = NULL;
193-
mutex_lock(&thread_mutex, 1);
194-
for (mp_obj_thread_thread_t *th = thread; th != NULL; prev = th, th = th->next) {
201+
lock_acquire(&t_mutex, 1);
202+
for (mp_obj_thread_thread_t *th = t_thread; th != NULL; prev = th, th = th->next) {
195203
if ((void *)th->id == tcb) {
196204
if (prev != NULL) {
197205
prev->next = th->next;
198206
} else {
199-
thread = th->next;
207+
t_thread = th->next;
200208
}
201209
break;
202210
}
203211
}
204212

205-
mutex_unlock(&thread_mutex);
213+
lock_release(&t_mutex);
206214
}

0 commit comments

Comments
 (0)