import: Don't deadlock state receiver

This commit is contained in:
Elias Projahn 2021-04-09 13:01:47 +02:00
parent 2d5ce29aae
commit 8a00e4588c
3 changed files with 7 additions and 8 deletions

View file

@ -6,7 +6,6 @@ use gstreamer::tags::{Duration, TrackNumber};
use log::info;
use sha2::{Sha256, Digest};
use std::path::PathBuf;
use std::sync::Mutex;
use tokio::sync::watch;
/// Create a new import session for the default disc drive.
@ -150,7 +149,7 @@ pub(super) fn new() -> Result<ImportSession> {
tracks,
copy: Some(Box::new(copy)),
state_sender,
state_receiver: Mutex::new(state_receiver),
state_receiver,
};
Ok(session)

View file

@ -6,7 +6,6 @@ use log::{warn, info};
use sha2::{Sha256, Digest};
use std::fs::DirEntry;
use std::path::PathBuf;
use std::sync::Mutex;
use tokio::sync::watch;
/// Create a new import session for the specified folder.
@ -65,7 +64,7 @@ pub(super) fn new(path: PathBuf) -> Result<ImportSession> {
tracks,
copy: None,
state_sender,
state_receiver: Mutex::new(state_receiver),
state_receiver,
};
Ok(session)

View file

@ -2,7 +2,7 @@ use crate::{disc, folder};
use crate::error::Result;
use std::path::PathBuf;
use std::thread;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use tokio::sync::{oneshot, watch};
/// The current state of the import process.
@ -36,7 +36,7 @@ pub struct ImportSession {
pub(super) state_sender: watch::Sender<State>,
/// Receiver for state changes.
pub(super) state_receiver: Mutex<watch::Receiver<State>>,
pub(super) state_receiver: watch::Receiver<State>,
}
impl ImportSession {
@ -76,12 +76,13 @@ impl ImportSession {
/// Retrieve the current state of the import process.
pub fn state(&self) -> State {
self.state_receiver.lock().unwrap().borrow().clone()
self.state_receiver.borrow().clone()
}
/// Wait for the next state change and get the new state.
pub async fn state_change(&self) -> State {
match self.state_receiver.lock().unwrap().changed().await {
let mut receiver = self.state_receiver.clone();
match receiver.changed().await {
Ok(()) => self.state(),
Err(_) => State::Error,
}