Don't lock the URI mutex all the time while {Sink,Source}::{start,stop} is called but only when needed

This commit is contained in:
Sebastian Dröge 2016-09-03 17:37:51 +03:00
parent fa855ec2b2
commit 245d576159
5 changed files with 24 additions and 25 deletions

View file

@ -60,7 +60,7 @@ impl Sink for FileSink {
Box::new(validate_uri)
}
fn start(&mut self, uri: &Url) -> Result<(), ErrorMessage> {
fn start(&mut self, uri: Url) -> Result<(), ErrorMessage> {
if let StreamingState::Started { .. } = self.streaming_state {
return Err(error_msg!(SinkError::Failure, ["Sink already started"]));
}

View file

@ -72,7 +72,7 @@ impl Source for FileSrc {
}
}
fn start(&mut self, uri: &Url) -> Result<(), ErrorMessage> {
fn start(&mut self, uri: Url) -> Result<(), ErrorMessage> {
if let StreamingState::Started { .. } = self.streaming_state {
return Err(error_msg!(SourceError::Failure, ["Source already started"]));
}

View file

@ -153,9 +153,9 @@ impl Source for HttpSrc {
}
}
fn start(&mut self, uri: &Url) -> Result<(), ErrorMessage> {
fn start(&mut self, uri: Url) -> Result<(), ErrorMessage> {
self.streaming_state = StreamingState::Stopped;
self.streaming_state = try!(self.do_request(uri.clone(), 0, None));
self.streaming_state = try!(self.do_request(uri, 0, None));
Ok(())
}

View file

@ -60,7 +60,7 @@ pub struct SinkWrapper {
pub trait Sink {
fn uri_validator(&self) -> Box<UriValidator>;
fn start(&mut self, uri: &Url) -> Result<(), ErrorMessage>;
fn start(&mut self, uri: Url) -> Result<(), ErrorMessage>;
fn stop(&mut self) -> Result<(), ErrorMessage>;
fn render(&mut self, data: &[u8]) -> Result<(), FlowError>;
@ -147,10 +147,13 @@ pub unsafe extern "C" fn sink_get_uri(ptr: *const SinkWrapper) -> *mut c_char {
pub unsafe extern "C" fn sink_start(ptr: *mut SinkWrapper) -> GBoolean {
let wrap: &mut SinkWrapper = &mut *ptr;
let sink = &mut wrap.sink.lock().unwrap();
let uri_storage = &mut wrap.uri.lock().unwrap();
let (uri, started) = match **uri_storage {
(Some(ref uri), ref mut started) => (uri, started),
let uri = match *wrap.uri.lock().unwrap() {
(Some(ref uri), ref mut started) => {
*started = true;
uri.clone()
}
(None, _) => {
error_msg!(SinkError::OpenFailed, ["No URI given"]).post(wrap.sink_raw);
return GBoolean::False;
@ -158,12 +161,9 @@ pub unsafe extern "C" fn sink_start(ptr: *mut SinkWrapper) -> GBoolean {
};
match sink.start(uri) {
Ok(..) => {
*started = true;
GBoolean::True
}
Ok(..) => GBoolean::True,
Err(ref msg) => {
wrap.uri.lock().unwrap().1 = false;
msg.post(wrap.sink_raw);
GBoolean::False
}
@ -174,11 +174,10 @@ pub unsafe extern "C" fn sink_start(ptr: *mut SinkWrapper) -> GBoolean {
pub unsafe extern "C" fn sink_stop(ptr: *mut SinkWrapper) -> GBoolean {
let wrap: &mut SinkWrapper = &mut *ptr;
let sink = &mut wrap.sink.lock().unwrap();
let uri_storage = &mut wrap.uri.lock().unwrap();
match sink.stop() {
Ok(..) => {
uri_storage.1 = false;
wrap.uri.lock().unwrap().1 = false;
GBoolean::True
}
Err(ref msg) => {

View file

@ -63,7 +63,7 @@ pub trait Source {
fn is_seekable(&self) -> bool;
fn get_size(&self) -> Option<u64>;
fn start(&mut self, uri: &Url) -> Result<(), ErrorMessage>;
fn start(&mut self, uri: Url) -> Result<(), ErrorMessage>;
fn stop(&mut self) -> Result<(), ErrorMessage>;
fn fill(&mut self, offset: u64, data: &mut [u8]) -> Result<usize, FlowError>;
fn seek(&mut self, start: u64, stop: Option<u64>) -> Result<(), ErrorMessage>;
@ -169,10 +169,13 @@ pub unsafe extern "C" fn source_get_size(ptr: *const SourceWrapper) -> u64 {
pub unsafe extern "C" fn source_start(ptr: *mut SourceWrapper) -> GBoolean {
let wrap: &mut SourceWrapper = &mut *ptr;
let source = &mut wrap.source.lock().unwrap();
let uri_storage = &mut wrap.uri.lock().unwrap();
let (uri, started) = match **uri_storage {
(Some(ref uri), ref mut started) => (uri, started),
let uri = match *wrap.uri.lock().unwrap() {
(Some(ref uri), ref mut started) => {
*started = true;
uri.clone()
}
(None, _) => {
error_msg!(SourceError::OpenFailed, ["No URI given"]).post(wrap.source_raw);
return GBoolean::False;
@ -180,11 +183,9 @@ pub unsafe extern "C" fn source_start(ptr: *mut SourceWrapper) -> GBoolean {
};
match source.start(uri) {
Ok(..) => {
*started = true;
GBoolean::True
}
Ok(..) => GBoolean::True,
Err(ref msg) => {
wrap.uri.lock().unwrap().1 = false;
msg.post(wrap.source_raw);
GBoolean::False
}
@ -195,11 +196,10 @@ pub unsafe extern "C" fn source_start(ptr: *mut SourceWrapper) -> GBoolean {
pub unsafe extern "C" fn source_stop(ptr: *mut SourceWrapper) -> GBoolean {
let wrap: &mut SourceWrapper = &mut *ptr;
let source = &mut wrap.source.lock().unwrap();
let uri_storage = &mut wrap.uri.lock().unwrap();
match source.stop() {
Ok(..) => {
uri_storage.1 = false;
wrap.uri.lock().unwrap().1 = false;
GBoolean::True
}
Err(ref msg) => {