2018-02-12 08:44:44 +00:00
|
|
|
// 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;
|
2019-03-19 07:58:20 +00:00
|
|
|
use glib_sys;
|
2018-02-12 08:44:44 +00:00
|
|
|
|
2020-02-28 14:29:48 +00:00
|
|
|
#[must_use = "if unused the Mutex will immediately unlock"]
|
2019-03-19 07:58:20 +00:00
|
|
|
pub struct MutexGuard<'a>(&'a glib_sys::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)]
|
2019-03-19 07:58:20 +00:00
|
|
|
pub fn lock(mutex: &'a glib_sys::GMutex) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-02-12 08:44:44 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::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 {
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::g_mutex_unlock(mut_override(self.0));
|
2018-02-12 08:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|