mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2025-02-09 01:22:21 +00:00
threadshare: Implement throttling for the poll loop
This commit is contained in:
parent
6aa9f642ba
commit
f53efc6e6f
1 changed files with 50 additions and 10 deletions
|
@ -67,6 +67,7 @@ struct IOContextRunner {
|
||||||
impl IOContextRunner {
|
impl IOContextRunner {
|
||||||
fn start_single_threaded(
|
fn start_single_threaded(
|
||||||
name: &str,
|
name: &str,
|
||||||
|
wait: u32,
|
||||||
reactor: reactor::Reactor,
|
reactor: reactor::Reactor,
|
||||||
) -> (IOContextExecutor, IOContextShutdown) {
|
) -> (IOContextExecutor, IOContextShutdown) {
|
||||||
let handle = reactor.handle().clone();
|
let handle = reactor.handle().clone();
|
||||||
|
@ -84,7 +85,7 @@ impl IOContextRunner {
|
||||||
pending_futures: Some(pending_futures),
|
pending_futures: Some(pending_futures),
|
||||||
};
|
};
|
||||||
let join = thread::spawn(move || {
|
let join = thread::spawn(move || {
|
||||||
runner.run(reactor);
|
runner.run(wait, reactor);
|
||||||
});
|
});
|
||||||
|
|
||||||
let executor = IOContextExecutor {
|
let executor = IOContextExecutor {
|
||||||
|
@ -102,7 +103,7 @@ impl IOContextRunner {
|
||||||
(executor, shutdown)
|
(executor, shutdown)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start(name: &str, reactor: reactor::Reactor) -> IOContextShutdown {
|
fn start(name: &str, wait: u32, reactor: reactor::Reactor) -> IOContextShutdown {
|
||||||
let handle = reactor.handle().clone();
|
let handle = reactor.handle().clone();
|
||||||
let shutdown = Arc::new(atomic::AtomicUsize::new(RUNNING));
|
let shutdown = Arc::new(atomic::AtomicUsize::new(RUNNING));
|
||||||
let shutdown_clone = shutdown.clone();
|
let shutdown_clone = shutdown.clone();
|
||||||
|
@ -114,7 +115,7 @@ impl IOContextRunner {
|
||||||
pending_futures: None,
|
pending_futures: None,
|
||||||
};
|
};
|
||||||
let join = thread::spawn(move || {
|
let join = thread::spawn(move || {
|
||||||
runner.run(reactor);
|
runner.run(wait, reactor);
|
||||||
});
|
});
|
||||||
|
|
||||||
let shutdown = IOContextShutdown {
|
let shutdown = IOContextShutdown {
|
||||||
|
@ -127,7 +128,10 @@ impl IOContextRunner {
|
||||||
shutdown
|
shutdown
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self, reactor: reactor::Reactor) {
|
fn run(&mut self, wait: u32, reactor: reactor::Reactor) {
|
||||||
|
use std::time;
|
||||||
|
let wait = time::Duration::from_millis(wait as u64);
|
||||||
|
|
||||||
gst_debug!(CONTEXT_CAT, "Started reactor thread '{}'", self.name);
|
gst_debug!(CONTEXT_CAT, "Started reactor thread '{}'", self.name);
|
||||||
|
|
||||||
if let Some(ref pending_futures) = self.pending_futures {
|
if let Some(ref pending_futures) = self.pending_futures {
|
||||||
|
@ -139,6 +143,8 @@ impl IOContextRunner {
|
||||||
let mut current_thread = current_thread::CurrentThread::new_with_park(reactor);
|
let mut current_thread = current_thread::CurrentThread::new_with_park(reactor);
|
||||||
|
|
||||||
::tokio_reactor::with_default(&handle, &mut enter, |enter| loop {
|
::tokio_reactor::with_default(&handle, &mut enter, |enter| loop {
|
||||||
|
let now = time::Instant::now();
|
||||||
|
|
||||||
if self.shutdown.load(atomic::Ordering::SeqCst) > RUNNING {
|
if self.shutdown.load(atomic::Ordering::SeqCst) > RUNNING {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -154,11 +160,19 @@ impl IOContextRunner {
|
||||||
gst_trace!(CONTEXT_CAT, "Turning current thread '{}'", self.name);
|
gst_trace!(CONTEXT_CAT, "Turning current thread '{}'", self.name);
|
||||||
current_thread.enter(enter).turn(None).unwrap();
|
current_thread.enter(enter).turn(None).unwrap();
|
||||||
gst_trace!(CONTEXT_CAT, "Turned current thread '{}'", self.name);
|
gst_trace!(CONTEXT_CAT, "Turned current thread '{}'", self.name);
|
||||||
|
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if elapsed < wait {
|
||||||
|
gst_trace!(CONTEXT_CAT, "Waiting for {:?} before polling again", wait - elapsed);
|
||||||
|
thread::sleep(wait - elapsed);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let mut reactor = reactor;
|
let mut reactor = reactor;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
let now = time::Instant::now();
|
||||||
|
|
||||||
if self.shutdown.load(atomic::Ordering::SeqCst) > RUNNING {
|
if self.shutdown.load(atomic::Ordering::SeqCst) > RUNNING {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -166,6 +180,12 @@ impl IOContextRunner {
|
||||||
gst_trace!(CONTEXT_CAT, "Turning reactor '{}'", self.name);
|
gst_trace!(CONTEXT_CAT, "Turning reactor '{}'", self.name);
|
||||||
reactor.turn(None).unwrap();
|
reactor.turn(None).unwrap();
|
||||||
gst_trace!(CONTEXT_CAT, "Turned reactor '{}'", self.name);
|
gst_trace!(CONTEXT_CAT, "Turned reactor '{}'", self.name);
|
||||||
|
|
||||||
|
let elapsed = now.elapsed();
|
||||||
|
if elapsed < wait {
|
||||||
|
gst_trace!(CONTEXT_CAT, "Waiting for {:?} before polling again", wait - elapsed);
|
||||||
|
thread::sleep(wait - elapsed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -235,7 +255,7 @@ impl Drop for IOContextInner {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IOContext {
|
impl IOContext {
|
||||||
fn new(name: &str, n_threads: isize) -> Self {
|
fn new(name: &str, n_threads: isize, wait: u32) -> Self {
|
||||||
let mut contexts = CONTEXTS.lock().unwrap();
|
let mut contexts = CONTEXTS.lock().unwrap();
|
||||||
if let Some(context) = contexts.get(name) {
|
if let Some(context) = contexts.get(name) {
|
||||||
if let Some(context) = context.upgrade() {
|
if let Some(context) = context.upgrade() {
|
||||||
|
@ -249,7 +269,7 @@ impl IOContext {
|
||||||
let (pool, shutdown) = if n_threads >= 0 {
|
let (pool, shutdown) = if n_threads >= 0 {
|
||||||
let handle = reactor.handle().clone();
|
let handle = reactor.handle().clone();
|
||||||
|
|
||||||
let shutdown = IOContextRunner::start(name, reactor);
|
let shutdown = IOContextRunner::start(name, wait, reactor);
|
||||||
|
|
||||||
let mut pool_builder = thread_pool::Builder::new();
|
let mut pool_builder = thread_pool::Builder::new();
|
||||||
pool_builder.around_worker(move |w, enter| {
|
pool_builder.around_worker(move |w, enter| {
|
||||||
|
@ -263,7 +283,7 @@ impl IOContext {
|
||||||
}
|
}
|
||||||
(Either::Left(pool_builder.build()), shutdown)
|
(Either::Left(pool_builder.build()), shutdown)
|
||||||
} else {
|
} else {
|
||||||
let (executor, shutdown) = IOContextRunner::start_single_threaded(name, reactor);
|
let (executor, shutdown) = IOContextRunner::start_single_threaded(name, wait, reactor);
|
||||||
|
|
||||||
(Either::Right(executor), shutdown)
|
(Either::Right(executor), shutdown)
|
||||||
};
|
};
|
||||||
|
@ -381,7 +401,8 @@ impl Socket {
|
||||||
.for_each(move |buffer| {
|
.for_each(move |buffer| {
|
||||||
let res = func(buffer);
|
let res = func(buffer);
|
||||||
match res {
|
match res {
|
||||||
Ok(()) => future::Either::A(YieldOnce::new()),
|
Ok(()) => future::Either::A(Ok(()).into_future()),
|
||||||
|
//Ok(()) => future::Either::A(YieldOnce::new()),
|
||||||
Err(err) => future::Either::B(Err(err).into_future()),
|
Err(err) => future::Either::B(Err(err).into_future()),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -552,6 +573,7 @@ const DEFAULT_CAPS: Option<gst::Caps> = None;
|
||||||
const DEFAULT_MTU: u32 = 1500;
|
const DEFAULT_MTU: u32 = 1500;
|
||||||
const DEFAULT_CONTEXT: &'static str = "";
|
const DEFAULT_CONTEXT: &'static str = "";
|
||||||
const DEFAULT_CONTEXT_THREADS: i32 = 0;
|
const DEFAULT_CONTEXT_THREADS: i32 = 0;
|
||||||
|
const DEFAULT_CONTEXT_WAIT: u32 = 0;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Settings {
|
struct Settings {
|
||||||
|
@ -561,6 +583,7 @@ struct Settings {
|
||||||
mtu: u32,
|
mtu: u32,
|
||||||
context: String,
|
context: String,
|
||||||
context_threads: i32,
|
context_threads: i32,
|
||||||
|
context_wait: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
|
@ -572,11 +595,12 @@ impl Default for Settings {
|
||||||
mtu: DEFAULT_MTU,
|
mtu: DEFAULT_MTU,
|
||||||
context: DEFAULT_CONTEXT.into(),
|
context: DEFAULT_CONTEXT.into(),
|
||||||
context_threads: DEFAULT_CONTEXT_THREADS,
|
context_threads: DEFAULT_CONTEXT_THREADS,
|
||||||
|
context_wait: DEFAULT_CONTEXT_WAIT,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static PROPERTIES: [Property; 6] = [
|
static PROPERTIES: [Property; 7] = [
|
||||||
Property::String(
|
Property::String(
|
||||||
"address",
|
"address",
|
||||||
"Address",
|
"Address",
|
||||||
|
@ -622,6 +646,14 @@ static PROPERTIES: [Property; 6] = [
|
||||||
DEFAULT_CONTEXT_THREADS,
|
DEFAULT_CONTEXT_THREADS,
|
||||||
PropertyMutability::ReadWrite,
|
PropertyMutability::ReadWrite,
|
||||||
),
|
),
|
||||||
|
Property::UInt(
|
||||||
|
"context-wait",
|
||||||
|
"Context Wait",
|
||||||
|
"Throttle poll loop to run at most once every this many ms",
|
||||||
|
(0, 1000),
|
||||||
|
DEFAULT_CONTEXT_WAIT,
|
||||||
|
PropertyMutability::ReadWrite,
|
||||||
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
|
@ -758,7 +790,7 @@ impl UdpSrc {
|
||||||
// TODO: Error handling
|
// TODO: Error handling
|
||||||
let mut state = self.state.lock().unwrap();
|
let mut state = self.state.lock().unwrap();
|
||||||
|
|
||||||
let io_context = IOContext::new(&settings.context, settings.context_threads as isize);
|
let io_context = IOContext::new(&settings.context, settings.context_threads as isize, settings.context_wait);
|
||||||
|
|
||||||
let addr: IpAddr = match settings.address {
|
let addr: IpAddr = match settings.address {
|
||||||
None => return Err(()),
|
None => return Err(()),
|
||||||
|
@ -926,6 +958,10 @@ impl ObjectImpl<Element> for UdpSrc {
|
||||||
let mut settings = self.settings.lock().unwrap();
|
let mut settings = self.settings.lock().unwrap();
|
||||||
settings.context_threads = value.get().unwrap();
|
settings.context_threads = value.get().unwrap();
|
||||||
}
|
}
|
||||||
|
Property::UInt("context-wait", ..) => {
|
||||||
|
let mut settings = self.settings.lock().unwrap();
|
||||||
|
settings.context_wait = value.get().unwrap();
|
||||||
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -958,6 +994,10 @@ impl ObjectImpl<Element> for UdpSrc {
|
||||||
let mut settings = self.settings.lock().unwrap();
|
let mut settings = self.settings.lock().unwrap();
|
||||||
Ok(settings.context_threads.to_value())
|
Ok(settings.context_threads.to_value())
|
||||||
}
|
}
|
||||||
|
Property::UInt("context-wait", ..) => {
|
||||||
|
let mut settings = self.settings.lock().unwrap();
|
||||||
|
Ok(settings.context_wait.to_value())
|
||||||
|
}
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue