Runtime removal: refactor helper threads
This patch continues the runtime removal by moving libnative::io::helper_thread into sys::helper_signal and sys_common::helper_thread Because this eliminates APIs in `libnative` and `librustrt`, it is a: [breaking-change] This functionality is likely to be available publicly, in some form, from `std` in the future.
This commit is contained in:
parent
d34b1b0ca9
commit
3d195482a4
@ -32,8 +32,6 @@ use std::num;
|
||||
// Local re-exports
|
||||
pub use self::process::Process;
|
||||
|
||||
mod helper_thread;
|
||||
|
||||
// Native I/O implementations
|
||||
pub mod process;
|
||||
mod util;
|
||||
|
@ -22,14 +22,15 @@
|
||||
|
||||
#![macro_escape]
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
use std::mem;
|
||||
use std::rt::bookkeeping;
|
||||
use std::rt::mutex::StaticNativeMutex;
|
||||
use std::rt;
|
||||
use std::task::TaskBuilder;
|
||||
use mem;
|
||||
use rt::bookkeeping;
|
||||
use rt::mutex::StaticNativeMutex;
|
||||
use rt;
|
||||
use cell::UnsafeCell;
|
||||
use sys::helper_signal;
|
||||
use prelude::*;
|
||||
|
||||
use NativeTaskBuilder;
|
||||
use task;
|
||||
|
||||
/// A structure for management of a helper thread.
|
||||
///
|
||||
@ -77,17 +78,17 @@ impl<M: Send> Helper<M> {
|
||||
/// This function is safe to be called many times.
|
||||
pub fn boot<T: Send>(&'static self,
|
||||
f: || -> T,
|
||||
helper: fn(imp::signal, Receiver<M>, T)) {
|
||||
helper: fn(helper_signal::signal, Receiver<M>, T)) {
|
||||
unsafe {
|
||||
let _guard = self.lock.lock();
|
||||
if !*self.initialized.get() {
|
||||
let (tx, rx) = channel();
|
||||
*self.chan.get() = mem::transmute(box tx);
|
||||
let (receive, send) = imp::new();
|
||||
let (receive, send) = helper_signal::new();
|
||||
*self.signal.get() = send as uint;
|
||||
|
||||
let t = f();
|
||||
TaskBuilder::new().native().spawn(proc() {
|
||||
task::spawn(proc() {
|
||||
bookkeeping::decrement();
|
||||
helper(receive, rx, t);
|
||||
self.lock.lock().signal()
|
||||
@ -111,7 +112,7 @@ impl<M: Send> Helper<M> {
|
||||
// send the message.
|
||||
assert!(!self.chan.get().is_null());
|
||||
(**self.chan.get()).send(msg);
|
||||
imp::signal(*self.signal.get() as imp::signal);
|
||||
helper_signal::signal(*self.signal.get() as helper_signal::signal);
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,7 +127,7 @@ impl<M: Send> Helper<M> {
|
||||
let chan: Box<Sender<M>> = mem::transmute(*self.chan.get());
|
||||
*self.chan.get() = 0 as *mut Sender<M>;
|
||||
drop(chan);
|
||||
imp::signal(*self.signal.get() as imp::signal);
|
||||
helper_signal::signal(*self.signal.get() as helper_signal::signal);
|
||||
|
||||
// Wait for the child to exit
|
||||
guard.wait();
|
||||
@ -134,64 +135,8 @@ impl<M: Send> Helper<M> {
|
||||
|
||||
// Clean up after ourselves
|
||||
self.lock.destroy();
|
||||
imp::close(*self.signal.get() as imp::signal);
|
||||
helper_signal::close(*self.signal.get() as helper_signal::signal);
|
||||
*self.signal.get() = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
mod imp {
|
||||
use libc;
|
||||
use std::os;
|
||||
|
||||
use io::file::FileDesc;
|
||||
|
||||
pub type signal = libc::c_int;
|
||||
|
||||
pub fn new() -> (signal, signal) {
|
||||
let os::Pipe { reader, writer } = unsafe { os::pipe().unwrap() };
|
||||
(reader, writer)
|
||||
}
|
||||
|
||||
pub fn signal(fd: libc::c_int) {
|
||||
FileDesc::new(fd, false).inner_write([0]).ok().unwrap();
|
||||
}
|
||||
|
||||
pub fn close(fd: libc::c_int) {
|
||||
let _fd = FileDesc::new(fd, true);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod imp {
|
||||
use libc::{BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
|
||||
use std::ptr;
|
||||
use libc;
|
||||
|
||||
pub type signal = HANDLE;
|
||||
|
||||
pub fn new() -> (HANDLE, HANDLE) {
|
||||
unsafe {
|
||||
let handle = CreateEventA(ptr::null_mut(), libc::FALSE, libc::FALSE,
|
||||
ptr::null());
|
||||
(handle, handle)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn signal(handle: HANDLE) {
|
||||
assert!(unsafe { SetEvent(handle) != 0 });
|
||||
}
|
||||
|
||||
pub fn close(handle: HANDLE) {
|
||||
assert!(unsafe { CloseHandle(handle) != 0 });
|
||||
}
|
||||
|
||||
extern "system" {
|
||||
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
||||
bManualReset: BOOL,
|
||||
bInitialState: BOOL,
|
||||
lpName: LPCSTR) -> HANDLE;
|
||||
fn SetEvent(hEvent: HANDLE) -> BOOL;
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@ use path::BytesContainer;
|
||||
use collections;
|
||||
|
||||
pub mod net;
|
||||
pub mod helper_thread;
|
||||
|
||||
// common error constructors
|
||||
|
||||
|
@ -24,6 +24,8 @@ use prelude::*;
|
||||
use cmp;
|
||||
use io;
|
||||
|
||||
// FIXME: move uses of Arc and deadline tracking to std::io
|
||||
|
||||
#[deriving(Show)]
|
||||
pub enum SocketStatus {
|
||||
Readable,
|
||||
|
29
src/libstd/sys/unix/helper_signal.rs
Normal file
29
src/libstd/sys/unix/helper_signal.rs
Normal file
@ -0,0 +1,29 @@
|
||||
// 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.
|
||||
|
||||
use libc;
|
||||
use os;
|
||||
|
||||
use sys::fs::FileDesc;
|
||||
|
||||
pub type signal = libc::c_int;
|
||||
|
||||
pub fn new() -> (signal, signal) {
|
||||
let os::Pipe { reader, writer } = unsafe { os::pipe().unwrap() };
|
||||
(reader, writer)
|
||||
}
|
||||
|
||||
pub fn signal(fd: libc::c_int) {
|
||||
FileDesc::new(fd, false).write([0]).ok().unwrap();
|
||||
}
|
||||
|
||||
pub fn close(fd: libc::c_int) {
|
||||
let _fd = FileDesc::new(fd, true);
|
||||
}
|
@ -17,12 +17,23 @@ use prelude::*;
|
||||
use io::{mod, IoResult, IoError};
|
||||
use sys_common::mkerr_libc;
|
||||
|
||||
|
||||
macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => (
|
||||
static $name: Helper<$m> = Helper {
|
||||
lock: ::rt::mutex::NATIVE_MUTEX_INIT,
|
||||
chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> },
|
||||
signal: ::cell::UnsafeCell { value: 0 },
|
||||
initialized: ::cell::UnsafeCell { value: false },
|
||||
};
|
||||
) )
|
||||
|
||||
pub mod c;
|
||||
pub mod fs;
|
||||
pub mod os;
|
||||
pub mod tcp;
|
||||
pub mod udp;
|
||||
pub mod pipe;
|
||||
pub mod helper_signal;
|
||||
|
||||
pub mod addrinfo {
|
||||
pub use sys_common::net::get_host_addresses;
|
||||
|
38
src/libstd/sys/windows/helper_signal.rs
Normal file
38
src/libstd/sys/windows/helper_signal.rs
Normal file
@ -0,0 +1,38 @@
|
||||
// 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.
|
||||
|
||||
use libc::{mod, BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
|
||||
use ptr;
|
||||
|
||||
pub type signal = HANDLE;
|
||||
|
||||
pub fn new() -> (HANDLE, HANDLE) {
|
||||
unsafe {
|
||||
let handle = CreateEventA(ptr::null_mut(), libc::FALSE, libc::FALSE,
|
||||
ptr::null());
|
||||
(handle, handle)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn signal(handle: HANDLE) {
|
||||
assert!(unsafe { SetEvent(handle) != 0 });
|
||||
}
|
||||
|
||||
pub fn close(handle: HANDLE) {
|
||||
assert!(unsafe { CloseHandle(handle) != 0 });
|
||||
}
|
||||
|
||||
extern "system" {
|
||||
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
||||
bManualReset: BOOL,
|
||||
bInitialState: BOOL,
|
||||
lpName: LPCSTR) -> HANDLE;
|
||||
fn SetEvent(hEvent: HANDLE) -> BOOL;
|
||||
}
|
@ -39,6 +39,7 @@ pub mod os;
|
||||
pub mod tcp;
|
||||
pub mod udp;
|
||||
pub mod pipe;
|
||||
pub mod helper_signal;
|
||||
|
||||
pub mod addrinfo {
|
||||
pub use sys_common::net::get_host_addresses;
|
||||
|
Loading…
Reference in New Issue
Block a user