Skip to content

Commit

Permalink
Merge pull request #177 from HeartLinked/dev_test_asyncgen
Browse files Browse the repository at this point in the history
feat: add sys_clock_getres and fix: poll bug of dgram unix socket
  • Loading branch information
ken4647 authored Feb 19, 2025
2 parents cfd3040 + aaf7896 commit 62068cd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 38 deletions.
18 changes: 18 additions & 0 deletions api/ruxos_posix_api/src/imp/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ pub unsafe fn sys_clock_settime(_clk: ctypes::clockid_t, ts: *const ctypes::time
})
}

/// Return the resolution (precision) of a specified clock `clk_id`.
/// TODO: Currently we only have one simple clock source in the OS.
/// We ignore `clk_id` and always return a fixed resolution of 1ns.
/// In the future, we may:
/// - Distinguish different clock IDs (e.g., REALTIME vs MONOTONIC).
/// - Return a more realistic resolution based on hardware capabilities.
pub unsafe fn sys_clock_getres(clk_id: ctypes::clockid_t, ts: *mut ctypes::timespec) -> c_int {
syscall_body!(sys_clock_getres, {
if ts.is_null() {
return Err(LinuxError::EFAULT);
}
(*ts).tv_sec = 0;
(*ts).tv_nsec = 1;
debug!("sys_clock_getres: clk_id={}, returning 0s + 1ns", clk_id);
Ok(0)
})
}

/// Sleep until some nanoseconds
///
/// TODO: should be woken by signals, and set errno
Expand Down
4 changes: 2 additions & 2 deletions api/ruxos_posix_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub use imp::sys_invalid;
pub use imp::task::sys_wait4;
pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield};
pub use imp::time::{
sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_gettimeofday, sys_nanosleep,
sys_times,
sys_clock_getres, sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_gettimeofday,
sys_nanosleep, sys_times,
};

#[cfg(all(feature = "fd", feature = "musl"))]
Expand Down
89 changes: 53 additions & 36 deletions modules/ruxnet/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ruxfs::root::{create_file, lookup};
use ruxtask::yield_now;

const SOCK_ADDR_UN_PATH_LEN: usize = 108;
const MAX_DGRAM_QUEUE_SIZE: usize = 1024;
static ANONYMOUS_ADDR_COUNTER: AtomicUsize = AtomicUsize::new(0);

/// rust form for ctype sockaddr_un
Expand Down Expand Up @@ -589,47 +590,63 @@ impl UnixSocket {
/// Polls the socket's readiness for reading or writing.
pub fn poll(&self) -> LinuxResult<PollState> {
let now_state = self.get_state();
match now_state {
UnixSocketStatus::Connecting => self.poll_connect(),
UnixSocketStatus::Connected => {
let remote_is_close = {
let remote_handle = self.get_peerhandle();
match remote_handle {
Some(handle) => {
let mut binding = UNIX_TABLE.write();
if let Some(inner) = binding.get_mut(handle) {
inner.lock().get_state() == UnixSocketStatus::Closed
} else {
true
match self.get_sockettype() {
UnixSocketType::SockStream => match now_state {
UnixSocketStatus::Connecting => self.poll_connect(),
UnixSocketStatus::Connected => {
let remote_is_close = {
let remote_handle = self.get_peerhandle();
match remote_handle {
Some(handle) => {
let mut binding = UNIX_TABLE.write();
if let Some(inner) = binding.get_mut(handle) {
inner.lock().get_state() == UnixSocketStatus::Closed
} else {
true
}
}
None => {
return Err(LinuxError::ENOTCONN);
}
}
None => {
return Err(LinuxError::ENOTCONN);
}
}
};
let mut binding = UNIX_TABLE.write();
let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock();
Ok(PollState {
readable: !socket_inner.may_recv() || socket_inner.can_recv(),
writable: !socket_inner.may_send() || socket_inner.can_send(),
pollhup: remote_is_close,
})
}
UnixSocketStatus::Listening => {
let mut binding = UNIX_TABLE.write();
let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock();
Ok(PollState {
readable: socket_inner.can_accept(),
};
let mut binding = UNIX_TABLE.write();
let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock();
Ok(PollState {
readable: !socket_inner.may_recv() || socket_inner.can_recv(),
writable: !socket_inner.may_send() || socket_inner.can_send(),
pollhup: remote_is_close,
})
}
UnixSocketStatus::Listening => {
let mut binding = UNIX_TABLE.write();
let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock();
Ok(PollState {
readable: socket_inner.can_accept(),
writable: false,
pollhup: false,
})
}
_ => Ok(PollState {
readable: false,
writable: false,
pollhup: false,
}),
},
UnixSocketType::SockDgram => {
let binding = UNIX_TABLE.read();
let socket_inner = binding.get(self.get_sockethandle()).unwrap().lock();

let readable = socket_inner.datagram_queue.len() > 0;
let writable = true;
let pollhup = false;
Ok(PollState {
readable,
writable,
pollhup,
})
}
_ => Ok(PollState {
readable: false,
writable: false,
pollhup: false,
}),
UnixSocketType::SockSeqpacket => unimplemented!(),
}
}

Expand Down Expand Up @@ -796,7 +813,7 @@ impl UnixSocket {
}

// check if the target socket is connected
if target_inner.datagram_queue.len() >= target_inner.buf.capacity() {
if target_inner.datagram_queue.len() >= MAX_DGRAM_QUEUE_SIZE {
return Err(LinuxError::EAGAIN);
}
target_inner
Expand Down
4 changes: 4 additions & 0 deletions ulib/ruxmusl/src/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize {
args[0] as ctypes::clockid_t,
args[1] as *mut ctypes::timespec,
) as _,
SyscallId::CLOCK_GETRES => ruxos_posix_api::sys_clock_getres(
args[0] as ctypes::clockid_t,
args[1] as *mut ctypes::timespec,
) as _,
SyscallId::CLOCK_NANOSLEEP => ruxos_posix_api::sys_clock_nanosleep(
args[0] as ctypes::clockid_t,
args[1] as c_int,
Expand Down
1 change: 1 addition & 0 deletions ulib/ruxmusl/src/aarch64/syscall_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum SyscallId {
NANO_SLEEP = 101,
CLOCK_SETTIME = 112,
CLOCK_GETTIME = 113,
CLOCK_GETRES = 114,
CLOCK_NANOSLEEP = 115,
SCHED_YIELD = 124,
#[cfg(feature = "signal")]
Expand Down

0 comments on commit 62068cd

Please sign in to comment.