Skip to content

Commit 4c7b7f5

Browse files
committed
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.
1 parent c2dcd5e commit 4c7b7f5

File tree

8 files changed

+96
-71
lines changed

8 files changed

+96
-71
lines changed

src/libnative/io/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ use std::num;
3232
// Local re-exports
3333
pub use self::process::Process;
3434

35-
mod helper_thread;
36-
3735
// Native I/O implementations
3836
pub mod process;
3937
mod util;

src/libnative/io/helper_thread.rs renamed to src/libstd/sys/common/helper_thread.rs

Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
2323
#![macro_escape]
2424

25-
use std::cell::UnsafeCell;
26-
use std::mem;
27-
use std::rt::bookkeeping;
28-
use std::rt::mutex::StaticNativeMutex;
29-
use std::rt;
30-
use std::task::TaskBuilder;
25+
use mem;
26+
use rt::bookkeeping;
27+
use rt::mutex::StaticNativeMutex;
28+
use rt;
29+
use cell::UnsafeCell;
30+
use sys::helper_signal;
31+
use prelude::*;
3132

32-
use NativeTaskBuilder;
33+
use task;
3334

3435
/// A structure for management of a helper thread.
3536
///
@@ -77,17 +78,17 @@ impl<M: Send> Helper<M> {
7778
/// This function is safe to be called many times.
7879
pub fn boot<T: Send>(&'static self,
7980
f: || -> T,
80-
helper: fn(imp::signal, Receiver<M>, T)) {
81+
helper: fn(helper_signal::signal, Receiver<M>, T)) {
8182
unsafe {
8283
let _guard = self.lock.lock();
8384
if !*self.initialized.get() {
8485
let (tx, rx) = channel();
8586
*self.chan.get() = mem::transmute(box tx);
86-
let (receive, send) = imp::new();
87+
let (receive, send) = helper_signal::new();
8788
*self.signal.get() = send as uint;
8889

8990
let t = f();
90-
TaskBuilder::new().native().spawn(proc() {
91+
task::spawn(proc() {
9192
bookkeeping::decrement();
9293
helper(receive, rx, t);
9394
self.lock.lock().signal()
@@ -111,7 +112,7 @@ impl<M: Send> Helper<M> {
111112
// send the message.
112113
assert!(!self.chan.get().is_null());
113114
(**self.chan.get()).send(msg);
114-
imp::signal(*self.signal.get() as imp::signal);
115+
helper_signal::signal(*self.signal.get() as helper_signal::signal);
115116
}
116117
}
117118

@@ -126,72 +127,16 @@ impl<M: Send> Helper<M> {
126127
let chan: Box<Sender<M>> = mem::transmute(*self.chan.get());
127128
*self.chan.get() = 0 as *mut Sender<M>;
128129
drop(chan);
129-
imp::signal(*self.signal.get() as imp::signal);
130+
helper_signal::signal(*self.signal.get() as helper_signal::signal);
130131

131132
// Wait for the child to exit
132133
guard.wait();
133134
drop(guard);
134135

135136
// Clean up after ourselves
136137
self.lock.destroy();
137-
imp::close(*self.signal.get() as imp::signal);
138+
helper_signal::close(*self.signal.get() as helper_signal::signal);
138139
*self.signal.get() = 0;
139140
}
140141
}
141142
}
142-
143-
#[cfg(unix)]
144-
mod imp {
145-
use libc;
146-
use std::os;
147-
148-
use io::file::FileDesc;
149-
150-
pub type signal = libc::c_int;
151-
152-
pub fn new() -> (signal, signal) {
153-
let os::Pipe { reader, writer } = unsafe { os::pipe().unwrap() };
154-
(reader, writer)
155-
}
156-
157-
pub fn signal(fd: libc::c_int) {
158-
FileDesc::new(fd, false).inner_write([0]).ok().unwrap();
159-
}
160-
161-
pub fn close(fd: libc::c_int) {
162-
let _fd = FileDesc::new(fd, true);
163-
}
164-
}
165-
166-
#[cfg(windows)]
167-
mod imp {
168-
use libc::{BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
169-
use std::ptr;
170-
use libc;
171-
172-
pub type signal = HANDLE;
173-
174-
pub fn new() -> (HANDLE, HANDLE) {
175-
unsafe {
176-
let handle = CreateEventA(ptr::null_mut(), libc::FALSE, libc::FALSE,
177-
ptr::null());
178-
(handle, handle)
179-
}
180-
}
181-
182-
pub fn signal(handle: HANDLE) {
183-
assert!(unsafe { SetEvent(handle) != 0 });
184-
}
185-
186-
pub fn close(handle: HANDLE) {
187-
assert!(unsafe { CloseHandle(handle) != 0 });
188-
}
189-
190-
extern "system" {
191-
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
192-
bManualReset: BOOL,
193-
bInitialState: BOOL,
194-
lpName: LPCSTR) -> HANDLE;
195-
fn SetEvent(hEvent: HANDLE) -> BOOL;
196-
}
197-
}

src/libstd/sys/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use path::BytesContainer;
2020
use collections;
2121

2222
pub mod net;
23+
pub mod helper_thread;
2324

2425
// common error constructors
2526

src/libstd/sys/common/net.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use prelude::*;
2424
use cmp;
2525
use io;
2626

27+
// FIXME: move uses of Arc and deadline tracking to std::io
28+
2729
#[deriving(Show)]
2830
pub enum SocketStatus {
2931
Readable,

src/libstd/sys/unix/helper_signal.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use libc;
12+
use os;
13+
14+
use sys::fs::FileDesc;
15+
16+
pub type signal = libc::c_int;
17+
18+
pub fn new() -> (signal, signal) {
19+
let os::Pipe { reader, writer } = unsafe { os::pipe().unwrap() };
20+
(reader, writer)
21+
}
22+
23+
pub fn signal(fd: libc::c_int) {
24+
FileDesc::new(fd, false).write([0]).ok().unwrap();
25+
}
26+
27+
pub fn close(fd: libc::c_int) {
28+
let _fd = FileDesc::new(fd, true);
29+
}

src/libstd/sys/unix/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ use prelude::*;
1717
use io::{mod, IoResult, IoError};
1818
use sys_common::mkerr_libc;
1919

20+
21+
macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => (
22+
static $name: Helper<$m> = Helper {
23+
lock: ::rt::mutex::NATIVE_MUTEX_INIT,
24+
chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> },
25+
signal: ::cell::UnsafeCell { value: 0 },
26+
initialized: ::cell::UnsafeCell { value: false },
27+
};
28+
) )
29+
2030
pub mod c;
2131
pub mod fs;
2232
pub mod os;
2333
pub mod tcp;
2434
pub mod udp;
2535
pub mod pipe;
36+
pub mod helper_signal;
2637

2738
pub mod addrinfo {
2839
pub use sys_common::net::get_host_addresses;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use libc::{mod, BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
12+
use ptr;
13+
14+
pub type signal = HANDLE;
15+
16+
pub fn new() -> (HANDLE, HANDLE) {
17+
unsafe {
18+
let handle = CreateEventA(ptr::null_mut(), libc::FALSE, libc::FALSE,
19+
ptr::null());
20+
(handle, handle)
21+
}
22+
}
23+
24+
pub fn signal(handle: HANDLE) {
25+
assert!(unsafe { SetEvent(handle) != 0 });
26+
}
27+
28+
pub fn close(handle: HANDLE) {
29+
assert!(unsafe { CloseHandle(handle) != 0 });
30+
}
31+
32+
extern "system" {
33+
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
34+
bManualReset: BOOL,
35+
bInitialState: BOOL,
36+
lpName: LPCSTR) -> HANDLE;
37+
fn SetEvent(hEvent: HANDLE) -> BOOL;
38+
}

src/libstd/sys/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod os;
3939
pub mod tcp;
4040
pub mod udp;
4141
pub mod pipe;
42+
pub mod helper_signal;
4243

4344
pub mod addrinfo {
4445
pub use sys_common::net::get_host_addresses;

0 commit comments

Comments
 (0)