my device is still busy. So I have to currently call
nix::unistd::fsync(output_fd).expect("Call to fsync failed");
To ensure that the write is finished. But I want my writecall to only return when the write operation has finished in the device. I don't mind blocking the thread.
How can I get a blocking write to the device? For example, this program never needs to call fsync. I don't want the user to wait for an indefinite time for fsync to finish.
The imagewrite example also doesn't perform any fsync(2) call so it should behave identically with your Rust snippet without fsync, except it throws unwritten data away if it fails to write entire buffer within single system call, which is handled correctly via .write_all() in Rust.
Edit: Oops: On second look I see you already shared variable "pBar" and "written" to communicate progress. So disregard most of below.
I notice that this program uses the Qt GUI library and it's event loop. In the writeData() method I see this:
for (i = 0; i <= realSize; i++)
{
...
written = ::write(ofd, buffer, read);
if (written == -1)
{
...
break;
}
...
qApp->processEvents();
if (progress.wasCanceled())
break;
}
So what is happening there is that it write chunks of data of size 1048576 bytes each, and makes a call to "qApp->processEvents();" after each write. In this way the Qt GUI interface does not freeze whilst the entire image is being written. The user gets to see the progress bar move and is able to cancel the operation because of that processEvents call. The entire write could take a long time but the program remains responsive throughout.
I notice in you Rust code you have a similar loop to do the write inside a thread of it's own. But there you indcate the operation is complete by setting a "finished" flag. Then your main line is polling that finished flag. So presumably everything freezes until the whole job is done.
I would look into changing that finish flag into a percentage amount of completion. The the UI thread could read that and display some kind of progress indicator to the user.
Actually I am polling on progress, which is bytes written and it's a load()ed value from written: Arc<AtomicUsize>. While it is lower than tl (total len) the polling is kept alive.
In action my writer thread freezes, granted but it also keeps updating the atomic usize and atomic bool. After the end of the loop it sets the finished flag. The Glib polling picks it up sometime later and cuts off the animation.