# mod.rs - source
Source of the Rust file `src/task/mod.rs`.
Keywords: rust, rustlang, rust-lang

    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
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212

    mod context;
    mod id;
    mod manager;
    mod process;
    mod processor;
    mod signal;
    mod switch;
    pub mod stackless_coroutine;
    #[allow(clippy::module_inception)]
    mod task;

    use crate::fs::{open_file, OpenFlags};
    use alloc::sync::Arc;
    use lazy_static::*;
    use manager::fetch_task;
    use process::ProcessControlBlock;
    use switch::__switch;

    pub use context::TaskContext;
    pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle};
    pub use manager::{add_task, pid2process, remove_from_pid2process};
    pub use processor::{
        current_kstack_top, current_process, current_task, current_trap_cx, current_trap_cx_user_va,
        current_user_token, run_tasks, schedule, take_current_task,
    };
    pub use signal::SignalFlags;
    pub use task::{TaskControlBlock, TaskStatus};

    pub use stackless_coroutine::kernel_stackless_coroutine_test;

    pub fn suspend_current_and_run_next() {
        // There must be an application running.
        let task = take_current_task().unwrap();

        // ---- access current TCB exclusively
        let mut task_inner = task.inner_exclusive_access();
        let task_cx_ptr = &mut task_inner.task_cx as *mut TaskContext;
        // Change status to Ready
        task_inner.task_status = TaskStatus::Ready;
        drop(task_inner);
        // ---- release current TCB

        // push back to ready queue.
        add_task(task);
        // jump to scheduling cycle
        schedule(task_cx_ptr);
    }

    pub fn block_current_and_run_next() {
        let task = take_current_task().unwrap();
        let mut task_inner = task.inner_exclusive_access();
        let task_cx_ptr = &mut task_inner.task_cx as *mut TaskContext;
        task_inner.task_status = TaskStatus::Blocking;
        drop(task_inner);
        schedule(task_cx_ptr);
    }

    pub fn exit_current_and_run_next(exit_code: i32) {
        let task = take_current_task().unwrap();
        let mut task_inner = task.inner_exclusive_access();
        let process = task.process.upgrade().unwrap();
        let tid = task_inner.res.as_ref().unwrap().tid;
        // record exit code
        task_inner.exit_code = Some(exit_code);
        task_inner.res = None;
        // here we do not remove the thread since we are still using the kstack
        // it will be deallocated when sys_waittid is called
        drop(task_inner);
        drop(task);
        // however, if this is the main thread of current process
        // the process should terminate at once
        if tid == 0 {
            remove_from_pid2process(process.getpid());
            let mut process_inner = process.inner_exclusive_access();
            // mark this process as a zombie process
            process_inner.is_zombie = true;
            // record exit code of main process
            process_inner.exit_code = exit_code;

            {
                // move all child processes under init process
                let mut initproc_inner = INITPROC.inner_exclusive_access();
                for child in process_inner.children.iter() {
                    child.inner_exclusive_access().parent = Some(Arc::downgrade(&INITPROC));
                    initproc_inner.children.push(child.clone());
                }
            }

            // deallocate user res (including tid/trap_cx/ustack) of all threads
            // it has to be done before we dealloc the whole memory_set
            // otherwise they will be deallocated twice
            for task in process_inner.tasks.iter().filter(|t| t.is_some()) {
                let task = task.as_ref().unwrap();
                let mut task_inner = task.inner_exclusive_access();
                task_inner.res = None;
            }

            process_inner.children.clear();
            // deallocate other data in user space i.e. program code/data section
            process_inner.memory_set.recycle_data_pages();
            // drop file descriptors
            process_inner.fd_table.clear();
        }
        drop(process);
        // we do not have to save task context
        let mut _unused = TaskContext::zero_init();
        schedule(&mut _unused as *mut _);
    }

    lazy_static! {
        pub static ref INITPROC: Arc<ProcessControlBlock> = {
            let inode = open_file("initproc", OpenFlags::RDONLY).unwrap();
            let v = inode.read_all();
            ProcessControlBlock::new(v.as_slice())
        };
    }

    pub fn add_initproc() {
        let _initproc = INITPROC.clone();
    }

    pub fn check_signals_of_current() -> Option<(i32, &'static str)> {
        let process = current_process();
        let process_inner = process.inner_exclusive_access();
        process_inner.signals.check_error()
    }

    pub fn current_add_signal(signal: SignalFlags) {
        let process = current_process();
        let mut process_inner = process.inner_exclusive_access();
        process_inner.signals |= signal;
    }

    #[no_mangle]
    pub fn kthread_create(f: fn()) {

        println!("kthread_create");

        // create kernel thread
        let new_tcb = TaskControlBlock::create_kthread(f);
        // let kernel_stack = new_tcb.get_kernel_stack();
        let new_task = Arc::new(new_tcb);

        // add kernel thread into TASK_MANAGER
        // println!("add task");
        add_task(Arc::clone(&new_task));
    }

    #[no_mangle]
    pub fn kernel_stackful_coroutine_test() {
        println!("kernel_stackful_coroutine_test");
        kthread_create( ||
            {
                let id = 1;
                println!("kernel thread {:?} STARTING", id);
                for i in 0..10 {
                    println!("kernel thread: {} counter: {}", id, i);
                }
                println!("kernel thread {:?} FINISHED", id);
                kthread_stop();
            }
        );
        kthread_create( ||
            {
                let id = 2;
                println!("kernel thread {:?} STARTING", 2);
                for i in 0..10 {
                    println!("kernel thread: {} counter: {}", 2, i);
                    kthread_yield();
                }
                println!("kernel thread {:?} FINISHED", 2);
                kthread_stop();
            }
        );
        kthread_create( ||
            {
                let id = 3;
                println!("kernel thread {:?} STARTING", 3);
                for i in 0..10 {
                    println!("kernel thread: {} counter: {}", 3, i);
                    kthread_yield();
                }
                println!("kernel thread {:?} FINISHED", 3);
                kthread_stop();
            }
        );
    }

    pub fn kthread_stop(){
        do_exit();
    }
    #[no_mangle]
    pub fn do_exit(){
        println!("kthread do exit");
        exit_kthread_and_run_next(0);
        panic!("Unreachable in sys_exit!");
    }

    pub fn kthread_yield(){
        suspend_current_and_run_next();
    }

    #[no_mangle]
    pub fn exit_kthread_and_run_next(exit_code: i32) {
        println!("exit_kthread_and_run_next");
        // we do not have to save task context
        let mut _unused = TaskContext::zero_init();
        schedule(&mut _unused as *mut _);
    }