2013-11-12 16:38:28 -06:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
//! Native thread-blocking I/O implementation
|
|
|
|
//!
|
|
|
|
//! This module contains the implementation of native thread-blocking
|
|
|
|
//! implementations of I/O on all platforms. This module is not intended to be
|
|
|
|
//! used directly, but rather the rust runtime will fall back to using it if
|
|
|
|
//! necessary.
|
|
|
|
//!
|
|
|
|
//! Rust code normally runs inside of green tasks with a local scheduler using
|
|
|
|
//! asynchronous I/O to cooperate among tasks. This model is not always
|
|
|
|
//! available, however, and that's where these native implementations come into
|
|
|
|
//! play. The only dependencies of these modules are the normal system libraries
|
|
|
|
//! that you would find on the respective platform.
|
|
|
|
|
2013-12-12 19:54:53 -06:00
|
|
|
use std::c_str::CString;
|
2014-01-22 14:38:19 -06:00
|
|
|
use std::io;
|
|
|
|
use std::io::IoError;
|
|
|
|
use std::io::net::ip::SocketAddr;
|
|
|
|
use std::io::process::ProcessConfig;
|
|
|
|
use std::io::signal::Signum;
|
2013-12-12 19:54:53 -06:00
|
|
|
use std::libc::c_int;
|
|
|
|
use std::libc;
|
|
|
|
use std::os;
|
|
|
|
use std::rt::rtio;
|
|
|
|
use std::rt::rtio::{RtioTcpStream, RtioTcpListener, RtioUdpSocket,
|
|
|
|
RtioUnixListener, RtioPipe, RtioFileStream, RtioProcess,
|
|
|
|
RtioSignal, RtioTTY, CloseBehavior, RtioTimer};
|
|
|
|
use ai = std::io::net::addrinfo;
|
2013-11-12 16:38:28 -06:00
|
|
|
|
|
|
|
// Local re-exports
|
|
|
|
pub use self::file::FileDesc;
|
|
|
|
pub use self::process::Process;
|
|
|
|
|
|
|
|
// Native I/O implementations
|
2014-01-22 14:38:19 -06:00
|
|
|
pub mod addrinfo;
|
2013-11-12 16:38:28 -06:00
|
|
|
pub mod file;
|
2013-12-27 19:50:16 -06:00
|
|
|
pub mod net;
|
2014-01-22 14:38:19 -06:00
|
|
|
pub mod process;
|
2013-11-12 16:38:28 -06:00
|
|
|
|
Implement native timers
Native timers are a much hairier thing to deal with than green timers due to the
interface that we would like to expose (both a blocking sleep() and a
channel-based interface). I ended up implementing timers in three different ways
for the various platforms that we supports.
In all three of the implementations, there is a worker thread which does send()s
on channels for timers. This worker thread is initialized once and then
communicated to in a platform-specific manner, but there's always a shared
channel available for sending messages to the worker thread.
* Windows - I decided to use windows kernel timer objects via
CreateWaitableTimer and SetWaitableTimer in order to provide sleeping
capabilities. The worker thread blocks via WaitForMultipleObjects where one of
the objects is an event that is used to wake up the helper thread (which then
drains the incoming message channel for requests).
* Linux/(Android?) - These have the ideal interface for implementing timers,
timerfd_create. Each timer corresponds to a timerfd, and the helper thread
uses epoll to wait for all active timers and then send() for the next one that
wakes up. The tricky part in this implementation is updating a timerfd, but
see the implementation for the fun details
* OSX/FreeBSD - These obviously don't have the windows APIs, and sadly don't
have the timerfd api available to them, so I have thrown together a solution
which uses select() plus a timeout in order to ad-hoc-ly implement a timer
solution for threads. The implementation is backed by a sorted array of timers
which need to fire. As I said, this is an ad-hoc solution which is certainly
not accurate timing-wise. I have done this implementation due to the lack of
other primitives to provide an implementation, and I've done it the best that
I could, but I'm sure that there's room for improvement.
I'm pretty happy with how these implementations turned out. In theory we could
drop the timerfd implementation and have linux use the select() + timeout
implementation, but it's so inaccurate that I would much rather continue to use
timerfd rather than my ad-hoc select() implementation.
The only change that I would make to the API in general is to have a generic
sleep() method on an IoFactory which doesn't require allocating a Timer object.
For everything but windows it's super-cheap to request a blocking sleep for a
set amount of time, and it's probably worth it to provide a sleep() which
doesn't do something like allocate a file descriptor on linux.
2013-12-29 01:33:56 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2014-01-23 03:08:34 -06:00
|
|
|
#[cfg(target_os = "android")]
|
Implement native timers
Native timers are a much hairier thing to deal with than green timers due to the
interface that we would like to expose (both a blocking sleep() and a
channel-based interface). I ended up implementing timers in three different ways
for the various platforms that we supports.
In all three of the implementations, there is a worker thread which does send()s
on channels for timers. This worker thread is initialized once and then
communicated to in a platform-specific manner, but there's always a shared
channel available for sending messages to the worker thread.
* Windows - I decided to use windows kernel timer objects via
CreateWaitableTimer and SetWaitableTimer in order to provide sleeping
capabilities. The worker thread blocks via WaitForMultipleObjects where one of
the objects is an event that is used to wake up the helper thread (which then
drains the incoming message channel for requests).
* Linux/(Android?) - These have the ideal interface for implementing timers,
timerfd_create. Each timer corresponds to a timerfd, and the helper thread
uses epoll to wait for all active timers and then send() for the next one that
wakes up. The tricky part in this implementation is updating a timerfd, but
see the implementation for the fun details
* OSX/FreeBSD - These obviously don't have the windows APIs, and sadly don't
have the timerfd api available to them, so I have thrown together a solution
which uses select() plus a timeout in order to ad-hoc-ly implement a timer
solution for threads. The implementation is backed by a sorted array of timers
which need to fire. As I said, this is an ad-hoc solution which is certainly
not accurate timing-wise. I have done this implementation due to the lack of
other primitives to provide an implementation, and I've done it the best that
I could, but I'm sure that there's room for improvement.
I'm pretty happy with how these implementations turned out. In theory we could
drop the timerfd implementation and have linux use the select() + timeout
implementation, but it's so inaccurate that I would much rather continue to use
timerfd rather than my ad-hoc select() implementation.
The only change that I would make to the API in general is to have a generic
sleep() method on an IoFactory which doesn't require allocating a Timer object.
For everything but windows it's super-cheap to request a blocking sleep for a
set amount of time, and it's probably worth it to provide a sleep() which
doesn't do something like allocate a file descriptor on linux.
2013-12-29 01:33:56 -06:00
|
|
|
#[path = "timer_other.rs"]
|
|
|
|
pub mod timer;
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[path = "timer_timerfd.rs"]
|
|
|
|
pub mod timer;
|
|
|
|
|
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
#[path = "timer_win32.rs"]
|
|
|
|
pub mod timer;
|
|
|
|
|
2014-02-07 12:10:48 -06:00
|
|
|
#[cfg(unix)]
|
2014-02-07 12:37:58 -06:00
|
|
|
#[path = "pipe_unix.rs"]
|
2014-02-07 12:10:48 -06:00
|
|
|
pub mod pipe;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-02-07 12:37:58 -06:00
|
|
|
#[path = "pipe_win32.rs"]
|
2014-02-07 12:10:48 -06:00
|
|
|
pub mod pipe;
|
|
|
|
|
Implement native timers
Native timers are a much hairier thing to deal with than green timers due to the
interface that we would like to expose (both a blocking sleep() and a
channel-based interface). I ended up implementing timers in three different ways
for the various platforms that we supports.
In all three of the implementations, there is a worker thread which does send()s
on channels for timers. This worker thread is initialized once and then
communicated to in a platform-specific manner, but there's always a shared
channel available for sending messages to the worker thread.
* Windows - I decided to use windows kernel timer objects via
CreateWaitableTimer and SetWaitableTimer in order to provide sleeping
capabilities. The worker thread blocks via WaitForMultipleObjects where one of
the objects is an event that is used to wake up the helper thread (which then
drains the incoming message channel for requests).
* Linux/(Android?) - These have the ideal interface for implementing timers,
timerfd_create. Each timer corresponds to a timerfd, and the helper thread
uses epoll to wait for all active timers and then send() for the next one that
wakes up. The tricky part in this implementation is updating a timerfd, but
see the implementation for the fun details
* OSX/FreeBSD - These obviously don't have the windows APIs, and sadly don't
have the timerfd api available to them, so I have thrown together a solution
which uses select() plus a timeout in order to ad-hoc-ly implement a timer
solution for threads. The implementation is backed by a sorted array of timers
which need to fire. As I said, this is an ad-hoc solution which is certainly
not accurate timing-wise. I have done this implementation due to the lack of
other primitives to provide an implementation, and I've done it the best that
I could, but I'm sure that there's room for improvement.
I'm pretty happy with how these implementations turned out. In theory we could
drop the timerfd implementation and have linux use the select() + timeout
implementation, but it's so inaccurate that I would much rather continue to use
timerfd rather than my ad-hoc select() implementation.
The only change that I would make to the API in general is to have a generic
sleep() method on an IoFactory which doesn't require allocating a Timer object.
For everything but windows it's super-cheap to request a blocking sleep for a
set amount of time, and it's probably worth it to provide a sleep() which
doesn't do something like allocate a file descriptor on linux.
2013-12-29 01:33:56 -06:00
|
|
|
mod timer_helper;
|
|
|
|
|
2014-01-25 20:25:02 -06:00
|
|
|
pub type IoResult<T> = Result<T, IoError>;
|
2013-11-12 16:38:28 -06:00
|
|
|
|
|
|
|
fn unimpl() -> IoError {
|
|
|
|
IoError {
|
|
|
|
kind: io::IoUnavailable,
|
|
|
|
desc: "unimplemented I/O interface",
|
|
|
|
detail: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 20:28:24 -06:00
|
|
|
fn translate_error(errno: i32, detail: bool) -> IoError {
|
2013-11-12 16:38:28 -06:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn get_err(errno: i32) -> (io::IoErrorKind, &'static str) {
|
|
|
|
match errno {
|
|
|
|
libc::EOF => (io::EndOfFile, "end of file"),
|
2014-02-07 12:37:58 -06:00
|
|
|
libc::ERROR_NO_DATA => (io::BrokenPipe, "the pipe is being closed"),
|
|
|
|
libc::ERROR_FILE_NOT_FOUND => (io::FileNotFound, "file not found"),
|
|
|
|
libc::ERROR_INVALID_NAME => (io::InvalidInput, "invalid file name"),
|
2013-12-27 19:50:16 -06:00
|
|
|
libc::WSAECONNREFUSED => (io::ConnectionRefused, "connection refused"),
|
|
|
|
libc::WSAECONNRESET => (io::ConnectionReset, "connection reset"),
|
|
|
|
libc::WSAEACCES => (io::PermissionDenied, "permission denied"),
|
|
|
|
libc::WSAEWOULDBLOCK =>
|
|
|
|
(io::ResourceUnavailable, "resource temporarily unavailable"),
|
|
|
|
libc::WSAENOTCONN => (io::NotConnected, "not connected"),
|
|
|
|
libc::WSAECONNABORTED => (io::ConnectionAborted, "connection aborted"),
|
|
|
|
libc::WSAEADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
|
|
|
|
libc::WSAEADDRINUSE => (io::ConnectionRefused, "address in use"),
|
2014-02-07 12:37:58 -06:00
|
|
|
libc::ERROR_BROKEN_PIPE => (io::BrokenPipe, "the pipe has ended"),
|
2013-12-27 19:50:16 -06:00
|
|
|
|
|
|
|
x => {
|
|
|
|
debug!("ignoring {}: {}", x, os::last_os_error());
|
|
|
|
(io::OtherIoError, "unknown error")
|
|
|
|
}
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn get_err(errno: i32) -> (io::IoErrorKind, &'static str) {
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME: this should probably be a bit more descriptive...
|
2013-11-12 16:38:28 -06:00
|
|
|
match errno {
|
|
|
|
libc::EOF => (io::EndOfFile, "end of file"),
|
2013-12-27 19:50:16 -06:00
|
|
|
libc::ECONNREFUSED => (io::ConnectionRefused, "connection refused"),
|
|
|
|
libc::ECONNRESET => (io::ConnectionReset, "connection reset"),
|
|
|
|
libc::EPERM | libc::EACCES =>
|
|
|
|
(io::PermissionDenied, "permission denied"),
|
|
|
|
libc::EPIPE => (io::BrokenPipe, "broken pipe"),
|
|
|
|
libc::ENOTCONN => (io::NotConnected, "not connected"),
|
|
|
|
libc::ECONNABORTED => (io::ConnectionAborted, "connection aborted"),
|
|
|
|
libc::EADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
|
|
|
|
libc::EADDRINUSE => (io::ConnectionRefused, "address in use"),
|
2014-02-07 12:37:58 -06:00
|
|
|
libc::ENOENT => (io::FileNotFound, "no such file or directory"),
|
2013-11-12 16:38:28 -06:00
|
|
|
|
|
|
|
// These two constants can have the same value on some systems, but
|
|
|
|
// different values on others, so we can't use a match clause
|
|
|
|
x if x == libc::EAGAIN || x == libc::EWOULDBLOCK =>
|
|
|
|
(io::ResourceUnavailable, "resource temporarily unavailable"),
|
|
|
|
|
2013-12-27 19:50:16 -06:00
|
|
|
x => {
|
|
|
|
debug!("ignoring {}: {}", x, os::last_os_error());
|
|
|
|
(io::OtherIoError, "unknown error")
|
|
|
|
}
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 20:28:24 -06:00
|
|
|
let (kind, desc) = get_err(errno);
|
2013-11-12 16:38:28 -06:00
|
|
|
IoError {
|
|
|
|
kind: kind,
|
|
|
|
desc: desc,
|
2013-12-26 20:28:24 -06:00
|
|
|
detail: if detail {Some(os::last_os_error())} else {None},
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 20:28:24 -06:00
|
|
|
fn last_error() -> IoError { translate_error(os::errno() as i32, true) }
|
|
|
|
|
2013-11-13 16:48:45 -06:00
|
|
|
// unix has nonzero values as errors
|
|
|
|
fn mkerr_libc(ret: libc::c_int) -> IoResult<()> {
|
|
|
|
if ret != 0 {
|
|
|
|
Err(last_error())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// windows has zero values as errors
|
2013-12-08 01:55:28 -06:00
|
|
|
#[cfg(windows)]
|
2013-11-13 16:48:45 -06:00
|
|
|
fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
|
|
|
|
if ret == 0 {
|
|
|
|
Err(last_error())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-04 01:49:56 -06:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[inline]
|
|
|
|
fn retry(f: || -> libc::c_int) -> libc::c_int {
|
|
|
|
loop {
|
|
|
|
match f() {
|
|
|
|
-1 if os::errno() as int == libc::WSAEINTR as int => {}
|
|
|
|
n => return n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 20:28:24 -06:00
|
|
|
#[cfg(unix)]
|
2014-01-04 01:49:56 -06:00
|
|
|
#[inline]
|
|
|
|
fn retry(f: || -> libc::c_int) -> libc::c_int {
|
2013-12-26 20:28:24 -06:00
|
|
|
loop {
|
|
|
|
match f() {
|
|
|
|
-1 if os::errno() as int == libc::EINTR as int => {}
|
2014-01-04 01:49:56 -06:00
|
|
|
n => return n,
|
2013-12-26 20:28:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-12 16:38:28 -06:00
|
|
|
/// Implementation of rt::rtio's IoFactory trait to generate handles to the
|
|
|
|
/// native I/O functionality.
|
2013-12-27 19:50:16 -06:00
|
|
|
pub struct IoFactory {
|
|
|
|
priv cannot_construct_outside_of_this_module: ()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IoFactory {
|
|
|
|
pub fn new() -> IoFactory {
|
|
|
|
net::init();
|
|
|
|
IoFactory { cannot_construct_outside_of_this_module: () }
|
|
|
|
}
|
|
|
|
}
|
2013-11-12 16:38:28 -06:00
|
|
|
|
|
|
|
impl rtio::IoFactory for IoFactory {
|
|
|
|
// networking
|
2013-12-27 19:50:16 -06:00
|
|
|
fn tcp_connect(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpStream> {
|
|
|
|
net::TcpStream::connect(addr).map(|s| ~s as ~RtioTcpStream)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-12-27 19:50:16 -06:00
|
|
|
fn tcp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioTcpListener> {
|
|
|
|
net::TcpListener::bind(addr).map(|s| ~s as ~RtioTcpListener)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-12-28 18:40:15 -06:00
|
|
|
fn udp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioUdpSocket> {
|
|
|
|
net::UdpSocket::bind(addr).map(|u| ~u as ~RtioUdpSocket)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2014-02-07 12:10:48 -06:00
|
|
|
fn unix_bind(&mut self, path: &CString) -> IoResult<~RtioUnixListener> {
|
|
|
|
pipe::UnixListener::bind(path).map(|s| ~s as ~RtioUnixListener)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2014-02-07 12:10:48 -06:00
|
|
|
fn unix_connect(&mut self, path: &CString) -> IoResult<~RtioPipe> {
|
|
|
|
pipe::UnixStream::connect(path).map(|s| ~s as ~RtioPipe)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2014-01-22 14:38:19 -06:00
|
|
|
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
|
|
|
|
hint: Option<ai::Hint>) -> IoResult<~[ai::Info]> {
|
|
|
|
addrinfo::GetAddrInfoRequest::run(host, servname, hint)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// filesystem operations
|
|
|
|
fn fs_from_raw_fd(&mut self, fd: c_int,
|
|
|
|
close: CloseBehavior) -> ~RtioFileStream {
|
|
|
|
let close = match close {
|
|
|
|
rtio::CloseSynchronously | rtio::CloseAsynchronously => true,
|
|
|
|
rtio::DontClose => false
|
|
|
|
};
|
|
|
|
~file::FileDesc::new(fd, close) as ~RtioFileStream
|
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_open(&mut self, path: &CString, fm: io::FileMode, fa: io::FileAccess)
|
2013-11-12 16:38:28 -06:00
|
|
|
-> IoResult<~RtioFileStream> {
|
2013-11-13 16:48:45 -06:00
|
|
|
file::open(path, fm, fa).map(|fd| ~fd as ~RtioFileStream)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_unlink(&mut self, path: &CString) -> IoResult<()> {
|
|
|
|
file::unlink(path)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_stat(&mut self, path: &CString) -> IoResult<io::FileStat> {
|
|
|
|
file::stat(path)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_mkdir(&mut self, path: &CString,
|
|
|
|
mode: io::FilePermission) -> IoResult<()> {
|
|
|
|
file::mkdir(path, mode)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_chmod(&mut self, path: &CString,
|
|
|
|
mode: io::FilePermission) -> IoResult<()> {
|
|
|
|
file::chmod(path, mode)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_rmdir(&mut self, path: &CString) -> IoResult<()> {
|
|
|
|
file::rmdir(path)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_rename(&mut self, path: &CString, to: &CString) -> IoResult<()> {
|
|
|
|
file::rename(path, to)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_readdir(&mut self, path: &CString, _flags: c_int) -> IoResult<~[Path]> {
|
|
|
|
file::readdir(path)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_lstat(&mut self, path: &CString) -> IoResult<io::FileStat> {
|
|
|
|
file::lstat(path)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_chown(&mut self, path: &CString, uid: int, gid: int) -> IoResult<()> {
|
|
|
|
file::chown(path, uid, gid)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_readlink(&mut self, path: &CString) -> IoResult<Path> {
|
|
|
|
file::readlink(path)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_symlink(&mut self, src: &CString, dst: &CString) -> IoResult<()> {
|
|
|
|
file::symlink(src, dst)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_link(&mut self, src: &CString, dst: &CString) -> IoResult<()> {
|
|
|
|
file::link(src, dst)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn fs_utime(&mut self, src: &CString, atime: u64,
|
|
|
|
mtime: u64) -> IoResult<()> {
|
|
|
|
file::utime(src, atime, mtime)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// misc
|
|
|
|
fn timer_init(&mut self) -> IoResult<~RtioTimer> {
|
Implement native timers
Native timers are a much hairier thing to deal with than green timers due to the
interface that we would like to expose (both a blocking sleep() and a
channel-based interface). I ended up implementing timers in three different ways
for the various platforms that we supports.
In all three of the implementations, there is a worker thread which does send()s
on channels for timers. This worker thread is initialized once and then
communicated to in a platform-specific manner, but there's always a shared
channel available for sending messages to the worker thread.
* Windows - I decided to use windows kernel timer objects via
CreateWaitableTimer and SetWaitableTimer in order to provide sleeping
capabilities. The worker thread blocks via WaitForMultipleObjects where one of
the objects is an event that is used to wake up the helper thread (which then
drains the incoming message channel for requests).
* Linux/(Android?) - These have the ideal interface for implementing timers,
timerfd_create. Each timer corresponds to a timerfd, and the helper thread
uses epoll to wait for all active timers and then send() for the next one that
wakes up. The tricky part in this implementation is updating a timerfd, but
see the implementation for the fun details
* OSX/FreeBSD - These obviously don't have the windows APIs, and sadly don't
have the timerfd api available to them, so I have thrown together a solution
which uses select() plus a timeout in order to ad-hoc-ly implement a timer
solution for threads. The implementation is backed by a sorted array of timers
which need to fire. As I said, this is an ad-hoc solution which is certainly
not accurate timing-wise. I have done this implementation due to the lack of
other primitives to provide an implementation, and I've done it the best that
I could, but I'm sure that there's room for improvement.
I'm pretty happy with how these implementations turned out. In theory we could
drop the timerfd implementation and have linux use the select() + timeout
implementation, but it's so inaccurate that I would much rather continue to use
timerfd rather than my ad-hoc select() implementation.
The only change that I would make to the API in general is to have a generic
sleep() method on an IoFactory which doesn't require allocating a Timer object.
For everything but windows it's super-cheap to request a blocking sleep for a
set amount of time, and it's probably worth it to provide a sleep() which
doesn't do something like allocate a file descriptor on linux.
2013-12-29 01:33:56 -06:00
|
|
|
timer::Timer::new().map(|t| ~t as ~RtioTimer)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
fn spawn(&mut self, config: ProcessConfig)
|
|
|
|
-> IoResult<(~RtioProcess, ~[Option<~RtioPipe>])> {
|
|
|
|
process::Process::spawn(config).map(|(p, io)| {
|
|
|
|
(~p as ~RtioProcess,
|
|
|
|
io.move_iter().map(|p| p.map(|p| ~p as ~RtioPipe)).collect())
|
|
|
|
})
|
|
|
|
}
|
2013-11-13 16:48:45 -06:00
|
|
|
fn pipe_open(&mut self, fd: c_int) -> IoResult<~RtioPipe> {
|
|
|
|
Ok(~file::FileDesc::new(fd, true) as ~RtioPipe)
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
|
|
|
|
if unsafe { libc::isatty(fd) } != 0 {
|
2013-12-27 19:50:16 -06:00
|
|
|
Ok(~file::FileDesc::new(fd, true) as ~RtioTTY)
|
2013-11-12 16:38:28 -06:00
|
|
|
} else {
|
2013-11-13 16:48:45 -06:00
|
|
|
Err(IoError {
|
|
|
|
kind: io::MismatchedFileTypeForOperation,
|
|
|
|
desc: "file descriptor is not a TTY",
|
|
|
|
detail: None,
|
|
|
|
})
|
2013-11-12 16:38:28 -06:00
|
|
|
}
|
|
|
|
}
|
Rewrite channels yet again for upgradeability
This, the Nth rewrite of channels, is not a rewrite of the core logic behind
channels, but rather their API usage. In the past, we had the distinction
between oneshot, stream, and shared channels, but the most recent rewrite
dropped oneshots in favor of streams and shared channels.
This distinction of stream vs shared has shown that it's not quite what we'd
like either, and this moves the `std::comm` module in the direction of "one
channel to rule them all". There now remains only one Chan and one Port.
This new channel is actually a hybrid oneshot/stream/shared channel under the
hood in order to optimize for the use cases in question. Additionally, this also
reduces the cognitive burden of having to choose between a Chan or a SharedChan
in an API.
My simple benchmarks show no reduction in efficiency over the existing channels
today, and a 3x improvement in the oneshot case. I sadly don't have a
pre-last-rewrite compiler to test out the old old oneshots, but I would imagine
that the performance is comparable, but slightly slower (due to atomic reference
counting).
This commit also brings the bonus bugfix to channels that the pending queue of
messages are all dropped when a Port disappears rather then when both the Port
and the Chan disappear.
2014-01-08 20:31:48 -06:00
|
|
|
fn signal(&mut self, _signal: Signum, _channel: Chan<Signum>)
|
2013-11-12 16:38:28 -06:00
|
|
|
-> IoResult<~RtioSignal> {
|
|
|
|
Err(unimpl())
|
|
|
|
}
|
|
|
|
}
|