2014-09-30 17:03:56 -07: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.
|
|
|
|
|
|
2014-11-09 13:59:23 +01:00
|
|
|
#![allow(missing_docs)]
|
2014-09-30 17:03:56 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
2014-12-22 09:04:23 -08:00
|
|
|
use prelude::v1::*;
|
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
use ffi::OsStr;
|
2015-02-02 21:39:14 -08:00
|
|
|
use io::{self, ErrorKind};
|
2015-01-27 12:20:58 -08:00
|
|
|
use libc;
|
2014-09-30 17:03:56 -07:00
|
|
|
use mem;
|
2015-01-23 10:46:14 -08:00
|
|
|
use old_io::{self, IoResult, IoError};
|
2015-02-02 21:39:14 -08:00
|
|
|
use num::Int;
|
2015-01-27 12:20:58 -08:00
|
|
|
use os::windows::OsStrExt;
|
2014-09-30 17:03:56 -07:00
|
|
|
use sync::{Once, ONCE_INIT};
|
|
|
|
|
|
2014-12-31 10:20:31 -08:00
|
|
|
macro_rules! helper_init { (static $name:ident: Helper<$m:ty>) => (
|
|
|
|
|
static $name: Helper<$m> = Helper {
|
|
|
|
|
lock: ::sync::MUTEX_INIT,
|
|
|
|
|
cond: ::sync::CONDVAR_INIT,
|
2015-01-02 09:24:56 -08:00
|
|
|
chan: ::cell::UnsafeCell { value: 0 as *mut ::sync::mpsc::Sender<$m> },
|
2014-12-31 10:20:31 -08:00
|
|
|
signal: ::cell::UnsafeCell { value: 0 },
|
|
|
|
|
initialized: ::cell::UnsafeCell { value: false },
|
|
|
|
|
shutdown: ::cell::UnsafeCell { value: false },
|
|
|
|
|
};
|
|
|
|
|
) }
|
|
|
|
|
|
2014-11-23 19:21:17 -08:00
|
|
|
pub mod backtrace;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub mod c;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod condvar;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod ext;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub mod fs;
|
2015-02-02 21:39:14 -08:00
|
|
|
pub mod fs2;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod handle;
|
2014-11-14 14:20:57 -08:00
|
|
|
pub mod helper_signal;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod mutex;
|
2015-02-05 16:50:11 -08:00
|
|
|
pub mod net;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub mod os;
|
2015-01-21 15:55:31 -08:00
|
|
|
pub mod os_str;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub mod pipe;
|
2015-02-06 09:42:57 -08:00
|
|
|
pub mod pipe2;
|
2014-10-09 16:27:28 -07:00
|
|
|
pub mod process;
|
2015-02-06 09:42:57 -08:00
|
|
|
pub mod process2;
|
std: Rewrite the `sync` module
This commit is a reimplementation of `std::sync` to be based on the
system-provided primitives wherever possible. The previous implementation was
fundamentally built on top of channels, and as part of the runtime reform it has
become clear that this is not the level of abstraction that the standard level
should be providing. This rewrite aims to provide as thin of a shim as possible
on top of the system primitives in order to make them safe.
The overall interface of the `std::sync` module has in general not changed, but
there are a few important distinctions, highlighted below:
* The condition variable type, `Condvar`, has been separated out of a `Mutex`.
A condition variable is now an entirely separate type. This separation
benefits users who only use one mutex, and provides a clearer distinction of
who's responsible for managing condition variables (the application).
* All of `Condvar`, `Mutex`, and `RWLock` are now directly built on top of
system primitives rather than using a custom implementation. The `Once`,
`Barrier`, and `Semaphore` types are still built upon these abstractions of
the system primitives.
* The `Condvar`, `Mutex`, and `RWLock` types all have a new static type and
constant initializer corresponding to them. These are provided primarily for C
FFI interoperation, but are often useful to otherwise simply have a global
lock. The types, however, will leak memory unless `destroy()` is called on
them, which is clearly documented.
* The `Condvar` implementation for an `RWLock` write lock has been removed. This
may be added back in the future with a userspace implementation, but this
commit is focused on exposing the system primitives first.
* The fundamental architecture of this design is to provide two separate layers.
The first layer is that exposed by `sys_common` which is a cross-platform
bare-metal abstraction of the system synchronization primitives. No attempt is
made at making this layer safe, and it is quite unsafe to use! It is currently
not exported as part of the API of the standard library, but the stabilization
of the `sys` module will ensure that these will be exposed in time. The
purpose of this layer is to provide the core cross-platform abstractions if
necessary to implementors.
The second layer is the layer provided by `std::sync` which is intended to be
the thinnest possible layer on top of `sys_common` which is entirely safe to
use. There are a few concerns which need to be addressed when making these
system primitives safe:
* Once used, the OS primitives can never be **moved**. This means that they
essentially need to have a stable address. The static primitives use
`&'static self` to enforce this, and the non-static primitives all use a
`Box` to provide this guarantee.
* Poisoning is leveraged to ensure that invalid data is not accessible from
other tasks after one has panicked.
In addition to these overall blanket safety limitations, each primitive has a
few restrictions of its own:
* Mutexes and rwlocks can only be unlocked from the same thread that they
were locked by. This is achieved through RAII lock guards which cannot be
sent across threads.
* Mutexes and rwlocks can only be unlocked if they were previously locked.
This is achieved by not exposing an unlocking method.
* A condition variable can only be waited on with a locked mutex. This is
achieved by requiring a `MutexGuard` in the `wait()` method.
* A condition variable cannot be used concurrently with more than one mutex.
This is guaranteed by dynamically binding a condition variable to
precisely one mutex for its entire lifecycle. This restriction may be able
to be relaxed in the future (a mutex is unbound when no threads are
waiting on the condvar), but for now it is sufficient to guarantee safety.
* Condvars now support timeouts for their blocking operations. The
implementation for these operations is provided by the system.
Due to the modification of the `Condvar` API, removal of the `std::sync::mutex`
API, and reimplementation, this is a breaking change. Most code should be fairly
easy to port using the examples in the documentation of these primitives.
[breaking-change]
Closes #17094
Closes #18003
2014-11-24 11:16:40 -08:00
|
|
|
pub mod rwlock;
|
2014-11-23 19:21:17 -08:00
|
|
|
pub mod stack_overflow;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod sync;
|
2014-11-14 14:20:57 -08:00
|
|
|
pub mod tcp;
|
2014-11-23 19:21:17 -08:00
|
|
|
pub mod thread;
|
2014-11-14 14:20:57 -08:00
|
|
|
pub mod thread_local;
|
2015-01-27 12:20:58 -08:00
|
|
|
pub mod time;
|
2014-10-16 18:57:11 -07:00
|
|
|
pub mod timer;
|
2014-10-17 13:33:08 -07:00
|
|
|
pub mod tty;
|
2014-11-14 14:20:57 -08:00
|
|
|
pub mod udp;
|
2014-10-10 10:11:49 -07:00
|
|
|
|
|
|
|
|
pub mod addrinfo {
|
|
|
|
|
pub use sys_common::net::get_host_addresses;
|
2014-12-28 15:44:25 -07:00
|
|
|
pub use sys_common::net::get_address_name;
|
2014-10-10 10:11:49 -07:00
|
|
|
}
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2014-10-10 10:11:49 -07:00
|
|
|
// FIXME: move these to c module
|
2014-09-30 17:03:56 -07:00
|
|
|
pub type sock_t = libc::SOCKET;
|
|
|
|
|
pub type wrlen = libc::c_int;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub type msglen_t = libc::c_int;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub unsafe fn close_sock(sock: sock_t) { let _ = libc::closesocket(sock); }
|
|
|
|
|
|
|
|
|
|
// windows has zero values as errors
|
|
|
|
|
fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
|
|
|
|
|
if ret == 0 {
|
|
|
|
|
Err(last_error())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn last_error() -> IoError {
|
|
|
|
|
let errno = os::errno() as i32;
|
|
|
|
|
let mut err = decode_error(errno);
|
|
|
|
|
err.detail = Some(os::error_string(errno));
|
|
|
|
|
err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn last_net_error() -> IoError {
|
|
|
|
|
let errno = unsafe { c::WSAGetLastError() as i32 };
|
|
|
|
|
let mut err = decode_error(errno);
|
|
|
|
|
err.detail = Some(os::error_string(errno));
|
|
|
|
|
err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn last_gai_error(_errno: i32) -> IoError {
|
|
|
|
|
last_net_error()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert an `errno` value into a high-level error variant and description.
|
|
|
|
|
pub fn decode_error(errno: i32) -> IoError {
|
|
|
|
|
let (kind, desc) = match errno {
|
2015-01-23 10:46:14 -08:00
|
|
|
libc::EOF => (old_io::EndOfFile, "end of file"),
|
|
|
|
|
libc::ERROR_NO_DATA => (old_io::BrokenPipe, "the pipe is being closed"),
|
|
|
|
|
libc::ERROR_FILE_NOT_FOUND => (old_io::FileNotFound, "file not found"),
|
|
|
|
|
libc::ERROR_INVALID_NAME => (old_io::InvalidInput, "invalid file name"),
|
|
|
|
|
libc::WSAECONNREFUSED => (old_io::ConnectionRefused, "connection refused"),
|
|
|
|
|
libc::WSAECONNRESET => (old_io::ConnectionReset, "connection reset"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::ERROR_ACCESS_DENIED | libc::WSAEACCES =>
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::PermissionDenied, "permission denied"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::WSAEWOULDBLOCK => {
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::ResourceUnavailable, "resource temporarily unavailable")
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
2015-01-23 10:46:14 -08:00
|
|
|
libc::WSAENOTCONN => (old_io::NotConnected, "not connected"),
|
|
|
|
|
libc::WSAECONNABORTED => (old_io::ConnectionAborted, "connection aborted"),
|
|
|
|
|
libc::WSAEADDRNOTAVAIL => (old_io::ConnectionRefused, "address not available"),
|
|
|
|
|
libc::WSAEADDRINUSE => (old_io::ConnectionRefused, "address in use"),
|
|
|
|
|
libc::ERROR_BROKEN_PIPE => (old_io::EndOfFile, "the pipe has ended"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::ERROR_OPERATION_ABORTED =>
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::TimedOut, "operation timed out"),
|
|
|
|
|
libc::WSAEINVAL => (old_io::InvalidInput, "invalid argument"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::ERROR_CALL_NOT_IMPLEMENTED =>
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::IoUnavailable, "function not implemented"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::ERROR_INVALID_HANDLE =>
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::MismatchedFileTypeForOperation,
|
2014-09-30 17:03:56 -07:00
|
|
|
"invalid handle provided to function"),
|
|
|
|
|
libc::ERROR_NOTHING_TO_TERMINATE =>
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::InvalidInput, "no process to kill"),
|
2014-12-25 21:34:42 +01:00
|
|
|
libc::ERROR_ALREADY_EXISTS =>
|
2015-01-23 10:46:14 -08:00
|
|
|
(old_io::PathAlreadyExists, "path already exists"),
|
2014-09-30 17:03:56 -07:00
|
|
|
|
|
|
|
|
// libuv maps this error code to EISDIR. we do too. if it is found
|
|
|
|
|
// to be incorrect, we can add in some more machinery to only
|
|
|
|
|
// return this message when ERROR_INVALID_FUNCTION after certain
|
|
|
|
|
// Windows calls.
|
2015-01-23 10:46:14 -08:00
|
|
|
libc::ERROR_INVALID_FUNCTION => (old_io::InvalidInput,
|
2014-09-30 17:03:56 -07:00
|
|
|
"illegal operation on a directory"),
|
|
|
|
|
|
2015-01-23 10:46:14 -08:00
|
|
|
_ => (old_io::OtherIoError, "unknown error")
|
2014-09-30 17:03:56 -07:00
|
|
|
};
|
|
|
|
|
IoError { kind: kind, desc: desc, detail: None }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn decode_error_detailed(errno: i32) -> IoError {
|
|
|
|
|
let mut err = decode_error(errno);
|
|
|
|
|
err.detail = Some(os::error_string(errno));
|
|
|
|
|
err
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-31 20:24:36 -08:00
|
|
|
pub fn decode_error_kind(errno: i32) -> ErrorKind {
|
|
|
|
|
match errno as libc::c_int {
|
|
|
|
|
libc::ERROR_ACCESS_DENIED => ErrorKind::PermissionDenied,
|
|
|
|
|
libc::ERROR_ALREADY_EXISTS => ErrorKind::PathAlreadyExists,
|
|
|
|
|
libc::ERROR_BROKEN_PIPE => ErrorKind::BrokenPipe,
|
|
|
|
|
libc::ERROR_FILE_NOT_FOUND => ErrorKind::FileNotFound,
|
|
|
|
|
libc::ERROR_INVALID_FUNCTION => ErrorKind::InvalidInput,
|
|
|
|
|
libc::ERROR_INVALID_HANDLE => ErrorKind::MismatchedFileTypeForOperation,
|
|
|
|
|
libc::ERROR_INVALID_NAME => ErrorKind::InvalidInput,
|
|
|
|
|
libc::ERROR_NOTHING_TO_TERMINATE => ErrorKind::InvalidInput,
|
|
|
|
|
libc::ERROR_NO_DATA => ErrorKind::BrokenPipe,
|
|
|
|
|
libc::ERROR_OPERATION_ABORTED => ErrorKind::TimedOut,
|
|
|
|
|
|
|
|
|
|
libc::WSAEACCES => ErrorKind::PermissionDenied,
|
|
|
|
|
libc::WSAEADDRINUSE => ErrorKind::ConnectionRefused,
|
|
|
|
|
libc::WSAEADDRNOTAVAIL => ErrorKind::ConnectionRefused,
|
|
|
|
|
libc::WSAECONNABORTED => ErrorKind::ConnectionAborted,
|
|
|
|
|
libc::WSAECONNREFUSED => ErrorKind::ConnectionRefused,
|
|
|
|
|
libc::WSAECONNRESET => ErrorKind::ConnectionReset,
|
|
|
|
|
libc::WSAEINVAL => ErrorKind::InvalidInput,
|
|
|
|
|
libc::WSAENOTCONN => ErrorKind::NotConnected,
|
|
|
|
|
libc::WSAEWOULDBLOCK => ErrorKind::ResourceUnavailable,
|
|
|
|
|
|
|
|
|
|
_ => ErrorKind::Other,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-30 17:03:56 -07:00
|
|
|
#[inline]
|
2014-12-07 14:15:25 -05:00
|
|
|
pub fn retry<I, F>(f: F) -> I where F: FnOnce() -> I { f() } // PR rust-lang/rust/#17020
|
2014-09-30 17:03:56 -07:00
|
|
|
|
|
|
|
|
pub fn ms_to_timeval(ms: u64) -> libc::timeval {
|
|
|
|
|
libc::timeval {
|
|
|
|
|
tv_sec: (ms / 1000) as libc::c_long,
|
|
|
|
|
tv_usec: ((ms % 1000) * 1000) as libc::c_long,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn wouldblock() -> bool {
|
|
|
|
|
let err = os::errno();
|
2015-01-27 12:20:58 -08:00
|
|
|
err == libc::WSAEWOULDBLOCK as i32
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
|
|
|
|
|
let mut set = nb as libc::c_ulong;
|
|
|
|
|
if unsafe { c::ioctlsocket(fd, c::FIONBIO, &mut set) != 0 } {
|
|
|
|
|
Err(last_error())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init_net() {
|
|
|
|
|
unsafe {
|
|
|
|
|
static START: Once = ONCE_INIT;
|
|
|
|
|
|
2014-12-29 15:03:01 -08:00
|
|
|
START.call_once(|| {
|
2014-09-30 17:03:56 -07:00
|
|
|
let mut data: c::WSADATA = mem::zeroed();
|
|
|
|
|
let ret = c::WSAStartup(0x202, // version 2.2
|
|
|
|
|
&mut data);
|
|
|
|
|
assert_eq!(ret, 0);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-31 10:20:31 -08:00
|
|
|
pub fn unimpl() -> IoError {
|
|
|
|
|
IoError {
|
2015-01-23 10:46:14 -08:00
|
|
|
kind: old_io::IoUnavailable,
|
2014-12-31 10:20:31 -08:00
|
|
|
desc: "operation is not implemented",
|
|
|
|
|
detail: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
fn to_utf16(s: Option<&str>) -> IoResult<Vec<u16>> {
|
2014-09-30 17:03:56 -07:00
|
|
|
match s {
|
2015-01-27 12:20:58 -08:00
|
|
|
Some(s) => Ok(to_utf16_os(OsStr::from_str(s))),
|
2014-09-30 17:03:56 -07:00
|
|
|
None => Err(IoError {
|
2015-01-23 10:46:14 -08:00
|
|
|
kind: old_io::InvalidInput,
|
2014-09-30 17:03:56 -07:00
|
|
|
desc: "valid unicode input required",
|
2015-01-27 12:20:58 -08:00
|
|
|
detail: None,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_utf16_os(s: &OsStr) -> Vec<u16> {
|
|
|
|
|
let mut v: Vec<_> = s.encode_wide().collect();
|
|
|
|
|
v.push(0);
|
|
|
|
|
v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Many Windows APIs follow a pattern of where we hand the a buffer and then
|
|
|
|
|
// they will report back to us how large the buffer should be or how many bytes
|
|
|
|
|
// currently reside in the buffer. This function is an abstraction over these
|
|
|
|
|
// functions by making them easier to call.
|
|
|
|
|
//
|
|
|
|
|
// The first callback, `f1`, is yielded a (pointer, len) pair which can be
|
|
|
|
|
// passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
|
|
|
|
|
// The closure is expected to return what the syscall returns which will be
|
|
|
|
|
// interpreted by this function to determine if the syscall needs to be invoked
|
|
|
|
|
// again (with more buffer space).
|
|
|
|
|
//
|
|
|
|
|
// Once the syscall has completed (errors bail out early) the second closure is
|
|
|
|
|
// yielded the data which has been read from the syscall. The return value
|
|
|
|
|
// from this closure is then the return value of the function.
|
2015-02-02 21:39:14 -08:00
|
|
|
fn fill_utf16_buf_base<F1, F2, T>(mut f1: F1, f2: F2) -> Result<T, ()>
|
2015-01-27 12:20:58 -08:00
|
|
|
where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
|
|
|
|
|
F2: FnOnce(&[u16]) -> T
|
|
|
|
|
{
|
|
|
|
|
// Start off with a stack buf but then spill over to the heap if we end up
|
|
|
|
|
// needing more space.
|
|
|
|
|
let mut stack_buf = [0u16; 512];
|
|
|
|
|
let mut heap_buf = Vec::new();
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut n = stack_buf.len();
|
|
|
|
|
loop {
|
|
|
|
|
let buf = if n <= stack_buf.len() {
|
|
|
|
|
&mut stack_buf[]
|
|
|
|
|
} else {
|
|
|
|
|
let extra = n - heap_buf.len();
|
|
|
|
|
heap_buf.reserve(extra);
|
|
|
|
|
heap_buf.set_len(n);
|
|
|
|
|
&mut heap_buf[]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// This function is typically called on windows API functions which
|
|
|
|
|
// will return the correct length of the string, but these functions
|
|
|
|
|
// also return the `0` on error. In some cases, however, the
|
|
|
|
|
// returned "correct length" may actually be 0!
|
|
|
|
|
//
|
|
|
|
|
// To handle this case we call `SetLastError` to reset it to 0 and
|
|
|
|
|
// then check it again if we get the "0 error value". If the "last
|
|
|
|
|
// error" is still 0 then we interpret it as a 0 length buffer and
|
|
|
|
|
// not an actual error.
|
|
|
|
|
c::SetLastError(0);
|
|
|
|
|
let k = match f1(buf.as_mut_ptr(), n as libc::DWORD) {
|
|
|
|
|
0 if libc::GetLastError() == 0 => 0,
|
2015-02-02 21:39:14 -08:00
|
|
|
0 => return Err(()),
|
2015-01-27 12:20:58 -08:00
|
|
|
n => n,
|
|
|
|
|
} as usize;
|
|
|
|
|
if k == n && libc::GetLastError() ==
|
|
|
|
|
libc::ERROR_INSUFFICIENT_BUFFER as libc::DWORD {
|
|
|
|
|
n *= 2;
|
|
|
|
|
} else if k >= n {
|
|
|
|
|
n = k;
|
|
|
|
|
} else {
|
|
|
|
|
return Ok(f2(&buf[..k]))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
fn fill_utf16_buf<F1, F2, T>(f1: F1, f2: F2) -> IoResult<T>
|
|
|
|
|
where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
|
|
|
|
|
F2: FnOnce(&[u16]) -> T
|
|
|
|
|
{
|
|
|
|
|
fill_utf16_buf_base(f1, f2).map_err(|()| IoError::last_error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fill_utf16_buf_new<F1, F2, T>(f1: F1, f2: F2) -> io::Result<T>
|
|
|
|
|
where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
|
|
|
|
|
F2: FnOnce(&[u16]) -> T
|
|
|
|
|
{
|
|
|
|
|
fill_utf16_buf_base(f1, f2).map_err(|()| io::Error::last_os_error())
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 12:20:58 -08:00
|
|
|
fn os2path(s: &[u16]) -> Path {
|
|
|
|
|
// FIXME: this should not be a panicking conversion (aka path reform)
|
|
|
|
|
Path::new(String::from_utf16(s).unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
|
|
|
|
|
match v.iter().position(|c| *c == 0) {
|
|
|
|
|
// don't include the 0
|
|
|
|
|
Some(i) => &v[..i],
|
|
|
|
|
None => v
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-02 21:39:14 -08:00
|
|
|
|
|
|
|
|
fn cvt<I: Int>(i: I) -> io::Result<I> {
|
|
|
|
|
if i == Int::zero() {
|
|
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ms_to_filetime(ms: u64) -> libc::FILETIME {
|
|
|
|
|
// A FILETIME is a count of 100 nanosecond intervals, so we multiply by
|
|
|
|
|
// 10000 b/c there are 10000 intervals in 1 ms
|
|
|
|
|
let ms = ms * 10000;
|
|
|
|
|
libc::FILETIME {
|
|
|
|
|
dwLowDateTime: ms as u32,
|
|
|
|
|
dwHighDateTime: (ms >> 32) as u32,
|
|
|
|
|
}
|
|
|
|
|
}
|