mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 09:26:26 +04:00
cargo fmt
This commit is contained in:
parent
214898ccfd
commit
36b1d763f1
@ -144,7 +144,6 @@ pub fn frame_allocator_test() {
|
||||
println!("frame_allocator_test passed!");
|
||||
}
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn frame_allocator_alloc_more_test() {
|
||||
let mut v: Vec<FrameTracker> = Vec::new();
|
||||
|
@ -1,7 +1,7 @@
|
||||
pub mod port_table;
|
||||
pub mod socket;
|
||||
pub mod tcp;
|
||||
pub mod udp;
|
||||
pub mod port_table;
|
||||
|
||||
pub use lose_net_stack::IPv4;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use alloc::{vec::Vec, sync::Arc};
|
||||
use alloc::{sync::Arc, vec::Vec};
|
||||
use lazy_static::lazy_static;
|
||||
use lose_net_stack::packets::tcp::TCPPacket;
|
||||
|
||||
@ -11,7 +11,7 @@ use super::tcp::TCP;
|
||||
pub struct Port {
|
||||
pub port: u16,
|
||||
pub receivable: bool,
|
||||
pub schedule: Option<Arc<TaskControlBlock>>
|
||||
pub schedule: Option<Arc<TaskControlBlock>>,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
@ -32,7 +32,7 @@ pub fn listen(port: u16) -> Option<usize> {
|
||||
let listen_port = Port {
|
||||
port,
|
||||
receivable: false,
|
||||
schedule: None
|
||||
schedule: None,
|
||||
};
|
||||
|
||||
if index == usize::MAX {
|
||||
@ -66,10 +66,13 @@ pub fn port_acceptable(listen_index: usize) -> bool {
|
||||
// check whether it can accept request
|
||||
pub fn check_accept(port: u16, tcp_packet: &TCPPacket) -> Option<()> {
|
||||
LISTEN_TABLE.exclusive_session(|listen_table| {
|
||||
let mut listen_ports: Vec<&mut Option<Port>> = listen_table.iter_mut().filter(|x| match x {
|
||||
let mut listen_ports: Vec<&mut Option<Port>> = listen_table
|
||||
.iter_mut()
|
||||
.filter(|x| match x {
|
||||
Some(t) => t.port == port && t.receivable == true,
|
||||
None => false,
|
||||
}).collect();
|
||||
})
|
||||
.collect();
|
||||
if listen_ports.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
@ -90,7 +93,13 @@ pub fn accept_connection(_port: u16, tcp_packet: &TCPPacket, task: Arc<TaskContr
|
||||
let mut inner = process.inner_exclusive_access();
|
||||
let fd = inner.alloc_fd();
|
||||
|
||||
let tcp_socket = TCP::new(tcp_packet.source_ip, tcp_packet.dest_port, tcp_packet.source_port, tcp_packet.seq, tcp_packet.ack);
|
||||
let tcp_socket = TCP::new(
|
||||
tcp_packet.source_ip,
|
||||
tcp_packet.dest_port,
|
||||
tcp_packet.source_port,
|
||||
tcp_packet.seq,
|
||||
tcp_packet.ack,
|
||||
);
|
||||
|
||||
inner.fd_table[fd] = Some(Arc::new(tcp_socket));
|
||||
|
||||
@ -130,4 +139,3 @@ impl File for PortFd {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ pub struct Socket {
|
||||
pub rport: u16, // rempote port
|
||||
pub buffers: VecDeque<Vec<u8>>, // datas
|
||||
pub seq: u32,
|
||||
pub ack: u32
|
||||
pub ack: u32,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
@ -26,11 +26,9 @@ pub fn get_s_a_by_index(index: usize) -> Option<(u32, u32)> {
|
||||
|
||||
assert!(index < socket_table.len());
|
||||
|
||||
socket_table.get(index).map_or(None, |x| {
|
||||
match x {
|
||||
socket_table.get(index).map_or(None, |x| match x {
|
||||
Some(x) => Some((x.seq, x.ack)),
|
||||
None => None,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -40,9 +38,7 @@ pub fn set_s_a_by_index(index: usize, seq: u32, ack: u32) {
|
||||
assert!(socket_table.len() > index);
|
||||
assert!(socket_table[index].is_some());
|
||||
|
||||
let sock = socket_table[index]
|
||||
.as_mut()
|
||||
.unwrap();
|
||||
let sock = socket_table[index].as_mut().unwrap();
|
||||
|
||||
sock.ack = ack;
|
||||
sock.seq = seq;
|
||||
@ -84,7 +80,7 @@ pub fn add_socket(raddr: IPv4, lport: u16, rport: u16) -> Option<usize> {
|
||||
rport,
|
||||
buffers: VecDeque::new(),
|
||||
seq: 0,
|
||||
ack: 0
|
||||
ack: 0,
|
||||
};
|
||||
|
||||
if index == usize::MAX {
|
||||
|
@ -1,8 +1,8 @@
|
||||
use alloc::vec;
|
||||
use lose_net_stack::MacAddress;
|
||||
use lose_net_stack::IPv4;
|
||||
use lose_net_stack::TcpFlags;
|
||||
use lose_net_stack::packets::tcp::TCPPacket;
|
||||
use lose_net_stack::IPv4;
|
||||
use lose_net_stack::MacAddress;
|
||||
use lose_net_stack::TcpFlags;
|
||||
|
||||
use crate::{drivers::NET_DEVICE, fs::File};
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::sync::{Mutex, UPIntrFreeCell};
|
||||
use crate::task::{
|
||||
wakeup_task, block_current_and_run_next, block_current_task, current_task, TaskContext,
|
||||
block_current_and_run_next, block_current_task, current_task, wakeup_task, TaskContext,
|
||||
TaskControlBlock,
|
||||
};
|
||||
use alloc::{collections::VecDeque, sync::Arc};
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::UPIntrFreeCell;
|
||||
use crate::task::TaskControlBlock;
|
||||
use crate::task::{wakeup_task, current_task};
|
||||
use crate::task::{block_current_and_run_next, suspend_current_and_run_next};
|
||||
use crate::task::{current_task, wakeup_task};
|
||||
use alloc::{collections::VecDeque, sync::Arc};
|
||||
|
||||
pub trait Mutex: Sync + Send {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::sync::UPIntrFreeCell;
|
||||
use crate::task::{wakeup_task, block_current_and_run_next, current_task, TaskControlBlock};
|
||||
use crate::task::{block_current_and_run_next, current_task, wakeup_task, TaskControlBlock};
|
||||
use alloc::{collections::VecDeque, sync::Arc};
|
||||
|
||||
pub struct Semaphore {
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::net::port_table::{listen, PortFd, accept, port_acceptable};
|
||||
use crate::net::port_table::{accept, listen, port_acceptable, PortFd};
|
||||
use crate::net::udp::UDP;
|
||||
use crate::net::{IPv4, net_interrupt_handler};
|
||||
use crate::net::{net_interrupt_handler, IPv4};
|
||||
use crate::task::{current_process, current_task, current_trap_cx};
|
||||
use alloc::sync::Arc;
|
||||
|
||||
|
@ -19,7 +19,7 @@ use switch::__switch;
|
||||
|
||||
pub use context::TaskContext;
|
||||
pub use id::{kstack_alloc, pid_alloc, KernelStack, PidHandle, IDLE_PID};
|
||||
pub use manager::{add_task, wakeup_task, pid2process, remove_from_pid2process};
|
||||
pub use manager::{add_task, pid2process, remove_from_pid2process, wakeup_task};
|
||||
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,
|
||||
|
@ -40,7 +40,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..thread_count {
|
||||
|
@ -57,7 +57,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..thread_count {
|
||||
|
@ -42,7 +42,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
|
||||
let start = get_time();
|
||||
assert_eq!(mutex_blocking_create(), 0);
|
||||
|
@ -43,7 +43,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
|
||||
let start = get_time();
|
||||
assert_eq!(mutex_create(), 0);
|
||||
|
@ -9,8 +9,8 @@ extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid};
|
||||
use core::sync::atomic::{compiler_fence, Ordering};
|
||||
use user_lib::{exit, get_time, thread_create, waittid};
|
||||
|
||||
static mut A: usize = 0;
|
||||
static mut FLAG: [bool; 2] = [false; 2];
|
||||
@ -66,7 +66,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
|
||||
// uncomment this if you want to check the assembly
|
||||
// println!(
|
||||
@ -76,7 +78,10 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
// );
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
assert_eq!(thread_count, 2, "Peterson works when there are only 2 threads.");
|
||||
assert_eq!(
|
||||
thread_count, 2,
|
||||
"Peterson works when there are only 2 threads."
|
||||
);
|
||||
for id in 0..thread_count {
|
||||
v.push(thread_create(f as usize, id) as usize);
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, get_time, thread_create, waittid, yield_};
|
||||
use core::sync::atomic::{compiler_fence, Ordering};
|
||||
use user_lib::{exit, get_time, thread_create, waittid, yield_};
|
||||
|
||||
static mut A: usize = 0;
|
||||
static mut FLAG: [bool; 2] = [false; 2];
|
||||
@ -64,7 +64,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
|
||||
// uncomment this if you want to check the assembly
|
||||
// println!(
|
||||
@ -75,7 +77,10 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
assert_eq!(thread_count, 2, "Peterson works when there are only 2 threads.");
|
||||
assert_eq!(
|
||||
thread_count, 2,
|
||||
"Peterson works when there are only 2 threads."
|
||||
);
|
||||
for id in 0..thread_count {
|
||||
v.push(thread_create(f as usize, id) as usize);
|
||||
}
|
||||
|
@ -53,7 +53,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..thread_count {
|
||||
|
@ -55,7 +55,9 @@ pub fn main(argc: usize, argv: &[&str]) -> i32 {
|
||||
per_thread = argv[2].parse().unwrap();
|
||||
}
|
||||
}
|
||||
unsafe { PER_THREAD = per_thread; }
|
||||
unsafe {
|
||||
PER_THREAD = per_thread;
|
||||
}
|
||||
let start = get_time();
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..thread_count {
|
||||
|
@ -5,10 +5,13 @@
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{thread_create, exit, waittid, mutex_create, mutex_lock, mutex_unlock, condvar_create, condvar_signal, condvar_wait};
|
||||
use alloc::vec::Vec;
|
||||
use core::cell::UnsafeCell;
|
||||
use lazy_static::*;
|
||||
use user_lib::{
|
||||
condvar_create, condvar_signal, condvar_wait, exit, mutex_create, mutex_lock, mutex_unlock,
|
||||
thread_create, waittid,
|
||||
};
|
||||
|
||||
const THREAD_NUM: usize = 3;
|
||||
|
||||
@ -31,7 +34,9 @@ impl Barrier {
|
||||
let count = self.count.get();
|
||||
// SAFETY: Here, the accesses of the count is in the
|
||||
// critical section protected by the mutex.
|
||||
unsafe { *count = *count + 1; }
|
||||
unsafe {
|
||||
*count = *count + 1;
|
||||
}
|
||||
if unsafe { *count } == THREAD_NUM {
|
||||
condvar_signal(self.condvar_id);
|
||||
} else {
|
||||
@ -50,11 +55,17 @@ lazy_static! {
|
||||
}
|
||||
|
||||
fn thread_fn() {
|
||||
for _ in 0..300 { print!("a"); }
|
||||
for _ in 0..300 {
|
||||
print!("a");
|
||||
}
|
||||
BARRIER_AB.block();
|
||||
for _ in 0..300 { print!("b"); }
|
||||
for _ in 0..300 {
|
||||
print!("b");
|
||||
}
|
||||
BARRIER_BC.block();
|
||||
for _ in 0..300 { print!("c"); }
|
||||
for _ in 0..300 {
|
||||
print!("c");
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
|
||||
|
@ -5,8 +5,8 @@
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
|
||||
use user_lib::{thread_create, exit, waittid};
|
||||
use alloc::vec::Vec;
|
||||
use user_lib::{exit, thread_create, waittid};
|
||||
|
||||
const THREAD_NUM: usize = 3;
|
||||
|
||||
|
@ -9,7 +9,7 @@ extern crate alloc;
|
||||
use alloc::vec;
|
||||
use user_lib::exit;
|
||||
use user_lib::{
|
||||
semaphore_create, semaphore_down, semaphore_up, mutex_blocking_create, mutex_lock, mutex_unlock,
|
||||
mutex_blocking_create, mutex_lock, mutex_unlock, semaphore_create, semaphore_down, semaphore_up,
|
||||
};
|
||||
use user_lib::{sleep, thread_create, waittid};
|
||||
|
||||
|
@ -35,7 +35,11 @@ impl DrawingBoard {
|
||||
.into_styled(PrimitiveStyle::with_fill(Rgb888::BLUE))
|
||||
.draw(&mut self.disp)
|
||||
.ok();
|
||||
Triangle::new(self.latest_pos + Point::new(0, 150), self.latest_pos + Point::new(80, 200), self.latest_pos + Point::new(-120, 300))
|
||||
Triangle::new(
|
||||
self.latest_pos + Point::new(0, 150),
|
||||
self.latest_pos + Point::new(80, 200),
|
||||
self.latest_pos + Point::new(-120, 300),
|
||||
)
|
||||
.into_styled(PrimitiveStyle::with_stroke(Rgb888::GREEN, 10))
|
||||
.draw(&mut self.disp)
|
||||
.ok();
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::{VIRTGPU_XRES, VIRTGPU_YRES, Display};
|
||||
use embedded_graphics::prelude::Size;
|
||||
use user_lib::{Display, VIRTGPU_XRES, VIRTGPU_YRES};
|
||||
|
||||
#[no_mangle]
|
||||
pub fn main() -> i32 {
|
||||
|
@ -1,11 +1,11 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
extern crate user_lib;
|
||||
extern crate alloc;
|
||||
extern crate user_lib;
|
||||
|
||||
use user_lib::console::getchar;
|
||||
use user_lib::{Display, key_pressed, sleep, VIRTGPU_XRES, VIRTGPU_YRES};
|
||||
use user_lib::{key_pressed, sleep, Display, VIRTGPU_XRES, VIRTGPU_YRES};
|
||||
|
||||
use embedded_graphics::pixelcolor::*;
|
||||
use embedded_graphics::prelude::{Drawable, Point, RgbColor, Size};
|
||||
|
@ -10,7 +10,7 @@ extern crate alloc;
|
||||
|
||||
// use http://localhost:6201/ to access the http server
|
||||
|
||||
use user_lib::{read, write, listen, accept};
|
||||
use user_lib::{accept, listen, read, write};
|
||||
|
||||
// get url from the tcp request list.
|
||||
fn get_url_from_tcp_request(req: &[u8]) -> String {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
use embedded_graphics::pixelcolor::Rgb888;
|
||||
use embedded_graphics::prelude::{RgbColor, Size};
|
||||
use embedded_graphics::{draw_target::DrawTarget, prelude::OriginDimensions};
|
||||
use embedded_graphics::pixelcolor::Rgb888;
|
||||
use virtio_input_decoder::Decoder;
|
||||
pub use virtio_input_decoder::{DecodeType, Key, KeyType, Mouse};
|
||||
|
||||
@ -24,8 +24,7 @@ pub struct Display {
|
||||
impl Display {
|
||||
pub fn new(size: Size) -> Self {
|
||||
let fb_ptr = framebuffer() as *mut u8;
|
||||
let fb =
|
||||
unsafe { core::slice::from_raw_parts_mut(fb_ptr, VIRTGPU_LEN as usize) };
|
||||
let fb = unsafe { core::slice::from_raw_parts_mut(fb_ptr, VIRTGPU_LEN as usize) };
|
||||
Self { size, fb }
|
||||
}
|
||||
pub fn framebuffer(&mut self) -> &mut [u8] {
|
||||
@ -53,9 +52,7 @@ impl DrawTarget for Display {
|
||||
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
|
||||
{
|
||||
pixels.into_iter().for_each(|px| {
|
||||
let idx = (px.0.y * VIRTGPU_XRES as i32 + px.0.x)
|
||||
as usize
|
||||
* 4;
|
||||
let idx = (px.0.y * VIRTGPU_XRES as i32 + px.0.x) as usize * 4;
|
||||
if idx + 2 >= self.fb.len() {
|
||||
return;
|
||||
}
|
||||
@ -113,6 +110,7 @@ impl InputEvent {
|
||||
self.event_type as usize,
|
||||
self.code as usize,
|
||||
self.value as usize,
|
||||
).ok()
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user