2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-02-12 08:44:44 +00:00
|
|
|
|
|
|
|
use glib::translate::mut_override;
|
|
|
|
|
2020-02-28 14:29:48 +00:00
|
|
|
#[must_use = "if unused the Mutex will immediately unlock"]
|
2021-06-01 13:15:59 +00:00
|
|
|
#[doc(alias = "GMutex")]
|
2020-11-21 17:59:22 +00:00
|
|
|
pub struct MutexGuard<'a>(&'a glib::ffi::GMutex);
|
2018-02-12 08:44:44 +00:00
|
|
|
|
|
|
|
impl<'a> MutexGuard<'a> {
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "g_mutex_lock")]
|
2020-11-21 17:59:22 +00:00
|
|
|
pub fn lock(mutex: &'a glib::ffi::GMutex) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-02-12 08:44:44 +00:00
|
|
|
unsafe {
|
2020-11-21 17:59:22 +00:00
|
|
|
glib::ffi::g_mutex_lock(mut_override(mutex));
|
2018-02-12 08:44:44 +00:00
|
|
|
}
|
|
|
|
MutexGuard(mutex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for MutexGuard<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2020-11-21 17:59:22 +00:00
|
|
|
glib::ffi::g_mutex_unlock(mut_override(self.0));
|
2018-02-12 08:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|