Skip to content

Commit cdcd4f4

Browse files
authored
refactor: axtask (Starry-OS#55)
1 parent 8aa238a commit cdcd4f4

File tree

20 files changed

+200
-239
lines changed

20 files changed

+200
-239
lines changed

Cargo.lock

Lines changed: 10 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/file/event.rs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::{
88
use axerrno::AxError;
99
use axio::{Buf, BufMut, Read, Write};
1010
use axpoll::{IoEvents, PollSet, Pollable};
11-
use axtask::future::Poller;
11+
use axtask::future::{block_on, poll_io};
1212

1313
use crate::file::{FileLike, Kstat, SealedBuf, SealedBufMut};
1414

@@ -40,28 +40,26 @@ impl FileLike for EventFd {
4040
return Err(AxError::InvalidInput);
4141
}
4242

43-
Poller::new(self, IoEvents::IN)
44-
.non_blocking(self.nonblocking())
45-
.poll(|| {
46-
let result =
47-
self.count
48-
.fetch_update(Ordering::Release, Ordering::Acquire, |count| {
49-
if count > 0 {
50-
let dec = if self.semaphore { 1 } else { count };
51-
Some(count - dec)
52-
} else {
53-
None
54-
}
55-
});
56-
match result {
57-
Ok(count) => {
58-
dst.write(&count.to_ne_bytes())?;
59-
self.poll_tx.wake();
60-
Ok(size_of::<u64>())
43+
block_on(poll_io(self, IoEvents::IN, self.nonblocking(), || {
44+
let result = self
45+
.count
46+
.fetch_update(Ordering::Release, Ordering::Acquire, |count| {
47+
if count > 0 {
48+
let dec = if self.semaphore { 1 } else { count };
49+
Some(count - dec)
50+
} else {
51+
None
6152
}
62-
Err(_) => Err(AxError::WouldBlock),
53+
});
54+
match result {
55+
Ok(count) => {
56+
dst.write(&count.to_ne_bytes())?;
57+
self.poll_tx.wake();
58+
Ok(size_of::<u64>())
6359
}
64-
})
60+
Err(_) => Err(AxError::WouldBlock),
61+
}
62+
}))
6563
}
6664

6765
fn write(&self, src: &mut SealedBuf) -> axio::Result<usize> {
@@ -76,26 +74,24 @@ impl FileLike for EventFd {
7674
return Err(AxError::InvalidInput);
7775
}
7876

79-
Poller::new(self, IoEvents::OUT)
80-
.non_blocking(self.nonblocking())
81-
.poll(|| {
82-
let result =
83-
self.count
84-
.fetch_update(Ordering::Release, Ordering::Acquire, |count| {
85-
if u64::MAX - count > value {
86-
Some(count + value)
87-
} else {
88-
None
89-
}
90-
});
91-
match result {
92-
Ok(_) => {
93-
self.poll_rx.wake();
94-
Ok(size_of::<u64>())
77+
block_on(poll_io(self, IoEvents::OUT, self.nonblocking(), || {
78+
let result = self
79+
.count
80+
.fetch_update(Ordering::Release, Ordering::Acquire, |count| {
81+
if u64::MAX - count > value {
82+
Some(count + value)
83+
} else {
84+
None
9585
}
96-
Err(_) => Err(AxError::WouldBlock),
86+
});
87+
match result {
88+
Ok(_) => {
89+
self.poll_rx.wake();
90+
Ok(size_of::<u64>())
9791
}
98-
})
92+
Err(_) => Err(AxError::WouldBlock),
93+
}
94+
}))
9995
}
10096

10197
fn stat(&self) -> axio::Result<Kstat> {

api/src/file/fs.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use axfs_ng::{FS_CONTEXT, FsContext};
1212
use axfs_ng_vfs::{Location, Metadata, NodeFlags};
1313
use axpoll::{IoEvents, Pollable};
1414
use axsync::Mutex;
15-
use axtask::future::Poller;
15+
use axtask::future::{block_on, poll_io};
1616
use linux_raw_sys::general::{AT_EMPTY_PATH, AT_FDCWD, AT_SYMLINK_NOFOLLOW};
1717

1818
use super::{FileLike, Kstat, get_file_like};
@@ -131,9 +131,9 @@ impl FileLike for File {
131131
if likely(self.is_blocking()) {
132132
inner.read(dst)
133133
} else {
134-
Poller::new(self, IoEvents::IN)
135-
.non_blocking(self.nonblocking())
136-
.poll(|| inner.read(dst))
134+
block_on(poll_io(self, IoEvents::IN, self.nonblocking(), || {
135+
inner.read(dst)
136+
}))
137137
}
138138
}
139139

@@ -142,9 +142,9 @@ impl FileLike for File {
142142
if likely(self.is_blocking()) {
143143
inner.write(src)
144144
} else {
145-
Poller::new(self, IoEvents::OUT)
146-
.non_blocking(self.nonblocking())
147-
.poll(|| inner.write(src))
145+
block_on(poll_io(self, IoEvents::OUT, self.nonblocking(), || {
146+
inner.write(src)
147+
}))
148148
}
149149
}
150150

api/src/file/pipe.rs

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use axerrno::{AxError, AxResult};
1010
use axio::{Buf, BufMut, Read, Write};
1111
use axpoll::{IoEvents, PollSet, Pollable};
1212
use axsync::Mutex;
13-
use axtask::{current, future::Poller};
13+
use axtask::{
14+
current,
15+
future::{block_on, poll_io},
16+
};
1417
use linux_raw_sys::{general::S_IFIFO, ioctl::FIONREAD};
1518
use memory_addr::PAGE_SIZE_4K;
1619
use ringbuf::{
@@ -117,28 +120,26 @@ impl FileLike for Pipe {
117120
return Ok(0);
118121
}
119122

120-
Poller::new(self, IoEvents::IN)
121-
.non_blocking(self.nonblocking())
122-
.poll(|| {
123-
let read = {
124-
let cons = self.shared.buffer.lock();
125-
let (left, right) = cons.as_slices();
126-
let mut count = dst.write(left)?;
127-
if count >= left.len() {
128-
count += dst.write(right)?;
129-
}
130-
unsafe { cons.advance_read_index(count) };
131-
count
132-
};
133-
if read > 0 {
134-
self.shared.poll_tx.wake();
135-
Ok(read)
136-
} else if self.closed() {
137-
Ok(0)
138-
} else {
139-
Err(AxError::WouldBlock)
123+
block_on(poll_io(self, IoEvents::IN, self.nonblocking(), || {
124+
let read = {
125+
let cons = self.shared.buffer.lock();
126+
let (left, right) = cons.as_slices();
127+
let mut count = dst.write(left)?;
128+
if count >= left.len() {
129+
count += dst.write(right)?;
140130
}
141-
})
131+
unsafe { cons.advance_read_index(count) };
132+
count
133+
};
134+
if read > 0 {
135+
self.shared.poll_tx.wake();
136+
Ok(read)
137+
} else if self.closed() {
138+
Ok(0)
139+
} else {
140+
Err(AxError::WouldBlock)
141+
}
142+
}))
142143
}
143144

144145
fn write(&self, src: &mut SealedBuf) -> AxResult<usize> {
@@ -151,34 +152,32 @@ impl FileLike for Pipe {
151152
}
152153

153154
let mut total_written = 0;
154-
let non_blocking = self.nonblocking();
155-
Poller::new(self, IoEvents::OUT)
156-
.non_blocking(non_blocking)
157-
.poll(|| {
158-
if self.closed() {
159-
raise_pipe();
160-
return Err(AxError::BrokenPipe);
161-
}
162155

163-
let written = {
164-
let mut prod = self.shared.buffer.lock();
165-
let (left, right) = prod.vacant_slices_mut();
166-
let mut count = src.read(unsafe { left.assume_init_mut() })?;
167-
if count >= left.len() {
168-
count += src.read(unsafe { right.assume_init_mut() })?;
169-
}
170-
unsafe { prod.advance_write_index(count) };
171-
count
172-
};
173-
if written > 0 {
174-
self.shared.poll_rx.wake();
175-
total_written += written;
176-
if total_written == size || non_blocking {
177-
return Ok(total_written);
178-
}
156+
block_on(poll_io(self, IoEvents::OUT, self.nonblocking(), || {
157+
if self.closed() {
158+
raise_pipe();
159+
return Err(AxError::BrokenPipe);
160+
}
161+
162+
let written = {
163+
let mut prod = self.shared.buffer.lock();
164+
let (left, right) = prod.vacant_slices_mut();
165+
let mut count = src.read(unsafe { left.assume_init_mut() })?;
166+
if count >= left.len() {
167+
count += src.read(unsafe { right.assume_init_mut() })?;
179168
}
180-
Err(AxError::WouldBlock)
181-
})
169+
unsafe { prod.advance_write_index(count) };
170+
count
171+
};
172+
if written > 0 {
173+
self.shared.poll_rx.wake();
174+
total_written += written;
175+
if total_written == size || self.nonblocking() {
176+
return Ok(total_written);
177+
}
178+
}
179+
Err(AxError::WouldBlock)
180+
}))
182181
}
183182

184183
fn stat(&self) -> AxResult<Kstat> {

0 commit comments

Comments
 (0)