1
0
Fork 0
forked from wry/wry

io: use io_uring for all io

There should no longer be any

- read
- write
- connect
- sendmsg
- recvmsg
- accept

calls in the codebase. Previously we were using a mix of io_uring and
these calls which had some negative effects: Since we were using the old
system calls, we had to set the file descriptors to non-blocking. But
our io_uring code did not handle EAGAIN. This lead to programs sometimes
being killed when the wayland IO was actually blocking.

Now all file descriptors are set to blocking, but io_uring makes it
non-blocking from our perspective. The one exception are evdev files
because they are read via libinput and libinput uses the old system
calls.
This commit is contained in:
Julian Orth 2022-12-31 17:55:58 +01:00
parent 2db0ee8995
commit 9812a02f87
55 changed files with 900 additions and 672 deletions

View file

@ -56,7 +56,7 @@ pub enum ToolClientError {
#[error("The socket path is too long")]
SocketPathTooLong,
#[error("Could not connect to the compositor")]
Connect(#[source] OsError),
Connect(#[source] IoUringError),
#[error("The message length is smaller than 8 bytes")]
MsgLenTooSmall,
#[error("The size of the message is not a multiple of 4")]
@ -94,36 +94,53 @@ pub struct ToolClient {
jay_compositor: Cell<Option<JayCompositorId>>,
}
impl ToolClient {
pub fn new(level: Level) -> Rc<Self> {
match Self::try_new(level) {
Ok(s) => s,
Err(e) => {
fatal!("Could not create a tool client: {}", ErrorFmt(e));
}
}
pub fn with_tool_client<T, F>(level: Level, f: F)
where
F: FnOnce(Rc<ToolClient>) -> T + 'static,
T: Future<Output = ()> + 'static,
{
if let Err(e) = with_tool_client_(level, f) {
handle_error(e);
}
}
pub fn run<F>(&self, f: F)
where
F: Future<Output = ()> + 'static,
{
let _future = self.eng.spawn(async move {
f.await;
std::process::exit(0);
});
if let Err(e) = self.ring.run() {
fatal!("A fatal error occurred: {}", ErrorFmt(e));
}
}
fn handle_error(e: ToolClientError) -> ! {
fatal!("Could not create a tool client: {}", ErrorFmt(e));
}
pub fn try_new(level: Level) -> Result<Rc<Self>, ToolClientError> {
let logger = Logger::install_stderr(level);
let eng = AsyncEngine::new();
let ring = match IoUring::new(&eng, 32) {
Ok(e) => e,
Err(e) => return Err(ToolClientError::CreateRing(e)),
fn with_tool_client_<T, F>(level: Level, f: F) -> Result<(), ToolClientError>
where
F: FnOnce(Rc<ToolClient>) -> T + 'static,
T: Future<Output = ()> + 'static,
{
let logger = Logger::install_stderr(level);
let eng = AsyncEngine::new();
let ring = match IoUring::new(&eng, 32) {
Ok(e) => e,
Err(e) => return Err(ToolClientError::CreateRing(e)),
};
let eng2 = eng.clone();
let ring2 = ring.clone();
let _f = eng.spawn(async move {
let tc = match ToolClient::try_new(logger, eng2, ring2).await {
Ok(tc) => tc,
Err(e) => handle_error(e),
};
f(tc).await;
std::process::exit(0);
});
if let Err(e) = ring.run() {
fatal!("A fatal error occurred: {}", ErrorFmt(e));
}
Ok(())
}
impl ToolClient {
async fn try_new(
logger: Arc<Logger>,
eng: Rc<AsyncEngine>,
ring: Rc<IoUring>,
) -> Result<Rc<Self>, ToolClientError> {
let wheel = match Wheel::new(&eng, &ring) {
Ok(w) => w,
Err(e) => return Err(ToolClientError::CreateWheel(e)),
@ -137,11 +154,7 @@ impl ToolClient {
Err(_) => return Err(ToolClientError::WaylandDisplayNotSet),
};
let path = format_ustr!("{}/{}.jay", xrd, wd);
let socket = match uapi::socket(
c::AF_UNIX,
c::SOCK_STREAM | c::SOCK_CLOEXEC | c::SOCK_NONBLOCK,
0,
) {
let socket = match uapi::socket(c::AF_UNIX, c::SOCK_STREAM | c::SOCK_CLOEXEC, 0) {
Ok(s) => Rc::new(s),
Err(e) => return Err(ToolClientError::CreateSocket(e.into())),
};
@ -153,8 +166,8 @@ impl ToolClient {
let sun_path = uapi::as_bytes_mut(&mut addr.sun_path[..]);
sun_path[..path.len()].copy_from_slice(path.as_bytes());
sun_path[path.len()] = 0;
if let Err(e) = uapi::connect(socket.raw(), &addr) {
return Err(ToolClientError::Connect(e.into()));
if let Err(e) = ring.connect(&socket, &addr).await {
return Err(ToolClientError::Connect(e));
}
let mut obj_ids = Bitfield::default();
obj_ids.take(0);