Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix build error on windows #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
Cargo.lock
.vscode
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ exclude = [

[dependencies]
nix = "^0.7.0"
kernel32-sys = "^0.2.2"
lazy_static = "^1.3.0"
winapi = {version = "^0.3.7", features=["minwindef", "consoleapi"]}
13 changes: 9 additions & 4 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
extern crate graceful;

use std::sync::atomic::{ATOMIC_BOOL_INIT, AtomicBool, Ordering};
use std::time::Duration;
use std::thread;
use std::sync::atomic::{AtomicBool, Ordering};

use std::thread;
use std::time::Duration;
use graceful::SignalGuard;

static STOP: AtomicBool = ATOMIC_BOOL_INIT;
#[macro_use]
extern crate lazy_static;

lazy_static! {
static ref STOP: AtomicBool = AtomicBool::new(false);
}

fn main() {
let signal_guard = SignalGuard::new();
Expand Down
32 changes: 22 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,31 @@ mod platform {
}
}

#[cfg(windows)]
#[macro_use]
extern crate lazy_static;

#[cfg(windows)]
mod platform {
extern crate winapi;
extern crate kernel32;

use std::sync::mpsc::{sync_channel, SyncSender, Receiver};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::Mutex;

use kernel32::SetConsoleCtrlHandler;
use winapi::{BOOL, DWORD, TRUE};
use self::winapi::shared::minwindef::{BOOL, DWORD, TRUE};
use self::winapi::um::consoleapi::SetConsoleCtrlHandler;

static CHAN: (SyncSender<DWORD>, Receiver<DWORD>) = sync_channel(0);
lazy_static! {
static ref CHAN: (SyncSender<DWORD>, Mutex<Receiver<DWORD>>) = {
let channel = sync_channel(0);
(channel.0, Mutex::new(channel.1))
};
}

unsafe extern "system" fn handler(event: DWORD) -> BOOL {
CHAN.0.send(event);
CHAN.0.send(0);
FALSE
CHAN.0.send(event).unwrap();
CHAN.0.send(0).unwrap();
TRUE
}

pub struct SignalGuard;
Expand All @@ -115,9 +124,12 @@ mod platform {
}

pub fn at_exit<F: FnOnce(usize)>(&self, handler: F) {
let event = CHAN.1.recv().unwrap();
let event = {
let receiver = CHAN.1.lock().unwrap();
receiver.recv().unwrap()
};
handler(event as usize);
CHAN.1.recv().unwrap();
CHAN.1.lock().unwrap().recv().unwrap();
}
}
}
Expand Down