xwrust/xwos/
skd.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! XWOS RUST:调度器
//! ========
//!

use crate::types::*;

/// 上下文枚举
#[derive(Debug, Clone, Copy)]
pub enum Context {
    /// 启动
    Boot,
    /// 线程
    Thd,
    /// 中断(中断号)
    Isr(XwIrq),
    /// 中断底半部
    Bh,
    /// 空闲任务
    Idle,
}

extern "C" {
    fn xwrustffi_skd_get_context_lc(ctxbuf: *mut XwSq, irqnbuf: *mut XwIrq);
    fn xwrustffi_skd_dspmpt_lc();
    fn xwrustffi_skd_enpmpt_lc();
}

/// 获取当前代码的上下文
///
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::skd;
///
/// let ctx = skd::context();
/// ```
pub fn context() -> Context
{
    let mut ctx: XwSq = 0;
    let mut irq: XwIrq = 0;

    unsafe { xwrustffi_skd_get_context_lc(&mut ctx, &mut irq); }
    match ctx {
        0 => Context::Boot,
        1 => Context::Thd,
        2 => Context::Isr(irq),
        3 => Context::Bh,
        _ => Context::Idle,
    }
}

/// 关闭当前CPU调度器的抢占
///
pub fn dspmpt() {
    unsafe { xwrustffi_skd_dspmpt_lc(); }
}

/// 打开当前CPU调度器的抢占
///
pub fn enpmpt() {
    unsafe { xwrustffi_skd_enpmpt_lc(); }
}