#![no_std]
#![no_main]

#[macro_use]
extern crate user_lib;
extern crate alloc;

use alloc::vec;
use user_lib::{exit, thread_create, waittid};

// 教学目标：
// 验证线程创建、并发输出和 waittid 回收流程（含批量创建场景）。

pub fn thread_a() -> isize {
    for _ in 0..1000 {
        print!("a");
    }
    exit(1)
}

pub fn thread_b() -> isize {
    for _ in 0..1000 {
        print!("b");
    }
    exit(2)
}

pub fn thread_c() -> isize {
    for _ in 0..1000 {
        print!("c");
    }
    exit(3)
}

#[unsafe(no_mangle)]
pub extern "C" fn main() -> i32 {
    let mut v = vec![
        thread_create(thread_a as *const () as usize, 0),
        thread_create(thread_b as *const () as usize, 0),
        thread_create(thread_c as *const () as usize, 0),
    ];
    println!("v :{:?}", v);
    let max_len = 5;
    for i in 0..max_len {
        println!("create tid: {}", i + 4);
        let tid = thread_create(thread_b as *const () as usize, 0);
        println!("create tid: {} end", i + 4);
        v.push(tid);
    }
    println!("create end");
    for tid in v.iter() {
        let exit_code = waittid(*tid as usize);
        println!("thread#{} exited with code {}", tid, exit_code);
    }
    println!("main thread exited.");
    println!("threads test passed!");
    0
}
