1 // Licensed under the Apache License, Version 2.0
2 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4 // All files in the project carrying such notice may not be copied, modified, or distributed
5 // except according to those terms.
6 use {Result, k32, last_error, w};
7 use std::os::windows::io::{AsRawHandle};
8 use thread::{Thread};
9
queue<T>(func: T, thread: &Thread) -> Result<()> where T: FnOnce() + 'static10 pub fn queue<T>(func: T, thread: &Thread) -> Result<()> where T: FnOnce() + 'static {
11 unsafe extern "system" fn helper<T: FnOnce() + 'static>(thing: w::ULONG_PTR) {
12 let func = Box::from_raw(thing as *mut T);
13 func()
14 }
15 let thing = Box::into_raw(Box::new(func)) as w::ULONG_PTR;
16 match unsafe { k32::QueueUserAPC(Some(helper::<T>), thread.as_raw_handle(), thing) } {
17 0 => {
18 // If it fails we still need to deallocate the function
19 unsafe { Box::from_raw(thing as *mut T) };
20 last_error()
21 },
22 _ => Ok(()),
23 }
24 }
queue_current<T>(func: T) -> Result<()> where T: FnOnce() + 'static25 pub fn queue_current<T>(func: T) -> Result<()> where T: FnOnce() + 'static {
26 unsafe extern "system" fn helper<T: FnOnce() + 'static>(thing: w::ULONG_PTR) {
27 let func = Box::from_raw(thing as *mut T);
28 func()
29 }
30 let thing = Box::into_raw(Box::new(func)) as w::ULONG_PTR;
31 match unsafe { k32::QueueUserAPC(Some(helper::<T>), k32::GetCurrentThread(), thing) } {
32 0 => {
33 // If it fails we still need to deallocate the function
34 unsafe { Box::from_raw(thing as *mut T) };
35 last_error()
36 },
37 _ => Ok(()),
38 }
39 }
40