Files
tuliprox/backend/src/api/model/update_guard.rs
T
euzuandGitHub dd7d3c6fe3 Target is updated after all resolve tasks finish (#590)
Target is updated after all resolve tasks finish
2026-02-15 17:31:14 +01:00

53 lines
1.2 KiB
Rust

use std::sync::Arc;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
#[derive(Debug, Clone)]
pub struct UpdateGuard {
playlist: Arc<Semaphore>,
library: Arc<Semaphore>,
}
impl Default for UpdateGuard {
fn default() -> Self {
Self {
playlist: Arc::new(Semaphore::new(1)),
library: Arc::new(Semaphore::new(1)),
}
}
}
impl UpdateGuard {
pub fn new() -> Self {
Self::default()
}
pub fn try_playlist(&self) -> Option<UpdateGuardPermit> {
self.playlist
.clone()
.try_acquire_owned()
.ok()
.map(|permit| UpdateGuardPermit { _permit: permit })
}
pub async fn acquire_playlist_lock(&self) -> Option<UpdateGuardPermit> {
self.playlist
.clone()
.acquire_owned()
.await
.ok()
.map(|permit| UpdateGuardPermit { _permit: permit })
}
pub fn try_library(&self) -> Option<UpdateGuardPermit> {
self.library
.clone()
.try_acquire_owned()
.ok()
.map(|permit| UpdateGuardPermit { _permit: permit })
}
}
pub struct UpdateGuardPermit {
_permit: OwnedSemaphorePermit,
}