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
1424int 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
5969static 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
6373size_t thread_stack_size = 0 ;
6474
6575
6676void threading_init (void * stack ,uint32_t stack_len ) {
6777mp_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
8898void threading_deinit (void ) {
8999for (;;) {
90100// Find a task to delete
91101TaskHandle_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
95105if (th -> id != xTaskGetCurrentTaskHandle ()) {
96106id = th -> id ;
97107break ;
98108 }
99109 }
100- mutex_unlock ( & thread_mutex );
110+ lock_release ( & t_mutex );
101111
102112if (id == NULL ) {
103113// No tasks left to delete
@@ -112,8 +122,8 @@ void threading_deinit(void) {
112122
113123
114124void 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 ) {
117127gc_collect_root ((void * * )& th ,1 );
118128gc_collect_root (& th -> arg ,1 );// probably not needed
119129if (th -> id == xTaskGetCurrentTaskHandle ()) {
@@ -124,83 +134,81 @@ void threading_gc_others(void) {
124134 }
125135gc_collect_root (th -> stack ,th -> stack_len );
126136 }
127- mutex_unlock ( & thread_mutex );
137+ lock_release ( & t_mutex );
128138}
129139
130140
131141static void threading_freertos_entry (void * arg ) {
132142if (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 }
136146vTaskDelete (NULL );
137147for (;;) {;
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
144154ext_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 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_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
160169if (result != pdPASS ) {
161- mutex_unlock ( & thread_mutex );
170+ lock_release ( & t_mutex );
162171mp_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
187195void THREADING_FREERTOS_TASK_DELETE_HOOK (void * tcb ) {
188- if (thread == NULL ) {
196+ if (t_thread == NULL ) {
189197// threading not yet initialised
190198return ;
191199 }
192200mp_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 ) {
195203if ((void * )th -> id == tcb ) {
196204if (prev != NULL ) {
197205prev -> next = th -> next ;
198206 }else {
199- thread = th -> next ;
207+ t_thread = th -> next ;
200208 }
201209break ;
202210 }
203211 }
204212
205- mutex_unlock ( & thread_mutex );
213+ lock_release ( & t_mutex );
206214}