Shorten type names

This commit is contained in:
Ruihan Li
2024-10-25 00:05:09 +08:00
committed by Tate, Hongliang Tian
parent 781ee0fa71
commit 85e1f0973b
2 changed files with 51 additions and 56 deletions

View File

@ -11,7 +11,7 @@ use ostd::sync::{LocalIrqDisabled, Mutex, MutexGuard, SpinLock, SpinLockGuard};
use super::{EpollEvent, EpollFlags}; use super::{EpollEvent, EpollFlags};
use crate::{ use crate::{
events::{IoEvents, Observer}, events::{self, IoEvents},
fs::{file_handle::FileLike, file_table::FileDesc}, fs::{file_handle::FileLike, file_table::FileDesc},
process::signal::{PollHandle, Pollee}, process::signal::{PollHandle, Pollee},
}; };
@ -19,25 +19,25 @@ use crate::{
/// An epoll entry that is contained in an epoll file. /// An epoll entry that is contained in an epoll file.
/// ///
/// Each epoll entry can be added, modified, or deleted by the `EpollCtl` command. /// 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. // The file descriptor and the file.
key: EpollEntryKey, key: EntryKey,
// The event masks and flags. // The event masks and flags.
inner: Mutex<EpollEntryInner>, inner: Mutex<Inner>,
// The observer that receives events. // The observer that receives events.
// //
// Keep this in a separate `Arc` to avoid dropping `EpollEntry` in the observer callback, which // Keep this in a separate `Arc` to avoid dropping `Entry` in the observer callback, which may
// may cause deadlocks. // cause deadlocks.
observer: Arc<EpollEntryObserver>, observer: Arc<Observer>,
} }
#[derive(PartialEq, Eq, PartialOrd, Ord)] #[derive(PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct EpollEntryKey { pub(super) struct EntryKey {
fd: FileDesc, fd: FileDesc,
file: KeyableWeak<dyn FileLike>, file: KeyableWeak<dyn FileLike>,
} }
impl From<(FileDesc, KeyableWeak<dyn FileLike>)> for EpollEntryKey { impl From<(FileDesc, KeyableWeak<dyn FileLike>)> for EntryKey {
fn from(value: (FileDesc, KeyableWeak<dyn FileLike>)) -> Self { fn from(value: (FileDesc, KeyableWeak<dyn FileLike>)) -> Self {
Self { Self {
fd: value.0, fd: value.0,
@ -46,7 +46,7 @@ impl From<(FileDesc, KeyableWeak<dyn FileLike>)> for EpollEntryKey {
} }
} }
impl From<(FileDesc, &Arc<dyn FileLike>)> for EpollEntryKey { impl From<(FileDesc, &Arc<dyn FileLike>)> for EntryKey {
fn from(value: (FileDesc, &Arc<dyn FileLike>)) -> Self { fn from(value: (FileDesc, &Arc<dyn FileLike>)) -> Self {
Self { Self {
fd: value.0, fd: value.0,
@ -55,13 +55,13 @@ impl From<(FileDesc, &Arc<dyn FileLike>)> for EpollEntryKey {
} }
} }
struct EpollEntryInner { struct Inner {
event: EpollEvent, event: EpollEvent,
flags: EpollFlags, flags: EpollFlags,
poller: PollHandle, poller: PollHandle,
} }
impl EpollEntry { impl Entry {
/// Creates a new epoll entry associated with the given epoll file. /// Creates a new epoll entry associated with the given epoll file.
pub(super) fn new( pub(super) fn new(
fd: FileDesc, fd: FileDesc,
@ -69,9 +69,9 @@ impl EpollEntry {
ready_set: Arc<ReadySet>, ready_set: Arc<ReadySet>,
) -> Arc<Self> { ) -> Arc<Self> {
Arc::new_cyclic(|me| { 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 { event: EpollEvent {
events: IoEvents::empty(), events: IoEvents::empty(),
user_data: 0, user_data: 0,
@ -81,7 +81,7 @@ impl EpollEntry {
}; };
Self { Self {
key: EpollEntryKey { fd, file }, key: EntryKey { fd, file },
inner: Mutex::new(inner), inner: Mutex::new(inner),
observer, observer,
} }
@ -163,12 +163,12 @@ impl EpollEntry {
} }
/// Gets the underlying observer. /// Gets the underlying observer.
pub(super) fn observer(&self) -> &EpollEntryObserver { pub(super) fn observer(&self) -> &Observer {
&self.observer &self.observer
} }
/// Gets the key associated with the epoll entry. /// Gets the key associated with the epoll entry.
pub(super) fn key(&self) -> &EpollEntryKey { pub(super) fn key(&self) -> &EntryKey {
&self.key &self.key
} }
@ -183,8 +183,8 @@ impl EpollEntry {
} }
} }
/// A observer for [`EpollEntry`] that can receive events. /// A observer for [`Entry`] that can receive events.
pub(super) struct EpollEntryObserver { pub(super) struct Observer {
// Whether the entry is enabled. // Whether the entry is enabled.
is_enabled: AtomicBool, is_enabled: AtomicBool,
// Whether the entry is in the ready list. // 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. // The ready set of the epoll file that contains this epoll entry.
ready_set: Arc<ReadySet>, ready_set: Arc<ReadySet>,
// The epoll entry itself (always inside an `Arc`). // The epoll entry itself (always inside an `Arc`).
weak_entry: Weak<EpollEntry>, weak_entry: Weak<Entry>,
} }
impl EpollEntryObserver { impl Observer {
fn new(ready_set: Arc<ReadySet>, weak_entry: Weak<EpollEntry>) -> Self { fn new(ready_set: Arc<ReadySet>, weak_entry: Weak<Entry>) -> Self {
Self { Self {
is_enabled: AtomicBool::new(false), is_enabled: AtomicBool::new(false),
is_ready: 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 /// 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 /// to ensure that the "is ready" state matches the fact that the entry is actually in the
/// ready list. /// ready list.
fn set_ready(&self, _guard: &SpinLockGuard<VecDeque<Weak<EpollEntry>>, LocalIrqDisabled>) { fn set_ready(&self, _guard: &SpinLockGuard<VecDeque<Weak<Entry>>, LocalIrqDisabled>) {
self.is_ready.store(true, Ordering::Relaxed); 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 /// 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 /// to ensure that the "is ready" state matches the fact that the entry is actually in the
/// ready list. /// ready list.
fn reset_ready(&self, _guard: &SpinLockGuard<VecDeque<Weak<EpollEntry>>, LocalIrqDisabled>) { fn reset_ready(&self, _guard: &SpinLockGuard<VecDeque<Weak<Entry>>, LocalIrqDisabled>) {
self.is_ready.store(false, Ordering::Relaxed) 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 /// 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 /// only way to ensure that the "is enabled" state describes the correct combination of the
/// event masks and flags. /// event masks and flags.
fn set_enabled(&self, _guard: &MutexGuard<EpollEntryInner>) { fn set_enabled(&self, _guard: &MutexGuard<Inner>) {
self.is_enabled.store(true, Ordering::Relaxed) 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 /// 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 /// only way to ensure that the "is enabled" state describes the correct combination of the
/// event masks and flags. /// event masks and flags.
fn reset_enabled(&self, _guard: &MutexGuard<EpollEntryInner>) { fn reset_enabled(&self, _guard: &MutexGuard<Inner>) {
self.is_enabled.store(false, Ordering::Relaxed) self.is_enabled.store(false, Ordering::Relaxed)
} }
/// Gets an instance of `Weak` that refers to the epoll entry. /// Gets an instance of `Weak` that refers to the epoll entry.
fn weak_entry(&self) -> &Weak<EpollEntry> { fn weak_entry(&self) -> &Weak<Entry> {
&self.weak_entry &self.weak_entry
} }
} }
impl Observer<IoEvents> for EpollEntryObserver { impl events::Observer<IoEvents> for Observer {
fn on_events(&self, _events: &IoEvents) { fn on_events(&self, _events: &IoEvents) {
self.ready_set.push(self); self.ready_set.push(self);
} }
@ -274,7 +274,7 @@ impl Observer<IoEvents> for EpollEntryObserver {
/// A set of ready epoll entries. /// A set of ready epoll entries.
pub(super) struct ReadySet { pub(super) struct ReadySet {
// Entries that are probably ready (having events happened). // Entries that are probably ready (having events happened).
entries: SpinLock<VecDeque<Weak<EpollEntry>>, LocalIrqDisabled>, entries: SpinLock<VecDeque<Weak<Entry>>, LocalIrqDisabled>,
// A guard to ensure that ready entries can be popped by one thread at a time. // A guard to ensure that ready entries can be popped by one thread at a time.
pop_guard: Mutex<PopGuard>, pop_guard: Mutex<PopGuard>,
// A pollee for the ready set (i.e., for `EpollFile` itself). // A pollee for the ready set (i.e., for `EpollFile` itself).
@ -292,16 +292,16 @@ impl ReadySet {
} }
} }
pub(super) fn push(&self, observer: &EpollEntryObserver) { pub(super) fn push(&self, observer: &Observer) {
// Note that we cannot take the `EpollEntryInner` lock because we are in the callback of // Note that we cannot take the `Inner` lock because we are in the callback of the event
// the event observer. Doing so will cause dead locks due to inconsistent locking orders. // observer. Doing so will cause dead locks due to inconsistent locking orders.
// //
// We don't need to take the lock because // We don't need to take the lock because
// - We always call `file.poll()` immediately after calling `self.set_enabled()` and // - 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 // `file.register_observer()`, so all events are caught either here or by the immediate
// poll; in other words, we don't lose any events. // poll; in other words, we don't lose any events.
// - Catching spurious events here is always fine because we always check them later before // - 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() { if !observer.is_enabled() {
return; return;
} }
@ -340,7 +340,7 @@ pub(super) struct ReadySetPopIter<'a> {
} }
impl Iterator for ReadySetPopIter<'_> { impl Iterator for ReadySetPopIter<'_> {
type Item = Arc<EpollEntry>; type Item = Arc<Entry>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.limit == Some(0) { if self.limit == Some(0) {

View File

@ -7,7 +7,7 @@ use keyable_arc::KeyableWeak;
use ostd::sync::Mutex; use ostd::sync::Mutex;
use super::{ use super::{
entry::{EpollEntry, EpollEntryKey, ReadySet}, entry::{Entry, EntryKey, ReadySet},
EpollCtl, EpollEvent, EpollFlags, EpollCtl, EpollEvent, EpollFlags,
}; };
use crate::{ use crate::{
@ -35,7 +35,7 @@ use crate::{
/// event happens on the file. /// event happens on the file.
pub struct EpollFile { pub struct EpollFile {
// All interesting entries. // All interesting entries.
interest: Mutex<BTreeSet<EpollEntryHolder>>, interest: Mutex<BTreeSet<EntryHolder>>,
// A set of ready entries. // A set of ready entries.
// //
// Keep this in a separate `Arc` to avoid dropping `EpollFile` in the observer callback, which // 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 ready_entry = {
let mut interest = self.interest.lock(); let mut interest = self.interest.lock();
if interest.contains(&EpollEntryKey::from((fd, &file))) { if interest.contains(&EntryKey::from((fd, &file))) {
return_errno_with_message!( return_errno_with_message!(
Errno::EEXIST, Errno::EEXIST,
"the file is already in the interest list" "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 events = entry.update(ep_event, ep_flags);
let ready_entry = if !events.is_empty() { let ready_entry = if !events.is_empty() {
@ -130,11 +130,7 @@ impl EpollFile {
// because the strong reference count will reach zero and `Weak::upgrade` // because the strong reference count will reach zero and `Weak::upgrade`
// will fail. // will fail.
if !self if !self.interest.lock().remove(&EntryKey::from((fd, file))) {
.interest
.lock()
.remove(&EpollEntryKey::from((fd, file)))
{
return_errno_with_message!(Errno::ENOENT, "the file is not in the interest list"); return_errno_with_message!(Errno::ENOENT, "the file is not in the interest list");
} }
@ -154,9 +150,8 @@ impl EpollFile {
let ready_entry = { let ready_entry = {
let interest = self.interest.lock(); let interest = self.interest.lock();
let EpollEntryHolder(entry) = interest let EntryHolder(entry) =
.get(&EpollEntryKey::from((fd, &file))) interest.get(&EntryKey::from((fd, &file))).ok_or_else(|| {
.ok_or_else(|| {
Error::with_message(Errno::ENOENT, "the file is not in the interest list") Error::with_message(Errno::ENOENT, "the file is not in the interest list")
})?; })?;
let events = entry.update(new_ep_event, new_ep_flags); let events = entry.update(new_ep_event, new_ep_flags);
@ -281,38 +276,38 @@ impl FileLike for EpollFile {
} }
} }
struct EpollEntryHolder(Arc<EpollEntry>); struct EntryHolder(Arc<Entry>);
impl PartialOrd for EpollEntryHolder { impl PartialOrd for EntryHolder {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
impl Ord for EpollEntryHolder { impl Ord for EntryHolder {
fn cmp(&self, other: &Self) -> core::cmp::Ordering { fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.0.key().cmp(other.0.key()) self.0.key().cmp(other.0.key())
} }
} }
impl PartialEq for EpollEntryHolder { impl PartialEq for EntryHolder {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.0.key().eq(other.0.key()) self.0.key().eq(other.0.key())
} }
} }
impl Eq for EpollEntryHolder {} impl Eq for EntryHolder {}
impl Borrow<EpollEntryKey> for EpollEntryHolder { impl Borrow<EntryKey> for EntryHolder {
fn borrow(&self) -> &EpollEntryKey { fn borrow(&self) -> &EntryKey {
self.0.key() self.0.key()
} }
} }
impl From<Arc<EpollEntry>> for EpollEntryHolder { impl From<Arc<Entry>> for EntryHolder {
fn from(value: Arc<EpollEntry>) -> Self { fn from(value: Arc<Entry>) -> Self {
Self(value) Self(value)
} }
} }
impl Drop for EpollEntryHolder { impl Drop for EntryHolder {
fn drop(&mut self) { fn drop(&mut self) {
self.0.shutdown(); self.0.shutdown();
} }