Avoid spawning a disposal thread per driver (#151)
Adds a new field to Config, disposer, an Option<Sender<DisposalMessage>> responsible for dropping the DisposalMessage on a separate thread. If this is not set, and the Config is passed into manager::Songbird, a thread is spawned for this purpose (which previously was spawned per driver). If this is not set, and the Config is passed directly into Driver or Call, a thread is spawned locally, which is the current behavior as there is no where to store the Sender. This disposer is then used in Driver as previously, to run possibly blocking destructors (which should only block the disposal thread). I cannot see this disposal thread getting overloaded, but if it is the DisposalMessages will simply be queued in the flume channel until it can be dropped. Co-authored-by: Kyle Simpson <kyleandrew.simpson@gmail.com>
This commit is contained in:
@@ -1,6 +1,32 @@
|
||||
use super::message::*;
|
||||
use flume::Receiver;
|
||||
use tracing::instrument;
|
||||
use flume::{Receiver, Sender};
|
||||
use tracing::{instrument, trace};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DisposalThread(Sender<DisposalMessage>);
|
||||
|
||||
impl Default for DisposalThread {
|
||||
fn default() -> Self {
|
||||
Self::run()
|
||||
}
|
||||
}
|
||||
|
||||
impl DisposalThread {
|
||||
pub fn run() -> Self {
|
||||
let (mix_tx, mix_rx) = flume::unbounded();
|
||||
std::thread::spawn(move || {
|
||||
trace!("Disposal thread started.");
|
||||
runner(mix_rx);
|
||||
trace!("Disposal thread finished.");
|
||||
});
|
||||
|
||||
Self(mix_tx)
|
||||
}
|
||||
|
||||
pub(super) fn dispose(&self, message: DisposalMessage) {
|
||||
drop(self.0.send(message))
|
||||
}
|
||||
}
|
||||
|
||||
/// The mixer's disposal thread is also synchronous, due to tracks,
|
||||
/// inputs, etc. being based on synchronous I/O.
|
||||
@@ -8,6 +34,6 @@ use tracing::instrument;
|
||||
/// The mixer uses this to offload heavy and expensive drop operations
|
||||
/// to prevent deadline misses.
|
||||
#[instrument(skip(mix_rx))]
|
||||
pub(crate) fn runner(mix_rx: Receiver<DisposalMessage>) {
|
||||
fn runner(mix_rx: Receiver<DisposalMessage>) {
|
||||
while mix_rx.recv().is_ok() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user