1 // Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 use glib::translate::mut_override;
10 use glib_sys;
11 
12 #[must_use = "if unused the Mutex will immediately unlock"]
13 pub struct MutexGuard<'a>(&'a glib_sys::GMutex);
14 
15 impl<'a> MutexGuard<'a> {
16     #[allow(clippy::trivially_copy_pass_by_ref)]
lock(mutex: &'a glib_sys::GMutex) -> Self17     pub fn lock(mutex: &'a glib_sys::GMutex) -> Self {
18         unsafe {
19             glib_sys::g_mutex_lock(mut_override(mutex));
20         }
21         MutexGuard(mutex)
22     }
23 }
24 
25 impl<'a> Drop for MutexGuard<'a> {
drop(&mut self)26     fn drop(&mut self) {
27         unsafe {
28             glib_sys::g_mutex_unlock(mut_override(self.0));
29         }
30     }
31 }
32