Skip to content

Commit

Permalink
release: v0.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Jun 10, 2024
1 parent 131af9c commit bd5a9b4
Show file tree
Hide file tree
Showing 53 changed files with 1,406 additions and 768 deletions.
18 changes: 13 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions pkg/app/brk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ggos_brk"
version = "0.0.1"
edition = "2021"
authors = ["GZTime <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lib = { path="../../lib", package="gglib", default-features=false, features=["kernel_alloc"] }
116 changes: 116 additions & 0 deletions pkg/app/brk/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#![no_std]
#![no_main]

extern crate alloc;

use alloc::vec::Vec;
use lib::*;

extern crate lib;

fn main() -> isize {
println!("Welcome to Brk Test!");

pre_test();

loop {
print!("$ ");
let command = stdin().read_line();
let line: Vec<&str> = command.trim().split(' ').collect();

match line[0] {
"brk" => {
if line.len() != 2 {
println!("Usage: brk <addr>");
continue;
}

let addr = if line[1].starts_with("0x") {
usize::from_str_radix(&line[1][2..], 16)
} else {
line[1].parse::<usize>()
};

let addr = match addr {
Ok(addr) => addr,
Err(_) => {
println!("Invalid address: {}", line[1]);
continue;
}
};

match sys_brk(Some(addr)) {
Some(new_brk) => {
println!("Brk to {:#x} success, new brk addr: {:#x}", addr, new_brk)
}
None => println!("Brk to {:#x} failed", addr),
}

sys_stat();
}
"cur" => match sys_brk(None) {
Some(brk) => println!("Current brk addr: {:#x}", brk),
None => println!("Failed to get current brk addr"),
},
"exit" => {
break;
}
_ => {
println!("Unknown command: {}", line[0]);
}
}
}

0
}

fn pre_test() {
println!("Pre-test: Brk Test");

let brk = sys_brk(None);

let brk = match brk {
Some(brk) => {
println!("Current brk addr: {:#x}", brk);
brk
}
None => {
println!("Failed to get current brk addr");
sys_exit(1);
}
};

let new_brk = sys_brk(Some(brk + 0x1000));

match new_brk {
Some(new_brk) => {
println!(
"Brk to {:#x} success, new brk addr: {:#x}",
brk + 0x1000,
new_brk
);
sys_stat();
}
None => {
println!("Brk to {:#x} failed", brk + 0x1000);
sys_exit(1);
}
}

let new_brk = sys_brk(Some(brk));

match new_brk {
Some(new_brk) => {
println!("Brk to {:#x} success, new brk addr: {:#x}", brk, new_brk);
sys_stat();
}
None => {
println!("Brk to {:#x} failed", brk);
sys_exit(1);
}
}

println!("Pre-test finished");
}

entry!(main);
2 changes: 1 addition & 1 deletion pkg/app/clock/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Dimensions for SysDisplay {
}
}

impl<'a> DrawTarget for SysDisplay {
impl DrawTarget for SysDisplay {
type Color = Rgb888;
type Error = ();

Expand Down
3 changes: 2 additions & 1 deletion pkg/app/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]
#![allow(unreachable_code)]
#![allow(clippy::diverging_sub_expression)]

use embedded_graphics::pixelcolor::Rgb888;
use lib::*;
Expand Down Expand Up @@ -39,7 +40,7 @@ fn clock() -> ! {
let value = angle / 180f32 * core::f32::consts::PI;

let len = 24i32;
let (cx, cy) = (cx as i32 - len - 10, len + 8);
let (cx, cy) = (cx - len - 10, len + 8);

let (dx, dy) = (
(len as f32 * value.cos()) as i32,
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/dining/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ggos_dining"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["GZTime <[email protected]>"]

Expand Down
55 changes: 33 additions & 22 deletions pkg/app/dining/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ use lib::*;
extern crate lib;

static CHOPSTICK: [Semaphore; 5] = semaphore_array![0, 1, 2, 3, 4];
static WAITER: Semaphore = Semaphore::new(64);

fn main() -> usize {
fn main() -> isize {
let mut pids = [0u16; 5];

for i in 0..5 {
CHOPSTICK[i].init(1);
// allow 4 philosophers to eat at the same time
WAITER.init(4);

for chop in &CHOPSTICK {
chop.init(1);
}

for i in 0..5 {
for (i, item) in pids.iter_mut().enumerate() {
let pid = sys_fork();
if pid == 0 {
philosopher(i);
} else {
pids[i] = pid;
*item = pid;
}
}

Expand All @@ -29,33 +33,40 @@ fn main() -> usize {

sys_stat();

for i in 0..5 {
println!("#{} Waiting for #{}...", cpid, pids[i]);
sys_wait_pid(pids[i]);
for pid in pids {
println!("#{} Waiting for #{}...", cpid, pid);
sys_wait_pid(pid);
}

0
}

fn philosopher(id: usize) -> ! {
let pid = sys_get_pid();
for _ in 0..20 {
if id == 0 {
println!("philosopher #{}({}) is sleeping...", id, pid);
core::hint::spin_loop();
}

println!("philosopher #{}({}) is thinking...", id, pid);

CHOPSTICK[id].acquire();
CHOPSTICK[(id + 1) % 5].acquire();

println!("philosopher #{}({}) is eating...", id, pid);

CHOPSTICK[(id + 1) % 5].release();
CHOPSTICK[id].release();
for _ in 0..100 {
// thinking
println!("philosopher #{} ({}) is thinking...", id, pid);
delay();

// hungry
WAITER.wait();
CHOPSTICK[id].wait();
CHOPSTICK[(id + 1) % 5].wait();
println!("philosopher #{} ({}) is eating...", id, pid);
CHOPSTICK[(id + 1) % 5].signal();
CHOPSTICK[id].signal();
WAITER.signal();
}
sys_exit(0);
}

#[inline(never)]
#[no_mangle]
fn delay() {
for _ in 0..100 {
core::hint::spin_loop();
}
}

entry!(main);
2 changes: 1 addition & 1 deletion pkg/app/fact/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn factorial(n: u64) -> u64 {
}
}

fn main() -> usize {
fn main() -> isize {
print!("Input n: ");

let input = lib::stdin().read_line();
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/fork/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ggos_fork"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["GZTime <[email protected]>"]

Expand Down
20 changes: 16 additions & 4 deletions pkg/app/fork/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,53 @@

extern crate alloc;
extern crate lib;

use lib::*;

static mut M: u64 = 0xdeadbeef;

fn main() -> usize {
fn main() -> isize {
let mut c = 32;

// do not alloc heap before `fork`
// which may cause unexpected behavior since we won't copy the heap in `fork`
let ret = sys_fork();
let pid = sys_fork();

if ret == 0 {
if pid == 0 {
println!("I am the child process");

assert_eq!(c, 32);

unsafe {
println!("child read value of M: {:#x}", M);
M = 0x2333;
println!("child changed the value of M: {:#x}", M);
}

c += 32;
} else {
println!("I am the parent process");

sys_stat();

assert_eq!(c, 32);

println!("Waiting for child to exit...");

let ret = sys_wait_pid(ret);
let ret = sys_wait_pid(pid);

println!("Child exited with status {}", ret);

assert_eq!(ret, 64);

unsafe {
println!("parent read value of M: {:#x}", M);
assert_eq!(M, 0x2333);
}

c += 1024;

assert_eq!(c, 1056);
}

c
Expand Down
Loading

0 comments on commit bd5a9b4

Please sign in to comment.