Skip to content

Commit

Permalink
implement block outside helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tiif committed Nov 28, 2024
1 parent 7b10fd0 commit 1fde65b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/concurrency/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ pub enum BlockReason {
Epoll,
/// Blocked on eventfd.
Eventfd,
/// Blocked on unnamed_socket.
UnnamedSocket,

}

/// The state of a thread.
Expand Down
29 changes: 23 additions & 6 deletions src/shims/unix/unnamed_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,30 @@ impl FileDescription for AnonSocket {
// Since there is no ErrorKind for EAGAIN, WouldBlock is used.
return ecx.set_last_error_and_return(ErrorKind::WouldBlock, dest);
} else {
// TODO: move this to helper
// Blocking socketpair with writer and empty buffer.
// FIXME: blocking is currently not supported
throw_unsup_format!("socketpair/pipe/pipe2 read: blocking isn't supported yet");
let peer_fd = self.peer_fd().upgrade().unwrap();
bytes = bytes.clone();
ecx.block_thread(
BlockReason::UnnamedSocket,
None,
callback!(
@capture<'tcx> {
self_ref: FileDescriptionRef,
peer_fd: Option<FileDescriptionRef>,
bytes: [u8],
ptr: Pointer,
dest: MPlaceTy<'tcx>,
}
@unblock = |this| {
// TODO: We might need to decide what to do if peer_fd is closed when read is blocked.
anonsocket_read(self_ref, peer_fd, &bytes, ptr, dest, ecx)
}
),
);
}
}
}
// TODO: We might need to decide what to do if peer_fd is closed when read is blocked.
anonsocket_read(self, self.peer_fd().upgrade(), &mut bytes, ptr, dest, ecx)
}

fn write<'tcx>(
Expand Down Expand Up @@ -249,13 +265,14 @@ fn anonsocket_write<'tcx>(

/// Read from AnonSocket and return the number of bytes read.
fn anonsocket_read<'tcx>(
anonsocket: &AnonSocket,
self_ref: FileDescriptionRef,
peer_fd: Option<FileDescriptionRef>,
bytes: &mut [u8],
bytes: &[u8],
ptr: Pointer,
dest: &MPlaceTy<'tcx>,
ecx: &mut MiriInterpCx<'tcx>,
) -> InterpResult<'tcx> {
let anonsocket = self_ref.downcast::<AnonSocket>().unwrap();
let Some(readbuf) = &anonsocket.readbuf else {
// FIXME: This should return EBADF, but there's no nice way to do that as there's no
// corresponding ErrorKind variant.
Expand Down

0 comments on commit 1fde65b

Please sign in to comment.