Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ concurrency = "send+sync"
name = "GLib.MainLoop"
status = "generate"
concurrency = "send+sync"
[[object.function]]
name = "get_context"
[object.function.return]
nullable = false

[[object]]
name = "GLib.Source"
Expand Down
2 changes: 1 addition & 1 deletion src/auto/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl MainLoop {
}
}

pub fn get_context(&self) -> Option<MainContext> {
pub fn get_context(&self) -> MainContext {
unsafe {
from_glib_none(ffi::g_main_loop_get_context(self.to_glib_none().0))
}
Expand Down
84 changes: 83 additions & 1 deletion src/main_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use ffi;
use translate::*;
use std::mem;
use std::mem::transmute;
use std::mem;
use ffi as glib_ffi;
use ffi::{gpointer, gboolean};

Expand Down Expand Up @@ -45,6 +45,21 @@ impl MainContext {
func as gpointer, Some(destroy_closure::<F>))
}
}

/// Calls closure with context configured as the thread default one.
///
/// Thread default context is changed in panic-safe manner by calling
/// [`push_thread_default`][push_thread_default] before calling closure
/// and [`pop_thread_default`][pop_thread_default] afterwards regardless
/// of whether closure panicked or not.
///
/// [push_thread_default]: struct.MainContext.html#method.push_thread_default
/// [pop_thread_default]: struct.MainContext.html#method.pop_thread_default
pub fn with_thread_default<R, F: Sized>(&self, func: F) -> R
where F: FnOnce() -> R {
let _thread_default = ThreadDefaultContext::new(self);
func()
}
}

#[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))]
Expand All @@ -61,9 +76,27 @@ unsafe extern "C" fn destroy_closure<F: FnOnce() + Send + 'static>(ptr: gpointer
Box::<Option<Box<F>>>::from_raw(ptr as *mut _);
}


struct ThreadDefaultContext<'a>(&'a MainContext);

impl<'a> ThreadDefaultContext<'a> {
fn new(ctx: &MainContext) -> ThreadDefaultContext {
ctx.push_thread_default();
ThreadDefaultContext(ctx)
}
}

impl<'a> Drop for ThreadDefaultContext<'a> {
fn drop(&mut self) {
self.0.pop_thread_default();
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::panic;
use std::ptr;
use std::thread;

#[test]
Expand All @@ -78,4 +111,53 @@ mod tests {

l.run();
}

fn is_same_context(a: &MainContext, b: &MainContext) -> bool {
ptr::eq(a.to_glib_none().0, b.to_glib_none().0)
}

#[test]
fn test_with_thread_default() {
let a = MainContext::new();
let b = MainContext::new();

assert!(!is_same_context(&a, &b));

a.with_thread_default(|| {
let t = MainContext::get_thread_default().unwrap();
assert!(is_same_context(&a, &t));

&b.with_thread_default(|| {
let t = MainContext::get_thread_default().unwrap();
assert!(is_same_context(&b, &t));
});

let t = MainContext::get_thread_default().unwrap();
assert!(is_same_context(&a, &t));
});
}

#[test]
fn test_with_thread_default_is_panic_safe() {
let a = MainContext::new();
let b = MainContext::new();

assert!(!is_same_context(&a, &b));

a.with_thread_default(|| {
let t = MainContext::get_thread_default().unwrap();
assert!(is_same_context(&a, &t));

let result = panic::catch_unwind(|| {
&b.with_thread_default(|| {
panic!();
});
});
assert!(result.is_err());

let t = MainContext::get_thread_default().unwrap();
assert!(is_same_context(&a, &t));
});

}
}