@@ -128,13 +128,27 @@ extern "C" {
128
128
fn __CORTEXM_THREADS_wfe ( ) ;
129
129
}
130
130
131
+ #[ inline( always) ]
132
+ pub fn enable_threads ( ) {
133
+ unsafe {
134
+ __CORTEXM_THREADS_cpsie ( ) ;
135
+ }
136
+ }
137
+
138
+ #[ inline( always) ]
139
+ pub fn disable_threads ( ) {
140
+ unsafe {
141
+ __CORTEXM_THREADS_cpsid ( ) ;
142
+ }
143
+ }
144
+
131
145
/// Initialize the switcher system
132
146
pub fn init ( ) -> ! {
133
147
unsafe {
134
- __CORTEXM_THREADS_cpsid ( ) ;
148
+ disable_threads ( ) ;
135
149
let ptr: usize = core:: intrinsics:: transmute ( & __CORTEXM_THREADS_GLOBAL) ;
136
150
__CORTEXM_THREADS_GLOBAL_PTR = ptr as u32 ;
137
- __CORTEXM_THREADS_cpsie ( ) ;
151
+ enable_threads ( ) ;
138
152
let mut idle_stack = [ 0xDEADBEEF ; 64 ] ;
139
153
match create_tcb (
140
154
& mut idle_stack,
@@ -209,7 +223,7 @@ pub fn create_thread_with_config(
209
223
priviliged : bool ,
210
224
) -> Result < ( ) , u8 > {
211
225
unsafe {
212
- __CORTEXM_THREADS_cpsid ( ) ;
226
+ disable_threads ( ) ;
213
227
let handler = & mut __CORTEXM_THREADS_GLOBAL;
214
228
if handler. add_idx >= handler. threads . len ( ) {
215
229
return Err ( ERR_TOO_MANY_THREADS ) ;
@@ -223,11 +237,11 @@ pub fn create_thread_with_config(
223
237
handler. add_idx = handler. add_idx + 1 ;
224
238
}
225
239
Err ( e) => {
226
- __CORTEXM_THREADS_cpsie ( ) ;
240
+ enable_threads ( ) ;
227
241
return Err ( e) ;
228
242
}
229
243
}
230
- __CORTEXM_THREADS_cpsie ( ) ;
244
+ enable_threads ( ) ;
231
245
Ok ( ( ) )
232
246
}
233
247
}
@@ -241,9 +255,7 @@ pub fn create_thread_with_config(
241
255
/// * if context switch is required, will pend the PendSV exception, which will do the actual thread switching
242
256
#[ no_mangle]
243
257
pub extern "C" fn SysTick ( ) {
244
- unsafe {
245
- __CORTEXM_THREADS_cpsid ( ) ;
246
- }
258
+ disable_threads ( ) ;
247
259
let handler = unsafe { & mut __CORTEXM_THREADS_GLOBAL } ;
248
260
if handler. inited {
249
261
if handler. curr == handler. next {
@@ -260,9 +272,7 @@ pub extern "C" fn SysTick() {
260
272
}
261
273
}
262
274
}
263
- unsafe {
264
- __CORTEXM_THREADS_cpsie ( ) ;
265
- }
275
+ enable_threads ( ) ;
266
276
}
267
277
268
278
/// Get id of current thread
@@ -316,7 +326,7 @@ fn get_next_thread_idx() -> usize {
316
326
}
317
327
match handler
318
328
. threads
319
- . into_iter ( )
329
+ . iter ( )
320
330
. enumerate ( )
321
331
. filter ( |& ( idx, x) | idx > 0 && idx < handler. add_idx && x. status != ThreadStatus :: Sleeping )
322
332
. max_by ( |& ( _, a) , & ( _, b) | a. priority . cmp ( & b. priority ) )
0 commit comments