Skip to content

Commit a519ce3

Browse files
committedOct 4, 2020
Add enable/disable threads functions
1 parent d66580b commit a519ce3

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed
 

‎Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "cortexm-threads"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["nk"]
55
edition = "2018"
6-
license-file = "LICENSE.md"
6+
license="MIT"
77
description = "A simple library for context-switching on ARM Cortex-M ( 0, 0+, 3, 4, 4F ) micro-processors"
88

99
[build-dependencies]

‎build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use cc::Build;
1717
// }
1818
// }
1919

20-
fn main() -> Result<(), Box<Error>> {
20+
fn main() -> Result<(), Box<dyn Error>> {
2121
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
2222
println!("cargo:rustc-link-search={}", out_dir.display());
2323

‎src/lib.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,27 @@ extern "C" {
128128
fn __CORTEXM_THREADS_wfe();
129129
}
130130

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+
131145
/// Initialize the switcher system
132146
pub fn init() -> ! {
133147
unsafe {
134-
__CORTEXM_THREADS_cpsid();
148+
disable_threads();
135149
let ptr: usize = core::intrinsics::transmute(&__CORTEXM_THREADS_GLOBAL);
136150
__CORTEXM_THREADS_GLOBAL_PTR = ptr as u32;
137-
__CORTEXM_THREADS_cpsie();
151+
enable_threads();
138152
let mut idle_stack = [0xDEADBEEF; 64];
139153
match create_tcb(
140154
&mut idle_stack,
@@ -209,7 +223,7 @@ pub fn create_thread_with_config(
209223
priviliged: bool,
210224
) -> Result<(), u8> {
211225
unsafe {
212-
__CORTEXM_THREADS_cpsid();
226+
disable_threads();
213227
let handler = &mut __CORTEXM_THREADS_GLOBAL;
214228
if handler.add_idx >= handler.threads.len() {
215229
return Err(ERR_TOO_MANY_THREADS);
@@ -223,11 +237,11 @@ pub fn create_thread_with_config(
223237
handler.add_idx = handler.add_idx + 1;
224238
}
225239
Err(e) => {
226-
__CORTEXM_THREADS_cpsie();
240+
enable_threads();
227241
return Err(e);
228242
}
229243
}
230-
__CORTEXM_THREADS_cpsie();
244+
enable_threads();
231245
Ok(())
232246
}
233247
}
@@ -241,9 +255,7 @@ pub fn create_thread_with_config(
241255
/// * if context switch is required, will pend the PendSV exception, which will do the actual thread switching
242256
#[no_mangle]
243257
pub extern "C" fn SysTick() {
244-
unsafe {
245-
__CORTEXM_THREADS_cpsid();
246-
}
258+
disable_threads();
247259
let handler = unsafe { &mut __CORTEXM_THREADS_GLOBAL };
248260
if handler.inited {
249261
if handler.curr == handler.next {
@@ -260,9 +272,7 @@ pub extern "C" fn SysTick() {
260272
}
261273
}
262274
}
263-
unsafe {
264-
__CORTEXM_THREADS_cpsie();
265-
}
275+
enable_threads();
266276
}
267277

268278
/// Get id of current thread
@@ -316,7 +326,7 @@ fn get_next_thread_idx() -> usize {
316326
}
317327
match handler
318328
.threads
319-
.into_iter()
329+
.iter()
320330
.enumerate()
321331
.filter(|&(idx, x)| idx > 0 && idx < handler.add_idx && x.status != ThreadStatus::Sleeping)
322332
.max_by(|&(_, a), &(_, b)| a.priority.cmp(&b.priority))

0 commit comments

Comments
 (0)
Please sign in to comment.