2014-12-01 08:14:35 -08:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
|
//! Generic support for building blocking abstractions.
|
|
|
|
|
|
2015-02-17 15:10:25 -08:00
|
|
|
use thread::{self, Thread};
|
2014-12-29 15:03:01 -08:00
|
|
|
use sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
|
2014-12-01 08:14:35 -08:00
|
|
|
use sync::Arc;
|
2015-01-07 11:33:42 +13:00
|
|
|
use marker::{Sync, Send};
|
2014-12-01 08:14:35 -08:00
|
|
|
use mem;
|
|
|
|
|
use clone::Clone;
|
|
|
|
|
|
|
|
|
|
struct Inner {
|
|
|
|
|
thread: Thread,
|
|
|
|
|
woken: AtomicBool,
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-22 00:49:42 +01:00
|
|
|
unsafe impl Send for Inner {}
|
|
|
|
|
unsafe impl Sync for Inner {}
|
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2014-12-01 08:14:35 -08:00
|
|
|
pub struct SignalToken {
|
|
|
|
|
inner: Arc<Inner>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WaitToken {
|
|
|
|
|
inner: Arc<Inner>,
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-11 11:10:04 +01:00
|
|
|
impl !Send for WaitToken {}
|
|
|
|
|
|
|
|
|
|
impl !Sync for WaitToken {}
|
|
|
|
|
|
|
|
|
|
pub fn tokens() -> (WaitToken, SignalToken) {
|
|
|
|
|
let inner = Arc::new(Inner {
|
2015-02-17 15:10:25 -08:00
|
|
|
thread: thread::current(),
|
2015-01-11 11:10:04 +01:00
|
|
|
woken: ATOMIC_BOOL_INIT,
|
|
|
|
|
});
|
|
|
|
|
let wait_token = WaitToken {
|
|
|
|
|
inner: inner.clone(),
|
|
|
|
|
};
|
|
|
|
|
let signal_token = SignalToken {
|
|
|
|
|
inner: inner
|
|
|
|
|
};
|
|
|
|
|
(wait_token, signal_token)
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 08:14:35 -08:00
|
|
|
impl SignalToken {
|
2014-12-06 18:34:37 -08:00
|
|
|
pub fn signal(&self) -> bool {
|
2014-12-01 08:14:35 -08:00
|
|
|
let wake = !self.inner.woken.compare_and_swap(false, true, Ordering::SeqCst);
|
|
|
|
|
if wake {
|
|
|
|
|
self.inner.thread.unpark();
|
|
|
|
|
}
|
|
|
|
|
wake
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert to an unsafe uint value. Useful for storing in a pipe's state
|
|
|
|
|
/// flag.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn cast_to_uint(self) -> uint {
|
|
|
|
|
mem::transmute(self.inner)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert from an unsafe uint value. Useful for retrieving a pipe's state
|
|
|
|
|
/// flag.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn cast_from_uint(signal_ptr: uint) -> SignalToken {
|
|
|
|
|
SignalToken { inner: mem::transmute(signal_ptr) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WaitToken {
|
2014-12-06 18:34:37 -08:00
|
|
|
pub fn wait(self) {
|
2014-12-01 08:14:35 -08:00
|
|
|
while !self.inner.woken.load(Ordering::SeqCst) {
|
2015-02-17 15:10:25 -08:00
|
|
|
thread::park()
|
2014-12-01 08:14:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|