Skip to content

Commit dcb5959

Browse files
committed
Fix for possible spurious wakeups
Fixes #1
1 parent ec65341 commit dcb5959

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#![cfg_attr(feature="nightly", feature(static_condvar))]
44
#![cfg_attr(feature="nightly", feature(static_mutex))]
55

6+
use std::sync::atomic::Ordering;
7+
68
#[cfg(not(windows))]
79
extern crate libc;
810
#[cfg(windows)]
@@ -15,17 +17,21 @@ extern crate lazy_static;
1517

1618
#[cfg(feature="nightly")]
1719
mod features {
20+
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
1821
use std::sync::{StaticCondvar, CONDVAR_INIT, StaticMutex, MUTEX_INIT};
1922
pub static CVAR: StaticCondvar = CONDVAR_INIT;
2023
pub static MUTEX: StaticMutex = MUTEX_INIT;
24+
pub static DONE: AtomicBool = ATOMIC_BOOL_INIT;
2125
}
2226
#[cfg(not(feature="nightly"))]
2327
mod features {
28+
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
2429
use std::sync::{Condvar, Mutex};
2530
lazy_static! {
2631
pub static ref CVAR: Condvar = Condvar::new();
2732
pub static ref MUTEX: Mutex<bool> = Mutex::new(false);
2833
}
34+
pub static DONE: AtomicBool = ATOMIC_BOOL_INIT;
2935
}
3036
use self::features::*;
3137

@@ -35,9 +41,11 @@ mod platform {
3541
use libc::types::os::common::posix01::sighandler_t;
3642
use libc::consts::os::posix88::SIGINT;
3743
use libc::funcs::posix01::signal::signal;
44+
use std::sync::atomic::Ordering;
3845

3946
#[repr(C)]
4047
pub fn handler(_: c_int) {
48+
super::features::DONE.store(true, Ordering::Relaxed);
4149
super::features::CVAR.notify_all();
4250
}
4351
#[inline]
@@ -49,8 +57,10 @@ mod platform {
4957
mod platform {
5058
use kernel32::SetConsoleCtrlHandler;
5159
use winapi::{BOOL, DWORD, TRUE};
60+
use std::sync::atomic::Ordering;
5261

5362
pub unsafe extern "system" fn handler(_: DWORD) -> BOOL {
63+
super::features::DONE.store(true, Ordering::Relaxed);
5464
super::features::CVAR.notify_all();
5565
TRUE
5666
}
@@ -75,7 +85,9 @@ impl CtrlC {
7585
}
7686
::std::thread::spawn(move || {
7787
loop {
78-
let _ = CVAR.wait(MUTEX.lock().unwrap());
88+
if !DONE.load(Ordering::Relaxed) {
89+
let _ = CVAR.wait(MUTEX.lock().unwrap());
90+
}
7991
user_handler();
8092
}
8193
});

0 commit comments

Comments
 (0)