Migrate everything to object lock API instead of custom GMutex handling

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1212>
This commit is contained in:
Sebastian Dröge 2023-02-06 10:49:34 +02:00
parent 3ddbdbd383
commit 91eec7a33f
22 changed files with 51 additions and 166 deletions

View file

@ -6,6 +6,7 @@ use glib::object::Cast;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use glib::signal::{connect_raw, SignalHandlerId};
use glib::{object::IsA, translate::*};
use gst::prelude::*;
use crate::auto::{AudioAggregator, AudioAggregatorPad};
@ -95,7 +96,7 @@ impl<O: IsA<AudioAggregator>> AudioAggregatorExtManual for O {
fn current_caps(&self) -> Option<gst::Caps> {
unsafe {
let ptr = self.as_ptr() as *mut ffi::GstAudioAggregator;
let _guard = crate::utils::MutexGuard::lock(&(*(ptr as *mut gst::ffi::GstObject)).lock);
let _guard = self.as_ref().object_lock();
from_glib_none((*ptr).current_caps)
}
}

View file

@ -1,4 +1,5 @@
use glib::{object::IsA, translate::*};
use gst::prelude::*;
use crate::auto::AudioAggregatorPad;
@ -10,7 +11,7 @@ impl<O: IsA<AudioAggregatorPad>> AudioAggregatorPadExtManual for O {
fn audio_info(&self) -> Option<crate::AudioInfo> {
unsafe {
let ptr = self.as_ptr() as *mut ffi::GstAudioAggregatorPad;
let _guard = crate::utils::MutexGuard::lock(&(*(ptr as *mut gst::ffi::GstObject)).lock);
let _guard = self.as_ref().object_lock();
let info = &(*ptr).info;

View file

@ -68,8 +68,6 @@ mod audio_encoder;
mod audio_converter;
pub use crate::audio_converter::AudioConverterConfig;
mod utils;
// Re-export all the traits in a prelude module, so that applications
// can always "use gst_audio::prelude::*" without getting conflicts
pub mod prelude {

View file

@ -1,30 +0,0 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::mut_override;
#[must_use = "if unused the Mutex will immediately unlock"]
#[doc(alias = "GMutex")]
pub struct MutexGuard<'a>(&'a glib::ffi::GMutex);
impl<'a> MutexGuard<'a> {
#[allow(clippy::trivially_copy_pass_by_ref)]
#[allow(dead_code)]
#[doc(alias = "g_mutex_lock")]
#[inline]
pub fn lock(mutex: &'a glib::ffi::GMutex) -> Self {
skip_assert_initialized!();
unsafe {
glib::ffi::g_mutex_lock(mut_override(mutex));
}
MutexGuard(mutex)
}
}
impl<'a> Drop for MutexGuard<'a> {
#[inline]
fn drop(&mut self) {
unsafe {
glib::ffi::g_mutex_unlock(mut_override(self.0));
}
}
}

View file

@ -12,7 +12,7 @@ use std::{mem, ptr};
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
use glib::signal::{connect_raw, SignalHandlerId};
use glib::{prelude::*, translate::*};
use gst::format::FormattedValue;
use gst::{format::FormattedValue, prelude::*};
use crate::Aggregator;
@ -143,7 +143,7 @@ impl<O: IsA<Aggregator>> AggregatorExtManual for O {
unsafe {
let ptr: *mut ffi::GstAggregator = self.as_ref().to_glib_none().0;
let ptr = &mut *ptr;
let _guard = crate::utils::MutexGuard::lock(&ptr.parent.object.lock);
let _guard = self.as_ref().object_lock();
// gstaggregator.c asserts that the src pad is always of type GST_TYPE_AGGREGATOR_PAD,
// so the pointer cast here should be safe.

View file

@ -1,6 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::{prelude::*, translate::*};
use gst::prelude::*;
use crate::AggregatorPad;
@ -13,7 +14,7 @@ impl<O: IsA<AggregatorPad>> AggregatorPadExtManual for O {
fn segment(&self) -> gst::Segment {
unsafe {
let ptr: &ffi::GstAggregatorPad = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&ptr.parent.object.lock);
let _guard = self.as_ref().object_lock();
from_glib_none(&ptr.segment as *const gst::ffi::GstSegment)
}
}

View file

@ -3,6 +3,7 @@
use std::mem;
use glib::{prelude::*, translate::*};
use gst::prelude::*;
use crate::BaseSink;
@ -22,7 +23,7 @@ impl<O: IsA<BaseSink>> BaseSinkExtManual for O {
fn segment(&self) -> gst::Segment {
unsafe {
let sink: &ffi::GstBaseSink = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&sink.element.object.lock);
let _guard = self.as_ref().object_lock();
from_glib_none(&sink.segment as *const _)
}
}

View file

@ -3,6 +3,7 @@
use std::{mem, ptr};
use glib::{prelude::*, translate::*};
use gst::prelude::*;
use crate::BaseSrc;
@ -44,7 +45,7 @@ impl<O: IsA<BaseSrc>> BaseSrcExtManual for O {
fn segment(&self) -> gst::Segment {
unsafe {
let src: &ffi::GstBaseSrc = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&src.element.object.lock);
let _guard = self.as_ref().object_lock();
from_glib_none(&src.segment as *const _)
}
}

View file

@ -3,6 +3,7 @@
use std::{mem, ptr};
use glib::{prelude::*, translate::*};
use gst::prelude::*;
use crate::BaseTransform;
@ -36,7 +37,7 @@ impl<O: IsA<BaseTransform>> BaseTransformExtManual for O {
fn segment(&self) -> gst::Segment {
unsafe {
let trans: &ffi::GstBaseTransform = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&trans.element.object.lock);
let _guard = self.as_ref().object_lock();
from_glib_none(&trans.segment as *const _)
}
}

View file

@ -64,6 +64,4 @@ pub mod prelude {
};
}
mod utils;
pub mod subclass;

View file

@ -1,29 +0,0 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::mut_override;
#[must_use = "if unused the Mutex will immediately unlock"]
#[doc(alias = "GMutex")]
pub struct MutexGuard<'a>(&'a glib::ffi::GMutex);
impl<'a> MutexGuard<'a> {
#[allow(clippy::trivially_copy_pass_by_ref)]
#[doc(alias = "g_mutex_lock")]
#[inline]
pub fn lock(mutex: &'a glib::ffi::GMutex) -> Self {
skip_assert_initialized!();
unsafe {
glib::ffi::g_mutex_lock(mut_override(mutex));
}
MutexGuard(mutex)
}
}
impl<'a> Drop for MutexGuard<'a> {
#[inline]
fn drop(&mut self) {
unsafe {
glib::ffi::g_mutex_unlock(mut_override(self.0));
}
}
}

View file

@ -1,37 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::mut_override;
pub trait HasStreamLock {
#[doc(alias = "get_stream_lock")]
fn stream_lock(&self) -> *mut glib::ffi::GRecMutex;
#[doc(alias = "get_element_as_ptr")]
fn element_as_ptr(&self) -> *const gst::ffi::GstElement;
}
#[must_use = "if unused the Mutex will immediately unlock"]
#[doc(alias = "GMutex")]
pub struct MutexGuard<'a>(&'a glib::ffi::GMutex);
impl<'a> MutexGuard<'a> {
#[allow(clippy::trivially_copy_pass_by_ref)]
#[allow(dead_code)]
#[doc(alias = "g_mutex_lock")]
#[inline]
pub fn lock(mutex: &'a glib::ffi::GMutex) -> Self {
skip_assert_initialized!();
unsafe {
glib::ffi::g_mutex_lock(mut_override(mutex));
}
MutexGuard(mutex)
}
}
impl<'a> Drop for MutexGuard<'a> {
#[inline]
fn drop(&mut self) {
unsafe {
glib::ffi::g_mutex_unlock(mut_override(self.0));
}
}
}

View file

@ -1,4 +1,5 @@
use glib::{object::IsA, translate::*};
use gst::prelude::*;
use crate::auto::VideoAggregator;
@ -10,7 +11,7 @@ impl<O: IsA<VideoAggregator>> VideoAggregatorExtManual for O {
fn video_info(&self) -> Option<crate::VideoInfo> {
unsafe {
let ptr = self.as_ptr() as *mut ffi::GstVideoAggregator;
let _guard = crate::utils::MutexGuard::lock(&(*(ptr as *mut gst::ffi::GstObject)).lock);
let _guard = self.as_ref().object_lock();
let info = &(*ptr).info;

View file

@ -1,4 +1,5 @@
use glib::{object::IsA, translate::*};
use gst::prelude::*;
use crate::{auto::VideoAggregatorPad, subclass::AggregateFramesToken};
@ -54,7 +55,7 @@ impl<O: IsA<VideoAggregatorPad>> VideoAggregatorPadExtManual for O {
fn video_info(&self) -> Option<crate::VideoInfo> {
unsafe {
let ptr = self.as_ptr() as *mut ffi::GstVideoAggregatorPad;
let _guard = crate::utils::MutexGuard::lock(&(*(ptr as *mut gst::ffi::GstObject)).lock);
let _guard = self.as_ref().object_lock();
let info = &(*ptr).info;

View file

@ -9,7 +9,7 @@ use glib::{
GString,
};
use crate::{Bin, BinFlags, Element, LoggableError};
use crate::{prelude::*, Bin, BinFlags, Element, LoggableError};
impl Bin {
// rustdoc-stripper-ignore-next
@ -171,7 +171,7 @@ impl<O: IsA<Bin>> GstBinExtManual for O {
fn children(&self) -> Vec<Element> {
unsafe {
let bin: &ffi::GstBin = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&bin.element.object.lock);
let _guard = self.as_ref().object_lock();
FromGlibPtrContainer::from_glib_none(bin.children)
}
}
@ -199,7 +199,7 @@ impl<O: IsA<Bin>> GstBinExtManual for O {
fn set_bin_flags(&self, flags: BinFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags |= flags.into_glib();
}
}
@ -207,7 +207,7 @@ impl<O: IsA<Bin>> GstBinExtManual for O {
fn unset_bin_flags(&self, flags: BinFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags &= !flags.into_glib();
}
}
@ -215,7 +215,7 @@ impl<O: IsA<Bin>> GstBinExtManual for O {
fn bin_flags(&self) -> BinFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
from_glib((*ptr).flags)
}
}
@ -293,7 +293,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::prelude::*;
#[test]
fn test_get_children() {

View file

@ -17,8 +17,8 @@ use glib::{
use libc::c_void;
use crate::{
Clock, ClockEntryType, ClockError, ClockFlags, ClockReturn, ClockSuccess, ClockTime,
ClockTimeDiff,
prelude::*, Clock, ClockEntryType, ClockError, ClockFlags, ClockReturn, ClockSuccess,
ClockTime, ClockTimeDiff,
};
glib::wrapper! {
@ -515,7 +515,7 @@ impl<O: IsA<Clock>> ClockExtManual for O {
fn set_clock_flags(&self, flags: ClockFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags |= flags.into_glib();
}
}
@ -523,7 +523,7 @@ impl<O: IsA<Clock>> ClockExtManual for O {
fn unset_clock_flags(&self, flags: ClockFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags &= !flags.into_glib();
}
}
@ -531,7 +531,7 @@ impl<O: IsA<Clock>> ClockExtManual for O {
fn clock_flags(&self) -> ClockFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
from_glib((*ptr).flags)
}
}
@ -541,10 +541,8 @@ impl<O: IsA<Clock>> ClockExtManual for O {
mod tests {
use std::sync::mpsc::channel;
use super::{
super::{prelude::*, *},
*,
};
use super::*;
use crate::SystemClock;
#[test]
fn test_wait() {

View file

@ -345,7 +345,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn set_element_flags(&self, flags: ElementFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags |= flags.into_glib();
}
}
@ -353,7 +353,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn unset_element_flags(&self, flags: ElementFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags &= !flags.into_glib();
}
}
@ -361,7 +361,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn element_flags(&self) -> ElementFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
from_glib((*ptr).flags)
}
}
@ -483,7 +483,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn pads(&self) -> Vec<Pad> {
unsafe {
let elt: &ffi::GstElement = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&elt.object.lock);
let _guard = self.as_ref().object_lock();
FromGlibPtrContainer::from_glib_none(elt.pads)
}
}
@ -491,7 +491,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn sink_pads(&self) -> Vec<Pad> {
unsafe {
let elt: &ffi::GstElement = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&elt.object.lock);
let _guard = self.as_ref().object_lock();
FromGlibPtrContainer::from_glib_none(elt.sinkpads)
}
}
@ -499,7 +499,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn src_pads(&self) -> Vec<Pad> {
unsafe {
let elt: &ffi::GstElement = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&elt.object.lock);
let _guard = self.as_ref().object_lock();
FromGlibPtrContainer::from_glib_none(elt.srcpads)
}
}
@ -507,7 +507,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn num_pads(&self) -> u16 {
unsafe {
let elt: &ffi::GstElement = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&elt.object.lock);
let _guard = self.as_ref().object_lock();
elt.numpads
}
}
@ -515,7 +515,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn num_sink_pads(&self) -> u16 {
unsafe {
let elt: &ffi::GstElement = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&elt.object.lock);
let _guard = self.as_ref().object_lock();
elt.numsinkpads
}
}
@ -523,7 +523,7 @@ impl<O: IsA<Element>> ElementExtManual for O {
fn num_src_pads(&self) -> u16 {
unsafe {
let elt: &ffi::GstElement = &*(self.as_ptr() as *const _);
let _guard = crate::utils::MutexGuard::lock(&elt.object.lock);
let _guard = self.as_ref().object_lock();
elt.numsrcpads
}
}

View file

@ -77,7 +77,7 @@ impl<O: IsA<crate::Object>> GstObjectExtManual for O {
fn set_object_flags(&self, flags: ObjectFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags |= flags.into_glib();
}
}
@ -85,7 +85,7 @@ impl<O: IsA<crate::Object>> GstObjectExtManual for O {
fn unset_object_flags(&self, flags: ObjectFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags &= !flags.into_glib();
}
}
@ -93,7 +93,7 @@ impl<O: IsA<crate::Object>> GstObjectExtManual for O {
fn object_flags(&self) -> ObjectFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
from_glib((*ptr).flags)
}
}

View file

@ -997,7 +997,7 @@ impl<O: IsA<Pad>> PadExtManual for O {
fn set_pad_flags(&self, flags: PadFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags |= flags.into_glib();
}
}
@ -1005,7 +1005,7 @@ impl<O: IsA<Pad>> PadExtManual for O {
fn unset_pad_flags(&self, flags: PadFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags &= !flags.into_glib();
}
}
@ -1013,7 +1013,7 @@ impl<O: IsA<Pad>> PadExtManual for O {
fn pad_flags(&self) -> PadFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
from_glib((*ptr).flags)
}
}

View file

@ -2,7 +2,7 @@
use glib::{prelude::*, translate::*};
use crate::{Pipeline, PipelineFlags};
use crate::{prelude::*, Pipeline, PipelineFlags};
impl Pipeline {
// rustdoc-stripper-ignore-next
@ -27,7 +27,7 @@ impl<O: IsA<crate::Pipeline>> GstPipelineExtManual for O {
fn set_pipeline_flags(&self, flags: PipelineFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags |= flags.into_glib();
}
}
@ -35,7 +35,7 @@ impl<O: IsA<crate::Pipeline>> GstPipelineExtManual for O {
fn unset_pipeline_flags(&self, flags: PipelineFlags) {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
(*ptr).flags &= !flags.into_glib();
}
}
@ -43,7 +43,7 @@ impl<O: IsA<crate::Pipeline>> GstPipelineExtManual for O {
fn pipeline_flags(&self) -> PipelineFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.as_ref().object_lock();
from_glib((*ptr).flags)
}
}

View file

@ -2,7 +2,7 @@
use glib::{prelude::*, translate::*};
use crate::{Plugin, PluginFlags, StructureRef};
use crate::{prelude::*, Plugin, PluginFlags, StructureRef};
impl Plugin {
#[doc(alias = "get_cache_data")]
@ -22,7 +22,7 @@ impl Plugin {
pub fn plugin_flags(&self) -> PluginFlags {
unsafe {
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
let _guard = self.object_lock();
from_glib((*ptr).flags)
}
}

View file

@ -1,33 +1,5 @@
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::mut_override;
#[must_use = "if unused the Mutex will immediately unlock"]
#[doc(alias = "GMutex")]
pub struct MutexGuard<'a>(&'a glib::ffi::GMutex);
impl<'a> MutexGuard<'a> {
#[allow(clippy::trivially_copy_pass_by_ref)]
#[doc(alias = "g_mutex_lock")]
#[inline]
pub fn lock(mutex: &'a glib::ffi::GMutex) -> Self {
skip_assert_initialized!();
unsafe {
glib::ffi::g_mutex_lock(mut_override(mutex));
}
MutexGuard(mutex)
}
}
impl<'a> Drop for MutexGuard<'a> {
#[inline]
fn drop(&mut self) {
unsafe {
glib::ffi::g_mutex_unlock(mut_override(self.0));
}
}
}
// rustdoc-stripper-ignore-next
/// Trait that allows accessing `Display` implementation on types external to this crate.
pub trait Displayable {