From 85e1f0973b8e0e4aa3272edcd0f03cdb06eeb905 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Fri, 25 Oct 2024 00:05:09 +0800 Subject: [PATCH] Shorten type names --- kernel/src/fs/epoll/entry.rs | 68 ++++++++++++++++++------------------ kernel/src/fs/epoll/file.rs | 39 +++++++++------------ 2 files changed, 51 insertions(+), 56 deletions(-) diff --git a/kernel/src/fs/epoll/entry.rs b/kernel/src/fs/epoll/entry.rs index 571dbb83c..383cfdf72 100644 --- a/kernel/src/fs/epoll/entry.rs +++ b/kernel/src/fs/epoll/entry.rs @@ -11,7 +11,7 @@ use ostd::sync::{LocalIrqDisabled, Mutex, MutexGuard, SpinLock, SpinLockGuard}; use super::{EpollEvent, EpollFlags}; use crate::{ - events::{IoEvents, Observer}, + events::{self, IoEvents}, fs::{file_handle::FileLike, file_table::FileDesc}, process::signal::{PollHandle, Pollee}, }; @@ -19,25 +19,25 @@ use crate::{ /// An epoll entry that is contained in an epoll file. /// /// Each epoll entry can be added, modified, or deleted by the `EpollCtl` command. -pub(super) struct EpollEntry { +pub(super) struct Entry { // The file descriptor and the file. - key: EpollEntryKey, + key: EntryKey, // The event masks and flags. - inner: Mutex, + inner: Mutex, // The observer that receives events. // - // Keep this in a separate `Arc` to avoid dropping `EpollEntry` in the observer callback, which - // may cause deadlocks. - observer: Arc, + // Keep this in a separate `Arc` to avoid dropping `Entry` in the observer callback, which may + // cause deadlocks. + observer: Arc, } #[derive(PartialEq, Eq, PartialOrd, Ord)] -pub(super) struct EpollEntryKey { +pub(super) struct EntryKey { fd: FileDesc, file: KeyableWeak, } -impl From<(FileDesc, KeyableWeak)> for EpollEntryKey { +impl From<(FileDesc, KeyableWeak)> for EntryKey { fn from(value: (FileDesc, KeyableWeak)) -> Self { Self { fd: value.0, @@ -46,7 +46,7 @@ impl From<(FileDesc, KeyableWeak)> for EpollEntryKey { } } -impl From<(FileDesc, &Arc)> for EpollEntryKey { +impl From<(FileDesc, &Arc)> for EntryKey { fn from(value: (FileDesc, &Arc)) -> Self { Self { fd: value.0, @@ -55,13 +55,13 @@ impl From<(FileDesc, &Arc)> for EpollEntryKey { } } -struct EpollEntryInner { +struct Inner { event: EpollEvent, flags: EpollFlags, poller: PollHandle, } -impl EpollEntry { +impl Entry { /// Creates a new epoll entry associated with the given epoll file. pub(super) fn new( fd: FileDesc, @@ -69,9 +69,9 @@ impl EpollEntry { ready_set: Arc, ) -> Arc { Arc::new_cyclic(|me| { - let observer = Arc::new(EpollEntryObserver::new(ready_set, me.clone())); + let observer = Arc::new(Observer::new(ready_set, me.clone())); - let inner = EpollEntryInner { + let inner = Inner { event: EpollEvent { events: IoEvents::empty(), user_data: 0, @@ -81,7 +81,7 @@ impl EpollEntry { }; Self { - key: EpollEntryKey { fd, file }, + key: EntryKey { fd, file }, inner: Mutex::new(inner), observer, } @@ -163,12 +163,12 @@ impl EpollEntry { } /// Gets the underlying observer. - pub(super) fn observer(&self) -> &EpollEntryObserver { + pub(super) fn observer(&self) -> &Observer { &self.observer } /// Gets the key associated with the epoll entry. - pub(super) fn key(&self) -> &EpollEntryKey { + pub(super) fn key(&self) -> &EntryKey { &self.key } @@ -183,8 +183,8 @@ impl EpollEntry { } } -/// A observer for [`EpollEntry`] that can receive events. -pub(super) struct EpollEntryObserver { +/// A observer for [`Entry`] that can receive events. +pub(super) struct Observer { // Whether the entry is enabled. is_enabled: AtomicBool, // Whether the entry is in the ready list. @@ -192,11 +192,11 @@ pub(super) struct EpollEntryObserver { // The ready set of the epoll file that contains this epoll entry. ready_set: Arc, // The epoll entry itself (always inside an `Arc`). - weak_entry: Weak, + weak_entry: Weak, } -impl EpollEntryObserver { - fn new(ready_set: Arc, weak_entry: Weak) -> Self { +impl Observer { + fn new(ready_set: Arc, weak_entry: Weak) -> Self { Self { is_enabled: AtomicBool::new(false), is_ready: AtomicBool::new(false), @@ -219,7 +219,7 @@ impl EpollEntryObserver { /// This method must be called while holding the lock of the ready list. This is the only way /// to ensure that the "is ready" state matches the fact that the entry is actually in the /// ready list. - fn set_ready(&self, _guard: &SpinLockGuard>, LocalIrqDisabled>) { + fn set_ready(&self, _guard: &SpinLockGuard>, LocalIrqDisabled>) { self.is_ready.store(true, Ordering::Relaxed); } @@ -228,7 +228,7 @@ impl EpollEntryObserver { /// This method must be called while holding the lock of the ready list. This is the only way /// to ensure that the "is ready" state matches the fact that the entry is actually in the /// ready list. - fn reset_ready(&self, _guard: &SpinLockGuard>, LocalIrqDisabled>) { + fn reset_ready(&self, _guard: &SpinLockGuard>, LocalIrqDisabled>) { self.is_ready.store(false, Ordering::Relaxed) } @@ -246,7 +246,7 @@ impl EpollEntryObserver { /// This method must be called while holding the lock of the event masks and flags. This is the /// only way to ensure that the "is enabled" state describes the correct combination of the /// event masks and flags. - fn set_enabled(&self, _guard: &MutexGuard) { + fn set_enabled(&self, _guard: &MutexGuard) { self.is_enabled.store(true, Ordering::Relaxed) } @@ -255,17 +255,17 @@ impl EpollEntryObserver { /// This method must be called while holding the lock of the event masks and flags. This is the /// only way to ensure that the "is enabled" state describes the correct combination of the /// event masks and flags. - fn reset_enabled(&self, _guard: &MutexGuard) { + fn reset_enabled(&self, _guard: &MutexGuard) { self.is_enabled.store(false, Ordering::Relaxed) } /// Gets an instance of `Weak` that refers to the epoll entry. - fn weak_entry(&self) -> &Weak { + fn weak_entry(&self) -> &Weak { &self.weak_entry } } -impl Observer for EpollEntryObserver { +impl events::Observer for Observer { fn on_events(&self, _events: &IoEvents) { self.ready_set.push(self); } @@ -274,7 +274,7 @@ impl Observer for EpollEntryObserver { /// A set of ready epoll entries. pub(super) struct ReadySet { // Entries that are probably ready (having events happened). - entries: SpinLock>, LocalIrqDisabled>, + entries: SpinLock>, LocalIrqDisabled>, // A guard to ensure that ready entries can be popped by one thread at a time. pop_guard: Mutex, // A pollee for the ready set (i.e., for `EpollFile` itself). @@ -292,16 +292,16 @@ impl ReadySet { } } - pub(super) fn push(&self, observer: &EpollEntryObserver) { - // Note that we cannot take the `EpollEntryInner` lock because we are in the callback of - // the event observer. Doing so will cause dead locks due to inconsistent locking orders. + pub(super) fn push(&self, observer: &Observer) { + // Note that we cannot take the `Inner` lock because we are in the callback of the event + // observer. Doing so will cause dead locks due to inconsistent locking orders. // // We don't need to take the lock because // - We always call `file.poll()` immediately after calling `self.set_enabled()` and // `file.register_observer()`, so all events are caught either here or by the immediate // poll; in other words, we don't lose any events. // - Catching spurious events here is always fine because we always check them later before - // returning events to the user (in `EpollEntry::poll`). + // returning events to the user (in `Entry::poll`). if !observer.is_enabled() { return; } @@ -340,7 +340,7 @@ pub(super) struct ReadySetPopIter<'a> { } impl Iterator for ReadySetPopIter<'_> { - type Item = Arc; + type Item = Arc; fn next(&mut self) -> Option { if self.limit == Some(0) { diff --git a/kernel/src/fs/epoll/file.rs b/kernel/src/fs/epoll/file.rs index 879b62c93..f11845309 100644 --- a/kernel/src/fs/epoll/file.rs +++ b/kernel/src/fs/epoll/file.rs @@ -7,7 +7,7 @@ use keyable_arc::KeyableWeak; use ostd::sync::Mutex; use super::{ - entry::{EpollEntry, EpollEntryKey, ReadySet}, + entry::{Entry, EntryKey, ReadySet}, EpollCtl, EpollEvent, EpollFlags, }; use crate::{ @@ -35,7 +35,7 @@ use crate::{ /// event happens on the file. pub struct EpollFile { // All interesting entries. - interest: Mutex>, + interest: Mutex>, // A set of ready entries. // // Keep this in a separate `Arc` to avoid dropping `EpollFile` in the observer callback, which @@ -90,14 +90,14 @@ impl EpollFile { let ready_entry = { let mut interest = self.interest.lock(); - if interest.contains(&EpollEntryKey::from((fd, &file))) { + if interest.contains(&EntryKey::from((fd, &file))) { return_errno_with_message!( Errno::EEXIST, "the file is already in the interest list" ); } - let entry = EpollEntry::new(fd, Arc::downgrade(&file).into(), self.ready.clone()); + let entry = Entry::new(fd, Arc::downgrade(&file).into(), self.ready.clone()); let events = entry.update(ep_event, ep_flags); let ready_entry = if !events.is_empty() { @@ -130,11 +130,7 @@ impl EpollFile { // because the strong reference count will reach zero and `Weak::upgrade` // will fail. - if !self - .interest - .lock() - .remove(&EpollEntryKey::from((fd, file))) - { + if !self.interest.lock().remove(&EntryKey::from((fd, file))) { return_errno_with_message!(Errno::ENOENT, "the file is not in the interest list"); } @@ -154,9 +150,8 @@ impl EpollFile { let ready_entry = { let interest = self.interest.lock(); - let EpollEntryHolder(entry) = interest - .get(&EpollEntryKey::from((fd, &file))) - .ok_or_else(|| { + let EntryHolder(entry) = + interest.get(&EntryKey::from((fd, &file))).ok_or_else(|| { Error::with_message(Errno::ENOENT, "the file is not in the interest list") })?; let events = entry.update(new_ep_event, new_ep_flags); @@ -281,38 +276,38 @@ impl FileLike for EpollFile { } } -struct EpollEntryHolder(Arc); +struct EntryHolder(Arc); -impl PartialOrd for EpollEntryHolder { +impl PartialOrd for EntryHolder { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl Ord for EpollEntryHolder { +impl Ord for EntryHolder { fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.0.key().cmp(other.0.key()) } } -impl PartialEq for EpollEntryHolder { +impl PartialEq for EntryHolder { fn eq(&self, other: &Self) -> bool { self.0.key().eq(other.0.key()) } } -impl Eq for EpollEntryHolder {} +impl Eq for EntryHolder {} -impl Borrow for EpollEntryHolder { - fn borrow(&self) -> &EpollEntryKey { +impl Borrow for EntryHolder { + fn borrow(&self) -> &EntryKey { self.0.key() } } -impl From> for EpollEntryHolder { - fn from(value: Arc) -> Self { +impl From> for EntryHolder { + fn from(value: Arc) -> Self { Self(value) } } -impl Drop for EpollEntryHolder { +impl Drop for EntryHolder { fn drop(&mut self) { self.0.shutdown(); }