Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16f113f

Browse files
committed
it compiles!!!!
1 parent84065f3 commit16f113f

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
#defineTHREAD_UNUSED(x) ((void)x)
16+
#defineTHREADING_MIN_STACK_SIZE (4 * 1024)
17+
#defineTHREADING_DEFAULT_STACK_SIZE (THREADING_MIN_STACK_SIZE + 1024)
18+
#defineTHREADING_PRIORITY (ESP_TASK_PRIO_MIN + 1)
1419

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

1722
typedefstruct_threading_mutex_t {
1823
SemaphoreHandle_thandle;
@@ -27,15 +32,18 @@
2732
voidrlock_release(threading_mutex_t*mutex);
2833
voidrlock_init(threading_mutex_t*mutex);
2934

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

32-
mp_uint_tthread_create(threading_thread_entry_cb_tentry,mp_obj_thread_thread_t*th);
37+
mp_uint_tthread_create(threading_thread_entry_cb_tentry,mp_obj_thread_thread_t*self);
3338

3439
voidthreading_init(void*stack,uint32_tstack_len);
3540
voidthreading_deinit(void);
3641
voidthreading_gc_others(void);
3742

38-
esternsize_tthread_stack_size;
43+
externsize_tthread_stack_size;
44+
externmp_obj_thread_thread_t*t_thread;
45+
externthreading_mutex_tt_mutex;
46+
externmp_obj_thread_thread_t_main_thread;
3947

4048
voidTHREADING_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-
externconstmp_obj_fun_builtin_fixed_tthreading_lock_locked_obj;
21-
externconstmp_obj_fun_builtin_fixed_tthreading_lock_release_obj;
22-
externconstmp_obj_fun_builtin_var_tthreading_lock__exit__obj;
23-
externconstmp_obj_fun_builtin_var_tthreading_lock_acquire_obj;
24-
externconstmp_obj_fun_builtin_var_tthreading_lock__enter__obj;
20+
externconstmp_obj_fun_builtin_fixed_tthread_lock_locked_obj;
21+
externconstmp_obj_fun_builtin_fixed_tthread_lock_release_obj;
22+
externconstmp_obj_fun_builtin_var_tthread_lock__exit__obj;
23+
externconstmp_obj_fun_builtin_var_tthread_lock_acquire_obj;
24+
externconstmp_obj_fun_builtin_var_tthread_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
volatileintcount;
1818
}mp_obj_thread_rlock_t;
1919

20-
externconstmp_obj_fun_builtin_fixed_trlock_locked_obj;
21-
externconstmp_obj_fun_builtin_fixed_trlock_release_obj;
22-
externconstmp_obj_fun_builtin_var_trlock__exit__obj;
23-
externconstmp_obj_fun_builtin_var_trlock_acquire_obj;
24-
externconstmp_obj_fun_builtin_var_trlock__enter__obj;
20+
externconstmp_obj_fun_builtin_fixed_tthread_rlock_locked_obj;
21+
externconstmp_obj_fun_builtin_fixed_tthread_rlock_release_obj;
22+
externconstmp_obj_fun_builtin_var_tthread_rlock__exit__obj;
23+
externconstmp_obj_fun_builtin_var_tthread_rlock_acquire_obj;
24+
externconstmp_obj_fun_builtin_var_tthread_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
typedefstruct_mp_obj_thread_semaphore_t {
1214
mp_obj_base_tbase;
1315
threading_mutex_tmutex;
@@ -19,10 +21,10 @@
1921

2022
voidsemaphore_attr_func(mp_obj_tself_in,qstrattr,mp_obj_t*dest);
2123

22-
externconstmp_obj_fun_builtin_fixed_tsemaphore__del__obj;
23-
externconstmp_obj_fun_builtin_var_tsemaphore_acquire_obj;
24-
externconstmp_obj_fun_builtin_var_tsemaphore__enter__obj;
25-
externconstmp_obj_fun_builtin_var_tsemaphore_release_obj;
26-
externconstmp_obj_fun_builtin_var_tsemaphore__exit__obj;
24+
externconstmp_obj_fun_builtin_fixed_tthread_semaphore__del__obj;
25+
externconstmp_obj_fun_builtin_var_tthread_semaphore_acquire_obj;
26+
externconstmp_obj_fun_builtin_var_tthread_semaphore__enter__obj;
27+
externconstmp_obj_fun_builtin_var_tthread_semaphore_release_obj;
28+
externconstmp_obj_fun_builtin_var_tthread_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_tn_args;
2020
size_tn_kw;
2121
mp_obj_targs[];
22-
boolis_alive;
2322
}thread_entry_args_t;
2423

2524
typedefstruct_mp_obj_thread_thread_t {
@@ -29,10 +28,11 @@
2928

3029
mp_obj_tname;
3130

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

3433
intready;// whether the thread is ready and running
3534
intis_alive;
35+
uint8_tcore_id;
3636
void*arg;// thread Python args, a GC root pointer
3737
void*stack;// pointer to the stack
3838
size_tstack_len;// number of words in the stack
@@ -44,7 +44,7 @@
4444
externconstmp_obj_fun_builtin_fixed_tthread_start_obj;
4545
externconstmp_obj_fun_builtin_fixed_tthread_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
voidthread_attr_func(mp_obj_tself_in,qstrattr,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
intlock_acquire(threading_mutex_t*mutex,intwait)
1525
{
@@ -57,47 +67,47 @@ void rlock_init(threading_mutex_t *mutex)
5767

5868
// the mutex controls access to the linked list
5969
staticthreading_thread_entry_cb_text_threading_thread_entry=NULL;
60-
staticthreading_mutex_tthread_mutex;
61-
staticmp_obj_thread_thread_tthread_entry0;
62-
staticmp_obj_thread_thread_t*thread=NULL;// root pointer, handled by threading_gc_others
70+
threading_mutex_tt_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_tthread_stack_size=0;
6474

6575

6676
voidthreading_init(void*stack,uint32_tstack_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 afterthread_mutex is ready
84-
thread=&thread_entry0;
93+
// FREERTOS_TASK_DELETE_HOOK needs the thread ready aftert_mutex is ready
94+
t_thread=&_main_thread;
8595
}
8696

8797

8898
voidthreading_deinit(void) {
8999
for (;;) {
90100
// Find a task to delete
91101
TaskHandle_tid=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
voidthreading_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
staticvoidthreading_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_tthread_create_ex(threading_thread_entry_cb_tentry,mp_obj_thread_thread_t*th,intpriority,char*name) {
152+
mp_uint_tthread_create_ex(threading_thread_entry_cb_tentry,mp_obj_thread_thread_t*self,intpriority,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-
}elseif (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+
}elseif (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 outsidethread_mutex lock)
162+
// Allocate linked-list node (must be outsidet_mutex lock)
153163

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

156-
BaseType_tcore_id=xPortGetCoreID();
157166
// create thread
158-
BaseType_tresult=xTaskCreatePinnedToCore(threading_freertos_entry,name,*stack_size /sizeof(StackType_t),th,priority,&th->id,core_id);
167+
BaseType_tresult=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_tthread_create(threading_thread_entry_cb_tentry,mp_obj_thread_thread_t*th) {
183-
returnthread_create_ex(entry,th,MP_THREAD_PRIORITY,"mp_thread");
190+
mp_uint_tthread_create(threading_thread_entry_cb_tentry,mp_obj_thread_thread_t*self) {
191+
returnthread_create_ex(entry,self,THREADING_PRIORITY,"mp_thread");
184192
}
185193

186194

187195
voidTHREADING_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)

[8]ページ先頭

©2009-2025 Movatter.jp