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-10-17 13:39:27 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2014-12-31 10:20:31 -08:00
|
|
|
#![allow(unused_imports)]
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_unsafe)]
|
|
|
|
#![allow(unused_mut)]
|
2014-10-10 10:11:49 -07:00
|
|
|
|
2014-12-22 09:04:23 -08:00
|
|
|
use prelude::v1::*;
|
2014-11-25 13:28:35 -08:00
|
|
|
|
2015-02-17 22:47:40 -08:00
|
|
|
use ffi::CStr;
|
2015-02-05 16:50:11 -08:00
|
|
|
use io::{self, ErrorKind};
|
2014-11-25 13:28:35 -08:00
|
|
|
use libc;
|
|
|
|
use num::{Int, SignedInt};
|
|
|
|
use num;
|
2015-01-31 20:24:36 -08:00
|
|
|
use old_io::{self, IoResult, IoError};
|
2014-11-25 13:28:35 -08:00
|
|
|
use str;
|
2014-10-10 10:11:49 -07:00
|
|
|
use sys_common::mkerr_libc;
|
2014-09-30 17:03:56 -07:00
|
|
|
|
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,
|
|
|
|
chan: ::cell::UnsafeCell { value: 0 as *mut Sender<$m> },
|
|
|
|
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-02-02 21:39:14 -08:00
|
|
|
pub mod ext;
|
|
|
|
pub mod fd;
|
|
|
|
pub mod fs; // support for std::old_io
|
|
|
|
pub mod fs2; // support for std::fs
|
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;
|
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 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-13 21:24:26 -08:00
|
|
|
pub mod time;
|
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 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
|
|
|
|
pub type sock_t = self::fs::fd_t;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub type wrlen = libc::size_t;
|
2014-10-10 10:11:49 -07:00
|
|
|
pub type msglen_t = libc::size_t;
|
2014-09-30 17:03:56 -07:00
|
|
|
pub unsafe fn close_sock(sock: sock_t) { let _ = libc::close(sock); }
|
|
|
|
|
|
|
|
pub fn last_error() -> IoError {
|
2014-10-10 10:11:49 -07:00
|
|
|
decode_error_detailed(os::errno() as i32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_net_error() -> IoError {
|
|
|
|
last_error()
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "system" {
|
|
|
|
fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_gai_error(s: libc::c_int) -> IoError {
|
|
|
|
|
|
|
|
let mut err = decode_error(s);
|
|
|
|
err.detail = Some(unsafe {
|
2015-02-17 22:47:40 -08:00
|
|
|
let data = CStr::from_ptr(gai_strerror(s));
|
|
|
|
str::from_utf8(data.to_bytes()).unwrap().to_string()
|
2014-10-10 10:11:49 -07:00
|
|
|
});
|
2014-09-30 17:03:56 -07:00
|
|
|
err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert an `errno` value into a high-level error variant and description.
|
|
|
|
pub fn decode_error(errno: i32) -> IoError {
|
|
|
|
// FIXME: this should probably be a bit more descriptive...
|
|
|
|
let (kind, desc) = match errno {
|
2015-01-22 16:31:00 -08:00
|
|
|
libc::EOF => (old_io::EndOfFile, "end of file"),
|
|
|
|
libc::ECONNREFUSED => (old_io::ConnectionRefused, "connection refused"),
|
|
|
|
libc::ECONNRESET => (old_io::ConnectionReset, "connection reset"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::EPERM | libc::EACCES =>
|
2015-01-22 16:31:00 -08:00
|
|
|
(old_io::PermissionDenied, "permission denied"),
|
|
|
|
libc::EPIPE => (old_io::BrokenPipe, "broken pipe"),
|
|
|
|
libc::ENOTCONN => (old_io::NotConnected, "not connected"),
|
|
|
|
libc::ECONNABORTED => (old_io::ConnectionAborted, "connection aborted"),
|
|
|
|
libc::EADDRNOTAVAIL => (old_io::ConnectionRefused, "address not available"),
|
|
|
|
libc::EADDRINUSE => (old_io::ConnectionRefused, "address in use"),
|
|
|
|
libc::ENOENT => (old_io::FileNotFound, "no such file or directory"),
|
|
|
|
libc::EISDIR => (old_io::InvalidInput, "illegal operation on a directory"),
|
|
|
|
libc::ENOSYS => (old_io::IoUnavailable, "function not implemented"),
|
|
|
|
libc::EINVAL => (old_io::InvalidInput, "invalid argument"),
|
2014-09-30 17:03:56 -07:00
|
|
|
libc::ENOTTY =>
|
2015-01-22 16:31:00 -08:00
|
|
|
(old_io::MismatchedFileTypeForOperation,
|
2014-09-30 17:03:56 -07:00
|
|
|
"file descriptor is not a TTY"),
|
2015-01-22 16:31:00 -08:00
|
|
|
libc::ETIMEDOUT => (old_io::TimedOut, "operation timed out"),
|
|
|
|
libc::ECANCELED => (old_io::TimedOut, "operation aborted"),
|
2014-12-25 21:34:42 +01:00
|
|
|
libc::consts::os::posix88::EEXIST =>
|
2015-01-22 16:31:00 -08:00
|
|
|
(old_io::PathAlreadyExists, "path already exists"),
|
2014-09-30 17:03:56 -07: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 =>
|
2015-01-22 16:31:00 -08:00
|
|
|
(old_io::ResourceUnavailable, "resource temporarily unavailable"),
|
2014-09-30 17:03:56 -07:00
|
|
|
|
2015-01-22 16:31:00 -08:00
|
|
|
_ => (old_io::OtherIoError, "unknown error")
|
2014-09-30 17:03:56 -07:00
|
|
|
};
|
|
|
|
IoError { kind: kind, desc: desc, detail: None }
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:11:49 -07:00
|
|
|
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::ECONNREFUSED => ErrorKind::ConnectionRefused,
|
|
|
|
libc::ECONNRESET => ErrorKind::ConnectionReset,
|
|
|
|
libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
|
|
|
|
libc::EPIPE => ErrorKind::BrokenPipe,
|
|
|
|
libc::ENOTCONN => ErrorKind::NotConnected,
|
|
|
|
libc::ECONNABORTED => ErrorKind::ConnectionAborted,
|
|
|
|
libc::EADDRNOTAVAIL => ErrorKind::ConnectionRefused,
|
|
|
|
libc::EADDRINUSE => ErrorKind::ConnectionRefused,
|
|
|
|
libc::ENOENT => ErrorKind::FileNotFound,
|
|
|
|
libc::EISDIR => ErrorKind::InvalidInput,
|
|
|
|
libc::EINTR => ErrorKind::Interrupted,
|
|
|
|
libc::EINVAL => ErrorKind::InvalidInput,
|
|
|
|
libc::ENOTTY => ErrorKind::MismatchedFileTypeForOperation,
|
|
|
|
libc::ETIMEDOUT => ErrorKind::TimedOut,
|
|
|
|
libc::ECANCELED => ErrorKind::TimedOut,
|
|
|
|
libc::consts::os::posix88::EEXIST => ErrorKind::PathAlreadyExists,
|
|
|
|
|
|
|
|
// 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 =>
|
|
|
|
ErrorKind::ResourceUnavailable,
|
|
|
|
|
|
|
|
_ => ErrorKind::Other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 17:03:56 -07:00
|
|
|
#[inline]
|
2014-12-07 14:15:25 -05:00
|
|
|
pub fn retry<T, F> (mut f: F) -> T where
|
|
|
|
T: SignedInt,
|
|
|
|
F: FnMut() -> T,
|
|
|
|
{
|
2014-11-10 09:35:53 +11:00
|
|
|
let one: T = Int::one();
|
2014-09-30 17:03:56 -07:00
|
|
|
loop {
|
|
|
|
let n = f();
|
2015-01-27 12:20:58 -08:00
|
|
|
if n == -one && os::errno() == libc::EINTR as i32 { }
|
2014-09-30 17:03:56 -07:00
|
|
|
else { return n }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
pub fn cvt<T: SignedInt>(t: T) -> io::Result<T> {
|
|
|
|
let one: T = Int::one();
|
|
|
|
if t == -one {
|
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
} else {
|
|
|
|
Ok(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
|
|
|
|
where T: SignedInt, F: FnMut() -> T
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
match cvt(f()) {
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
|
|
|
|
other => return other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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::time_t,
|
|
|
|
tv_usec: ((ms % 1000) * 1000) as libc::suseconds_t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wouldblock() -> bool {
|
|
|
|
let err = os::errno();
|
2015-01-27 12:20:58 -08:00
|
|
|
err == libc::EWOULDBLOCK as i32 || err == libc::EAGAIN as i32
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
|
|
|
|
2015-02-23 23:50:32 +01:00
|
|
|
pub fn set_nonblocking(fd: sock_t, nb: bool) {
|
2014-09-30 17:03:56 -07:00
|
|
|
let set = nb as libc::c_int;
|
2015-02-23 23:50:32 +01:00
|
|
|
mkerr_libc(retry(|| unsafe { c::ioctl(fd, c::FIONBIO, &set) })).unwrap();
|
2014-09-30 17:03:56 -07:00
|
|
|
}
|
2014-10-10 10:11:49 -07:00
|
|
|
|
|
|
|
// nothing needed on unix platforms
|
|
|
|
pub fn init_net() {}
|