2014-01-25 20:37:51 +13:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-11-13 23:17:18 -08:00
|
|
|
// 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-02-15 12:01:52 +11:00
|
|
|
//! A native mutex and condition variable type.
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
|
|
|
//! This module contains bindings to the platform's native mutex/condition
|
2014-02-15 12:01:52 +11:00
|
|
|
//! variable primitives. It provides two types: `StaticNativeMutex`, which can
|
|
|
|
//! be statically initialized via the `NATIVE_MUTEX_INIT` value, and a simple
|
|
|
|
//! wrapper `NativeMutex` that has a destructor to clean up after itself. These
|
|
|
|
//! objects serve as both mutexes and condition variables simultaneously.
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
2014-02-15 12:01:52 +11:00
|
|
|
//! The static lock is lazily initialized, but it can only be unsafely
|
|
|
|
//! destroyed. A statically initialized lock doesn't necessarily have a time at
|
|
|
|
//! which it can get deallocated. For this reason, there is no `Drop`
|
|
|
|
//! implementation of the static mutex, but rather the `destroy()` method must
|
|
|
|
//! be invoked manually if destruction of the mutex is desired.
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
2014-02-15 12:01:52 +11:00
|
|
|
//! The non-static `NativeMutex` type does have a destructor, but cannot be
|
|
|
|
//! statically initialized.
|
|
|
|
//!
|
|
|
|
//! It is not recommended to use this type for idiomatic rust use. These types
|
|
|
|
//! are appropriate where no other options are available, but other rust
|
2014-02-15 14:24:51 +11:00
|
|
|
//! concurrency primitives should be used before them: the `sync` crate defines
|
|
|
|
//! `StaticMutex` and `Mutex` types.
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//!
|
2014-02-15 14:24:51 +11:00
|
|
|
//! ```rust
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
//! use std::rt::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT};
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
2014-02-15 14:24:51 +11:00
|
|
|
//! // Use a statically initialized mutex
|
|
|
|
//! static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
2014-02-15 14:24:51 +11:00
|
|
|
//! unsafe {
|
|
|
|
//! let _guard = LOCK.lock();
|
|
|
|
//! } // automatically unlocked here
|
2013-11-13 23:17:18 -08:00
|
|
|
//!
|
2014-02-15 14:24:51 +11:00
|
|
|
//! // Use a normally initialized mutex
|
|
|
|
//! unsafe {
|
|
|
|
//! let mut lock = NativeMutex::new();
|
2014-02-15 12:01:52 +11:00
|
|
|
//!
|
2014-02-15 14:24:51 +11:00
|
|
|
//! {
|
|
|
|
//! let _guard = lock.lock();
|
|
|
|
//! } // unlocked here
|
2014-02-15 11:18:49 +11:00
|
|
|
//!
|
2014-02-15 14:24:51 +11:00
|
|
|
//! // sometimes the RAII guard isn't appropriate
|
|
|
|
//! lock.lock_noguard();
|
|
|
|
//! lock.unlock_noguard();
|
|
|
|
//! } // `lock` is deallocated here
|
|
|
|
//! ```
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-11-13 23:17:18 -08:00
|
|
|
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
use core::prelude::*;
|
2014-02-13 17:17:50 +11:00
|
|
|
|
2014-02-15 11:18:49 +11:00
|
|
|
/// A native mutex suitable for storing in statics (that is, it has
|
|
|
|
/// the `destroy` method rather than a destructor).
|
2014-02-15 12:01:52 +11:00
|
|
|
///
|
2014-02-15 14:24:51 +11:00
|
|
|
/// Prefer the `NativeMutex` type where possible, since that does not
|
|
|
|
/// require manual deallocation.
|
2014-02-15 11:18:49 +11:00
|
|
|
pub struct StaticNativeMutex {
|
2014-03-27 15:09:47 -07:00
|
|
|
inner: imp::Mutex,
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
2014-02-15 12:01:52 +11:00
|
|
|
/// A native mutex with a destructor for clean-up.
|
|
|
|
///
|
|
|
|
/// See `StaticNativeMutex` for a version that is suitable for storing in
|
|
|
|
/// statics.
|
|
|
|
pub struct NativeMutex {
|
2014-03-27 15:09:47 -07:00
|
|
|
inner: StaticNativeMutex
|
2014-02-15 12:01:52 +11:00
|
|
|
}
|
|
|
|
|
2014-02-13 17:17:50 +11:00
|
|
|
/// Automatically unlocks the mutex that it was created from on
|
|
|
|
/// destruction.
|
|
|
|
///
|
|
|
|
/// Using this makes lock-based code resilient to unwinding/task
|
|
|
|
/// failure, because the lock will be automatically unlocked even
|
|
|
|
/// then.
|
|
|
|
#[must_use]
|
|
|
|
pub struct LockGuard<'a> {
|
2014-03-27 15:09:47 -07:00
|
|
|
lock: &'a StaticNativeMutex
|
2014-02-13 17:17:50 +11:00
|
|
|
}
|
|
|
|
|
2014-02-15 11:18:49 +11:00
|
|
|
pub static NATIVE_MUTEX_INIT: StaticNativeMutex = StaticNativeMutex {
|
2014-01-16 19:54:24 -08:00
|
|
|
inner: imp::MUTEX_INIT,
|
2013-11-13 23:17:18 -08:00
|
|
|
};
|
|
|
|
|
2014-02-15 11:18:49 +11:00
|
|
|
impl StaticNativeMutex {
|
|
|
|
/// Creates a new mutex.
|
|
|
|
///
|
|
|
|
/// Note that a mutex created in this way needs to be explicit
|
|
|
|
/// freed with a call to `destroy` or it will leak.
|
2014-05-24 08:01:23 +03:00
|
|
|
/// Also it is important to avoid locking until mutex has stopped moving
|
2014-02-15 11:18:49 +11:00
|
|
|
pub unsafe fn new() -> StaticNativeMutex {
|
|
|
|
StaticNativeMutex { inner: imp::Mutex::new() }
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquires this lock. This assumes that the current thread does not
|
|
|
|
/// already hold the lock.
|
2014-02-13 17:17:50 +11:00
|
|
|
///
|
|
|
|
/// # Example
|
2014-03-22 00:45:41 -07:00
|
|
|
///
|
2014-02-15 11:18:49 +11:00
|
|
|
/// ```rust
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
/// use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
|
2014-02-15 11:18:49 +11:00
|
|
|
/// static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2014-02-13 17:17:50 +11:00
|
|
|
/// unsafe {
|
2014-02-15 11:18:49 +11:00
|
|
|
/// let _guard = LOCK.lock();
|
|
|
|
/// // critical section...
|
|
|
|
/// } // automatically unlocked in `_guard`'s destructor
|
2014-02-13 17:17:50 +11:00
|
|
|
/// ```
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe because it will not function correctly if this
|
|
|
|
/// mutex has been *moved* since it was last used. The mutex can move an
|
|
|
|
/// arbitrary number of times before its first usage, but once a mutex has
|
|
|
|
/// been used once it is no longer allowed to move (or otherwise it invokes
|
|
|
|
/// undefined behavior).
|
|
|
|
///
|
|
|
|
/// Additionally, this type does not take into account any form of
|
|
|
|
/// scheduling model. This will unconditionally block the *os thread* which
|
|
|
|
/// is not always desired.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn lock<'a>(&'a self) -> LockGuard<'a> {
|
2014-02-13 17:17:50 +11:00
|
|
|
self.inner.lock();
|
|
|
|
|
|
|
|
LockGuard { lock: self }
|
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-02-13 17:17:50 +11:00
|
|
|
/// Attempts to acquire the lock. The value returned is `Some` if
|
|
|
|
/// the attempt succeeded.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe for the same reasons as `lock`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn trylock<'a>(&'a self) -> Option<LockGuard<'a>> {
|
2014-02-13 17:17:50 +11:00
|
|
|
if self.inner.trylock() {
|
|
|
|
Some(LockGuard { lock: self })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquire the lock without creating a `LockGuard`.
|
|
|
|
///
|
2014-02-15 14:24:51 +11:00
|
|
|
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
|
|
|
|
/// `.lock`.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe for the same reasons as `lock`. Additionally, this
|
|
|
|
/// does not guarantee that the mutex will ever be unlocked, and it is
|
|
|
|
/// undefined to drop an already-locked mutex.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn lock_noguard(&self) { self.inner.lock() }
|
2014-02-13 17:17:50 +11:00
|
|
|
|
|
|
|
/// Attempts to acquire the lock without creating a
|
|
|
|
/// `LockGuard`. The value returned is whether the lock was
|
|
|
|
/// acquired or not.
|
|
|
|
///
|
2014-02-15 14:24:51 +11:00
|
|
|
/// If `true` is returned, this needs to be paired with a call to
|
|
|
|
/// `.unlock_noguard`. Prefer using `.trylock`.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe for the same reasons as `lock_noguard`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn trylock_noguard(&self) -> bool {
|
2014-02-13 17:17:50 +11:00
|
|
|
self.inner.trylock()
|
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
|
|
|
|
/// Unlocks the lock. This assumes that the current thread already holds the
|
|
|
|
/// lock.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe for the same reasons as `lock`. Additionally, it
|
|
|
|
/// is not guaranteed that this is unlocking a previously locked mutex. It
|
|
|
|
/// is undefined to unlock an unlocked mutex.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn unlock_noguard(&self) { self.inner.unlock() }
|
2013-11-13 23:17:18 -08:00
|
|
|
|
|
|
|
/// Block on the internal condition variable.
|
|
|
|
///
|
2014-02-13 17:17:50 +11:00
|
|
|
/// This function assumes that the lock is already held. Prefer
|
|
|
|
/// using `LockGuard.wait` since that guarantees that the lock is
|
|
|
|
/// held.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe for the same reasons as `lock`. Additionally, this
|
|
|
|
/// is unsafe because the mutex may not be currently locked.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn wait_noguard(&self) { self.inner.wait() }
|
2013-11-13 23:17:18 -08:00
|
|
|
|
|
|
|
/// Signals a thread in `wait` to wake up
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe for the same reasons as `lock`. Additionally, this
|
|
|
|
/// is unsafe because the mutex may not be currently locked.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn signal_noguard(&self) { self.inner.signal() }
|
2013-11-13 23:17:18 -08:00
|
|
|
|
|
|
|
/// This function is especially unsafe because there are no guarantees made
|
|
|
|
/// that no other thread is currently holding the lock or waiting on the
|
|
|
|
/// condition variable contained inside.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn destroy(&self) { self.inner.destroy() }
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
2014-02-15 12:01:52 +11:00
|
|
|
impl NativeMutex {
|
|
|
|
/// Creates a new mutex.
|
|
|
|
///
|
|
|
|
/// The user must be careful to ensure the mutex is not locked when its is
|
|
|
|
/// being destroyed.
|
2014-05-24 08:01:23 +03:00
|
|
|
/// Also it is important to avoid locking until mutex has stopped moving
|
2014-02-15 12:01:52 +11:00
|
|
|
pub unsafe fn new() -> NativeMutex {
|
|
|
|
NativeMutex { inner: StaticNativeMutex::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquires this lock. This assumes that the current thread does not
|
|
|
|
/// already hold the lock.
|
|
|
|
///
|
|
|
|
/// # Example
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
2014-02-15 12:01:52 +11:00
|
|
|
/// ```rust
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
/// use std::rt::mutex::NativeMutex;
|
2014-02-15 12:01:52 +11:00
|
|
|
/// unsafe {
|
2014-02-15 14:24:51 +11:00
|
|
|
/// let mut lock = NativeMutex::new();
|
|
|
|
///
|
|
|
|
/// {
|
|
|
|
/// let _guard = lock.lock();
|
|
|
|
/// // critical section...
|
|
|
|
/// } // automatically unlocked in `_guard`'s destructor
|
|
|
|
/// }
|
2014-02-15 12:01:52 +11:00
|
|
|
/// ```
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::lock`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn lock<'a>(&'a self) -> LockGuard<'a> {
|
2014-02-15 12:01:52 +11:00
|
|
|
self.inner.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to acquire the lock. The value returned is `Some` if
|
|
|
|
/// the attempt succeeded.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::trylock`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn trylock<'a>(&'a self) -> Option<LockGuard<'a>> {
|
2014-02-15 12:01:52 +11:00
|
|
|
self.inner.trylock()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquire the lock without creating a `LockGuard`.
|
|
|
|
///
|
2014-02-15 14:24:51 +11:00
|
|
|
/// These needs to be paired with a call to `.unlock_noguard`. Prefer using
|
|
|
|
/// `.lock`.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::lock_noguard`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn lock_noguard(&self) { self.inner.lock_noguard() }
|
2014-02-15 12:01:52 +11:00
|
|
|
|
|
|
|
/// Attempts to acquire the lock without creating a
|
|
|
|
/// `LockGuard`. The value returned is whether the lock was
|
|
|
|
/// acquired or not.
|
|
|
|
///
|
2014-02-15 14:24:51 +11:00
|
|
|
/// If `true` is returned, this needs to be paired with a call to
|
|
|
|
/// `.unlock_noguard`. Prefer using `.trylock`.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::trylock_noguard`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn trylock_noguard(&self) -> bool {
|
2014-02-15 12:01:52 +11:00
|
|
|
self.inner.trylock_noguard()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unlocks the lock. This assumes that the current thread already holds the
|
|
|
|
/// lock.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::unlock_noguard`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn unlock_noguard(&self) { self.inner.unlock_noguard() }
|
2014-02-15 12:01:52 +11:00
|
|
|
|
|
|
|
/// Block on the internal condition variable.
|
|
|
|
///
|
|
|
|
/// This function assumes that the lock is already held. Prefer
|
|
|
|
/// using `LockGuard.wait` since that guarantees that the lock is
|
|
|
|
/// held.
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::wait_noguard`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn wait_noguard(&self) { self.inner.wait_noguard() }
|
2014-02-15 12:01:52 +11:00
|
|
|
|
|
|
|
/// Signals a thread in `wait` to wake up
|
2014-06-12 11:40:13 -07:00
|
|
|
///
|
|
|
|
/// # Unsafety
|
|
|
|
///
|
|
|
|
/// This method is unsafe due to the same reasons as
|
|
|
|
/// `StaticNativeMutex::signal_noguard`.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn signal_noguard(&self) { self.inner.signal_noguard() }
|
2014-02-15 12:01:52 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for NativeMutex {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {self.inner.destroy()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 17:17:50 +11:00
|
|
|
impl<'a> LockGuard<'a> {
|
|
|
|
/// Block on the internal condition variable.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn wait(&self) {
|
2014-02-13 17:17:50 +11:00
|
|
|
self.lock.wait_noguard()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Signals a thread in `wait` to wake up.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn signal(&self) {
|
2014-02-13 17:17:50 +11:00
|
|
|
self.lock.signal_noguard()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<'a> Drop for LockGuard<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {self.lock.unlock_noguard()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 23:17:18 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
mod imp {
|
|
|
|
use libc;
|
2014-01-16 19:54:24 -08:00
|
|
|
use self::os::{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,
|
|
|
|
pthread_mutex_t, pthread_cond_t};
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
use core::cell::UnsafeCell;
|
2013-11-13 23:17:18 -08:00
|
|
|
|
|
|
|
type pthread_mutexattr_t = libc::c_void;
|
|
|
|
type pthread_condattr_t = libc::c_void;
|
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
#[cfg(target_os = "freebsd")]
|
2014-07-29 16:44:39 +02:00
|
|
|
#[cfg(target_os = "dragonfly")]
|
2014-01-16 19:54:24 -08:00
|
|
|
mod os {
|
|
|
|
use libc;
|
|
|
|
|
2014-06-25 12:47:34 -07:00
|
|
|
pub type pthread_mutex_t = *mut libc::c_void;
|
|
|
|
pub type pthread_cond_t = *mut libc::c_void;
|
2014-01-16 19:54:24 -08:00
|
|
|
|
|
|
|
pub static PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t =
|
|
|
|
0 as pthread_mutex_t;
|
|
|
|
pub static PTHREAD_COND_INITIALIZER: pthread_cond_t =
|
|
|
|
0 as pthread_cond_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2014-05-05 10:07:49 +03:00
|
|
|
#[cfg(target_os = "ios")]
|
2014-01-16 19:54:24 -08:00
|
|
|
mod os {
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
static __PTHREAD_MUTEX_SIZE__: uint = 56;
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
static __PTHREAD_COND_SIZE__: uint = 40;
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
static __PTHREAD_MUTEX_SIZE__: uint = 40;
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
static __PTHREAD_COND_SIZE__: uint = 24;
|
2014-05-05 10:07:49 +03:00
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
static __PTHREAD_MUTEX_SIZE__: uint = 40;
|
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
static __PTHREAD_COND_SIZE__: uint = 24;
|
2014-05-24 08:01:23 +03:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
static _PTHREAD_MUTEX_SIG_init: libc::c_long = 0x32AAABA7;
|
|
|
|
static _PTHREAD_COND_SIG_init: libc::c_long = 0x3CB0B1BB;
|
|
|
|
|
2014-06-09 22:14:51 +02:00
|
|
|
#[repr(C)]
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct pthread_mutex_t {
|
|
|
|
__sig: libc::c_long,
|
|
|
|
__opaque: [u8, ..__PTHREAD_MUTEX_SIZE__],
|
|
|
|
}
|
2014-06-09 22:14:51 +02:00
|
|
|
#[repr(C)]
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct pthread_cond_t {
|
|
|
|
__sig: libc::c_long,
|
|
|
|
__opaque: [u8, ..__PTHREAD_COND_SIZE__],
|
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
pub static PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
|
|
|
|
__sig: _PTHREAD_MUTEX_SIG_init,
|
|
|
|
__opaque: [0, ..__PTHREAD_MUTEX_SIZE__],
|
|
|
|
};
|
|
|
|
pub static PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
|
|
|
|
__sig: _PTHREAD_COND_SIG_init,
|
|
|
|
__opaque: [0, ..__PTHREAD_COND_SIZE__],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
mod os {
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
// minus 8 because we have an 'align' field
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
static __SIZEOF_PTHREAD_MUTEX_T: uint = 40 - 8;
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
static __SIZEOF_PTHREAD_MUTEX_T: uint = 24 - 8;
|
2014-02-05 17:11:28 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
static __SIZEOF_PTHREAD_MUTEX_T: uint = 24 - 8;
|
2014-03-13 14:35:24 +08:00
|
|
|
#[cfg(target_arch = "mips")]
|
|
|
|
static __SIZEOF_PTHREAD_MUTEX_T: uint = 24 - 8;
|
2014-06-17 09:16:03 +02:00
|
|
|
#[cfg(target_arch = "mipsel")]
|
|
|
|
static __SIZEOF_PTHREAD_MUTEX_T: uint = 24 - 8;
|
2014-01-16 19:54:24 -08:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
static __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
static __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
|
2014-02-05 17:11:28 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
static __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
|
2014-03-13 14:35:24 +08:00
|
|
|
#[cfg(target_arch = "mips")]
|
|
|
|
static __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
|
2014-06-17 09:16:03 +02:00
|
|
|
#[cfg(target_arch = "mipsel")]
|
|
|
|
static __SIZEOF_PTHREAD_COND_T: uint = 48 - 8;
|
2014-01-16 19:54:24 -08:00
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
#[repr(C)]
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct pthread_mutex_t {
|
2014-02-05 17:11:28 -05:00
|
|
|
__align: libc::c_longlong,
|
2014-01-16 19:54:24 -08:00
|
|
|
size: [u8, ..__SIZEOF_PTHREAD_MUTEX_T],
|
|
|
|
}
|
2014-06-06 15:51:42 +02:00
|
|
|
#[repr(C)]
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct pthread_cond_t {
|
|
|
|
__align: libc::c_longlong,
|
|
|
|
size: [u8, ..__SIZEOF_PTHREAD_COND_T],
|
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
pub static PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
|
|
|
|
__align: 0,
|
|
|
|
size: [0, ..__SIZEOF_PTHREAD_MUTEX_T],
|
|
|
|
};
|
|
|
|
pub static PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
|
|
|
|
__align: 0,
|
|
|
|
size: [0, ..__SIZEOF_PTHREAD_COND_T],
|
|
|
|
};
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
2014-01-16 19:54:24 -08:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
mod os {
|
|
|
|
use libc;
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-06-09 22:14:51 +02:00
|
|
|
#[repr(C)]
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct pthread_mutex_t { value: libc::c_int }
|
2014-06-09 22:14:51 +02:00
|
|
|
#[repr(C)]
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct pthread_cond_t { value: libc::c_int }
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
pub static PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = pthread_mutex_t {
|
|
|
|
value: 0,
|
|
|
|
};
|
|
|
|
pub static PTHREAD_COND_INITIALIZER: pthread_cond_t = pthread_cond_t {
|
|
|
|
value: 0,
|
|
|
|
};
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
pub struct Mutex {
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
lock: UnsafeCell<pthread_mutex_t>,
|
|
|
|
cond: UnsafeCell<pthread_cond_t>,
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
pub static MUTEX_INIT: Mutex = Mutex {
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
lock: UnsafeCell { value: PTHREAD_MUTEX_INITIALIZER },
|
|
|
|
cond: UnsafeCell { value: PTHREAD_COND_INITIALIZER },
|
2014-01-16 19:54:24 -08:00
|
|
|
};
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
impl Mutex {
|
|
|
|
pub unsafe fn new() -> Mutex {
|
2014-05-24 08:01:23 +03:00
|
|
|
// As mutex might be moved and address is changing it
|
|
|
|
// is better to avoid initialization of potentially
|
|
|
|
// opaque OS data before it landed
|
2014-03-22 00:45:41 -07:00
|
|
|
let m = Mutex {
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
lock: UnsafeCell::new(PTHREAD_MUTEX_INITIALIZER),
|
|
|
|
cond: UnsafeCell::new(PTHREAD_COND_INITIALIZER),
|
2014-01-16 19:54:24 -08:00
|
|
|
};
|
2013-11-13 23:17:18 -08:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn lock(&self) { pthread_mutex_lock(self.lock.get()); }
|
|
|
|
pub unsafe fn unlock(&self) { pthread_mutex_unlock(self.lock.get()); }
|
|
|
|
pub unsafe fn signal(&self) { pthread_cond_signal(self.cond.get()); }
|
|
|
|
pub unsafe fn wait(&self) {
|
|
|
|
pthread_cond_wait(self.cond.get(), self.lock.get());
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn trylock(&self) -> bool {
|
|
|
|
pthread_mutex_trylock(self.lock.get()) == 0
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn destroy(&self) {
|
|
|
|
pthread_mutex_destroy(self.lock.get());
|
|
|
|
pthread_cond_destroy(self.cond.get());
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
2014-01-21 09:31:32 -05:00
|
|
|
fn pthread_mutex_destroy(lock: *mut pthread_mutex_t) -> libc::c_int;
|
|
|
|
fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> libc::c_int;
|
|
|
|
fn pthread_mutex_lock(lock: *mut pthread_mutex_t) -> libc::c_int;
|
|
|
|
fn pthread_mutex_trylock(lock: *mut pthread_mutex_t) -> libc::c_int;
|
|
|
|
fn pthread_mutex_unlock(lock: *mut pthread_mutex_t) -> libc::c_int;
|
|
|
|
|
|
|
|
fn pthread_cond_wait(cond: *mut pthread_cond_t,
|
|
|
|
lock: *mut pthread_mutex_t) -> libc::c_int;
|
|
|
|
fn pthread_cond_signal(cond: *mut pthread_cond_t) -> libc::c_int;
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
mod imp {
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
use alloc::libc_heap::malloc_raw;
|
2014-08-04 15:42:36 -07:00
|
|
|
use core::atomic;
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
use core::ptr;
|
2013-11-13 23:17:18 -08:00
|
|
|
use libc::{HANDLE, BOOL, LPSECURITY_ATTRIBUTES, c_void, DWORD, LPCSTR};
|
2014-01-16 19:54:24 -08:00
|
|
|
use libc;
|
2014-01-06 19:05:53 -08:00
|
|
|
|
2014-01-16 19:54:24 -08:00
|
|
|
type LPCRITICAL_SECTION = *mut c_void;
|
2013-11-13 23:17:18 -08:00
|
|
|
static SPIN_COUNT: DWORD = 4000;
|
2014-01-16 19:54:24 -08:00
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
static CRIT_SECTION_SIZE: uint = 24;
|
2014-03-19 00:42:02 -07:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
static CRIT_SECTION_SIZE: uint = 40;
|
2014-01-16 19:54:24 -08:00
|
|
|
|
|
|
|
pub struct Mutex {
|
|
|
|
// pointers for the lock/cond handles, atomically updated
|
2014-08-04 15:42:36 -07:00
|
|
|
lock: atomic::AtomicUint,
|
|
|
|
cond: atomic::AtomicUint,
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub static MUTEX_INIT: Mutex = Mutex {
|
2014-08-04 15:42:36 -07:00
|
|
|
lock: atomic::INIT_ATOMIC_UINT,
|
|
|
|
cond: atomic::INIT_ATOMIC_UINT,
|
2014-01-16 19:54:24 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
impl Mutex {
|
|
|
|
pub unsafe fn new() -> Mutex {
|
|
|
|
Mutex {
|
2014-08-04 15:42:36 -07:00
|
|
|
lock: atomic::AtomicUint::new(init_lock()),
|
|
|
|
cond: atomic::AtomicUint::new(init_cond()),
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
|
|
|
}
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn lock(&self) {
|
2014-01-16 19:54:24 -08:00
|
|
|
EnterCriticalSection(self.getlock() as LPCRITICAL_SECTION)
|
|
|
|
}
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn trylock(&self) -> bool {
|
2014-01-16 19:54:24 -08:00
|
|
|
TryEnterCriticalSection(self.getlock() as LPCRITICAL_SECTION) != 0
|
|
|
|
}
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn unlock(&self) {
|
2014-01-16 19:54:24 -08:00
|
|
|
LeaveCriticalSection(self.getlock() as LPCRITICAL_SECTION)
|
|
|
|
}
|
|
|
|
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn wait(&self) {
|
2014-01-16 19:54:24 -08:00
|
|
|
self.unlock();
|
|
|
|
WaitForSingleObject(self.getcond() as HANDLE, libc::INFINITE);
|
|
|
|
self.lock();
|
|
|
|
}
|
|
|
|
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn signal(&self) {
|
2014-01-16 19:54:24 -08:00
|
|
|
assert!(SetEvent(self.getcond() as HANDLE) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is especially unsafe because there are no guarantees made
|
|
|
|
/// that no other thread is currently holding the lock or waiting on the
|
|
|
|
/// condition variable contained inside.
|
2014-03-22 00:45:41 -07:00
|
|
|
pub unsafe fn destroy(&self) {
|
2014-08-04 15:42:36 -07:00
|
|
|
let lock = self.lock.swap(0, atomic::SeqCst);
|
|
|
|
let cond = self.cond.swap(0, atomic::SeqCst);
|
2014-01-16 19:54:24 -08:00
|
|
|
if lock != 0 { free_lock(lock) }
|
|
|
|
if cond != 0 { free_cond(cond) }
|
|
|
|
}
|
|
|
|
|
2014-03-22 00:45:41 -07:00
|
|
|
unsafe fn getlock(&self) -> *mut c_void {
|
2014-08-04 15:42:36 -07:00
|
|
|
match self.lock.load(atomic::SeqCst) {
|
2014-01-16 19:54:24 -08:00
|
|
|
0 => {}
|
|
|
|
n => return n as *mut c_void
|
|
|
|
}
|
|
|
|
let lock = init_lock();
|
2014-08-04 15:42:36 -07:00
|
|
|
match self.lock.compare_and_swap(0, lock, atomic::SeqCst) {
|
2014-01-16 19:54:24 -08:00
|
|
|
0 => return lock as *mut c_void,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
free_lock(lock);
|
2014-08-04 15:42:36 -07:00
|
|
|
return self.lock.load(atomic::SeqCst) as *mut c_void;
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
|
|
|
|
2014-03-22 00:45:41 -07:00
|
|
|
unsafe fn getcond(&self) -> *mut c_void {
|
2014-08-04 15:42:36 -07:00
|
|
|
match self.cond.load(atomic::SeqCst) {
|
2014-01-16 19:54:24 -08:00
|
|
|
0 => {}
|
|
|
|
n => return n as *mut c_void
|
|
|
|
}
|
|
|
|
let cond = init_cond();
|
2014-08-04 15:42:36 -07:00
|
|
|
match self.cond.compare_and_swap(0, cond, atomic::SeqCst) {
|
2014-01-16 19:54:24 -08:00
|
|
|
0 => return cond as *mut c_void,
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
free_cond(cond);
|
2014-08-04 15:42:36 -07:00
|
|
|
return self.cond.load(atomic::SeqCst) as *mut c_void;
|
2014-01-16 19:54:24 -08:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
|
|
|
|
pub unsafe fn init_lock() -> uint {
|
2014-01-16 19:54:24 -08:00
|
|
|
let block = malloc_raw(CRIT_SECTION_SIZE as uint) as *mut c_void;
|
2013-11-13 23:17:18 -08:00
|
|
|
InitializeCriticalSectionAndSpinCount(block, SPIN_COUNT);
|
|
|
|
return block as uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn init_cond() -> uint {
|
|
|
|
return CreateEventA(ptr::mut_null(), libc::FALSE, libc::FALSE,
|
|
|
|
ptr::null()) as uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn free_lock(h: uint) {
|
|
|
|
DeleteCriticalSection(h as LPCRITICAL_SECTION);
|
2014-01-21 09:31:32 -05:00
|
|
|
libc::free(h as *mut c_void);
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub unsafe fn free_cond(h: uint) {
|
|
|
|
let block = h as HANDLE;
|
|
|
|
libc::CloseHandle(block);
|
|
|
|
}
|
|
|
|
|
2014-05-30 17:07:16 +01:00
|
|
|
#[allow(non_snake_case_functions)]
|
2013-11-13 23:17:18 -08:00
|
|
|
extern "system" {
|
|
|
|
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
|
|
|
|
bManualReset: BOOL,
|
|
|
|
bInitialState: BOOL,
|
|
|
|
lpName: LPCSTR) -> HANDLE;
|
|
|
|
fn InitializeCriticalSectionAndSpinCount(
|
|
|
|
lpCriticalSection: LPCRITICAL_SECTION,
|
|
|
|
dwSpinCount: DWORD) -> BOOL;
|
|
|
|
fn DeleteCriticalSection(lpCriticalSection: LPCRITICAL_SECTION);
|
|
|
|
fn EnterCriticalSection(lpCriticalSection: LPCRITICAL_SECTION);
|
|
|
|
fn LeaveCriticalSection(lpCriticalSection: LPCRITICAL_SECTION);
|
|
|
|
fn TryEnterCriticalSection(lpCriticalSection: LPCRITICAL_SECTION) -> BOOL;
|
|
|
|
fn SetEvent(hEvent: HANDLE) -> BOOL;
|
|
|
|
fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
use std::prelude::*;
|
2013-12-28 19:44:52 -08:00
|
|
|
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
use std::mem::drop;
|
2014-02-15 11:18:49 +11:00
|
|
|
use super::{StaticNativeMutex, NATIVE_MUTEX_INIT};
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
use std::rt::thread::Thread;
|
2013-12-28 19:32:16 -08:00
|
|
|
|
2013-11-13 23:17:18 -08:00
|
|
|
#[test]
|
2014-02-13 19:29:13 +11:00
|
|
|
fn smoke_lock() {
|
2014-02-15 11:18:49 +11:00
|
|
|
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2013-11-13 23:17:18 -08:00
|
|
|
unsafe {
|
2014-02-13 17:17:50 +11:00
|
|
|
let _guard = lock.lock();
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-02-13 19:29:13 +11:00
|
|
|
fn smoke_cond() {
|
2014-02-15 11:18:49 +11:00
|
|
|
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2013-11-13 23:17:18 -08:00
|
|
|
unsafe {
|
2014-03-17 14:34:25 -07:00
|
|
|
let guard = lock.lock();
|
2014-01-26 22:42:26 -05:00
|
|
|
let t = Thread::start(proc() {
|
2014-03-17 14:34:25 -07:00
|
|
|
let guard = lock.lock();
|
2014-02-13 17:17:50 +11:00
|
|
|
guard.signal();
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2014-02-13 17:17:50 +11:00
|
|
|
guard.wait();
|
|
|
|
drop(guard);
|
|
|
|
|
2013-11-13 23:17:18 -08:00
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 17:55:41 -08:00
|
|
|
|
2014-02-13 19:29:13 +11:00
|
|
|
#[test]
|
|
|
|
fn smoke_lock_noguard() {
|
2014-02-15 11:18:49 +11:00
|
|
|
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2014-02-13 19:29:13 +11:00
|
|
|
unsafe {
|
|
|
|
lock.lock_noguard();
|
|
|
|
lock.unlock_noguard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_cond_noguard() {
|
2014-02-15 11:18:49 +11:00
|
|
|
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2014-02-13 19:29:13 +11:00
|
|
|
unsafe {
|
|
|
|
lock.lock_noguard();
|
|
|
|
let t = Thread::start(proc() {
|
|
|
|
lock.lock_noguard();
|
|
|
|
lock.signal_noguard();
|
|
|
|
lock.unlock_noguard();
|
|
|
|
});
|
|
|
|
lock.wait_noguard();
|
|
|
|
lock.unlock_noguard();
|
|
|
|
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 17:55:41 -08:00
|
|
|
#[test]
|
|
|
|
fn destroy_immediately() {
|
|
|
|
unsafe {
|
2014-03-28 09:16:22 -07:00
|
|
|
let m = StaticNativeMutex::new();
|
2013-11-25 17:55:41 -08:00
|
|
|
m.destroy();
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 23:17:18 -08:00
|
|
|
}
|