Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback support to FileDescription::read #4110

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shamb0
Copy link
Contributor

@shamb0 shamb0 commented Dec 26, 2024

Key Changes

  • Introduces IoTransferOperation to manage atomic file transfers
  • Adds callback support to FileDescription::read

Context

This work completes stages 2 of the async file operations roadmap discussed here:

  1. ✅ Generalize callback system (PR Concurrency: Generalize UnblockCallback to MachineCallback #4106)
  2. ✅ Add callback support to FileDescription::read (this PR)
  3. 🔲 Implement readv() using callbacks (this PR)

@shamb0 shamb0 force-pushed the support-shim-fs-atomic-read branch from abfa97f to 88cbd55 Compare December 26, 2024 10:07
@shamb0
Copy link
Contributor Author

shamb0 commented Dec 26, 2024

Hi @RalfJung,

I wanted to share some updates and seek your guidance on a few points:

  1. Callback Integration
    I’ve implemented the callback using the Mutex synchronization object but would appreciate your insights on whether this is the most suitable concurrency synchronization approach for this case.

  2. Clarification on read Behavior
    Regarding your statement:
    "The problem with read is that when it returns, the read may not actually have completed yet."
    I believe this behavior might be specific to the Miri interpreter context. Could you elaborate on this to help me better understand its implications?

  3. Exploration of std::fs Library

    While reviewing the std::fs library on the Unix platform, I found the following relevant implementations:

    • FileDesc: Defines core operations like read (e.g., view code).

      #[derive(Debug)]
      pub struct FileDesc(OwnedFd);
      
      impl FileDesc {
          pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
              let ret = cvt(unsafe {
                  libc::read(
                      self.as_raw_fd(),
                      buf.as_mut_ptr() as *mut libc::c_void,
                      cmp::min(buf.len(), READ_LIMIT),
                  )
              })?;
              Ok(ret as usize)
          }
      }
    • File (inner struct): Wraps FileDesc (view code).

      pub struct File(FileDesc);
    • File (outer struct): Provides higher-level abstractions (view code).

      use crate::sys::fs as fs_imp;
      
      #[stable(feature = "rust1", since = "1.0.0")]
      #[cfg_attr(not(test), rustc_diagnostic_item = "File")]
      pub struct File {
          inner: fs_imp::File,
      }
  4. Understanding Read Operation
    Based on my analysis, it appears that the read operation blocks and fills the destination buffer before returning:

    let read_result = (file_handle.file).read(op.borrow_mut().buffer_mut());

    Could you please confirm if this understanding is correct? If not, I’d be grateful if you could point me toward relevant documentation or files for clarification.

Thanks in advance for your help and guidance! Looking forward to your insights.

@shamb0 shamb0 marked this pull request as ready for review December 26, 2024 10:50
@shamb0
Copy link
Contributor Author

shamb0 commented Dec 26, 2024

@rustbot ready

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Dec 26, 2024
@RalfJung
Copy link
Member

RalfJung commented Dec 26, 2024

So this is blocked on #4106, I assume.

✅ Add callback support to FileDescription::read (this PR)
✅ Implement readv() using callbacks (this PR)

I would prefer if you could split this into two separate PRs. Refactoring an existing interface should not be done in the same changeset as adding new functionality, if it can be avoided. Splitting changes in smaller PRs greatly helps with making them easier to review.

@RalfJung RalfJung added the S-blocked Status: blocked on something happening somewhere else label Dec 26, 2024
@shamb0 shamb0 force-pushed the support-shim-fs-atomic-read branch from 88cbd55 to fb2d16a Compare December 26, 2024 15:41
@shamb0
Copy link
Contributor Author

shamb0 commented Dec 26, 2024

Hi @RalfJung,

Thank you for your feedback and for highlighting the review challenges—I completely understand.

This PR focuses on two key changes:

  1. ✅ Generalizing the callback system (PR #4106)
  2. ✅ Adding callback support to FileDescription::read (this PR).

Let me know if there's anything else you'd like me to adjust.

Thanks! 🙂

@shamb0
Copy link
Contributor Author

shamb0 commented Dec 26, 2024

@rustbot ready

    * Introduce MachineCallbackState enum to represent operation outcomes
    * Consolidate unblock/timeout methods into single callback interface
    * Update thread blocking system to use new callback mechanism
    * Refactor mutex and condvar implementations for new callback pattern

Signed-off-by: shamb0 <[email protected]>
    * Introduce UnblockKind enum to represent operation outcomes
    * Consolidate unblock/timeout methods into single callback interface
    * Update thread blocking system to use new callback mechanism
    * Refactor mutex and condvar implementations for new callback pattern

Signed-off-by: shamb0 <[email protected]>
   - Implementing atomic reads for contiguous buffers

Signed-off-by: shamb0 <[email protected]>
@shamb0 shamb0 force-pushed the support-shim-fs-atomic-read branch from fb2d16a to 59f70e8 Compare December 27, 2024 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: blocked on something happening somewhere else S-waiting-on-review Status: Waiting for a review to complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants