Add private utils module with a MutexGuard around GMutex

This commit is contained in:
Sebastian Dröge 2017-10-25 12:54:52 +02:00
parent d94bb0e0fb
commit f30121ec53
2 changed files with 31 additions and 0 deletions

View file

@ -174,3 +174,5 @@ pub mod prelude {
pub use tags::Tag;
pub use miniobject::MiniObject;
}
mod utils;

29
gstreamer/src/utils.rs Normal file
View file

@ -0,0 +1,29 @@
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use glib::translate::mut_override;
use glib_ffi;
pub struct MutexGuard<'a>(&'a glib_ffi::GMutex);
impl<'a> MutexGuard<'a> {
pub fn lock(mutex: &'a glib_ffi::GMutex) -> Self {
unsafe {
glib_ffi::g_mutex_lock(mut_override(mutex));
}
MutexGuard(mutex)
}
}
impl<'a> Drop for MutexGuard<'a> {
fn drop(&mut self) {
unsafe {
glib_ffi::g_mutex_unlock(mut_override(self.0));
}
}
}