Create threads with a argument. See bin/threads_arg.rs
This commit is contained in:
parent
c951c1781e
commit
5b56961b8c
11 changed files with 59 additions and 19 deletions
|
@ -28,7 +28,7 @@ pub fn main() -> i32 {
|
|||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize) as usize);
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
|
|
|
@ -11,8 +11,8 @@ use core::sync::atomic::{AtomicBool, Ordering};
|
|||
|
||||
static mut A: usize = 0;
|
||||
static OCCUPIED: AtomicBool = AtomicBool::new(false);
|
||||
const PER_THREAD: usize = 100000;
|
||||
const THREAD_COUNT: usize = 8;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
|
@ -34,7 +34,7 @@ pub fn main() -> i32 {
|
|||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize) as usize);
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
|
|
|
@ -10,8 +10,8 @@ use alloc::vec::Vec;
|
|||
|
||||
static mut A: usize = 0;
|
||||
static mut OCCUPIED: bool = false;
|
||||
const PER_THREAD: usize = 10000;
|
||||
const THREAD_COUNT: usize = 8;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
|
@ -35,7 +35,7 @@ pub fn main() -> i32 {
|
|||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize) as usize);
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
|
|
|
@ -10,8 +10,8 @@ use user_lib::{mutex_blocking_create, mutex_lock, mutex_unlock};
|
|||
use alloc::vec::Vec;
|
||||
|
||||
static mut A: usize = 0;
|
||||
const PER_THREAD: usize = 10000;
|
||||
const THREAD_COUNT: usize = 8;
|
||||
const PER_THREAD: usize = 1000;
|
||||
const THREAD_COUNT: usize = 16;
|
||||
|
||||
unsafe fn f() -> ! {
|
||||
let mut t = 2usize;
|
||||
|
@ -32,7 +32,7 @@ pub fn main() -> i32 {
|
|||
assert_eq!(mutex_blocking_create(), 0);
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize) as usize);
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
|
|
|
@ -32,7 +32,7 @@ pub fn main() -> i32 {
|
|||
assert_eq!(mutex_create(), 0);
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..THREAD_COUNT {
|
||||
v.push(thread_create(f as usize) as usize);
|
||||
v.push(thread_create(f as usize, 0) as usize);
|
||||
}
|
||||
let mut time_cost = Vec::new();
|
||||
for tid in v.iter() {
|
||||
|
|
|
@ -26,9 +26,9 @@ pub fn thread_c() -> ! {
|
|||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut v = Vec::new();
|
||||
v.push(thread_create(thread_a as usize));
|
||||
v.push(thread_create(thread_b as usize));
|
||||
v.push(thread_create(thread_c as usize));
|
||||
v.push(thread_create(thread_a as usize, 0));
|
||||
v.push(thread_create(thread_b as usize, 0));
|
||||
v.push(thread_create(thread_c as usize, 0));
|
||||
for tid in v.iter() {
|
||||
let exit_code = waittid(*tid as usize);
|
||||
println!("thread#{} exited with code {}", tid, exit_code);
|
||||
|
|
39
user/src/bin/threads_arg.rs
Normal file
39
user/src/bin/threads_arg.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
#[macro_use]
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{thread_create, waittid, exit};
|
||||
use alloc::vec::Vec;
|
||||
|
||||
struct Argument {
|
||||
pub ch: char,
|
||||
pub rc: i32,
|
||||
}
|
||||
|
||||
fn thread_print(arg: *const Argument) -> ! {
|
||||
let arg = unsafe { &*arg };
|
||||
for _ in 0..1000 { print!("{}", arg.ch); }
|
||||
exit(arg.rc)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
let mut v = Vec::new();
|
||||
let args = [
|
||||
Argument { ch: 'a', rc: 1, },
|
||||
Argument { ch: 'b', rc: 2, },
|
||||
Argument { ch: 'c', rc: 3, },
|
||||
];
|
||||
for i in 0..3 {
|
||||
v.push(thread_create(thread_print as usize, &args[i] as *const _ as usize));
|
||||
}
|
||||
for tid in v.iter() {
|
||||
let exit_code = waittid(*tid as usize);
|
||||
println!("thread#{} exited with code {}", tid, exit_code);
|
||||
}
|
||||
println!("main thread exited.");
|
||||
0
|
||||
}
|
|
@ -104,7 +104,7 @@ pub fn sleep(sleep_ms: usize) {
|
|||
sys_sleep(sleep_ms);
|
||||
}
|
||||
|
||||
pub fn thread_create(entry: usize) -> isize { sys_thread_create(entry) }
|
||||
pub fn thread_create(entry: usize, arg: usize) -> isize { sys_thread_create(entry, arg) }
|
||||
pub fn gettid() -> isize { sys_gettid() }
|
||||
pub fn waittid(tid: usize) -> isize {
|
||||
loop {
|
||||
|
|
|
@ -90,8 +90,8 @@ pub fn sys_waitpid(pid: isize, exit_code: *mut i32) -> isize {
|
|||
syscall(SYSCALL_WAITPID, [pid as usize, exit_code as usize, 0])
|
||||
}
|
||||
|
||||
pub fn sys_thread_create(entry: usize) -> isize {
|
||||
syscall(SYSCALL_THREAD_CREATE, [entry, 0, 0])
|
||||
pub fn sys_thread_create(entry: usize, arg: usize) -> isize {
|
||||
syscall(SYSCALL_THREAD_CREATE, [entry, arg, 0])
|
||||
}
|
||||
|
||||
pub fn sys_gettid() -> isize {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue