Skip to content

Commit

Permalink
improve the readybility and the error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Feb 4, 2024
1 parent 4632580 commit 9a31a04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/fd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,19 +380,29 @@ pub(crate) fn poll(fds: &mut [PollFd], timeout: i32) -> Result<(), IoError> {
}
}

#[inline]
async fn async_get_object(fd: FileDescriptor) -> Result<Arc<dyn ObjectInterface>, IoError> {
Ok((*(OBJECT_MAP.read().await.get(&fd).ok_or(IoError::EINVAL)?)).clone())
}

pub(crate) fn get_object(fd: FileDescriptor) -> Result<Arc<dyn ObjectInterface>, IoError> {
block_on(async_get_object(fd), None)
}

async fn async_get_object(fd: FileDescriptor) -> Result<Arc<dyn ObjectInterface>, IoError> {
Ok((*(OBJECT_MAP.read().await.get(&fd).ok_or(IoError::EINVAL)?)).clone())
#[inline]
async fn async_insert_object(
fd: FileDescriptor,
obj: Arc<dyn ObjectInterface>,
) -> Result<(), IoError> {
let _ = OBJECT_MAP.write().await.insert(fd, obj);
Ok(())
}

pub(crate) fn insert_object(
fd: FileDescriptor,
obj: Arc<dyn ObjectInterface>,
) -> Option<Arc<dyn ObjectInterface>> {
block_on(async { Ok(OBJECT_MAP.write().await.insert(fd, obj)) }, None).unwrap()
) -> Result<(), IoError> {
block_on(async_insert_object(fd, obj), None)
}

// The dup system call allocates a new file descriptor that refers
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ extern "C" fn initd(_arg: usize) {
riscv64::kernel::init_drivers();

syscalls::init();
fd::init().unwrap();
fd::init().expect("Unable to initialized standard file descriptors");
fs::init();

// Get the application arguments and environment variables.
Expand Down
8 changes: 4 additions & 4 deletions src/syscalls/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ extern "C" fn __sys_socket(domain: i32, type_: i32, protocol: i32) -> i32 {
drop(guard);
let socket = udp::Socket::new(handle);

insert_object(fd, Arc::new(socket));
insert_object(fd, Arc::new(socket)).expect("FD is already used");

return fd;
}
Expand All @@ -281,7 +281,7 @@ extern "C" fn __sys_socket(domain: i32, type_: i32, protocol: i32) -> i32 {
drop(guard);
let socket = tcp::Socket::new(handle);

insert_object(fd, Arc::new(socket));
insert_object(fd, Arc::new(socket)).expect("FD is already used");

return fd;
}
Expand All @@ -302,9 +302,9 @@ extern "C" fn __sys_accept(fd: i32, addr: *mut sockaddr, addrlen: *mut socklen_t
|e| -num::ToPrimitive::to_i32(&e).unwrap(),
|endpoint| {
let new_obj = dyn_clone::clone_box(&*v);
insert_object(fd, Arc::from(new_obj));
insert_object(fd, Arc::from(new_obj)).expect("FD is already used");
let new_fd = FD_COUNTER.fetch_add(1, Ordering::SeqCst);
insert_object(new_fd, v.clone());
insert_object(new_fd, v.clone()).expect("FD is already used");

if !addr.is_null() && !addrlen.is_null() {
let addrlen = unsafe { &mut *addrlen };
Expand Down

0 comments on commit 9a31a04

Please sign in to comment.